Spectrum and spectral density estimation with periodograms and window functions Gerhard Heinzel Max-Planck-Institut für Gravitationsphysik, (Albert-Einstein-Institut), Hannover, Germany 1 Bandpass 50−2000 Hz y (log.) [V rms] Noise 10 mV/sqrt(Hz) RMS Voltmeter 441 mVrms To ADC Sine wave 1234 Hz 2Vrms =2.82Vpk LSD PS LS 10 mV/sqrt(Hz) 50 This is easy to realize experimentally. Abbrev. PSD 2V Name power spectrum linear spectrum amplitude spectrum f[Hz] This is what we want as result. Relation power spectral density linear spectral density amplitude spectral density 1234 2000 LSD = √ PSD PS = PSD × ENBW √ √ LS = PS = LSD × ENBW Unit V2/Hz √ V/ Hz V2 V 2 e (f ) is its relation to the rms fluctuation One practically important relation for a linear spectral density U of the quantity U , assumed to be band-limited to the frequency range f1 ≤ f ≤ f2: v uf uZ 2h i2 u e (f ) Urms = u U df , t f1 - (1) In GEO we need both PSD and PS Their relationship is given by the Noise Equivalent BandWidth ENBW. ENBW depends on the details of the analysis. If ENBW is not recorded, the results cannot be converted later. Most software (including MATLAB’s pwelch) do not provide ENBW. 3 Some common units Vpk−pk Vpk Vrms U t Vpk Vrms = √ . 2 signal power ratio[dB] = 10 log10 reference power dBm: (2) signal amplitude = 20 log10 . reference amplitude √ Urms = P × 50 Ω , x −3 + 0.1 × 1 dBm P = 1 W × 10 n (3) (4) o (5) 2 Vrms = 2.828 Vpk = 5.657 Vpk−pk = 6.02 dBVrms = 126.02 dBµVrms = 19.03 dBm. (6) 4 A periodogram is just a Discrete Fourier Transform (DFT) of the time series xk : NX −1 mk (1) ym = xk exp −2πi , N k=0 1 (2) (1) √ ym = ym , N 1 (1) (3) ym . ym = N m = 0 . . . N − 1, (7) (8) (9) To become useful, periodograms have to be - modified by window functions and - averaged. Averaging reduces the intrinsically large variance of periodograms of stochastic signals. 5 Doing nothing is equivalent to using a Rectangular window: 5 0 Amplitude [dB] -5 -10 -15 -20 -25 -30 -35 -40 -45 -20 -15 -10 -5 0 5 10 Frequency offset [bins] 15 20 6 A window function to be used with a DFT of length N is defined by a vector of real numbers {wj }, j = 0 . . . N − 1. It is used by multiplying the time series xj with the window before performing the DFT, i.e. using x0j = xj · wj as input to the DFT. All windows studied here have the following symmetry: wj = wN −j . (10) This implies for even lengths N that w0 and wN/2 appear only once, while all other coefficients appear twice. Hence only the N/2 + 1 coefficients w0, . . . , wN/2 need to be computed and stored. 1 1.2 0.9 1 0.8 w3 window value wi 0.7 window value w4 0.6 0.5 0.4 w5 0.8 0.6 w2 w6 0.4 0.3 0.2 0.2 w1 0.1 w0 0 0 0 0.2 0.4 0.6 index i/N 0.8 1 w7 -5 0 5 index i 10 15 7 Name Rectangular Welch Bartlett Hanning Hamming Nuttall3 Nuttall4 Nuttall3a Kaiser3 Nuttall3b Nuttall4a BH92 Nuttall4b Kaiser4 Nuttall4c Kaiser5 PSLL [dB] 13.3 21.3 26.5 31.5 42.7 46.7 60.9 64.2 69.6 71.5 82.6 92.0 93.3 94.4 98.1 119.8 SLDR [f −n] 1 2 2 3 1 5 7 3 1 1 5 1 3 1 1 1 NENBW [bins] 1.0000 1.2000 1.3333 1.5000 1.3628 1.9444 2.3100 1.7721 1.7952 1.7037 2.1253 2.0044 2.0212 2.0533 1.9761 2.2830 3 dB BW [bins] 0.8845 1.1535 1.2736 1.4382 1.3008 1.8496 2.1884 1.6828 1.7025 1.6162 2.0123 1.8962 1.9122 1.9417 1.8687 2.1553 flatness [dB] −3.9224 −2.2248 −1.8242 −1.4236 −1.7514 −0.8630 −0.6184 −1.0453 −1.0226 −1.1352 −0.7321 −0.8256 −0.8118 −0.7877 −0.8506 −0.6403 ROV [%] 0.0 29.3 50.0 50.0 50.0 64.7 70.5 61.2 61.9 59.8 68.0 66.1 66.3 67.0 65.6 70.5 8 0 3 -20 2.5 -40 amplitude [dB] normalized window value 3.5 2 1.5 1 0.5 0 α 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 NENBW=2.2830 bins -60 -80 -100 -120 -140 0 0.2 PSLL [dB] −45.9 −57.6 −69.6 −81.9 −94.4 −107.0 −119.8 −132.6 −145.5 −158.4 −171.4 0.4 0.6 index j/N 0.8 1 NENBW [bins] 1.4963 1.6519 1.7952 1.9284 2.0533 2.1712 2.2830 2.3898 2.4920 2.5902 2.6848 3 dB BW [bins] 1.4270 1.5700 1.7025 1.8262 1.9417 2.0512 2.1553 2.2546 2.3499 2.4414 2.5297 -160 -40 zero [bins] 2.24 2.69 3.16 3.64 4.12 4.61 5.10 5.59 6.08 6.58 7.07 -20 0 20 frequency offset [bins] flatness [dB] −1.4527 −1.2010 −1.0226 −0.8900 −0.7877 −0.7064 −0.6403 −0.5854 −0.5392 −0.4998 −0.4657 40 ROV [%] 53.4 58.3 61.9 64.7 67.0 68.9 70.5 71.9 73.1 74.1 75.1 9 Name SFT3F SFT3M FTNI SFT4F SFT5F SFT4M FTHP HFT70 FTSRS SFT5M HFT90D HFT95 HFT116D HFT144D HFT169D HFT196D HFT223D HFT248D PSLL [dB] 31.7 44.2 44.4 44.7 57.3 66.5 70.4 70.4 76.6 89.9 90.2 95.0 116.8 144.1 169.5 196.2 223.0 248.4 SLDR [f −n] 3 1 1 5 7 1 1 1 3 1 3 1 3 3 3 3 3 3 NENBW [bins] 3.1681 2.9452 2.9656 3.7970 4.3412 3.3868 3.4279 3.4129 3.7702 3.8852 3.8832 3.8112 4.2186 4.5386 4.8347 5.1134 5.3888 5.6512 3 dB BW [bins] 3.1502 2.9183 2.9355 3.7618 4.2910 3.3451 3.3846 3.3720 3.7274 3.8340 3.8320 3.7590 4.1579 4.4697 4.7588 5.0308 5.3000 5.5567 flatness [dB] +0.0082 −0.0115 +0.0169 +0.0041 −0.0025 −0.0067 +0.0096 −0.0065 −0.0156 +0.0039 −0.0039 +0.0044 −0.0028 +0.0021 +0.0017 +0.0013 −0.0011 +0.0009 ROV [%] 66.7 65.5 65.6 75.0 78.5 72.1 72.3 72.2 75.4 76.0 76.0 75.6 78.2 79.9 81.2 82.3 83.3 84.1 10 Comparison between Rectangular, Hanning and Flat-top window. 5 4 amplitude [dB] normalized window value 6 3 2 1 0 -1 0 0.2 0.4 0.6 index i/N 0.8 1 0 -20 -40 -60 -80 -100 -120 -140 -160 -40 -20 0 20 frequency offset [bins] 40 11 Comparison between Rectangular, Hanning and Flat-top window. -50 amplitude [dB] amplitude [dB] 0 -100 -150 -200 -10 -5 0 5 frequency offset [bins] 10 0.5 0 -0.5 -1 -1.5 -2 -2.5 -3 -3.5 -4 -0.5 0 frequency offset [bins] 0.5 12 We define the following two sums for normalization purposes: S1 = S2 = NX −1 j=0 NX −1 wj , (11) wj2 . (12) j=0 Because we will use S1 and S2 in the normalization of our final results, we can multiply the window values wj with any convenient constant factor. The normalized equivalent noise bandwidth NENBW of the window, expressed in frequency bins, is given by S2 NENBW = N . (13) 2 (S1) The effective noise bandwidth ENBW is given by ENBW = NENBW · fres = NENBW · fs S = fs 2 2 , N (S1) (14) 13 The result of the FFT is a complex vector ym of length N/2 + 1. We interpret it as a power spectrum, expressed as V2 rms , as follows: 2 · |ym|2 PSrms(fm = m · fres) = ; S12 m = 0 . . . N/2 , (15) If the desired result is a power spectral density (PSD) expressed in V2/Hz, it is obtained by dividing the power spectrum (PS) by the effective noise-equivalent bandwidth ENBW: 2 · |ym|2 PSrms(fm) = ; PSDrms(fm = m · fres) = ENBW f s · S2 LSD = LS = √ √ m = 0 . . . N/2 , (16) PSD, (17) PS. (18) If several spectra/spectral densities are averaged , this averaging must be performed with the power spectra/spectral densities, and the square root, if desired, must be taken only at the end. 14 Window Window Overlapping avoids the loss of information when windows are used. N N N N overlap 15 The recommended overlap is defined as that overlap r where the distance between amplitude flatness AF and overlap correlation OC becomes maximal. OC(r) = rNP −1 j=0 wj wj+(1−r)N NP −1 j=0 (19) wj2 50.0% 1 overlap flatness / correlation 1 max. window value 0.8 0.6 min. 0.4 0.2 67% shift 0.8 AF 0.6 PF 0.4 0.2 OC 0 0 -1 -0.5 0 0.5 1 normalized data index i/N 1.5 2 0 10 20 30 40 50 60 70 80 90 100 overlap [%] 16 Input data: We assume to have a long stream of input data that has already been converted to floating point numbers (corresponding to ‘Volts’ at the input of the A/D converter). We also assume that the sampling frequency fs is known and fixed. Frequency resolution and length of DFT: Now choose a frequency resolution fres. Typical values range between fs/100 and fs/100 000. Find the corresponding length N of the DFT, according to N = fs/fres. Round N to the next convenient value, and re-compute the final frequency resolution fres = fs/N . Window function: At this point a window function can be chosen and computed. The first decision to be made is whether a flat-top window is necessary. That will always be the case if the amplitude of sinusoidal peaks is to be determined from the spectrum. Otherwise, the other (i.e. non-flattop) windows are preferable because of their smaller bandwidths. As a general rule, try to choose a window with a sidelobe suppression not less than the intrinsic signal-to-noise ratio of the input signal. If this signal-to-noise ratio is unknown, one can start with, e.g., a Hanning window and determine from the resulting spectrum how much the highest peak sticks out of the background noise. With this information, choose a suitable window. Good choices are the Kaiser window and the new flat-top windows HFTxx. Since N is already known, the window values wj can now be computed. At the same time, compute the ‘window sums’ S1 and S2. If overlapping is desired, select a suitable value for the overlap. Record fres, NENBW and ENBW (Equations (13) and (14)). 17 Splitting of the data stream: Identify continuous stretches of useful data (i.e. without glitches, pulses, missing periods etc.). Split these periods into segments of length N , overlapping as desired. Remove the DC and/or trend of each segment, if desired. FFT: Multiply each segment with the pre-computed window values wj . Put the product through the real-to-complex FFT algorithm. Unpack the result, compute the squared magnitude, and average it (separately for each frequency bin). scaling: After all segments have been processed, scale the average using Equation (15) or (16). If the linear spectrum/spectral density is required, apply Equation (17) or (18). Finally, convert to other units according to, if necessary. [Pxx,f] = pwelch(x,nwin,noverlap,nfft,fs) Here x is a vector with the time series, nwin either an integer (then a Hamming window of that length is used), or a vector holding the window weights wj , noverlap an integer (a fraction of the FFT length) that indicates the desired overlap, nfft an integer giving the length N of the FFT (nfft should be the same as the length of the window vector nwin; this is not clear from the help text), and fs the sampling frequency (according to the help text ‘an integer’, apparently a typo). The results are Pxx, a vector with the power spectral density (PSD), and f, a vector with the corresponding frequencies. If y is the unit of the input time series, the output Pxx has the unit y 2/Hz. 18 If we compare the functionality of pwelch with what we have listed above, we find that • pwelch nearly does what we want, except removing DC average and trend. • If you want fancy window functions, you have to compute them yourself and pass them as vector nwin. • MATLAB uses the FFTW package which means that we have some flexibility in choosing N : N = 2a3b5c7d11e13f , (20) • The main deficiency of pwelch is that it only computes power spectral densities (PSD) and has no default way of computing power spectra (PS) or the missing factor between PSD and PS, namely the equivalent noise bandwidth (ENBW). If you compute your own window function (recommended), this can, however, easily be remedied by using Equation (14). 19 #include <stdio.h> #include <math.h> #define TWOPI 6.28318530717959 int main (void) { double fs = 10000; double f1 = 1234; double amp1 = 2.82842712474619; double f2 = 2500.2157; double amp2 = 1; double ulsb = 1e-3; int i; double t, u, ur; /* /* /* /* /* /* sampling frequency [Hz] */ first signal frequency [Hz] */ 2 Vrms */ second signal frequency [Hz] */ 0.707 Vrms */ Value of 1 LSB in Volt */ for (i = 0; i < 1000000; i++) { t = (double) i / fs; u = amp1 * sin (TWOPI * f1 * t) + amp2 * sin ur = floor (u / ulsb + 0.5) * ulsb; /* printf ("%10.6f %8.5f\n", t, ur); /* fwrite (&ur, sizeof (double), 1, stdout); /* } return 0; (TWOPI * f2 * t); Rounding */ ASCII output */ alternative binary output */ } 20 By rounding the simulated time-series data to integer multiples of a suitably chosen unit ULSB, a noise floor with the density ULSB e √ U = (21) dig 6 · fs 101 101 1 1 10 Linear spectrum LS [V rms] Linear spectral density LSD [V/√(Hz) rms] is introduced in the spectrum. -1 10-2 10-3 10-4 Udig=4.08 µVrms/√Hz 10-5 10-6 0 1 2 3 Frequency [kHz] 4 5 2 Vrms 0.707 Vrms -1 10 10-2 fres=3.005 Hz NENBW=4.199 bins ENBW=12.618 Hz 10-3 10-4 10-5 10-6 0 1 2 3 Frequency [kHz] 4 5 21