1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
| #include <stdio.h> #include <malloc.h>
void print(float *x,int len);
float* aminusone(float* a,int ar);
float gtcet(float *t,float *x,int xr,int s,int d);
float* gfilter(float *b,int br,float *a,int ar,float *x,int xr);
void print(float *x,int len) { for(int i=0;i<len;i++) {printf("%9.3f ",x[i]); if((i%7)==6) printf("\n");} printf("\n"); }
float* aminusone(float* a,int ar){ float* am = (float *)malloc((ar-1) * sizeof(float)); for(int i=0;i<ar-1;i++) am[i]=a[i+1]; return am; } float gtcet(float *t,float *x,int xr,int s,int d) { float sum=0; float* w = (float *)malloc(d * sizeof(float)); for(int i=0;i<d;i++) { int temp=s-i; if(temp>=0&&temp<xr) w[i]=x[temp]; else w[i]=0; sum=sum+w[i]*t[i]; } return sum; } float* gfilter(float *b,int br,float *a,int ar,float *x,int xr) { float* y = (float *)malloc(xr* sizeof(float)); for(int ti=0;ti<xr;ti++) y[ti]=0; for(int i=0;i<xr;i++) { y[i]=gtcet(b,x,xr,i,br)-gtcet(aminusone(a,ar),y,xr,i-1,ar-1); } return y; }
int main() { float x[120]={-4, -2, 0, -4, -6, -4, -2, -4, -6, -6, -4, -4,\ -6, -6, -2, 6, 12, 8, 0, -16, -38, -60, -84,\ -90, -66, -32, -4, 2, -4, 8, 12, 12, 10, 6,\ 6, 6, 4, 0, 0, 0, 0, 0, -2, -4, 0, 0, 0, -2,\ -2, 0, 0, -2, -2, -2, -2, 0, -2, -4, -2, 0,\ -2, -4, -4, 2, 0, 0, -2, -4, -2, 0, 0, -2,\ -4, -2, 0, 0, -4, -4, -2, -2, -4, -6, -6,\ -4, -4, 8, -10, -8, -6, -6, -8, -12, -10,\ -8, -8, -10, -12, -10, -8, -8, -10, -10,\ -8, -6, -6, -8, -8, -4, -2, -4, -4, -4,\ 0, 0, -2, -4, -2, -2, 0, -4}; float b[7]={ 0.0007,0.0044,0.0111,0.0148,0.0111,0.0044,0.0007}; float a[7]={ 1.0000,-3.1836,4.6222,-3.7795,1.8136,-0.4800,0.0544}; int xr=sizeof(x)/sizeof(float); int br=sizeof(b)/sizeof(float); int ar=sizeof(a)/sizeof(float); float *y;
printf("参数b:\n");print(b,br); printf("参数a:\n");print(a,ar); printf("原数据:\n");print(x,xr);
y=gfilter(b,br,a,ar,x,xr);
printf("滤波后数据:\n");print(y,xr); getchar(); return 0; }
|