Sei sulla pagina 1di 4

Computer Assignment 3.

1
1.

Omitted.

2.

The following C program is to compute the magnitude and phase spectra of the
signal s(n) = 2cos[2(.05)n] + sin[2(.2)n] for n = 0, 1, 2, ", 255.

#include
#include
#define
#define
void

<stdio.h>
<math.h>
PI 3.14159265359
N
256
dft(double s[], double x[], double y[]);

void main()
{
int n;
double s[N], X[N], Y[N], magnit, phase; // X[N]:real part of DFT, 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", &s[n]);
dft (s, X, Y);
// s: time-domain sequence, X&Y: real and imaginary parts of DFT
for (n=0; n<N; n++)
{
magnit = sqrt( X[n]*X[n] + Y[n]*Y[n] );
// Compute the 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);
}
void dft(double s[], double x[], double y[]) // s[]:input, x[]&y[]:outputs
{
int n, k;
for (k=0; k<N; k++)
{
x[k] = 0.;
y[k] = 0.;
for (n=0; n<N; n++)
{
x[k] = x[k] + s[n]*cos(2.*PI*k*n/N);
// Computation of DFT
y[k] = y[k] - s[n]*sin(2.*PI*k*n/N);
}
}
}

300

4
2

200
0
100
-2
0

-4
0

100

200

(a) Magnitude spectrum

300

100

(b) Phase spectrum

200

300

3.

The following C program is to compute coefficients of a 16-point FIR lowpass


filter with c = 0.2 using three types of windows: the rectangular window, the
Hamming window, and the Blackman window. The frequency responses of the
filters are computed using the function dft().

//
Lowpass Fiter Design Using Windows
#include
<stdio.h>
#include
<math.h>
#define
PI 3.1415926536
#define
N
1000
void
magdft(double x[], double mag[]);
void
dft(double s[], double XR[], double YI[]);
void main()
{
int n;
double hrec[16], hham[16], hbla[N], x[N], mag[N], arg;
double norm1, norm2, norm3;
FILE
*filter, *out1, *out2, *out3;
filter = fopen("filter.dat", "w");
out1 = fopen("rect.dat", "w");
out2 = fopen("hamm.dat", "w");
out3 = fopen("blac.dat", "w");
for (n=0; n<=7; n++)
// LPF design with rectangular window
{
arg = (.5 + n) * 0.2 * PI;
// argument for sinc function
hrec[8+n] = .2 * 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
{
hham[n] = hrec[n]*(0.54 - 0.46*cos(2*PI*n/15) );
// Hamming window
hbla[n] = hrec[n]*(.42-.5*cos(2*PI*n/15)+.08*cos(4*PI*n/15)); //Blackman window
}
norm1 = norm2 = norm3 = 0.;
for (n=0; n<=15; n++)
// Find the frequency response at dc
{
norm1 = norm1 + hrec[n];
norm2 = norm2 + hham[n];
norm3 = norm3 + hbla[n];
}
for (n=0; n<=15; n++)
// Normalize filter coefficients
{
hrec[n] = hrec[n]/norm1;
hham[n] = hham[n]/norm2;
hbla[n] = hbla[n]/norm3;
}
fprintf (filter, "\tRectangular\tHamming\t\tBlackman\n");
for (n=0; n<=15; n++) fprintf (filter,"%d\t%f\t%f \t%f\n",n, hrec[n],hham[n],hbla[n]);
for (n=16; n<N; n++) x[n] = 0.0;
// Pad extra zeros
for (n=0; n<=15; n++) x[n] = hrec[n];
// Copy rectangular filter coefficients
magdft (x, mag);
// Magnitude response of LPF rectangular window
for (n=0; n<N; n++) fprintf (out1, "%f\n", mag[n]);
for (n=0; n<=15; n++) x[n] = hham[n];
// Copy Hamming filter coefficients
magdft (x, mag);
// Magnitude response of LPF using Hamming window
for (n=0; n<N; n++) fprintf (out2, "%f\n", mag[n]);
for (n=0; n<=15; n++) x[n] = hbla[n];
// Copy Blackman filter coefficients
magdft (x, mag);
// Magnitude response of LPF using Blackman window
for (n=0; n<N; n++) fprintf (out3, "%f\n", mag[n]);
fclose(filter);
fclose(out1);
fclose(out2);
fclose(out3);
}

void
magdft(double x[], double mag[])
{
int k;
double XR[N], XI[N];
dft(x, XR, XI);
for (k=0; k<N; k++)
{
mag[k] = XR[k]*XR[k] + XI[k]*XI[k];
mag[k] = 10.*log10(mag[k]);
}
}

// the magnitude squared

void
dft(double s[], double XR[], double XI[])
{
int n, k;
for (k=0; k<N; k++)
{
XR[k] = 0.;
XI[k] = 0.;
for (n=0; n<N; n++)
{
XR[k] = XR[k] + s[n]*cos(2.*PI*k*n/N);
XI[k] = XI[k] - s[n]*sin(2.*PI*k*n/N);
}
}
}
Rectangular
-0.043200
-0.040326
-0.018204
0.022249
0.074892
0.129600
0.174747
0.200242
0.200242
0.174747
0.129600
0.074892
0.022249
-0.018204
-0.040326
-0.043200

0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

Hamming
-0.003471
-0.004851
-0.004246
0.008891
0.044237
0.100233
0.160100
0.199106
0.199106
0.160100
0.100233
0.044237
0.008891
-0.004246
-0.004851
-0.003471

// s[]:input, XR[]&XI[]:outputs

// Computation of the DFT

Blackman
0.000000
-0.000737
-0.001530
0.004870
0.032171
0.089016
0.161792
0.214417
0.214417
0.161792
0.089016
0.032171
0.004870
-0.001530
-0.000737
0.000000

-50

-50

-50

-100

-100

-100

-150
0

-150

(a) Rectangular

-150
2

(b) Hamming

Fig.P.3.1 Various FIR lowpass filters.

(c) Blackman

4. The following C program is the implementation of the lowpass filter.


#define
N
16
#define
N1 15
void
main()
{
int n;
double buf[N], h[N], low;
FILE
*in, *out;
in = fopen("signal.dat", "r");
out = fopen("output.dat", "w");
h[0] =-0.0035;
h[1] =-0.0049;
// LPF coefficients
h[2] =-0.0042;
h[3] = 0.0089;
h[4] = 0.0442;
h[5] = 0.1002;
h[6] = 0.1601;
h[7] = 0.1991;
for (n=0; n<=7; n++) h[N1-n] = h[n];
// Copy h[8] - h[15]
for (n=0; n<=N1; n++) buf[n] = 0.;
// Initialize the 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]);
// Read present input
low = 0;
// Initialize current output to be zero
for (n=0; n<N; n++) low = low + h[n]*buf[n]; // Lowpass filtering
fprintf (out, "%f\n", low);
for (n=N1; n>0; n--) buf[n] = buf[n-1];
// Update buffer
}
fclose(in);
fclose(out);
}

2
1
1
0

-1
-1
-2
-3

-2
0

100

200

(a) input signal

300

100

200

(b) lowpass filter output

Figure P.3.2 Input signal and lowpass filter output

300

Potrebbero piacerti anche