Sie sind auf Seite 1von 6

Computer Assignment 4.1 1.

The following C program is to compute the magnitude and phase spectra of the signal using the FFT function.
#include #include #define void <stdio.h> <math.h> N 256 fft(double X[], double Y[], int npt, int inv);

void main() { int n; double X[N], Y[N], magnit, phase; // X[N]:real part array, Y[N]:imaginary part FILE *in, *out1, *out2; // in:input file, out1:magnitude, out2:phase in = fopen("signal.dat", "r"); out1 = fopen("magnit.dat", "w"); out2 = fopen("phase.dat", "w"); for (n=0; n<N; n++) { fscanf (in, "%lf", &X[n]); Y[n] = 0; } fft (X, Y, 256, 0); // X&Y: real and imaginary parts of signal and DFT for (n=0; n<N; n++) { magnit = sqrt( X[n]*X[n] + Y[n]*Y[n] ); // Compute a magnitude response phase = atan2(Y[n],X[n]); // Phase response fprintf (out1, "%f\n", magnit); fprintf (out2, "%f\n", phase); } fclose(in); fclose(out1); fclose(out2); } #define PI 3.14159265359 void fft( double xr[], double xi[], int npt, int inv ) { int i, j, k, irem, m, l, le, le1, ip, sign; double ur, ui, wr, wi, tr, ti, temp; j = 0; for (i=0; i<(npt-1); i++) { if (i<j) { tr = xr[j]; ti = xi[j]; xr[j] = xr[i]; xi[j] = xi[i]; xr[i] = tr; xi[i] = ti; } k = npt/2; while (k<=j) { j = j-k; k = k/2; } j = j + k; } m = 0; irem = npt; while (irem>1) { irem = irem/2; m = m + 1; } if (inv==1) sign = 1; else sign = -1;

le = 1; for (l=1; l<=m; l++) { le = le*2; le1 = le/2; ur = 1.0; ui = 0; wr = cos(PI/le1); wi = sign * sin(PI/le1); for (j=0; j<le1; j++) { i = j; while(i < npt) { ip = i + le1; tr = xr[ip]*ur - xi[ip]*ui; ti = xi[ip]*ur + xr[ip]*ui; xr[ip] = xr[i] - tr; xi[ip] = xi[i] - ti; xr[i] = xr[i] + tr; xi[i] = xi[i] + ti; i = i + le; } temp = ur*wr - ui*wi; ui = ui*wr + ur*wi; ur = temp; } } if(inv == 1) { for (i=0; i<npt; i++) { xr[i] = xr[i]/npt; xi[i] = xi[i]/npt; } }
}

2. The following C program is to design a 16-point half-band lowpass filter using the Blackman window and computes the frequency response of the filter.
#include <stdio.h> #include <math.h> #define N 1024 #define PI 3.14159265359 void fft(double xr[], double xi[], int npt, int inv); void main() { int n; double hrec[16], hbla[16], xr[N], xi[N], mag, pha, arg; double norm; FILE *filter, *out; filter = fopen("filter.dat", "w"); out = fopen("blac.dat", "w"); for (n=0; n<=7; n++) // LPF design with rectangular window { arg = (.5 + n) * 0.5 * PI; // argument for sinc function hrec[8+n] = .5 * sin(arg)/arg; // 0.5*sinc(arg) hrec[7-n] = hrec[8+n]; // Use even symmetric property } for (n=0; n<=15; n++) // Compute LPF coefficients using Blackman window { hbla[n] = hrec[n]*(.42 - .5*cos(2*PI*(n+1)/17) + .08*cos(4*PI*(n+1)/17) ); } norm = 0.; for (n=0; n<=15; n++) norm = norm + hbla[n]; // Find the frequency response at dc for (n=0; n<=15; n++) { hbla[n] = hbla[n]/norm; // Normalize filter coefficients

fprintf (filter, "%d\t%f\n", n, hbla[n]); } for for for fft for { (n=16; n<N; n++) xr[n] = 0.0; (n=0; n<=15; n++) xr[n] = hbla[n]; (n=0; n<N; n++) xi[n] = 0.0; (xr, xi, N, 0); (n=0; n<N; n++) mag = xr[n]*xr[n] + xi[n]*xi[n]; mag = 10*log10(mag); pha = atan2(xi[n], xr[n]); fprintf (out, "%f\t%f\n", mag, pha); } fclose(filter); fclose(out); } // Pad extra zeros out // Copy filter coefficients // Make imaginary part zero

The resulting filter coefficients are given below.


0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 -0.000387 -0.002004 0.006093 0.014763 -0.031427 -0.062955 0.132130 0.443787 0.443787 0.132130 -0.062955 -0.031427 0.014763 0.006093 -0.002004 -0.000387

(a) signal 4 2 0 -2 -4 0 200 150 100 50 0 0 4 2 0 -2 -4 0 50 100 150 200 250 300 50 100 150 (c ) phas e 200 250 300 50 100 150 (b) m agnitude 200 250 300

Fig. 3P-1 Signal and its magnitude and phase spectra

3. The magnitude and phase response of the filter are shown below.

lowpas s m agnitude response 0

-50

-100

-150 0 0 -10 -20 -30 -40 0 0 200 400 600 800 highpas s m agnitude response 1000 1200 200 400 600 800 lowpas s phase res pons e 1000 1200

-50

-100

-150 0 200 400 600 800 1000 1200

Fig. 3P-2 Magnitude and phase response of the half-band lowpass filter and magnitude response of the half-band highpass filter

4. The following C program is to implement the pair of lowpass and highpass filters.
#include #define #define <stdio.h> N 16 N1 15

void main() { int n; double buf[N], h[N], top, bottom, low, high; FILE *in, *filt, *out1, *out2; in = fopen("signal.dat", "r"); filt = fopen("filter.dat", "r");

out1 = fopen("lpf.dat", "w"); out2 = fopen("hpf.dat", "w"); for (n=0; n<N; n++) fscanf (filt, "%lf\n", &h[n]); // Read LPF coefficients for (n=0; n<=N1; n++) buf[n] = 0.; // Initialize buffer with zero while ( !feof(in) ) // As long as there is data in the file, { // the filtering process is continued. fscanf (in, "%lf\n", &buf[0]); top = bottom = 0.; for (n=0; n<N; n=n+2) top = top + h[n]*buf[n]; // Even indexed coefficients for (n=1; n<N; n=n+2) bottom = bottom + h[n]*buf[n]; // Odd indexed coefficients low = top + bottom; // Compute LPF output high = top - bottom; // Compute HPF output fprintf (out1, "%f\n", low); fprintf (out2, "%f\n", high); for (n=N1; n>0; n--) buf[n] = buf[n-1]; // Update buffer } fclose(in); fclose(filt); fclose(out1); fclose(out2); }

lowpas s output 2 1 0 -1 -2 0 2 1 0 -1 -2 0 50 100 150 200 250 300 50 100 150 highpass output 200 250 300

Fig. 3P-3 Lowpass and highpass filter outputs

Das könnte Ihnen auch gefallen