# Confusion when applying Hamming window to signal

I am writing some code which takes a signal of length $n$, and processes it in 20 ms chunks with 5 ms of overlap... So basically I have a buffer of 20 ms worth of sampled data, and I am trying to apply a Hamming window to it. For some reason, when I listen back to the audio generated from this, I am getting nothing-- and the output of my Hamming window seems to be pretty static-- values that are all somewhere near 0.07999999989377744. I am wondering, what am I doing wrong in my calculations?

So, assuming s[n] is my 20 ms sample buffer, this is the code I'm running:

sampleRate = 44100;
frames     = (sampleRate / 1000) * 20;  // 20 ms

for (i = 0; i < frames; i++) {
hammingWindowValue = (0.54 - 0.46) * Math.cos((2 * PI * s[i]) / frames));
s[i] = hammingWindowValue;
}

• why in the world did someone down vote this question? – patrick Jan 13 '15 at 5:05

You're not applying the Hamming window correctly. You should first calculate the Hamming window values, with something like:

for (i = 0; i < frames; i++) {
hammingWindowsValue[i] = 0.54 - 0.46 * Math.cos(2*PI*i / (frames-1) );
}


(Note the lack of parenthesis around (0.54-0.46)). Then, you should multiply your signal by the window values:

for (i = 0; i < frames; i++) {
s[i] = hammingWindowValue[i] * s[i];
}


(Of course, in practice that code can be simplified. I separated it into two loops to make it conceptually clearer.)

In brief: when windowing a signal, you first calculate the window samples, and then multiply the window and your signal.

• @patrick, you're welcome, I'm glad it helped. – MBaz Nov 21 '14 at 23:09
• doesn't look like neither question nor answer are popular. :-\ – robert bristow-johnson Nov 22 '14 at 18:00
• @robertbristow-johnson: unexplained downvotes is one of the big negatives about this site for me... makes me want to quit sometimes. – MBaz Nov 22 '14 at 19:40