24-25 DSP Lab Manual - Reference
24-25 DSP Lab Manual - Reference
DSP Laboratory
Course Objectives:
To make students learn & explore how MATLAB programming supports time-
domain and frequency-domain DSP analysis, design & operations by process
simulation
To equip students with MATLAB skills in the area of DSP to solve problems &
build small applications and projects
To prepare students ready for the Advanced DSP courses by reinforcing DSP
concepts with simulation experiments
Students should learn
To implement Convolution and Transform techniques on a given sequence
To analyse frequency response of Discrete Time Signals and Systems
To design different Digital filters
To implement basic Image Processing techniques
Course Outcomes:
Students taking DSP Lab will be able to:
Simulate and Verify DSP concepts using MATLAB
Generate various deterministic & random signals
Apply transformations & Perform operations on DT signals, Compute & sketch
convolutions
Compute & Plot spectrum of non-periodic signals by computing: DTFT & DFT
Analyze LTI systems using: Impulse & Step response, Frequency response
Verify sampling theorem & understand the effects of under sampling
Apply FFT for Spectrum analysis of DT/CT signals
Design IIR & FIR digital filters to meet the Frequency response specifications
Implement LTI digital filtering
Implement Multi-Rate Up/Down sampling, Decimation & Interpolation operations
Implement basic Image Processing operations
1
Department of ECE, SRKR Engineering College
DSP Laboratory
2
Department of ECE, SRKR Engineering College
DSP Laboratory
3
Department of ECE, SRKR Engineering College
DSP Laboratory
P1: BPF
P2: LPF & HPF
4
Department of ECE, SRKR Engineering College
DSP Laboratory
End End
14.
15.
5
Department of ECE, SRKR Engineering College
DSP Laboratory
6
Department of ECE, SRKR Engineering College
DSP Laboratory
Aim: To Compute & Plot Discrete Linear & Circular Convolutions in Time-domain
7
Department of ECE, SRKR Engineering College
DSP Laboratory
8
Department of ECE, SRKR Engineering College
DSP Laboratory
P1: Sinusoidal, Complex exponential, Unit impulse & Unit step sequences
P2: Random Signals & Gaussian Noise
P3: Implement Amplitude scaling, DC biasing, Time shifting, Time reversal, Time
scaling on signals
P4: Perform Addition & Multiplication, Even/Odd decomposition on signals
9
Department of ECE, SRKR Engineering College
DSP Laboratory
MATLAB Programs:
% Plots
figure, subplot(211), stem(n,x1,'LineWidth',2);
axis([min(n)-1 max(n)+1 min(x1)-0.5 max(x1)+0.5]);grid;
title('Signal, \delta(n)','fontsize',12,'fontweight','b');
subplot(212), stem(n,x2,'LineWidth',2);
xlabel('time, n','fontsize',12,'fontweight','b');
axis([min(n)-1 max(n)+1 min(x2)-0.5 max(x2)+0.5]);grid;
title('x(n) = 2\delta(n-2)+\delta(n)-\delta(n-4)','fontsize',12,'fontweight','b');
% Plots
figure, subplot(211), stem(n,x1,'LineWidth',2);
title('Signal, u(n)','fontsize',12,'fontweight','b');
axis([min(n)-1 max(n)+1 min(x1)-0.5 max(x1)+0.5]); grid;
subplot(212), stem(n,x2,'LineWidth',2);
xlabel('time, n','fontsize',12,'fontweight','b');
title('x(n) = u(n-4)','fontsize',12,'fontweight','b');
axis([min(n)-1 max(n)+1 min(x1)-0.5 max(x1)+0.5]); grid;
10
Department of ECE, SRKR Engineering College
DSP Laboratory
figure,
subplot(2,2,1); stem(n,x1,'LineWidth',2);
title('x(n) = 2\delta(n+2) - \delta(n-4)'); grid;
axis([-2+min(n),max(n)+2,min(x1)-1,max(x1)+1]);
subplot(2,2,2); stem(n,x2,'LineWidth',2);
title('x(n) = u(n)-u(n-5)'); grid;
axis([-2+min(n),max(n)+2,min(x1)-1,max(x1)+1]);
subplot(2,2,3); stem(n,x3,'LineWidth',2);
legend('x(n) = 2(0.7)^n');
title('Exponential seq');grid;xlabel('time, n');
axis([-2+min(n),max(n)+2,min(x3)-1,max(x3)+1]);
% plot x(n) = sin(0.2*pi*n) 0<=n<=30
n2 = 0:30; x4 = 1*sin(0.2*pi*n2);
subplot(2,2,4); stem(n2,x4);
title('DT sine, x(n) = sin(0.2*pi*n)');
xlabel('time, n'); % axis([n2(1),n2(end),-0.2,1.2])
11
Department of ECE, SRKR Engineering College
DSP Laboratory
x = A*cos(2*pi*f*t); % signal
noisevar = sigpower/(10^(noisedB/10)); % compute noise variance or power
n = sqrt(noisevar)*randn(1,10*length(t)); % generate Gaussian noise
y = x + n(1:10:length(n)); % add noise
% plots
subplot(2,2,1); plot(0:length(n)-1,n);
title('Gaussian noise samples'); grid; xlim([0 4010]);
subplot(2,2,2); hist(n,50);
title('Noise pdf'); grid; % check density & plot
subplot(2,2,3); plot(t,x); xlabel('t in sec.');
title('Sinewave'); grid;
subplot(2,2,4); plot(t,y);
xlabel('t in sec.'); title('Sine + noise');grid;
% plot(t,y,'y-',t,y,'go')
% xlabel('time, n','fontsize',12,'fontweight','b');
% title('Signal, x(n) = e^{-0.5t} sin(5t)')
12
Department of ECE, SRKR Engineering College
DSP Laboratory
% Plots
figure,
subplot(2,1,1); stem(n1,x1,'LineWidth',2);
title('x_1(n)','FontSize',12,'Fontweight','b');grid;
subplot(2,1,2); stem(n2,x2,'LineWidth',2);
title('x_2(n)','FontSize',12,'Fontweight','b');grid;
xlabel('time, n','fontsize',12,'fontweight','b');
13
Department of ECE, SRKR Engineering College
DSP Laboratory
% Aim: To split real signal x(n) into Even & Odd parts
% Even-Odd Synthesis of x(n)
% function [xe,xo,m] = evenodd(x,n)
% [xe,xo,m] = evenodd(x,n); % split into even/odd parts
% x(n) = u(n)-u(n-10); % real sig, neither even nor odd
% n = [0:10]; % discrete time
% x = stepseq(0,0,10)-stepseq(10,0,10); % sequence
14
Department of ECE, SRKR Engineering College
DSP Laboratory
nm = n(1)-m(1); n1 = 1:length(n);
x1 = zeros(1,length(m)); % align sig
x1(n1+nm) = x; % x = x1; % copy
xe = 0.5*(x1 + fliplr(x1)); % even part
xo = 0.5*(x1 - fliplr(x1)); % odd part
xs = xe + xo; % synthesis
% Plots
% subplot(1,1,1);
figure(); % new figure
subplot(2,2,1); stem(n,x,'LineWidth',2);
title('Rectangular pulse, x(n)');
% axis([-10,10,-0.1,1.2]);
subplot(2,2,2); stem(m,xe,'LineWidth',2);
title('Even part, x_e(m)');
% axis([-10,10,-0.1,1.2]);
subplot(2,2,3); stem(m,xo,'LineWidth',2);
title('Odd part, x_o(m)');
xlabel('m','fontsize',12,'fontweight','b');
% axis([-10,10,-0.6,0.6]);
subplot(2,2,4); stem(m,xe+xo,'LineWidth',2);
title('Even + Odd, x_e(m)+x_o(m)');
xlabel('m','fontsize',12,'fontweight','b');
% axis([-10,10,-0.1,1.2]);
15
Department of ECE, SRKR Engineering College
DSP Laboratory
16
Department of ECE, SRKR Engineering College
DSP Laboratory
17
Department of ECE, SRKR Engineering College
DSP Laboratory
Fig. 1.5 P1.1D: Gaussian noise, PDF & Noisy sine signal
18
Department of ECE, SRKR Engineering College
DSP Laboratory
19
Department of ECE, SRKR Engineering College
DSP Laboratory
20
Department of ECE, SRKR Engineering College
DSP Laboratory
21
Department of ECE, SRKR Engineering College
DSP Laboratory
22
Department of ECE, SRKR Engineering College
DSP Laboratory
Aim: To Evaluate & Plot the Linear & Circular Convolutions of Discrete-Time
Signals in Time-Domain.
23
Department of ECE, SRKR Engineering College
DSP Laboratory
MATLAB Programs:
x = [1 2 3 4]; % sig
h = [5,6,7];
% h = input('Enter h vector using [] ');
% x = [1,2,2,1]; h = [1,2,3];
Nx = length(x); Nh = length(h); % sig lengths
Ny = Nx+Nh-1; % conv length
% plots
nx = 0:Nx-1; % causal time of x
nh = 0:Nh-1; % time of h
ny = 0:Ny-1; % time of y
figure, subplot(2,2,1); stem(nx,x,'LineWidth',2);
title(['x(n), Nx = ',int2str(Nx)]);
axis([min(nx)-1,max(nx)+1,min(x)-1,max(x)+1]);
subplot(2,2,2); stem(nh,h,'LineWidth',2);
title(['h(n), Nh = ',int2str(Nh)]);
axis([min(nh)-1,max(nh)+1,min(h)-1,max(h)+1]);
subplot(2,2,[3,4]); stem(ny,y,'LineWidth',2);
% axis([min(ny)-1,max(ny)+1,min(y)-1,max(y)+1]);
xlim([min(ny)-1,max(ny)+1]); xlabel('Time, n'); grid;
title(['Linear causal convolution, y(n), Ny = ',int2str(Ny)],'fontsize',12);
x = [2,3,4,5]; % sig
% x = [6,7,8,9,0]; % sig
24
Department of ECE, SRKR Engineering College
DSP Laboratory
N = 5; % period
m = 1; % shift, + (right), - (left)
% Plots
Nx = length(x); % sig length
nx = 0:Nx-1; % causal time of x
nxp = 0:N-1; % time of zero-padded sig over one period
figure, subplot(2,2,1); stem(nx,x,'LineWidth',2);
title('x(n)'); axis([min(nx)-1,max(nx)+1,min(x)-1,max(x)+1]);
subplot(2,2,2); stem(nxp,xp,'LineWidth',2);
title(['xp(n), period = ',int2str(N)]);
axis([min(nxp)-1,max(nxp)+1,min(xp)-1,max(xp)+1]);
subplot(2,2,3); stem(nxp,xcs,'LineWidth',2);
axis([min(nxp)-1,max(nxp)+1,min(xcs)-1,max(xcs)+1]);
title('Circular shift'); xlabel('Time, n'); grid;
subplot(2,2,4); stem(nxp,xcr,'LineWidth',2);
axis([min(nxp)-1,max(nxp)+1,min(xcr)-1,max(xcr)+1]);
title('Circular reversal'); xlabel('Time, n'); grid;
x = [1 2 3 4]; % sig
h = [5,6,7];
% x = [1,2,2,1]; h = [1,2,3,0];
N = 5; % period
Nx = length(x); Nh = length(h);
nx = 0:Nx-1; nh = 0:Nh-1; % time of x,h
xp = [x zeros(1,N-Nx)]; % append zeros if necessary
hp = [h zeros(1,N-Nh)];
hpr = [hp hp]; % repeat hp 2-times
25
Department of ECE, SRKR Engineering College
DSP Laboratory
% plots
n = 0:N-1; % time of xp,hp,yp
figure, subplot(2,2,1); stem(n,xp,'LineWidth',2);
title('xp(n)'); axis([min(n)-1,max(n)+1,min(xp)-1,max(xp)+1]);
subplot(2,2,2); stem(n,hp,'LineWidth',2);
title('hp(n)'); axis([min(n)-1,max(n)+1,min(hp)-1,max(hp)+1]);
subplot(2,2,[3,4]); stem(n,yp,'LineWidth',2);
% axis([min(n)-1,max(n)+1,min(yp)-1,max(yp)+1]);
xlim([min(n)-1,max(n)+1]); xlabel('Time, n'); grid;
title(['Periodic convolution, yp(n), period = ',int2str(N)],'fontsize',12);
26
Department of ECE, SRKR Engineering College
DSP Laboratory
27
Department of ECE, SRKR Engineering College
DSP Laboratory
28
Department of ECE, SRKR Engineering College
DSP Laboratory
29
Department of ECE, SRKR Engineering College
DSP Laboratory
30
Department of ECE, SRKR Engineering College
DSP Laboratory
P2: Obtain & plot the Frequency Response of a DT-LTI System: Magnitude/Phase
characteristics
31
Department of ECE, SRKR Engineering College
DSP Laboratory
MATLAB Programs:
% DTFT plots
figure, subplot(2,2,1); plot(w/pi,magX,'LineWidth',2);
title('DTFT Magnitude'); grid;
subplot(2,2,3); plot(w/pi,angX,'LineWidth',2); grid
xlabel('Frequency in pi units'); title('DTFT Angle, Radians');
subplot(2,2,2); plot(w/pi,realX,'LineWidth',2); grid
title('Real Part');
subplot(2,2,4); plot(w/pi,imagX,'LineWidth',2); grid
xlabel('Frequency in pi units'); title('Imaginary Part');
32
Department of ECE, SRKR Engineering College
DSP Laboratory
% help freqz
% H(z) = B(z)/A(z), % DT TF evaluation
% TF = b/a; % define transfer function as vectors b,a
% Digital Butterworth LP filter, retain digits
b = [0.0181, 0.0543, 0.0543, 0.0181]; % num coeff vector
a = [1.0000, -1.7600, 1.1829, -0.2781]; % den coeff vector
% Plots
figure,
% subplot(2,1,1); plot(w/pi,magH,'LineWidth',2);grid; % axis([0,1,0,1])
subplot(2,1,1); plot(w/pi,magHdB,'LineWidth',2); grid;
33
Department of ECE, SRKR Engineering College
DSP Laboratory
34
Department of ECE, SRKR Engineering College
DSP Laboratory
35
Department of ECE, SRKR Engineering College
DSP Laboratory
36
Department of ECE, SRKR Engineering College
DSP Laboratory
37
Department of ECE, SRKR Engineering College
DSP Laboratory
38
Department of ECE, SRKR Engineering College
DSP Laboratory
Aim: To compute DFT & FFT, Linear & Circular convolutions using FFT and to
verify DFT properties
39
Department of ECE, SRKR Engineering College
DSP Laboratory
MATLAB Programs:
Aim: To compute DFT & FFT, To compute convolutions using FFT, To verify DFT
properties
Expt-4.1A: DFT
xn = [0:7]; N = 8;
n = 0:N-1; % time index n, row vector
k = 0:N-1; % frequency index k, row vecor
WN = exp(-j*2*pi/N); % Wn factor
nk = n'*k; % creates a N by N matrix of nk values
WNnk = WN .^ nk; % DFT matrix
Xk = xn * WNnk; % row vector for DFT coefficients
Expt-4.1B: IDFT
Xk = [0:7]; N = 8;
n = 0:N-1; % row vector for n
k = 0:N-1; % row vecor for k
WN = exp(-j*2*pi/N); % Wn factor
nk = n'*k; % creates a N by N matrix of nk values
WNnk = WN .^ (-nk); % IDFT matrix
xn = (Xk * WNnk)/N; % row vector for IDFT values
40
Department of ECE, SRKR Engineering College
DSP Laboratory
x1 = [1,0,0,0,0,0,0,0]; % delta(n)
X1 = fft(x1);
x2 = [1,1,1,1,1,1,1,1]; % dc
X2 = fft(x2);
41
Department of ECE, SRKR Engineering College
DSP Laboratory
% Plots
figure, subplot(2,2,1);
stem(0:length(x1)-1,x1,'LineWidth',2); title('x_1(n)');
subplot(2,2,2);
stem(0:length(x2)-1,x2,'LineWidth',2); title('x_2(n)');
subplot(2,2,3);
stem(0:length(y1)-1,y1,'LineWidth',2); xlabel('time, n');
title('Linear convolution'); grid;
subplot(2,2,4); stem(0:length(y5)-1,y5,'LineWidth',2); xlabel('time, n');
title('L-point Circular convolution'); grid;
x = [1 2 3 4]; % seq
N = 4; X = fft(x,N);
xcr = [x(1),x(end:-1:2)]; % x reversal
LHS = fft(xcr,N);
RHS = [X(1),X(end:-1:2)]; % X reversal
LHS-RHS
42
Department of ECE, SRKR Engineering College
DSP Laboratory
x = [1 2 3 4];
N = 5; X = fft(x,N);
LHS = ifft(X); % ifft
RHS = (1/N)*conj(fft(conj(X))); % ifft using fft only
LHS-RHS
Lab-
x = [1 2 3 4]; % seq
N = 4; X = fft(x,N);
LHS = sum(x.^2); % energy from time domain
RHS = (1/N)*sum(abs(X).^2); % energy from DFT domain
LHS-RHS
x = [1 2 3 4]; % seq
N = 4; X = fft(x,N);
LHS = fft(X);
xcr = [x(1),x(end:-1:2)]; % x reversal
RHS = N*xcr;
LHS == RHS
43
Department of ECE, SRKR Engineering College
DSP Laboratory
44
Department of ECE, SRKR Engineering College
DSP Laboratory
45
Department of ECE, SRKR Engineering College
DSP Laboratory
46
Department of ECE, SRKR Engineering College
DSP Laboratory
47
Department of ECE, SRKR Engineering College
DSP Laboratory
48
Department of ECE, SRKR Engineering College
DSP Laboratory
Aim: To perform DT-LTI system simulations & compare with analytical results.
To simulate & verify the response of a DT-LTI system to different inputs:
Impulse, Step & Sinusoidal signals
49
Department of ECE, SRKR Engineering College
DSP Laboratory
MATLAB Programs:
Aim: To perform DT-LTI system simulations & compare with analytical results
DT-LTI System Simulation
Aim: To simulate & verify the response of a DT-LTI system to different inputs:
Impulse, Step & Sinusoidal signals
P1: Impulse response & Step response
P2: Steady-state sine response
% Aim: To simulate and get the Impulse & Step responses of a DT-LTI system
% Impulse response
start = -2; final = 20; n = [start:final]; % time interval
% x1 = impseq(0,start,final); % define system input
% x1 = [zeros(1,(n0-n1)), 1, zeros(1,(n2-n0))];
deln = @(n) (n == 0); % impulse
x1 = deln(n); % impulse input
% use filter function to get output when input is applied to the system
% filter: One-dimensional digital filter.
y1 = filter(b,a,x1); % impulse output
% Plots
figure,
subplot(2,2,1); stem(n,x1,'LineWidth',2);
axis([start,final,min(x1)-0.5,max(x1)+0.5]);
title('Impulse input, \delta(n)'); %ylabel('h(n)')
subplot(2,2,2); stem(n,y1,'LineWidth',2);
axis([start,final,min(y1)-0.5,max(y1)+0.5]);
title('Impulse Response, h(n)'); %ylabel('h(n)')
% Step response
b1 = conv(b,[1 0]); a1 = conv(a,[1 -1]); % define system or tf = b/a
[r2,p2,k2] = residuez(b1,a1); % z-partial fractions
50
Department of ECE, SRKR Engineering College
DSP Laboratory
% Aim: To simulate a DT-LTI system for a sinewave input & study its
% frequency response
b = 2*[0.0000,0.0054,0.0181,0.0040];
a =[1.0000,-3.0591,3.8323,-2.2919,0.5495]; % Chebyshev LP filter
figure,freqz(b,a); title('Freq response'); % get freq response
sys1 = filt(b,a); % system object
% w = 0.05*pi; % set frequency
w = 0.7*pi; % choose PB,TB,SB freq
% w = 0.334*pi; % notch freq
H = freqresp(sys1,w); % compute freq resp at w
% H = freqz(b,a,w);
magH = abs(H); angH = angle(H)*180/pi;
% Sine response
n = [0:200]; x = 1*cos(w*n); % input sine signal
y = filter(b,a,x); % give input to system to get output
% Plots
figure,
subplot(2,1,1); stem(n,x);
axis([min(n)-1,max(n)+1,min(x)-1,max(x)+1]);
51
Department of ECE, SRKR Engineering College
DSP Laboratory
52
Department of ECE, SRKR Engineering College
DSP Laboratory
Fig. 5.1 P5.1: DT-LTI System Simulation: Impulse & Step responses
Fig. 5.2 P5.2: DT-LTI system: Frequency response Magnitude & Phase
53
Department of ECE, SRKR Engineering College
DSP Laboratory
54
Department of ECE, SRKR Engineering College
DSP Laboratory
55
Department of ECE, SRKR Engineering College
DSP Laboratory
56
Department of ECE, SRKR Engineering College
DSP Laboratory
Aim: To Numerically Compute & Plot the Spectrum of DT & CT Signals using FFT
& to Compare with the Theoretical results.
57
Department of ECE, SRKR Engineering College
DSP Laboratory
MATLAB Programs:
% Plots
figure, stem(n,x,'LineWidth',2); grid;
xlabel('Time index, n'); title('Signal, x(n)');
axis([min(n)-1,max(n)+1,min(x)-0.5,max(x)+0.5]);
figure, subplot(211); plot(w/pi,fftshift(magX),'LineWidth',2);
title('Magnitude part: Sinc & Even');
ylabel('Magnitude'); grid;
subplot(212); plot(w/pi,fftshift(angX),'LineWidth',2);
xlabel('Frequency in pi units'); ylabel('Radians');
title('Phase part: Odd'); grid;
% Aim: To use FFT for the spectrum analysis of a CT tone signal & Observe
aliasing
58
Department of ECE, SRKR Engineering College
DSP Laboratory
% Plots
figure, stem(k,magX); xlabel('Freq index, k');
title('FFT mag');grid,
figure; subplot(211); plot(t1(1:500),x1(1:500)); grid;
title('Analog sig'); xlabel('Time, s');
subplot(212); stem(w2,fftshift(magX),'LineWidth',2); % fft zero-centered
axis([min(w2)-1,max(w2)+1,min(magX)-5,max(magX)+5]);
xlabel('Freq in Hz'); ylabel('FFT Mag'); grid;
title('Frequency analysis with FFT');
59
Department of ECE, SRKR Engineering College
DSP Laboratory
60
Department of ECE, SRKR Engineering College
DSP Laboratory
61
Department of ECE, SRKR Engineering College
DSP Laboratory
62
Department of ECE, SRKR Engineering College
DSP Laboratory
63
Department of ECE, SRKR Engineering College
DSP Laboratory
64
Department of ECE, SRKR Engineering College
DSP Laboratory
Aim: To design & verify IIR Low-pass Digital filters using Bilinear Transformation
& Impulse Invariance Techniques.
65
Department of ECE, SRKR Engineering College
DSP Laboratory
MATLAB Programs:
Aim: To design & verify IIR Low-pass Digital filters using Bilinear
Transformation & Impulse Invariance Techniques.
66
Department of ECE, SRKR Engineering College
DSP Laboratory
67
Department of ECE, SRKR Engineering College
DSP Laboratory
% [b,a] = butter(Nb,Wn,'s'); % AF
[Nc,Wn] = cheb1ord(OmegaP,OmegaS,Rp,As,'s'); % order
[bs,as] = cheby1(Nc,Rp,OmegaP,'s'); % AF
% figure, freqs(bs,as); % FR
% bs = [2]; as = [1 5 4]; T = 0.1;
% Print TF H(s) & Pole-Zero diagram
H1 = tf(bs,as), figure,pzmap(H1);axis equal; title('Pole-zero map: H(s)');
68
Department of ECE, SRKR Engineering College
DSP Laboratory
69
Department of ECE, SRKR Engineering College
DSP Laboratory
70
Department of ECE, SRKR Engineering College
DSP Laboratory
71
Department of ECE, SRKR Engineering College
DSP Laboratory
Fig. 7.8 P7.2: Digital Filter Frequency Response: Magnitude & Phase of a
Chebyshev-I LPF
72
Department of ECE, SRKR Engineering College
DSP Laboratory
73
Department of ECE, SRKR Engineering College
DSP Laboratory
74
Department of ECE, SRKR Engineering College
DSP Laboratory
Aim: To design & verify FIR Digital filters using Windows Technique.
P1: To verify FIR Digital low-pass Filter designs using different windows:
Rectangular, Hamming & Blackman.
P2: To verify FIR Digital low-pass Filter designs using a fixed window and
different filter lengths
75
Department of ECE, SRKR Engineering College
DSP Laboratory
MATLAB Programs:
Expt-8.1: To design & verify FIR Low-pass Digital filters using Windows
Technique
Aim: To verify FIR Digital low-pass Filter designs using different windows:
Rectangular, Hamming & Blackman.
% help fir1
% b = fir1(n,Wn,window) % n = order,
% help window % rectwin, hamming,
% wintool % GUI
% Specifications
% M can be even or odd
wp = 0.25*pi; % normalized passband cut-off freq
M = 51; % filter length
76
Department of ECE, SRKR Engineering College
DSP Laboratory
Aim: To verify FIR Digital lowpass Filter designs using a fixed window and
different filter lengths
77
Department of ECE, SRKR Engineering College
DSP Laboratory
78
Department of ECE, SRKR Engineering College
DSP Laboratory
Fig. 8.3 P8.1: FIR digital LP filter Impulse Response: with three
windows: Rectangular, Hamming & Blackman
Fig. 8.4 P8.1: FIR digital LP filter: Step Response with three windows:
Rectangular, Hamming & Blackman
79
Department of ECE, SRKR Engineering College
DSP Laboratory
Fig. 8.5 P8.2: FIR Digital LPF: Frequency Response Magnitude with
Hamming window & three different filter lengths, N = 21, 41 101
(observe roll-off)
Fig. 8.6 P8.2: FIR Digital LPF: Frequency Response Phase with Hamming
window & three different filter lengths, N = 21, 41 101 (observe slope)
80
Department of ECE, SRKR Engineering College
DSP Laboratory
Fig. 8.7 P8.2: FIR Digital LPF: Impulse Responses with Hamming
window &three different filter lengths, N = 21, 41 101 (observe delay)
Fig. 8.8 P8.2: FIR Digital LPF: Step Responses with Hamming window &
using three different filter lengths, N = 21, 41 101 (observe the delay)
81
Department of ECE, SRKR Engineering College
DSP Laboratory
82
Department of ECE, SRKR Engineering College
DSP Laboratory
Aim: To Design, implement & verify the LTI digital filtering operation in
time and frequency domains.
83
Department of ECE, SRKR Engineering College
DSP Laboratory
MATLAB Programs:
Aim: Design a digital filter to select/reject freq components & use FFT
for spectrum displays
% design an IIR Elliptic digital Band Pass filter to select 15Hz component
[b,a] = ellip(4,0.1,40,[10 20]*2/Fs); % Elliptic BP digital filter
[H,w] = freqz(b,a,512); % filter frequency response
subplot(2,2,2), plot(w*Fs/(2*pi),abs(H));
xlabel('Frequency (Hz)'); title('Mag. of filter response'); grid;
84
Department of ECE, SRKR Engineering College
DSP Laboratory
Fig. 9.1 P9.1: Signal Filtering using IIR Digital Filter: Elliptic Band-pass
filter to select 15 Hz freq component
85
Department of ECE, SRKR Engineering College
DSP Laboratory
86
Department of ECE, SRKR Engineering College
DSP Laboratory
87
Department of ECE, SRKR Engineering College
DSP Laboratory
MATLAB Programs:
L = 3; % Up-sampling Factor
N = 25*1; % Input length
n = 0:N-1; % input time
m = 0:L*N-1; % output time
f1 = 0.12; % Input signal frequency
% Generate the input sinusoidal sequence
x = sin(2*pi*f1*n);
% Generate the up-sampled sequence
y = zeros(1,L*N); % zero seq
y([1:L:length(y)]) = x; % fill
% Plot the input & output sequences
figure, subplot(2,1,1), stem(n,x); title('Input sequence');
xlabel('Time index, n'); ylabel('Amplitude');
subplot(2,1,2), stem(m,y(1:end));
title(['Output sequence: Up-sampled by ', num2str(L)]);
xlabel('Time index, n'); ylabel('Amplitude');
M = 2; % Down-sampling Factor
N = 25; % Output length
m = 0:N*M-1; % input time
n = 0:N-1; % output time
f2 = 0.042; % Input signal frequency
% Generate the input sinusoidal sequence
x = sin(2*pi*f2*m);
% Generate the down-sampled sequence
y = x(1:M:length(x));
89
Department of ECE, SRKR Engineering College
DSP Laboratory
90
Department of ECE, SRKR Engineering College
DSP Laboratory
91
Department of ECE, SRKR Engineering College
DSP Laboratory
92
Department of ECE, SRKR Engineering College
DSP Laboratory
Aim: To implement & study the Multi-Rate DSP Operations: Decimation &
Interpolation
93
Department of ECE, SRKR Engineering College
DSP Laboratory
MATLAB Programs:
Fs = 4000; Ts = 1/Fs; D = 3;
t = 0:Ts:1-Ts; % Time vector
x = sin(2*pi*30*t) + sin(2*pi*60*t);
y = decimate(x,D);
figure, subplot(211); stem(x(1:120)), % Original signal
axis([0 120 -2 2]); title('Original Signal');
subplot(212); stem(y(1:30)); % Decimated signal
title('Decimated Signal');
Fs = 1000; Ts = 1/Fs; I = 4;
t = 0:Ts:1-Ts; % Time vector
x = sin(2*pi*30*t) + sin(2*pi*60*t);
y = interp(x,I);
figure, subplot(211); stem(x(1:30)); title('Original Signal');
subplot(212); stem(y(1:120)); title('Interpolated Signal');
95
Department of ECE, SRKR Engineering College
DSP Laboratory
96
Department of ECE, SRKR Engineering College
DSP Laboratory
97
Department of ECE, SRKR Engineering College
DSP Laboratory
98
Department of ECE, SRKR Engineering College
DSP Laboratory
99
Department of ECE, SRKR Engineering College
DSP Laboratory
MATLAB Programs:
% Plots
% axis([min(n)-1,max(n)+1,min(y)-1,max(y)+1]);
figure('Name','Sampling theorem');
subplot(2,3,1); plot(t,xa); axis([-1 1 -1.2 1.2]); grid;
xlabel('t in sec.'); title('Analog Signal x(t)');
subplot(2,3,2); stem(nTs1,xn1,'LineWidth',2);
xlabel('n, t'); title('Discrete Signal x_1(n)');
hold on; plot(t,xa,'k:');hold off; axis([-1 1 -1.2 1.2]);grid;
subplot(2,3,3); stem(nTs2,xn2,'LineWidth',2);
xlabel('n, t'); title('Discrete Signal x_2(n)');
hold on; plot(t,xa,'k:'); hold off;axis([-1 1 -1.2 1.2]);grid;
100
Department of ECE, SRKR Engineering College
DSP Laboratory
% Plots
figure('Name','Reconstruction');
subplot(211); stairs(t2,xa3,'r','LineWidth',2);
title('ZOH approx.of x(t)');
hold on; plot(t,xa1,'k:','LineWidth',2); hold off;
axis([-1 1 -1.2 1.2]); grid;
subplot(212); plot(t2,xa3,'r','LineWidth',2);
title('FOH approx.of x(t)');
hold on; plot(t,xa1,'k:','LineWidth',2); hold off;
axis([-1 1 -1.2 1.2]); grid; xlabel('t in sec.');
% plot(nTs2,xn2,'r');
101
Department of ECE, SRKR Engineering College
DSP Laboratory
Fig. 12.2 P12.1: Sampling: Signal Reconstruction with ZOH & FOH
102
Department of ECE, SRKR Engineering College
DSP Laboratory
103
Department of ECE, SRKR Engineering College
DSP Laboratory
104
Department of ECE, SRKR Engineering College
DSP Laboratory
P1: Image Enhancement: Image size & display, Sampling and Quantization,
Brightness & Contrast Adjustment, Histogram & Equalization, Edge Detection,
105
Department of ECE, SRKR Engineering College
DSP Laboratory
MATLAB Programs:
% Load image
% I1 = imread('eight.tif');
% I1 = imread('XiLena.jpg'); % get image
% I1 = imread('Lena_image.bmp'); % get image
I1 = imread('Einstein.bmp'); % get original image
% imfinfo('Einstein.bmp') % Information about graphics file
figure, imshow(I1); title('Einstein image'); %axis ij;
% help imtool
imtool('cameraman.tif')
106
Department of ECE, SRKR Engineering College
DSP Laboratory
% help imadjust
I = imread('pout.tif');
J = imadjust(I);
figure, subplot(121),imshow(I),title('Low contrast image');
subplot(122),imshow(J),title('Intensity adjusted');
K = imadjust(I,[0.3 0.7],[]);
figure, imshow(K),title('Intensity adjusted');
107
Department of ECE, SRKR Engineering College
DSP Laboratory
% help imcontrast
% I1 = imread('Einstein.bmp')
imshow('pout.tif'); % which pout.tif
% imshow('Einstein.bmp');
imcontrast(gca)
I = imread('pout.tif');
Imlimits = stretchlim(I);
J = imadjust(I,Imlimits,[]);
figure, subplot(121),imshow(I),title('Low contrast image');
subplot(122),imshow(J),title('Contrast stretched');
% help imhist
% help histeq
% I = imread('tire.tif');
I = imread('pout.tif');
J = histeq(I); % Enhance contrast using histogram equalization
figure, subplot(211),imhist(I),title('Actual histogram');
subplot(212),imhist(J),title('Histogram equalized');
figure, subplot(121),imshow(I),title('Original image');
subplot(122),imshow(J),title('Histogram equalized');
% help edge
I = imread('circuit.tif');
BW1 = edge(I,'prewitt'); BW2 = edge(I,'canny');
figure, imshow(I),title('Original image');
figure, subplot(121),imshow(BW1),title('Prewitt edge operator');
subplot(122),imshow(BW2),title('Canny edge operator')
108
Department of ECE, SRKR Engineering College
DSP Laboratory
109
Department of ECE, SRKR Engineering College
DSP Laboratory
Fig. 13.5 P13.2: Low Contrast Image & Histogram Equalization Effect
110
Department of ECE, SRKR Engineering College
DSP Laboratory
111
Department of ECE, SRKR Engineering College
DSP Laboratory
112
Department of ECE, SRKR Engineering College
DSP Laboratory
% I = imread('coins.png');
I = imread('Einstein.bmp');
N = 11; h = ones(N,N)/(N*N); % averaging LP filter
I2 = imfilter(I,h); % 2D filtering
figure, subplot(1,2,1), imshow(I);title('Original Image');
subplot(1,2,2), imshow(I2), title('LP Filtered Image')
% x = imread('Lena.bmp');
x = imread('Einstein.bmp');
f = double(x); ff = abs(fft2(f)); % 2D fft mag
% Averaging filter
L = 5; h = ones(2*L+1,2*L+1)/((2*L+1)^2); % 2D filter IR
[X,Y] = meshgrid(-L:L);
[hf,Fx,Fy] = freqz2(h,512); % 2D freq res
figure(1);
subplot(221),imshow(x); title('Image');
subplot(2,2,2),mesh(X,Y,h*(2*L+1)^2),%axis([-10 10 -10 10 -1 2]),
title('2D Impulse resp');
subplot(223),imagesc(y); title('SD filtered image');
colormap(gray);truesize;axis off;
subplot(224),imshow(uint8(real(y2)),[])
title('FD filtered image');
% imagesc(real(y2)),colormap(gray);
% mlowv = min(min(y2)), mhighv = max(max(y2))
figure(2);
subplot(2,2,1),imagesc(fftshift(log(ff+1)));title('Spectrum image');
colormap(gray);%truesize;%axis off;
113
Department of ECE, SRKR Engineering College
DSP Laboratory
114
Department of ECE, SRKR Engineering College
DSP Laboratory
115
Department of ECE, SRKR Engineering College
DSP Laboratory
116
Department of ECE, SRKR Engineering College
DSP Laboratory
117
Department of ECE, SRKR Engineering College
DSP Laboratory
% NOTE: where LEN=31 is length of the blur in pixel and THETA indicates blur
image
% Use the deconvwnr function to deblur an image using the Wiener filter.
% By using Regularized filter, Lucy-Richardson algorithm, Blind deconvolution
algorithm also we can get deblurred image
% plot results
figure, subplot(131),imshow(I), title('Original Image');
subplot(132),imshow(Blurred); xlabel('Motion Blurred Image');
title(['PSNR = ', num2str(psnr1,4), ' dB']);
subplot(133),imshow(NoisyBlurred),xlabel('Blurred Image + Noise'); % blur +
noise
title(['PSNR = ', num2str(psnr2,4), ' dB']);
118
Department of ECE, SRKR Engineering College
DSP Laboratory
% I = imread('eight.tif');
I = imread('Einstein.bmp');
J1 = imnoise(I,'gaussian',0,0.01);
J2 = imnoise(I,'salt & pepper',0.1);
J3 = imnoise(I,'speckle',0.01);
figure, subplot(221), imshow(I), title('Image')
subplot(222), imshow(J1), title('Gaussian noise')
subplot(223), imshow(J2), title('Salt & pepper')
subplot(224), imshow(J3), title('Speckle')
119
Department of ECE, SRKR Engineering College
DSP Laboratory
120
Department of ECE, SRKR Engineering College
DSP Laboratory
% close all;
x1 = imread('Lena_image.bmp'); % imshow(x1);title('Lena image')
% X1 = fft2(double(x1)); x1r = ifft2(X1);
% figure, imagesc(real(x1r));colormap(gray);title('Lena image');truesize
x2 = imread('Einstein.bmp');
f1 = double(x1); f1fm = abs(fft2(f1)); f1fp = angle(fft2(f1));
f2 = double(x2); f2fm = abs(fft2(f2)); f2fp = angle(fft2(f2));
figure(1); subplot(231),imshow(x1);title('Lena')
subplot(232), imagesc(fftshift(log(f1fm+1))); title('FFT2 mag')
colormap(gray); truesize;axis off;
subplot(233), imshow(fftshift((f1fp))); title('FFT2 phase')
colormap(gray); truesize;axis off;
subplot(234), imshow(x2);title('Einstein')
subplot(235), imagesc(fftshift(log(f2fm+1))); title('FFT2 mag')
colormap(gray); truesize;axis off;
subplot(236), imshow(fftshift((f2fp))); title('FFT2 phase')
colormap(gray); truesize;axis off;
121
Department of ECE, SRKR Engineering College
DSP Laboratory
% Plots
figure, subplot(121), imshow(I), title('Image');
subplot(122), imshow(K,[0 255]); title('Image with DCT modified');
figure, subplot(121), imshow(DCT1,[]),colormap(jet),colorbar, title('DCT');
subplot(122), imshow(log(abs(J)+1),[]),colormap(jet),colorbar
title(sprintf('DCT coeffs thresholded at %d',th));
122
Department of ECE, SRKR Engineering College
DSP Laboratory
Fig. 13.14 P13.5: Two images with magnitude & phase swapped
123
Department of ECE, SRKR Engineering College
DSP Laboratory
124
Department of ECE, SRKR Engineering College
DSP Laboratory
125
Department of ECE, SRKR Engineering College
DSP Laboratory
126