bandpass butterworth filter implementation in C++

前端 未结 4 1591
醉酒成梦
醉酒成梦 2021-02-03 11:59

I am implementing an image analysis algorithm using openCV and c++, but I found out openCV doesnt have any function for Butterworth Bandpass filter officially. in my project I

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-03 12:39

    I know this is a post on an old thread, and I would usually leave this as a comment, but I'm apparently not able to do that.

    In any case, for people searching for similar code, I thought I would post the link from where this code originates (it also has C code for other types of Butterworth filter coefficients and some other cool signal processing code).

    The code is located here: http://www.exstrom.com/journal/sigproc/

    Additionally, I think there is a piece of code which calculates said scaling factor for you already.

    /**********************************************************************
    sf_bwbp - calculates the scaling factor for a butterworth bandpass filter.
    The scaling factor is what the c coefficients must be multiplied by so
    that the filter response has a maximum value of 1.
    */
    
    double sf_bwbp( int n, double f1f, double f2f )
    {
        int k;            // loop variables
        double ctt;       // cotangent of theta
        double sfr, sfi;  // real and imaginary parts of the scaling factor
        double parg;      // pole angle
        double sparg;     // sine of pole angle
        double cparg;     // cosine of pole angle
        double a, b, c;   // workspace variables
    
        ctt = 1.0 / tan(M_PI * (f2f - f1f) / 2.0);
        sfr = 1.0;
        sfi = 0.0;
    
        for( k = 0; k < n; ++k )
        {
            parg = M_PI * (double)(2*k+1)/(double)(2*n);
            sparg = ctt + sin(parg);
            cparg = cos(parg);
            a = (sfr + sfi)*(sparg - cparg);
            b = sfr * sparg;
            c = -sfi * cparg;
            sfr = b - c;
            sfi = a - b - c;
        }
    
        return( 1.0 / sfr );
    }
    

提交回复
热议问题