简体   繁体   中英

Hanning and Hamming window functions in C#

I'm trying to implement Hanning and Hamming window functions in C#. I can't find any.Net samples anywhere and I'm not sure if my attempts at converting from C++ samples does the job well.

My problem is mainly that looking at the formulas I imagine they need to have the original number somewhere on the right hand side of the equation - I just don't get it from looking at the formulas. (My math isn't that good yet obviously.)

What I have so far:


public Complex[] Hamming(Complex[] iwv)
{
    Complex[] owv = new Complex[iwv.Length];
    double omega = 2.0 * Math.PI / (iwv.Length);

    // owv[i].Re = real number (raw wave data)
    // owv[i].Im = imaginary number (0 since it hasn't gone through FFT yet)
    for (int i = 1; i < owv.Length; i++)
        // Translated from c++ sample I found somewhere
        owv[i].Re = (0.54 - 0.46 * Math.Cos(omega * (i))) * iwv[i].Re; 

    return owv;

}

public Complex[] Hanning(Complex[] iwv)
{
    Complex[] owv = new Complex[iwv.Length];
    double omega = 2.0 * Math.PI / (iwv.Length);

    for (int i = 1; i < owv.Length; i++)
        owv[i].Re = (0.5  + (1 - Math.Cos((2d * Math.PI ) / (i -1)))); // Uhm... wrong

    return owv;
}

Here's an example of a Hamming window in use in an open source C# application I wrote a while back. It's being used in a pitch detector for an autotune effect .

You can use the Math.NET library.

    double[] hannDoubles = MathNet.Numerics.Window.Hamming(dataIn.Length);
    for (int i = 0; i < dataIn.Length; i++)
    {
        dataOut[i] = hannDoubles[i] * dataIn[i];
    }

See my answer to a similar question: https://stackoverflow.com/a/42939606/246758

The operation of "windowing" means multiplying a signal by a window function. This code you found appears to generate the window function and scale the original signal. The equations are for just the window function itself, not the scaling.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM