0
$\begingroup$

I want compute a FFT (average) with these parameters:

  • Spectrum size: 4096
  • Windows function: Hanning
  • Overlap: 50%

This is my python code:

data = np.genfromtxt(file_path)
Freq = data[:,0]
Pdyn = data[:,1]
ft = np.fft.rfft(PdynA*np.hanning(len(Pdyn)))
mgft = np.abs(ft)

fig = plt.figure()
xVals = np.abs(np.fft.fftfreq(len(PdynA),d=1/12000))
plt.plot(xVals[:len(mgft)], mgft)
plt.show()

So I have my Hanning windows. But now, I don't know how I can set the Overlap to 50% and the spectrum size to 4096 ?

$\endgroup$
  • 1
    $\begingroup$ What is the question? $\endgroup$ – jojek Nov 30 '16 at 12:11
1
$\begingroup$

I can tell you what to do but I can't tell you how to do it in Python code. To average four spectra, do the following:

1) Multiply input samples x[0] –thru- x[4095] by a 4096-point Hanning sequence.

2) Compute a 4096-pt FFT of the above product sequence producing an X1[m] spectrum.

3) Multiply input samples x[2048] –thru- x[6143] by a 4096-point Hanning sequence.

4) Compute a 4096-pt FFT of the above product sequence producing an X2[m] spectrum.

5) Multiply input samples x[4096] –thru- x[8191] by a 4096-point Hanning sequence.

6) Compute a 4096-pt FFT of the above product sequence producing an X3[m] spectrum.

7) Multiply input samples x[6144] –thru- x[10239] by a 4096-point Hanning sequence.

8) Compute a 4096-pt FFT of the above product sequence producing an X4[m] spectrum.

9) Finally, average the magnitude-squared samples of the four spectra to produce a final |Xf[m]| magnitude spectrum. That is,

|Xf[0]| = (|X1[0]| + |X2[0]| + |X3[0]| + |X4[0]|)/4.

|Xf[1]| = (|X1[1]| + |X2[1]| + |X3[1]| + |X4[1]|)/4.

|Xf[2]| = (|X1[2]| + |X2[2]| + |X3[2]| + |X4[2]|)/4.

...

...

|Xf[4095]| = (|X1[4095]| + |X2[4095]| + |X3[4095]| + |X4[4095]|)/4.

If you desire a power spectrum, then merely square each of the |Xf[m]| magnitude samples. user25139, you're implementing Welch's Method of spectrum analysis. You can search the web for "Welch's Method" for more information.

| improve this answer | |
$\endgroup$
1
$\begingroup$

SciPy has a spectrogram function.

Assuming x is your input signal your code would be:

import numpy as np
from scipy.signal import spectrogram, hanning

M = 4096
NFFT = M
win = hanning(M)
overlap = 0.5
overlap_samples = int(round(M*overlap)) # overlap in samples
t, f, S = spectrogram(x,window=win,nperseg=M,noverlap=overlap_samples,nfft=NFFT)

# Compute average spectrum
avg_S = np.mean(S,axis=1)
| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.