Blackman Harris in C

I am trying to reproduce the function shown here: http://en.wikipedia.org/wiki/Window_function#Blackman.E2.80.93Harris_window

But I just can't get the point. This is my current code:

double blackman_harris(int n, int N){
double a0, a1, a2, a3, seg1, seg2, seg3, w_n;
a0 = 0.35875;
a1 = 0.48829;
a2 = 0.14128;
a3 = 0.01168;

seg1 = a1 * (double) cos((double)(2*M_PI*n)/(double) (N - 1));
seg2 = a2 * (double) cos((double)(4*M_PI*n)/(double) (N - 1));
seg3 = a3 * (double) cos((double)(6*M_PI*n)/(double) (N - 1));

w_n = a0 - seg1 + seg2 - seg3;

return w_n;
}

Thank you for your help.

+3
source share
1 answer

Define a window as an entire function.

bool VecBuildBlackmanHarrisWindow( float* pOut, unsigned int num )
{
    const float a0      = 0.35875f;
    const float a1      = 0.48829f;
    const float a2      = 0.14128f;
    const float a3      = 0.01168f;

    unsigned int idx    = 0;
    while( idx < num )
    {
        pOut[idx]   = a0 - (a1 * cosf( (2.0f * M_PI * idx) / (num - 1) )) + (a2 * cosf( (4.0f * M_PI * idx) / (num - 1) )) - (a3 * cosf( (6.0f * M_PI * idx) / (num - 1) ));
        idx++;
    }
    return true;
}

Then you can define the window function as follows:

std::vector< float > window( 1024 );
VecBuildBlackmanHarrisWindow( &window.front(), window.size() );

This means that you can pre-calculate the window function.

At this moment, I am sorry that I led you wrong. I'm sorry. I checked my code and calculated the value by averaging all the values ​​of the window samples together and then dividing by 2 (effectively adding them all and dividing by N / 2).

float fTotal    = 1.0f;
auto iter   = window.begin();
while( iter != window.end() )
{
    fTotal  += *iter;
    iter++;
}

fTotal  /= 1024.0f;
fTotal  /= 2.0f;

This gives me a value of 0.17969f

( , , , , , ).

+3

All Articles