📄 firfltr.c
字号:
/**********************************************************************
* firfltr.c - An FIR filter
*
* Block diagram:
* --------
* x(n) --->| b(n) |---> y(n)
* --------
* where
* x(n) is input signal
* b(n) is impulse response (coefficients) of FIR filter
* y(n) is filtered output
*
* This program is executed by keying in the following command:
*
* firfltr infile coefile outfile <enter>
*
* where
* infile is file name of input signal
* coefile is file name of filter coefficients
* outfile is file name output signal
*
**********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAX 2048 /* maximum order of filter */
void main(int argc, char *argv[])
{
void shift(float *, int, float); /* function to update signal vector
*/
float fir(float *, float *, int); /* function to perform FIR
filtering */
/*****************************************************************
* Define and initialize variables
*****************************************************************/
int len_imp = 0; /* order of filter */
float xn; /* x(n), input signal */
float bn; /* b(n), filter coefficients */
float yn; /* y(n), output signal */
float bn_coef[MAX]; /* b(n) vector */
float xn_buf[MAX]; /* x(n) vector */
FILE *fpin; /* file pointer of x(n) */
FILE *fpimp; /* file pointer of b(n) */
FILE *fpout; /* file pointer of y(n) */
/*****************************************************************
* Define usage of executable file
*****************************************************************/
if (argc < 4)
{
fprintf(stderr,"Usage: firfltr infile coefile outfile<enter>\n");
fprintf(stderr," infile is input file name\n");
fprintf(stderr," coefile is coefficients file name\n");
fprintf(stderr," outfile is output file name\n");
exit(0);
}
if ((fpin = fopen(argv[1],"rb")) == NULL) /* open input file */
{
fprintf(stderr,"Can't open input data file %s!\n",argv[1]);
exit(0);
}
if ((fpimp = fopen(argv[2],"rb")) == NULL) /* open coefficients file
*/
{
fprintf(stderr,"Can't open coefficients file %s!\n",argv[2]);
exit(0);
}
fpout = fopen(argv[3],"wb"); /* open output file */
while((fscanf(fpimp,"%f",&bn)) != EOF)
{
bn_coef[len_imp] = (float)bn; /* read coefficients */
xn_buf[len_imp] = 0.; /* clear x(n) vector */
len_imp++; /* order of filter */
}
/*****************************************************************
* Filtering of x(n) by FIR filter b(n) to get y(n)
*****************************************************************/
while(fread(&xn, sizeof(float), 1, fpin) == 1)
{
shift(xn_buf, len_imp, xn); /* update x(n) vector */
yn = fir(xn_buf,bn_coef,len_imp); /* FIR filtering */
fwrite(&yn, sizeof(float), 1, fpout); /* write output y(n) */
}
fcloseall(); /* close all data files */
}
/*****************************************************************
* SHIFT - This function updates signal vector of order N
* data stored as [x(n) x(n-1) ... x(n-N+1)]
*****************************************************************/
void shift(float *x, int N, float new)
{
int i; /* loop index */
for (i=N-1; i>0; i--)
{
x[i] = x[i-1]; /* shift old data x(n-i) */
}
x[0] = new; /* insert new data x(n) */
return;
}
/*****************************************************************
* FIR - This function performs FIR filtering (linear convolution)
* ntap-1
* y(n) = sum hi * x(n-i)
* i=0
*****************************************************************/
float fir(float *x, float *h, int ntap)
{
float yn = 0.0; /* output of FIR filter */
int i; /* loop index */
for (i=0; i<ntap; i++)
{
yn += h[i]*x[i]; /* convolution of x(n) with h(n) */
}
return(yn); /* return y(n) to main function */
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -