Code covered by the BSD License  

Highlights from
MatLab Solutions: "Introduction to Digital Signal Processing: A Computer Laboratory Textbook".

image thumbnail

MatLab Solutions: "Introduction to Digital Signal Processing: A Computer Laboratory Textbook".

by

 

29 Oct 2012 (Updated )

These files are the MatLab solutions of exercises contained in the above DSP lab textbook.

ex541b.m
% Exercise 5.4.1.b. Sketching the Magnitude Response.
% Pole & zeros & frequency response of a digital filter - animation
%
% Note: This m-file contains large portions of code originally presented in the 
% companion software of the comprehensive and well-written DSP textbook:
% "Digital Signal Processing: Concepts and Applications", 2nd Ed.
% by B.Mulgrew, Peter Grant and John Thompson, Palgrave-Macmillan (2003).
% It was modified as necessary for solving Exercises 5.4.1., 5.4.2. and 5.4.5. 
clear; colordef black; clc; close all;

% poles & zeros
pp = [0.5];
zz = [0];
[b,a] = zp2tf(zz',pp',1);

figure('Name','Exercise 5.4.1.b. Sketching Magnitude Response from Pole/Zero Plots');
subplot(1,2,1);
zplane(b,a);
title('H(z) = 1/(1 - 0.5z^{-1})');
hold on

fprintf(1,'Exercise 5.4.1.b.: pole/zero plot - press return to animate\n')
pause

th = -pi;
pt = exp( j*th);
xx = real(pt);
yy = imag(pt);
l1 = line('Xdata',xx,'Ydata',yy);
set(l1,'Color','w','Marker','o','EraseMode','xor');
l2 = line('Xdata', real ([ pt pp(1)] ),'Ydata',imag([ pt pp(1)]),'Color','g','LineStyle','-','EraseMode','xor'); 
% l3 = line('Xdata', real ([ pt pp(2)] ),'Ydata',imag([ pt pp(2)]),'Color','g','LineStyle','-','EraseMode','xor'); 
l4 = line('Xdata', real ([ pt zz(1)] ),'Ydata',imag([ pt zz(1)]),'Color','b','LineStyle','-','EraseMode','xor'); 
l6 = line('Xdata', real ([ 0 pt ] ),'Ydata',imag([0 pt ]),'Color','r','LineStyle','-','EraseMode','xor'); 

M = 128;
w = -pi:pi/M:pi;
hh = freqz(b,a,w);

h = abs(hh);
ang = unwrap(angle(hh))/pi*180;
angmax = max(ang);
angmin = min(ang);
maxh = max(h);
if min(h) < 0
	minh = min(h);
	else
	minh = 0;
	end

subplot(2,2,2)
title('|H(j\omega)|');
xx = [ w(1) w(1) ];
yy = [ h(1) h(1) ];
ll1 = line ('Xdata',xx,'Ydata',yy,'Color','y','LineStyle','-','Erasemode','none');
ll2 = line ('Xdata',w(1),'Ydata',h(1),'Color','w','Marker','o','Erasemode','xor');
set(gca,'XTick',-pi:pi/4:pi);
set(gca,'XTickLabel',{'-pi','-3pi/4','-pi/2','-pi/4','0','pi/4','pi/2','3pi/4','pi' });
axis ([ -pi pi minh maxh ]);
grid
ylabel ('gain')
xlabel ('frequency \omega (rad/sample)');
hold on

subplot(2,2,4)
title('\angleH(j\omega)');
xx = [ w(1) w(1) ];
yy = [ ang(1) ang(1) ];
lx1 = line ('Xdata',xx,'Ydata',yy,'Color','y','LineStyle','-','Erasemode','none');
lx2 = line ('Xdata',w(1),'Ydata',h(1),'Color','w','Marker','o','Erasemode','xor');
set(gca,'XTick',-pi:pi/4:pi);
set(gca,'XTickLabel',{'-pi','-3pi/4','-pi/2','-pi/4','0','pi/4','pi/2','3pi/4','pi' });
axis ([ -pi pi angmin angmax ]);
grid
ylabel ('phase (degrees)')
xlabel ('frequency \omega (rad/sample)');
hold on

step = 2*pi/M;

for ii = 1:4:2*M+2;

th = -pi + (ii-1)*step/2;
pt = exp( j*th);
set(l1,'Xdata',real(pt),'Ydata',imag(pt));
set(l2,'Xdata', real ([ pt pp(1)] ),'Ydata',imag([ pt pp(1)])); 
% set(l3,'Xdata', real ([ pt pp(2)] ),'Ydata',imag([ pt pp(2)])); 
set(l4,'Xdata', real ([ pt zz(1)] ),'Ydata',imag([ pt zz(1)])); 
set(l6,'Xdata', real ([0 pt ] ),'Ydata',imag([0 pt ])); 

if ii==1,
	xx = [ w(1) w(1) ];
	yy = [ h(1) h(1) ];
else
	xx = w(ii-1:ii);
	yy = h(ii-1:ii);
end
set(ll1,'Xdata',w(1:ii),'Ydata',h(1:ii));
set(ll2,'Xdata',w(ii),'Ydata',h(ii));

if ii==1,
	xx = [ w(1) w(1) ];
	yy = [ ang(1) ang(1) ];
else
	xx = w(ii-1:ii);
	yy = ang(ii-1:ii);
end
set(lx1,'Xdata',w(1:ii),'Ydata',ang(1:ii));
set(lx2,'Xdata',w(ii),'Ydata',ang(ii));

pause(0.25)

end

Contact us