/***************************************************************** * psd_wel_floatpt.c - C program computes FFT-based PSD using * Welch periodogram ****************************************************************** * System configuration: * * _____ ____ __________ __________ ____ * | | | | | | | | | | * x(n)-->| Buf |-->| BR |-->| (1/N)*FFT|-->| |X(k)|^2 |-->| Av |-> Pi(k) * |_____| |____| |__________| |__________| |____| * ****************************************************************** * System simulation configuration: * * x(n) is the input data from data file "in.dat" * (assuming that the length of x(n) has been zero padded to * the right length for averaged periodogram) * out(n) is the power spectrum data to data file "out.dat" * * Number of segments have been precomputed *****************************************************************/ #include /* header files */ #include #include #include "def_complex.h" /* complex.h header file */ /* external functions */ extern void ditr2fft(complex *, unsigned int, complex *, unsigned int); extern void bit_reversal(complex *, unsigned int); /* variables and constants */ #define N 256 /* number of FFT points */ #define EXP 8 /* EXP=log2(N) */ #define pi 3.1415926535897 #define overlap 128 /* overlap = 50% */ #define K 2 /* K = int[(length(x)-N)/(N-overlap)+1] */ complex X[N]; /* declare input array */ float X_old[overlap]; float X_new[N-overlap]; complex W[EXP]; /* twiddle e^(-j2pi/N) table */ complex temp; float spectrum[K][N]; float spectrum_av[N]; float win[N]; float xn; float U=0.0; void main() { unsigned int i,j,L,LE,LE1; /***************************************************************** * Declare file pointers *****************************************************************/ FILE *xn_in; /* file pointer of x(n) */ FILE *yn_out; /* file pointer of y(n) */ xn_in = fopen("in.dat","r"); /* open file for input x(n) */ yn_out = fopen("out.dat","w"); /* open file for output y(n) */ /* Step 1: Create a twiddle factor table */ for (L=1; L<=EXP; L++) /* Create twiddle factor table */ { LE=1<>1; /* number of butterflies in sub-DFT */ W[L-1].re = cos(pi/LE1); W[L-1].im = -sin(pi/LE1); } /* Create Hamming window and init spectrum_av */ for (i=0;i 0) { for(i=0; i<(N-overlap); i++) { /* new input signal */ fscanf(xn_in,"%f",&xn) ; X_new[i] = xn; } } for(i=0;i= 0) { /* for overlap =< 50% */ for (i=0; i 50% */ for (i=0; i<((2*overlap)-N);i++) { X_old[i] = X_old[i+(N-overlap)]; } for (i=0; i<(N-overlap); i++) { X_old[i+(N-overlap)] = X_new[i]; } } /* Step 3: Perform bit reversal, follow by FFT */ /* Start FFT */ bit_reversal(X,EXP); /* arrange X[] in bit-reverse order */ ditr2fft(X,EXP,W,1); /* perform FFT with scaling of 0.5 in each stage */ /* Step 4: Perform magnitude-square and display results in MATLAB */ for (i=0; i