📄 mov_avg.c
字号:
/*************************************************************
* mov_avg.c - C program for moving-average filtering
**************************************************************
* System configuration:
*
* x(n) |-----------------------| y(n)
* ---->| moving-average filter |----->
* |-----------------------|
* x(n) is the input data from data file "xn.dat"
* y(n) is the output data to data file "yn.dat"
*************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
const float bnCoef[4] = {0.25, 0.25, 0.25, 0.25};
void main()
{
void datamov(); // function to update signal vector
float fir(); // function to perform FIR filtering
/*************************************************************
* Define variables and arrays
*************************************************************/
int i; // array index
int order = 4; // order of moving-average filter
int input; // input data from integer file
float xn = 0; // x(n), input from xn.dat file
float yn = 0.0; // y(n), output from FIR filter
float xnBuf[4]; // signal buffer for FIR filter
/**********************************************************
* Declare file pointers, open files, clear array
**********************************************************/
FILE *xn_in; // file pointer of x(n)
FILE *yn_out; // file pointer of y(n)
xn_in = fopen("xn.dat","r"); // open file for input x(n)
yn_out = fopen("yn.dat","w");// open file for output y(n)
for (i=0; i<order; i++)
xnBuf[i] = 0.; // clear signal buffer
/**********************************************************
* Start of main program
**********************************************************/
while( (fscanf(xn_in,"%d",&input)) != EOF)
{ // read x(n) from data file
xn = (float)input; // convert to floating-point
datamov(xnBuf, order, xn);// refresh signal buffer
yn = fir(xnBuf, bnCoef, order);// FIR filtering
fprintf(yn_out,"%d\n",(int)(yn+0.5));
} // round to integer and write y(n) to output file
fcloseall(); // close all opened files
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -