我需要用 C 实现巴特沃斯滤波器。获得具有此功能的库或编写代码是否更容易? [关闭]

2024-03-31

我的项目是用 C 语言编写的,CodeBlocks 是我的 IDE,我在 Windows Vista 上运行。我需要对我的数据应用巴特沃斯滤波器。我可以使用 Matlab 自动执行此操作,因为它具有此(和其他)过滤器作为内置函数。计算机和编程不完全是我的领域,我从来没有“导入”新的库,也不知道如何做到这一点。复杂吗?我最好做什么?将巴特沃斯滤波器的代码编写为函数?或者“导入”一个库来为我做这件事? (就我个人而言,如果不是那么复杂,我更喜欢获得一个库,因为它可能还有我也可以测试的其他类型的过滤器)


我们必须对我最近从事的一个项目的一些数据应用巴特沃斯滤波器。 (由于测量仪器变热,压力测量值会随着时间的推移而漂移。)实际上,它比包含库还要简单。您实际上只需要包含一个函数并将其添加到您需要使用它的任何 C 文件中。

这是我们用来生成过滤器的站点:

http://www-users.cs.york.ac.uk/~fisher/mkfilter/ http://www-users.cs.york.ac.uk/~fisher/mkfilter/

如果您指定参数,它将为您生成函数。这是我们在应用程序中使用的函数示例,基于上述网站生成的代码。 (我们 typedef DOUBLE 因为它是在 PC 上开发的,但针对的是嵌入式平台——我们需要确保大小不会改变。)

我喜欢阅读这个网站,很高兴终于能做出一些贡献!

/* Butterworth filter constants */

#define NZEROS 4
#define NPOLES 4
static DOUBLE xv[NZEROS+1], yv[NPOLES+1];

/* Sampling rate 3000 for testing existing */
/* raw data.  Change to 300 for final product */
#define SAMPLING_RATE 3000

/*******************************************************************************
 * Function:      ButterworthFilter
 *
 * Description:   This function uses the Butterworth filter and returns a new
 *                value for an individual floating point value.
 *
 * Access:        PRIVATE
 *
 * Parameters:    DOUBLE input - value to be converted
 *
 * Globals:       None
 *
 * Returns:       DOUBLE - new value that has been converted
 ******************************************************************************/
static DOUBLE ButterworthFilter (DOUBLE input)
{ 
    /* http://www-users.cs.york.ac.uk/~fisher/cgi-bin/mkfscript */
    /* Butterworth Bandpass filter */
    /* 2nd order */
    /* sample rate - choice of 300 or 3000 Hz */
    /* corner1 freq. = 0.5 or 1 Hz */
    /* corner2 freq. = 20 Hz */
    /* removes high and low frequency noise */

    DOUBLE dCoefficient1 = 0.0;
    DOUBLE dCoefficient2 = 0.0;
    DOUBLE dCoefficient3 = 0.0;
    DOUBLE dCoefficient4 = 0.0;
    DOUBLE dCoefficient5 = 0.0;
    DOUBLE dGain = 0.0;

    /* coefficients will vary depending on sampling rate */
    /* and cornering frequencies                         */
    switch(SAMPLING_RATE)
    {
        case 300:
            /* 1 to 20 Hz */
            dCoefficient1 = 2.0;
            dCoefficient2 = -0.5698403540;
            dCoefficient3 = 2.5753677309;
            dCoefficient4 = -4.4374523505;
            dCoefficient5 = 3.4318654424;
            dGain = 3.198027802e+01;
            break; 

        case 3000:
        default:
            /* 0.5 to 20 Hz */ 
            dCoefficient1 = 2.0;
            dCoefficient2 = -0.9438788347;
            dCoefficient3 = 3.8299315572;
            dCoefficient4 = -5.8282241502;
            dCoefficient5 = 3.9421714258;
            dGain = 2.406930558e+03;
            break;  
    } 

    xv[0] = xv[1]; 
    xv[1] = xv[2]; 
    xv[2] = xv[3]; 
    xv[3] = xv[4]; 
    xv[4] = (DOUBLE)(input / dGain);
    yv[0] = yv[1]; 
    yv[1] = yv[2]; 
    yv[2] = yv[3]; 
    yv[3] = yv[4]; 
    yv[4] = (DOUBLE)((xv[0] + xv[4]) - (dCoefficient1 * xv[2]) + (dCoefficient2 * yv[0]) +
                    (dCoefficient3 * yv[1]) + (dCoefficient4 * yv[2]) +
                    (dCoefficient5 * yv[3]));

    return (yv[4]);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

我需要用 C 实现巴特沃斯滤波器。获得具有此功能的库或编写代码是否更容易? [关闭] 的相关文章

随机推荐