Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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
300
100
200
300
3.
//
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]);
}
}
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
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
(c) Blackman
2
1
1
0
-1
-1
-2
-3
-2
0
100
200
300
100
200
300