# Hamming Window, python 2.7 [closed]

Hi I have a FFT which is quite noisy. How to apply to my code Hamming window to make it less noisy. Look at my code:

# Loop for FFT data
for dataset in [fft1]:
dataset = np.asarray(dataset)
psd = np.abs(np.fft.fft(dataset))
freq = np.fft.fftfreq(dataset.size, float(300)/dataset.size)
plt.semilogy(freq[freq>0], psd[freq>0]/dataset.size**2, color='r')

for dataset2 in [fft2]:
dataset2 = np.asarray(dataset2)
psd2 = np.abs(np.fft.fft(dataset2))
freq2 = np.fft.fftfreq(dataset2.size, float(300)/dataset2.size)
plt.semilogy(freq2[freq2>0], psd2[freq2>0]/dataset2.size**2, color='b')


I put an answer in the questions.

• Hamming window doesn't denoise – it reduces spectral leakage, so I'm not sure you're going in the right direction at all. Why are you doing a DFT in the first place? What's the goal of all this? My gut feeling tells me that you should probably be applying Welch's method (or Bartlett, which is Welch with 0 overlap), or a different frequency or spectrum estimator alltogether. Dec 3, 2017 at 11:48
• Well those are good questions. I am trying to receive a spectra of Schumann Resonance. I have a measurements from every 5 minutes. I am new to python and signal processing, so my answers and questions might not be precise. Do You have any ideas how you might help me? Dec 3, 2017 at 12:27
• Ah, a wild physicist appears! I have no idea what "Schumann Resonance" is, but what exactly is your requirement for the spectrum you're trying to estimate? The optimal way to help you is by trying to understand which problem you're trying to solve, and any background you can give on what you want to do with the results would definitely be worth being in the question itself, rather than in some comment that aside from me no-one will ever read. Dec 3, 2017 at 12:31
• Thank you for trying at least. Ok, so I will upgrade my question with the rest of the code so you might be able to understand it better :) But some info for you, Schumann Resonance appears when there is a thunder strike in the atmosphere, it makes a resonance between the ionosphere and the ground. This resonance appears in +/- 8Hz, so that's why I need FFT of my data. I will reedit my question with more code and photos. Dec 3, 2017 at 12:43
• Ah, "so I need FFT of my data" is, I think, a fallacy. What you want to estimate is the spectrum, and the FFT (which is just an implementation of the DFT, by the way), is just one way to estimate spectra. It's a non-parametric one, and it gives your equal resolution all over your observation bandwidth. What your problem actually demands is a spectrum estimator that is "fine" close to the frequencies you care about, has low variance with the same amount of data, and estimates a discrete set of oscillations with known parameters – so, I'd argue that the FFT is a very suboptimal approach here. Dec 3, 2017 at 12:51

In fact Welch was a good idea. End of post. Problem solved.

# Loop for FFT data
for dataset in [fft1]:
dataset = np.asarray(dataset)
freqs, psd = welch(dataset, fs=266336/300, window='hamming', nperseg=8192)
plt.semilogy(freqs, psd/dataset.size**2, color='r')

for dataset2 in [fft2]:
dataset2 = np.asarray(dataset2)
freqs2, psd2 = welch(dataset2, fs=266336/300, window='hamming', nperseg=8192)
plt.semilogy(freqs2, psd2/dataset2.size**2, color='b')

• watch your integer division! fs=266336/300 is 887.786666… in Python3 (which you should be using, as Python 2 is going to be obsolete), but 887 in Python 2. Dec 19, 2017 at 13:25