实施Hann Window

Tim*_*Tim 7 c signal-processing fft fftw

我获取输入数据块并通过fftw传递它们以获得一些光谱信息.一切似乎都在起作用,但我认为我遇到了一些别名问题.

我一直在努力研究如何在我的数据块上实现一个hann窗口.谷歌让我失望的例子.我应该关注的任何想法或链接?

double dataIn[2048] > /* windowing here? */ > FFT > double freqBins[2048]
Run Code Online (Sandbox Code Playgroud)

更新

感谢Oli指出我实际上要解决的问题是光谱泄漏,而不是混叠...

Joo*_*kka 18

http://en.wikipedia.org/wiki/Hann_function.该实现非常直接地从定义开始.只需使用w(n)函数作为乘数,循环遍历所有样本(随时更改n),就是这样.

for (int i = 0; i < 2048; i++) {
    double multiplier = 0.5 * (1 - cos(2*PI*i/2047));
    dataOut[i] = multiplier * dataIn[i];
}
Run Code Online (Sandbox Code Playgroud)


Oli*_*rth 5

不是对您问题的答案,而是对您问题的旁白。加窗有助于解决频谱泄漏问题,而不是混叠问题。

当波形的频率分量不是采样率的精确整数因数时,就会出现频谱泄漏效应。

如果你有别名,那么你就彻底完蛋了。您需要提高采样率,或者在采样之前放入(更好的)抗锯齿滤波器。


her*_*tao 5

hanning.m可以在此处找到与 MATLAB 等效的完整函数:

/*  function w = hanning(varargin)
%   HANNING   Hanning window.
%   HANNING(N) returns the N-point symmetric Hanning window in a column
%   vector.  Note that the first and last zero-weighted window samples
%   are not included.
%
%   HANNING(N,'symmetric') returns the same result as HANNING(N).
%
%   HANNING(N,'periodic') returns the N-point periodic Hanning window,
%   and includes the first zero-weighted window sample.
%
%   NOTE: Use the HANN function to get a Hanning window which has the
%          first and last zero-weighted samples.ep
    itype = 1 --> periodic
    itype = 0 --> symmetric
    default itype=0 (symmetric)

    Copyright 1988-2004 The MathWorks, Inc.
%   $Revision: 1.11.4.3 $  $Date: 2007/12/14 15:05:04 $
*/

float *hanning(int N, short itype)
{
    int half, i, idx, n;
    float *w;

    w = (float*) calloc(N, sizeof(float));
    memset(w, 0, N*sizeof(float));

    if(itype==1)    //periodic function
        n = N-1;
    else
        n = N;

    if(n%2==0)
    {
        half = n/2;
        for(i=0; i<half; i++) //CALC_HANNING   Calculates Hanning window samples.
            w[i] = 0.5 * (1 - cos(2*PI*(i+1) / (n+1)));

        idx = half-1;
        for(i=half; i<n; i++) {
            w[i] = w[idx];
            idx--;
        }
    }
    else
    {
        half = (n+1)/2;
        for(i=0; i<half; i++) //CALC_HANNING   Calculates Hanning window samples.
            w[i] = 0.5 * (1 - cos(2*PI*(i+1) / (n+1)));

        idx = half-2;
        for(i=half; i<n; i++) {
            w[i] = w[idx];
            idx--;
        }
    }

    if(itype==1)    //periodic function
    {
        for(i=N-1; i>=1; i--)
            w[i] = w[i-1];
        w[0] = 0.0;
    }
    return(w);
}
Run Code Online (Sandbox Code Playgroud)