C 中的双二阶滤波器示例实现

Biquad Filter Example Implementation in C

我正在为嵌入式系统开发低通双二阶滤波器。在 C 中搜索示例,我在斯坦福大学的网站上找到了这个 link:https://ccrma.stanford.edu/~jos/filters/Biquad_Software_Implementations.html。将代码添加到我的 Eclipse 项目中的 filter.c 文件后,出现以下构建错误:

1-“typedef word double”语法错误。

2- 无法解析类型 'word'(对于“typedef struct _biquadVars”中的 s2、s1、gain、a2、a1、b2 和 b1)。

3- 类型 'dbl' 无法解析(对于“void biquad(biquadVars *a)”中的“A”)。

4- 无法解析类型 'word'(对于“void biquad(biquadVars *a)”中的“s0”)。

5- 无法解析符号“NTICK”(在“void biquad(biquadVars *a)”中)。

我做了什么:

1- 更改了“typedef word double;”到“typedef 双字;”。这解决了相关的错误。 2- 更改了“dbl A;”到“双A;”。显然有帮助。

问题:

1- 我应该相信原始文章并搜索更多关于为什么选择“word”而不是“double”的文章吗?还是“dbl”是我不知道的类型?

2- 知道 NTICK 是什么吗?

谢谢。

编辑(添加示例代码):

typedef double *pp;// pointer to array of length NTICK
typedef word double; // signal and coefficient data type

typedef struct _biquadVars {
    pp output;
    pp input;
    word s2;
    word s1;
    word gain;
    word a2;
    word a1;
    word b2;
    word b1;

} biquadVars;

void biquad(biquadVars *a)
{

    int i;
    dbl A;
    word s0;
    for (i=0; i<NTICK; i++) {
    
        A = a->gain * a->input[i];
        A -= a->a1 * a->s1;
        A -= a->a2 * a->s2;
        s0 = A;
        A += a->b1 * a->s1;
        a->output[i] = a->b2 * a->s2 + A;
        a->s2 = a->s1;
        a->s1 = s0;

    }
}

typedef 的用法是 typedef <native C type> <alias symbol>
其目的通常是将原生 C type 符号映射到某个新符号,以提高特定代码库中的可读性或相关性。在这种情况下,参数只是颠倒过来:

typedef word double;//two issues, word is not a native type, double is, reverse them.

应该是

typedef double word;  creates a new type 'word', equivalent to double  

还要注意这一点,下面也会有问题:

typedef double int;//attempting to typedef to a native type is not allowed

导致错误类似于:无法与先前的 'double' 声明说明符组合

而且,下面没有语法错误,没问题:

typedef double * pp; //creates a new type pp equivalent to double *
  • 1- 我应该相信原始文章并搜索更多关于为什么选择“word”而不是“double”的文章吗?还是“dbl”是我不知道的类型?

没有原生类型dbl,这可能是文章中的错字。查看此类算法的多个来源永远不会有坏处。如果您有权访问同行评审的文章,请尝试包括它们,即来自 IEEE 等来源的文章。

  • 2- 知道 NTICK 是什么吗?

不能确定,但​​考虑到它的使用背景

for (i=0; i<NTICK; i++) {

它可能是 #define 某个最大刻度值。即 1000 个时钟滴答

#define NTICK 1000