#include <stdio.h> #include <math.h> #include <chplot.h> #include <numeric.h> #define N 512 int main() { array double t[N], x[N], y[N], Pyy[N/2], f[N/2], u[7], v[7]; array double complex Y[N]; int i; class CPlot plot; /* The filter arguments u[i] and v[i] are designed according to the characteristic of filter specified. In this example program, we use the designed filter to filter the raw data. */ u[0]=1;u[1]=-5.66792131;u[2]=13.48109005;u[3]=-17.22250511; u[4]=12.46418230;u[5]=-4.84534157;u[6]=0.79051978; v[0]=0.00598202; v[1]=-0.02219918; v[2]=0.02645738; v[3]=0; v[4]=-0.02645738;v[5]=0.02219918;v[6]=-0.00598202; linspace(t,0,511); t = t/N; for (i=0; i<N; i++) { x[i] = sin(2*M_PI*5*t[i]) + sin(2*M_PI*15*t[i]) + sin(2*M_PI*t[i]*30); x[i]=x[i]+3*(urand(NULL)-0.5); } filter(v,u,x,y); plotxy(t,x,"Time domain original signal","Time (second)","Magnitude "); plotxy(t,y,"Time domain filtered signal","Time (second)","Magnitude "); fft(Y,x); for (i=0; i<N/2; i++) Pyy[i] = abs(Y[i]); linspace(f,0,N/2); plotxy(f,Pyy,"Frequency domain Original signal","frequency (Hz)","Magnitude (db)"); fft(Y,y); for (i=0; i<256; i++) Pyy[i] = abs(Y[i]); linspace(f,0,255); plotxy(f,Pyy,"Frequency domain filtered signal","frequency (Hz)","Magnitude (db)"); }