数字滤波器算法

发布于 2024-11-11 13:52:50 字数 999 浏览 3 评论 0原文

我刚刚在维基百科上找到了一个fir算法

http://en.wikipedia.org/wiki/Digital_filter

// if the size of NB_COEF = 2^n use a bit mask instead of the modulo (%)
// %=NB_COEF => &=(NB_COEF-1)
// pipe is a circular buffer

#define NB_COEF 16  // numbers of coefficients of the filter
double x_buffer[NB_COEF]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
double coef[NB_COEF]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int ptr_x_buffer=0;

double FiltreFIR(double x)
// x: Signal d'entrée
// y: Signal de sortie
{
  int n, y=0;
  x_buffer[ptr_x_buffer++] = x;
  ptr_x_buffer %= NB_COEF;

  for( n = (NB_COEF-1) ; n >= 0 ; n-- )
  {
    y += coef[n] * x_buffer[ptr_x_buffer++];
    ptr_x_buffer %= NB_COEF;
  }
  return(y);
}

-任何人都可以告诉我为什么我们需要不断地执行这个表达式

ptr_x_buffer%= NB_COEFF.

因为对我来说这意味着变量 ptr_x_buffer 总是取值 0 ?! 在我看来这与逻辑相去甚远?!

有人可以向我解释关于位掩码和模数的第一条评论吗?

先感谢您 :)

I just found a fir algorithm on wikipedia

http://en.wikipedia.org/wiki/Digital_filter

// if the size of NB_COEF = 2^n use a bit mask instead of the modulo (%)
// %=NB_COEF => &=(NB_COEF-1)
// pipe is a circular buffer

#define NB_COEF 16  // numbers of coefficients of the filter
double x_buffer[NB_COEF]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
double coef[NB_COEF]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int ptr_x_buffer=0;

double FiltreFIR(double x)
// x: Signal d'entrée
// y: Signal de sortie
{
  int n, y=0;
  x_buffer[ptr_x_buffer++] = x;
  ptr_x_buffer %= NB_COEF;

  for( n = (NB_COEF-1) ; n >= 0 ; n-- )
  {
    y += coef[n] * x_buffer[ptr_x_buffer++];
    ptr_x_buffer %= NB_COEF;
  }
  return(y);
}

-Can anybody tell me why do we need constantly to do this expression

ptr_x_buffer%= NB_COEFF.

Because for me it would mean the variable ptr_x_buffer always take the value 0 ?!
And it seems to me far from logic?!

And also can somebody explain to me the first comment about bit masking and modulo.

thank you in advance
:)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

红衣飘飘貌似仙 2024-11-18 13:52:50

它用于确保 ptr_x_buffer 始终是用作循环缓冲区的 x_buffer 数组内的有效索引。每当 ptr_x_buffer 溢出数组大小时,它就会重置为 0

ptr_x_buffer 会随着函数的每次调用以及 x_buffer[ptr_x_buffer++] 中循环的每次迭代而递增。

您还可以将行 ptr_x_buffer %= NB_COEF; 替换为:

 if( ptr_x_buffer == NB_COEF )
     ptr_x_buffer = 0;

或者,如果您确定 NB_COEF 是 2 的幂,则可以使用位掩码屏蔽它们(如果是)评论中已说明:ptr_x_buffer &= NB_COEF-1;

It is used to ensure that the ptr_x_buffer is always a valid index inside the x_buffer array which is used as a cyclic buffer. Whenever ptr_x_buffer would overflow the array size it gets reset to 0.

ptr_x_buffer gets incremented with each invocation of the function and on each iteration of the loop in x_buffer[ptr_x_buffer++].

You could also replace the lines ptr_x_buffer %= NB_COEF; with this:

 if( ptr_x_buffer == NB_COEF )
     ptr_x_buffer = 0;

Or if you are sure that NB_COEF is a power of 2 you could mask them with a bitmask if as is stated in the comment already: ptr_x_buffer &= NB_COEF-1;.

寂寞美少年 2024-11-18 13:52:50

很简单,x_buffer 是一个循环缓冲区; ptr_x_buffer 指向缓冲区中的当前位置:

  • ptr_x_buffer++ 递增 ptr_x_buffer
  • ptr_x_buffer %= NB_COEF 一旦达到 NB_COEF,就会将 ptr_x_buffer 重置为零。

该评论建议针对 NB_COEF 的某些值修改代码。无论是谁写下这条评论,似乎都认为建议的修改是性能的改进。然而,这一改变是否会带来更好的性能是非常值得怀疑的,因此,可以忽略该评论。

Quite simply, x_buffer is a circular buffer; ptr_x_buffer points to the current location in the buffer:

  • ptr_x_buffer++ increments ptr_x_buffer;
  • ptr_x_buffer %= NB_COEF resets ptr_x_buffer to zero as soon as it reaches NB_COEF.

The comment suggests a modification of the code for certain values of NB_COEF. Whoever wrote that comments appears to have considered the suggested modification to be a performance improvement. However, it is highly doubtful that the change would lead to better performance and, therefore, the remark can be ignored.

爱的十字路口 2024-11-18 13:52:50

表达式 ptr_x_buffer%=NB_COEFF 表示 ptr_x_bufferNB_COEFF 为模进行重置。因此它不是“始终为 0”,而是除以 16 的模数。因此可以保证数组访问 x_buffer[ptr_x_buffer] 始终位于范围 0..NB_COEFF- 1..

对于 2 的幂,模运算可以用按位和掩码 2^n-1(或 N-1)替换,其中 n 是模数的 log2。

The expression ptr_x_buffer%=NB_COEFF means that ptr_x_buffer is resetted modulo NB_COEFF. Therefore it is not "always 0" but the modulus with respect to division by 16. Therefore it is guaranteed that the array access x_buffer[ptr_x_buffer] always lies within the bounds 0..NB_COEFF-1.

For powers of 2 the modulo operation can be replaced by a bitwise and with a mask of 2^n-1 (or N-1) where n is the log2 of your modulus.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文