syntel previous paper
TRANSCRIPT
8/3/2019 Syntel Previous Paper
http://slidepdf.com/reader/full/syntel-previous-paper 1/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 <stdio.h>
#include <math.h>#define N 256
void 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;
8/3/2019 Syntel Previous Paper
http://slidepdf.com/reader/full/syntel-previous-paper 2/6
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
8/3/2019 Syntel Previous Paper
http://slidepdf.com/reader/full/syntel-previous-paper 3/6
fprintf (filter, "%d\t%f\n", n, hbla[n]);
}
for (n=16; n<N; n++) xr[n] = 0.0; // Pad extra zeros out
for (n=0; n<=15; n++) xr[n] = hbla[n]; // Copy filter coefficients
for (n=0; n<N; n++) xi[n] = 0.0; // Make imaginary part zero
fft (xr, xi, N, 0);
for (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);
}
The resulting filter coefficients are given below.
0 -0.000387
1 -0.002004
2 0.006093
3 0.014763
4 -0.031427
5 -0.062955
6 0.1321307 0.443787
8 0.443787
9 0.132130
10 -0.062955
11 -0.031427
12 0.014763
13 0.006093
14 -0.002004
15 -0.000387
8/3/2019 Syntel Previous Paper
http://slidepdf.com/reader/full/syntel-previous-paper 4/6
0 50 100 150 200 250 300-4
-2
0
2
4
(a) signal
0 50 100 150 200 250 3000
50
100
150
200
(b) magnitude
0 50 100 150 200 250 300-4
-2
0
2
4
(c) phase
Fig. 3P-1 Signal and its magnitude and phase spectra
3. The magnitude and phase response of the filter are shown below.
8/3/2019 Syntel Previous Paper
http://slidepdf.com/reader/full/syntel-previous-paper 5/6
0 200 400 600 800 1000 1200-150
-100
-50
0
lowpass magnitude response
0 200 400 600 800 1000 1200-40
-30
-20
-10
0
lowpass phase response
0 200 400 600 800 1000 1200-150
-100
-50
0
highpass magnitude response
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 <stdio.h>
#define N 16
#define 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");
8/3/2019 Syntel Previous Paper
http://slidepdf.com/reader/full/syntel-previous-paper 6/6
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 coefficientsfor (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);
}
0 50 100 150 200 250 300-2
-1
0
1
2
lowpass output
0 50 100 150 200 250 300-2
-1
0
1
2
highpass output
Fig. 3P-3 Lowpass and highpass filter outputs