#define N 10 // number of FIR coefficients.
#define BLKSIZE 20 // block size of samples filtered each time.
#define M (N + BLKSIZE - 1)
float workbuf[M] = {0}; // history + current input block, initialize to 0.
// float h[N]: array of fir filter coefficients.
// float x[BLKSIZE]: array of input samples block
// float y[BLKSIZE]: array of samples block after filtering
void fir(float h[N], float x[BLKSIZE], float y[BLKSIZE])
{
int i, j;
float *p;
// copy current input block to work buffer
// workbuf[N-1] = x[0], workbuf[N] = x[1],
// workbuf[N+1] = x[2], ..., workbuf[M-1] = x[BLKSIZE-1];
for (i=0; i<BLKSIZE; i++)
{
workbuf[i+N-1] = x[i];
}
// FIR filter loop
for (i=0; i<BLKSIZE; i++)
{
p = &workbuf[i+N-1];// each y[i] needs right starting x[i].
for (j=0; j<N; j++)
{
y[i] += (h[j] * (*p--));
}
}
// copy last N-1 samples of x[n] to workbuf
// as history for next block
p = &x[BLKSIZE-1]; // p points the last element;
for (i=0; i<N-1; i++)
{
workbuf[N-2-i] = *p--;
}
}
临时写的,没有验证,不保证没有错误。 |