简体   繁体   English

Hanning 和 Hamming window 函数在 C# 中

[英]Hanning and Hamming window functions in C#

I'm trying to implement Hanning and Hamming window functions in C#.我正在尝试在 C# 中实现Hanning 和 Hamming window 函数 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.我在任何地方都找不到任何.Net 样本,我不确定我从 C++ 样本转换的尝试是否能很好地完成工作。

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.这是我不久前写的开源 C# 应用程序中使用的 Hamming window示例。 It's being used in a pitch detector for an autotune effect .它被用于音高检测器以实现自动调谐效果

You can use the Math.NET library.您可以使用 Math.NET 库。

    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请参阅我对类似问题的回答: https://stackoverflow.com/a/42939606/246758

The operation of "windowing" means multiplying a signal by a window function. “加窗”操作是指将信号乘以 window function。 This code you found appears to generate the window function and scale the original signal.您找到的这段代码似乎生成了 window function 并缩放原始信号。 The equations are for just the window function itself, not the scaling.这些方程式仅适用于 window function 本身,而不是缩放比例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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