# How to apply Hamming Window and compute FFT?

I'm borrowing an EEG device whose accompanying software also computes the FFT of the data it collects. The software provides the user with a file of EEG values and a file of FFT values computed from the EEG file, and I wanted to learn how to compute the FFT values on my own.

Here's the manufacturer's description of how they do it: "Each path contains 129 decimal values with a range of roughly -40.0 to 20.0. Each array represents FFT coefficients (expressed as Power Spectral Density) for each channel, for a frequency range from 0hz-110Hz divided into 129 bins. We use a Hamming window of 256 samples(at 220Hz), then for the next FFT we slide the window 22 samples over(1/10th of a second). This gives a 90% overlap from one window to the next. These values are emitted at 10Hz."

Going through MATLAB's documentation I ended up with this code:

X = data(:, 1);
h = hamming(256);
buf = buffer(X, 256, 234);
hWin = buf.*repmat(h(:), [1, size(buf, 2)]);
Y = fft(hWin);
P2 = (abs(Y))';
P1 = P2(:, 1:256/2 + 1);
P1(:, 2:end-1) = 2*P1(:, 2:end-1);
P1 = 10 * log10(P1);


But it doesn't produce the sames values that I see in the four FFT files given to me by the manufacturer. First, let me start by saying that their EEG file contains a 193011 X 4 matrix of data, and each column corresponds to the data collected by one of the four channels/sensors on the EEG device. And with each column of data, they were able to compute four separate files, which they named raw_fft0, raw_fft1, raw_fft2, and raw_fft3. Each raw_fft file contains a 8607 X 129 matrix whose values should be somewhere between -40 and -20. The actual observed values for their FFT files are somewhere between -30 to -20 to the low-to-mid 40s.

On the other hand, my FFT files are of the dimension 8774 X 129, and the range of values in my files are bigger than what the manufacturer says they should be. Would anyone mind pointing out what I've done wrong or pointing me to information that could clarify what I've done wrong?

• I dont understand how your question regarding Hamming windows relates to your problem? I have the impression that your problem is rather the buffer function and not hamming window as the output dimensions depend on buffer. Mar 9, 2018 at 12:01
• Thanks for the response. I should've make it clearer, but I wasn't sure what the problem was. And I'd tried before to lower the overlap parameter in the buffer to reduce the the number of rows, but if I so much as lower 234 to 233, the number of rows becomes some number below 8607. So, I thought I'd might've missed some step elsewhere. Also, the values I'd produced with this code aren't in the range that the manufacturer specifies they should be in.
– vkd
Mar 9, 2018 at 18:14
• What is the total number of samples? It's not clear whether 193011 is the number of samples (input buffer) prior to the overlapping or after. Mar 9, 2018 at 18:27
• Thanks dsp_user for replying as well. 193011 is the number of samples prior to the overlapping.
– vkd
Mar 9, 2018 at 21:48

1. The Discrete Fourier Transform definition that the MATLAB fft uses does not preserve the energy of the vector. Multiply it by $1/\sqrt{N}$, where $N$ is the FFT size, if you want to keep the energy constant. The DFT used by the other code might (or might not) have different scaling.
2. You are computing $10log_{10}$ of the magnitude of the signal, but to compute the power spectral density you should be computing $10log_{10}$ of the magnitude squared - or, alternatively, $20log_{10}$ of the magnitude.
3. I don't know how the other code got 8607 rows, but if you form windows such that each window contains data in the original vector, you would get 8762 rows by my calculation. In your use of buffer, you get some windows that are stuffed with 0s at the end.