数字滤波器算法
我刚刚在维基百科上找到了一个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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它用于确保 ptr_x_buffer 始终是用作循环缓冲区的 x_buffer 数组内的有效索引。每当
ptr_x_buffer
溢出数组大小时,它就会重置为0
。ptr_x_buffer
会随着函数的每次调用以及 x_buffer[ptr_x_buffer++] 中循环的每次迭代而递增。您还可以将行
ptr_x_buffer %= NB_COEF;
替换为:或者,如果您确定
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 thex_buffer
array which is used as a cyclic buffer. Wheneverptr_x_buffer
would overflow the array size it gets reset to0
.ptr_x_buffer
gets incremented with each invocation of the function and on each iteration of the loop inx_buffer[ptr_x_buffer++]
.You could also replace the lines
ptr_x_buffer %= NB_COEF;
with this: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;
.很简单,
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++
incrementsptr_x_buffer
;ptr_x_buffer %= NB_COEF
resetsptr_x_buffer
to zero as soon as it reachesNB_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.表达式
ptr_x_buffer%=NB_COEFF
表示ptr_x_buffer
以NB_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 thatptr_x_buffer
is resetted moduloNB_COEFF
. Therefore it is not "always 0" but the modulus with respect to division by 16. Therefore it is guaranteed that the array accessx_buffer[ptr_x_buffer]
always lies within the bounds0..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.