Friday, July 17, 2009

Chapter 3

% Program 3_1
% Discrete-Time Fourier Transform Computation
%
% Read in the desired number of frequency samples
k = input('Number of frequency points = ');
% Read in the numerator and denominator coefficients
num = input('Numerator coefficients = ');
den = input('Denominator coefficients = ');
% Compute the frequency response
w = 0:pi/(k-1):pi;
h = freqz(num, den, w);
% Plot the frequency response
subplot(2,2,1)
plot(w/pi,real(h));grid
title('Real part')
xlabel('\omega/\pi'); ylabel('Amplitude')
subplot(2,2,2)
plot(w/pi,imag(h));grid
title('Imaginary part')
xlabel('\omega/\pi'); ylabel('Amplitude')
subplot(2,2,3)
plot(w/pi,abs(h));grid
title('Magnitude Spectrum')
xlabel('\omega/\pi'); ylabel('Magnitude')
subplot(2,2,4)
plot(w/pi,angle(h));grid
title('Phase Spectrum')
xlabel('\omega/\pi'); ylabel('Phase, radians')

% Program 3_2
% Generate the filter coefficients
h1 = ones(1,5)/5; h2 = ones(1,14)/14;
% Compute the frequency responses
[H1,w] = freqz(h1, 1, 256);
[H2,w] = freqz(h2, 1, 256);
% Compute and plot the magnitude responses
m1 = abs(H1); m2 = abs(H2);
plot(w/pi,m1,'r-',w/pi,m2,'b--');
ylabel('Magnitude'); xlabel('\omega/\pi');
legend('M=5','M=14');
pause
% Compute and plot the phase responses
ph1 = angle(H1)*180/pi; ph2 = angle(H2)*180/pi;
plot(w/pi,ph1,w/pi,ph2);
ylabel('Phase, degrees');xlabel('\omega/\pi');
legend('M=5','M=14');

% Program 3_3
% Set up the filter coefficients
b = [-6.76195 13.456335 -6.76195];
% Set initial conditions to zero values
zi = [0 0];
% Generate the two sinusoidal sequences
n = 0:99;
x1 = cos(0.1*n);
x2 = cos(0.4*n);
% Generate the filter output sequence
y = filter(b, 1, x1+x2, zi);
% Plot the input and the output sequences
plot(n,y,'r-',n,x2,'b--',n,x1,'g-.');grid
axis([0 100 -1.2 4]);
ylabel('Amplitude'); xlabel('Time index n');
legend('y[n]','x2[n]','x1[n]')

No comments: