Hann/Hanning & Hamming window functions in C#

Not so much output from me lately. The reason is that I’m occupied with an exciting and extremely complex hobby project. My geeksuperpower (or one of them) is that I’m able to keep extremely focused for long periods of time (months). Even when its boring, tough and seems impossible to solve the task I work my self through it. The prerequisite being that its complex and interesting enough. A quality many coders possess, for good and for bad.

Anyway, I’ve been thinking I should share some of my findings. So here goes.

Hann (Hanning) and Hamming window functions which you apply to the wave stream frame before passing it to the FFT. I looked everywhere and couldn’t find it for C#, but with some help of Wikipedia and the good people at StackOverflow I managed to put it together.

// http://en.wikipedia.org/wiki/Window_function#Window_examples public Complex[] Hamming(Complex[] iwv)
{
    int N = iwv.Length;

    // iwv[i].Re = real number (raw wave data) // iwv[i].Im = imaginary number (0 since it hasn't gone through FFT yet) for (int n = 1; n < N; n++)
        iwv[n].Re *= 0.54f - 0.46f * (float)Math.Cos((2 * Math.PI * n) / (N - 1));

    return iwv;
}

public Complex[] Hann(Complex[] iwv)
{
    int N = iwv.Length;

EDIT: See blog comments below on this method.
    for (int n = 1; n < N; n++)
        iwv[n].Re = 0.5f * (float)Math.Cos((2 * Math.PI * n) / (N - 1));

    return iwv;
}

Note: Parts of Hamming code borrowed from this file which is part of a C# project that can do autotuning.

3 Responses to Hann/Hanning & Hamming window functions in C#

  • dummy says:

    Dunno what you’re trying to achieve, but I guess it’ll work better if you implement the Hann window correctly …

    iwv[n].Re *= 0.5f – 0.5f * (float)Math.Cos((2 * Math.PI * n) / (N – 1));

  • Kresna Tito says:

    i’m sorry for my misplaced question, but i’m working on phase coding steganography for that requires fft

    and i get stuck in the hanning window and complex data in aforge. can you give me a help? i will very pleased if you help me

    thanks.. and sorry for my bad english. i’m from Indonesia

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>