C或C++实现BUTTERWORTH滤波器

Python014

C或C++实现BUTTERWORTH滤波器,第1张

/// 巴特沃斯滤波器, 带增益

Mat BoostButterworthFilter( const int &row, const int &col,  const float &cutoff, const int &n, const float &boost )const

{

    assert( row>1 && col>1 )

    assert( cutoff>0 && cutoff<0.5 )

    assert( n>=1 )

 

    if ( boost>=1 )

    {

        return  (1-1/boost)*( BHPF( row, col, cutoff, n ) ) + 1/boost

    }

    else

    {

        return (1-boost)*BLPF( row, col, cutoff, n) + boost

    }

}

/// 巴特沃斯高通滤波器 

Mat CIllumNorm::BHPF( const int &row, const int &col,  const float &cutoff, const int &n )

{

    return 1-BLPF( row, col, cutoff, n )

}

 

/// 巴特沃斯低通滤波器   

/*

cutoff is the cutoff frequency of the filter 0 - 0.5

 n      is the order of the filter, the higher n is the sharper

                      1

     f =    --------------------

                                        2n

             1.0 + (w/cutoff)

*/

Mat BLPF( const int &row, const int &col,  const float &cutoff, const int &n )

{     

    assert( row>1 && col>1 )

    assert( cutoff>0 && cutoff<0.5 )

    assert( n>=1 )

    

    Mat X = Mat::zeros( row, col, CV_32F )

    Mat Y = Mat::zeros( row, col, CV_32F )

 

    if ( col%2 )

    {

        int t = -(col-1)/2

 

        for ( int j=0 j<col j++ )

        {

            for ( int i=0 i<row i++ )

            {

 

                X.at<float>( i, j ) = ((float)t)/(col-1)

            }

            t++

        }

    }

    else

    {

        int t = -col/2

 

        for ( int j=0 j<col j++ )

        {

            for ( int i=0 i<row i++ )

            {

                X.at<float>( i, j ) = ((float)t)/col

            }

            t++

        }

    }

 

 

    if ( row%2 )

    {

        int t = -(row-1)/2

 

        for ( int i=0 i<row i++ )

        {

            for ( int j=0 j<col j++ )

            {

                Y.at<float>( i, j ) = ((float)t)/(row-1)

            }

            t++

        }

    }

    else

    {

        int t = -row/2

 

        for ( int i=0 i<row i++ )

        {

            for ( int j=0 j<col j++ )

            {

                Y.at<float>( i, j ) = ((float)t)/row

            }

            t++

        }

    }

 

 

    Mat H = Mat::zeros( row, col, CV_32F )

 

    /// 计算频域的巴特沃斯分类器

    for ( int i=0 i<row i++ )

    {

        for ( int j=0 j<col j++ )

        {

            float x = X.at<float>( i, j )

            float y = Y.at<float>( i, j )

            float radius = sqrtf( x*x + y*y )

 

            /// 1.0 ./ (1.0 + (radius ./ cutoff).^(2*n)) 

            H.at<float>( i, j ) = 1.0 / ( 1.0+std::pow( radius/cutoff, 2*n ) )

        }

    }

 

    /// shift 将中心点移到左上角 

    H = CenterShift(H)

 

    return H

}  

 

Mat CenterShift( const Mat &_src )

{

    Mat src = Mat_<float>(_src)

    Mat dst = Mat::zeros( src.rows, src.cols, src.type() ) 

 

    int N = src.rows

    int D = src.cols

 

    int *rowIndex = new int[N]

    int *colIndex =new int[D]

 

    memset( rowIndex, 0, sizeof(rowIndex[0])*N )

    memset( colIndex, 0, sizeof(colIndex[0])*D )

 

    /// 行 顺序

    int begin = N/2

    for ( int i=0 i<N i++ )

    {

        rowIndex[i] = begin

        begin++

        if ( begin>=N )

        {

            begin = 0

        }

    }

 

    /// 列 顺序

    begin = D/2

    for ( int i=0 i<D i++ )

    {

        colIndex[i] = begin

        begin++

        if ( begin>=D )

        {

            begin = 0

        }

    }

 

    /// 重新排序

    for ( int i=0 i<N i++ )

    {

        for ( int j=0 j<D j++ )

        {

            dst.at<float>( i, j ) = src.at<float>( rowIndex[i], colIndex[j] )

        }

    }

 

    /// 释放

    delete []rowIndex

    delete []colIndex

    rowIndex = NULL

    colIndex = NULL

 

    return dst

}

#define pi 3.14159265358979384626

double h(double n)

{

double x

x=1.0/(pi*(n-64))

x*=sin(pi*(n-64))-sin(3*pi/4*(n-64))

x*=0.54-0.46*cos(2*pi*n/128)

x*=r128(n)

return x

}

double r128(double n)

{

double x

/*函数R128的公式你没给出,你自己完成吧*/

return x

}