Advanced Octave A45 — Power spectra and windows
Advanced GNU Octave with GX-TXT · Module 08
A finitely applied FFT distributes the energy between discrete frequencies. The window and normalization influence the result: we compare two estimates of the spectral density of the same signal.
Objectives and prerequisites
The lesson deepens the FFT presented in the basic course and requires familiarity with sampling and units. We use GX-TXT dev112-P011-bis and GNU Octave 9.4.0. Import finestre_psd.m, register it and run it in the Runner. The Dataset octave_advanced_hann_psd is created-only: repeat in a new Session or change its ID.
Signal and normalization
We generate 256 samples at 128 Hz of a 7.3 Hz synenoid and 1 V amplitude. The spacing between the points of the FFT is 128/256 = 0.5 Hz: 7.3 Hz does not coincide with a frequency of the grid. The program compares a rectangular window with a symmetric Hann.
For each w window, the script calculates abs(fft(x.*w)).^2/(fs*sum(w.^2)); to obtain the one-sided PSD doubles the internal values, leaving the DC and Nyquist bins unchanged. The density is expressed in V²/Hz. Summarizing and multiplying by 0.5 Hz we get an estimated average power in V², not an amplitude in V. The PSD Hann is published as a separate Dataset.
In the test the peak of the grid is 7.5 Hz for both windows. The peak value is about 0.55765 V²/Hz with the rectangular and 0.53997 V²/Hz with Hann; the integrated powers are about 0.49685 and 0.50000 V² respectively. The 7.5 Hz peak is the maximum of the available points, not a proof that the original sinusoid is exactly 7.5 Hz.
Choose the window
A rectangular window has a relatively narrow main lobe but higher side lobes; Hann reduces lateral lobes at cost to widen the main lobe. The choice depends on frequency separation, observed duration and level of neighboring components. The one-sided PSD, its integral and a spectrum of amplitude answer different questions: do not directly compare their values without considering unity and normalization.
Exercise
- How much is the spacing between the FFT points?
- What units have the PSD and its integral in frequency?
- Why doesn’t the maximum at 7.5 Hz exactly match the generated frequency?
Worked solution
- 0.5 Hz.
- V²/Hz and V² respectively.
- The frequency 7.3 Hz falls between points of the FFT grid; window and duration distribute the energy on multiple frequencies.
Other steps of this project
- Advanced Octave A75 — Dynamical systems in phase space
- Advanced Octave A81 — Geography of the waves and a 3D surface
- Advanced Octave A82 — Age, sex and demographic inequalities
- Advanced Octave A83 — All-cause mortality, certified COVID deaths and surveillance
- Advanced Octave A84 — A reproducible public account
- Advanced Octave A73 — Optimization landscapes and 3D trajectories