#include "stdio.h" #include "stdlib.h" #include "math.h" #define TRUE 1 #define FALSE 0 #define TWOPI 6.283185307179586476925287 int WriteSeries(char *,double *,double *,double *,int); int FFT(int,int,double *,double *); /* Test-bed for Fourier digital filter design Illustrative code only....not a "product". */ #define N 1024 /* Number of samples */ #define NP 10 /* Power of 2, 2^NP = N */ #define FS 500.0 /* Sampling frequency */ #define F1 50.0 /* Cutoff frequency */ #define W 40 /* Desired filter width */ #define RECT 0 #define HANN 1 #define WINDOW 1 int main(int argc,char **argv) { int i; double re[N],im[N],freq[N],time[N],window[N],mag[N],pha[N]; int floorMid,mid,ceilMid; double tmp[N]; /* Fill in the time and frequency indices These are only used to give more informative file output although they are useful to ensure you understand the data arrangement */ for (i=0;i= (N - F1 * N / FS)) re[i] = 1; im[i] = 0; } WriteSeries("impulse1.f",freq,re,im,N); /* Inverse FFT the signal into the time domain If the filter is created properly then the imaginary components should be zero....baring numerical error. */ FFT(-1,NP,re,im); WriteSeries("impulse.t",time,re,im,N); /* Create the window so to make the series finite duration */ for (i=0;i= N - W/2) { if (WINDOW == RECT) window[i] = 1; else window[i] = (0.5 + 0.5 * cos((N - i) * TWOPI / (W+1))); } } WriteSeries("window.t",time,window,im,N); /* Apply the window */ for (i=0;i=N/2;i--) re[i] = re[i - W/2]; for (i=0;i x(k) e = forward transform N / n=0..N-1 --- k=0 Formula: reverse N-1 --- \ j k 2 pi n / N X(n) = > x(k) e = forward transform / n=0..N-1 --- k=0 */ int FFT(int dir,int m,double *x,double *y) { long nn,i,i1,j,k,i2,l,l1,l2; double c1,c2,tx,ty,t1,t2,u1,u2,z; /* Calculate the number of points */ nn = 1; for (i=0;i> 1; j = 0; for (i=0;i>= 1; } j += k; } /* Compute the FFT */ c1 = -1.0; c2 = 0.0; l2 = 1; for (l=0;l