Python FFT (average) Hanning spectrum size 4096 and 50% overlap

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 ?

• What is the question?
– jojek
Nov 30 '16 at 12:11

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 –thru- x 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 –thru- x 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 –thru- x 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 –thru- x 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| = (|X1| + |X2| + |X3| + |X4|)/4.

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

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

...

...

|Xf| = (|X1| + |X2| + |X3| + |X4|)/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.

SciPy has a spectrogram function.