📄 fir.c
字号:
/***************************************************************************/
/* */
/* Experiement1 . C */
/* */
/* File In/Out and processing in a main loop */
/* */
/* */
/* 2004/04/04 */
/***************************************************************************/
#include <stdio.h>
#include <math.h>
#define TRUE 1
#define FALSE 0
#define N 8
#define BUFSIZE 100 /* the buffer size is 100 */
/* Global declarations */
int in_buffer[BUFSIZE]; /* processing data buffers */
int out_buffer[BUFSIZE];
/* Functions */
static int processing(int *input, int *output);
static void dataIO(void);
static long round(long a);
void main()
{
int *input = &in_buffer[0];
int *output = &out_buffer[0];
puts("the 1st experiment started\n");
/* loop forever */
while(TRUE)
{
/*
* Read input data using a probe-point connected to a host file.
* Write output data to a graph connected through a probe-point.
*/
// read the input signal.
// if the input file is sine1.dat, the signal contains 300Hz,400Hz and 500Hz.
// if the input file is sine2.dat, the signal contains 100Hz,400Hz and 500Hz.
// the sampling frequency is 1200Hz.
dataIO();
/* Remove the frequency compoment of 400Hz and 500Hz*/
processing(input, output);
// write the output signal.
// the output file is result.dat.
dataIO();
}
}
/*
* ======== processing ========
*
* FUNCTION: apply a Low-Pass FIR filter to input signal and remove the frequency higher than 350Hz.
*
* PARAMETERS: address of input and output buffers.
*
* RETURN VALUE: TRUE.
*/
static int processing(int *input, int *output)
{
int i,size = BUFSIZE;
short xx0,x,y;
// short z[N]={0,0,0,0,0,0,0,0,0};
short z[N]={0,0,0,0,0,0,0,0};
//short w[2*N+1]={22,356,155,990,466,220,777,216,777,26,466,9,155,0,22};
//short w[2*N+1]={6,457,56,1024,224,418,523,382,784,99,784,43,523};
// short w[2*N+1]={330*2,3299*2,1982*2,6867*2,4955*2,1594*2,6607*2,1065*2,4955*2,109*2,1982*2,17*2,330*2};
//short w[2*N+1]={661,6598,3964,13733,9910,3187,13214,2131,9910,217,3964,34,661};
// short w[2*N+1]={58,5628,526,8192,2105,5883,4913,3829,7369,1543,7369,504,4913,102,2105,14,526,1,58};
//short w[2*N+1]={28,4432,280,8192,1259,4883,3356,3975,5873,1509,7048,644,5873,142,3356,30,1259,3,280,0,28};
// short w[2*N+1]={26,651,182,1024,545,421,909,247,909,51,545,11,182,1,26};
//short w[2*N+1]={831,20846,5815,32768,17445,13486,29075,7888,29075,1647,17445,349,5815,21,831};
//short w[2*N+1]={208,5211,1454,8192,4361,3371,7269,1972,7269,412,4361,87,1454,5,208};
short w[2*N+1]={101,4356,810,8192,2835,3403,5670,2517,7088,605,5670,193,2835,21,810};
// short w[2*N+1]={101,4356,810,8192,2835,3403,5670,2517,7088,605,5670,193,2835,21,810,2,101};
// short w[2*N+1]={50,3814,454,8192,1815,3504,4235*,3084,6353,831,6353,349,4235,50,1815,8,454,0,50};
long y0,z0;//22222222222222
while(size--){
xx0=*input++;
x=xx0*6;
z0=(long)x<<15;
y0=0;
for(i=0;i<N;i++)
{
z0-=(long)w[2*i+1]*(long)z[i];
y0+=(long)w[2*i+2]*(long)z[i];
}
y0+=(long)w[0]*(z0>>15);
y0=round(y0);
for(i=N-1;i>0;i--)
z[i]=z[i-1];
z0=round(z0);
z[0]=(short)(z0>>15);
y=(short)(y0>>15);
*output++ =y;
}
/* additional processing load */
return(TRUE);
}
/*
* ======== dataIO ========
*
* FUNCTION: read input signal and write processed output signal.
*
* PARAMETERS: none.
*
* RETURN VALUE: none.
*/
static void dataIO()
{
/* do data I/O */
return;
}
static long round(long a)
{
long x3;
x3=a&(0xffff0000);
return x3;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -