dsp_lab_manual_matlab_ankit
DESCRIPTION
Digital, Signal, Processing, DSP, Lab, Manual, Matlab, AnkitTRANSCRIPT
D.S.P. Lab Book Submitted by:
Ankit A. Bhurane [2009MEC001]
Shri Guru Gobind Singhji Institute of Engineering & Technology,
Nanded, Maharashtra.
CONTENTS
MATLAB® programs:
1. Basic discrete-time sequences……………………………………………………... 1
2. Decomposition of real signal into even & odd components………………………. 5
3. Range of digital frequency of a sinusoid…………………………………………... 7
4. Sampling Theorem
a) Verification of sampling theorem……………………………………………... 9
b) Demonstrate sampling theorem (alternative)………………………………….. 11
5. Convolution
a) 1-D linear convolution & deconvolution using Formula……………………… 13
b) 1-D linear and circular convolution & deconvolution using
Matrix Multiplication………………………………………………………….. 14
c) 1-D circular convolution from linear convolution…………………………….. 15
6. Discrete Fourier Transform
a) N-point Discrete Fourier Transform (DFT) & Inverse Discrete Fourier
Transform (IDFT)……………………………………………………………... 17
b) Getting IDFT from DFT………………………………………………………. 18
7. Convolution Theorem……………………………………………………………… 20
8. Impulse response & frequency response of an IIR & FIR systems………………… 21
9. Gibbs Phenomenon…………………………………………………………………. 23
10. FIR filter design using windowing…………………………………………………. 25
11. Butterworth IIR filter design using bilinear transform……………………………… 27
12. Filtering of a signal using FIR filter……………………………………………….. 29
13. Filtering of a signal using IIR filter………………………………………………… 31
Appendix
S.G.G.S.I..E.T., Nanded. D.S.P. Lab Book
Ankit A. Bhurane 3
Lab 1: Basic discrete-time sequences.
Matlab code:
%% Copyright Notice
% ========================================================================= % DSP Lab 1: Matlab code for plotting basic discrete time sequences. % Code By: Ankit A. Bhurane ( [email protected] ) % Last Updated: 25 April 2011 % ========================================================================= % Copyright © 2011 Ankit A. Bhurane % All Rights Reserved. % ------------------------------------------------------------------------- % Permission to use, copy, modify, distribute or sell this software and its % documentation for educational and research purposes only and without fee % is hereby granted, provided that this copyright notice and the original % author's name appear on all copies and supporting documentation. This % program shall not be used, rewritten, or adapted as the basis of a % commercial software or hardware product without first obtaining % permission of the author. The author make no representations about the % suitability of this software for any purpose. It is provided "as is" % without express or implied warranty. % ------------------------------------------------------------------------- % You can get this code from http://www.ankitbhurane.co.nr % or with a request to: [email protected] % =============================== START ===================================
%% Clear previous Data (if required)
clc; close all; clear all;
%% Define desired Signals
x = -10:10; % Time axis
y1 = zeros(1,21); y1(1,11) = 1; % Unit Impulse y2 = zeros(1,21); y2(1,11:21) = 1; % Unit Step y3 = 0:10; % Unit Ramp y4 = exp(0:2*pi/10:2*pi); % Exponential (Increasing) y5 = exp(-(0:2*pi/10:2*pi)); % Exponential (Decreasing) y6 = sin(0:2*pi/10:2*pi); % Sinusoid
%% Plot desired signals
subplot(3,2,1); stem(x,y1); title('Unit Impulse'); subplot(3,2,2); stem(x,y2); title('Unit Step'); subplot(3,2,3); stem(y3,y3); title('Unit Ramp'); subplot(3,2,4); stem(y3,y4); title('Exponential Increasing'); subplot(3,2,5); stem(y3,y5); title('Exponential Decreasing'); subplot(3,2,6); stem(y3,y6); title('Sinusoid');
% ================================ END ====================================
S.G.G.S.I..E.T., Nanded. D.S.P. Lab Book
Ankit A. Bhurane 4
Output:
Lab 2: Decomposition of real signal into even & odd components.
Matlab code:
%% Copyright Notice
S.G.G.S.I..E.T., Nanded. D.S.P. Lab Book
Ankit A. Bhurane 5
% ========================================================================= % DSP Lab 2: Matlab code for decomposition of real signal into even & odd. % Code By: Ankit A. Bhurane ( [email protected] ) % Last Updated: 25 April 2011 % ========================================================================= % Copyright © 2011 Ankit A. Bhurane % All Rights Reserved. % ------------------------------------------------------------------------- % Permission to use, copy, modify, distribute or sell this software and its % documentation for educational and research purposes only and without fee % is hereby granted, provided that this copyright notice and the original % author's name appear on all copies and supporting documentation. This % program shall not be used, rewritten, or adapted as the basis of a % commercial software or hardware product without first obtaining % permission of the author. The author make no representations about the % suitability of this software for any purpose. It is provided "as is" % without express or implied warranty. %-------------------------------------------------------------------------- % You can get this code from http://www.ankitbhurane.co.nr % or with a request to: [email protected] % =============================== START ===================================
%% Clear previous Data (if required)
clc; close all; clear all;
x = -10:10; % Time axis y = zeros(1,21); y(1,11:21) = 1; % Step Signal e = (y + fliplr(y))/2; % Even decomposition o = (y - fliplr(y))/2; % Odd decomposition
%% Plot desired Signals
subplot(5,1,1); stem(x,y); title('Original signal'); % Plot original signal subplot(5,1,2); stem(x,fliplr(y)); title('Fliped signal'); % Its Fliped ver subplot(5,1,3); stem(x,e); title('Even Decomposition'); subplot(5,1,4); stem(x,o); title('Odd Decomposition'); subplot(5,1,5); stem(x,e+o); title('Reconstructed/ Synthesized signal');
% ================================ END ====================================
Output:
S.G.G.S.I..E.T., Nanded. D.S.P. Lab Book
Ankit A. Bhurane 6
S.G.G.S.I..E.T., Nanded. D.S.P. Lab Book
Ankit A. Bhurane 7
Lab 3: Range of digital frequency of a sinusoid.
Matlab code:
%% Copyright Notice
% ========================================================================= % DSP Lab 3: Matlab code to verify the range of digital frequency. % Code By: Ankit A. Bhurane ( [email protected] ) % Last Updated: 25 April 2011 % ========================================================================= % Copyright © 2011 Ankit A. Bhurane % All Rights Reserved. % ------------------------------------------------------------------------- % Permission to use, copy, modify, distribute or sell this software and its % documentation for educational and research purposes only and without fee % is hereby granted, provided that this copyright notice and the original % author's name appear on all copies and supporting documentation. This % program shall not be used, rewritten, or adapted as the basis of a % commercial software or hardware product without first obtaining % permission of the author. The author make no representations about the % suitability of this software for any purpose. It is provided "as is" % without express or implied warranty. % ------------------------------------------------------------------------- % You can get this code from http://www.ankitbhurane.co.nr % or with a request to: [email protected] % =============================== START ===================================
%% Clear previous Data (if required)
clc; clear all; close all;
%% Define required Parameters & Plot Signals
w = 0:pi/4:2*pi; % Omega ( For unique sequences maximium value is pi ) n = 0:49; % Number of samples = 50 t = 0; % Theta for k = 1:9 subplot(3,3,k); stem(cos(w(k)*n + t)); title(strcat('w =',num2str(w(k)))); end
% ================================ END ====================================
S.G.G.S.I..E.T., Nanded. D.S.P. Lab Book
Ankit A. Bhurane 8
Output:
S.G.G.S.I..E.T., Nanded. D.S.P. Lab Book
Ankit A. Bhurane 9
Lab 4: Sampling theorem.
(a) Verify sampling theorem.
(b) Demonstrate sampling theorem (alternate).
Matlab code:
(a)
%% Copyright Notice
% ========================================================================= % DSP Lab 4(a): Maltab code to verify sampling theorem ( Nyquist Rate ). % Code By: Ankit A. Bhurane ( [email protected] ) % Last Updated: 25 April 2011 % ========================================================================= % Copyright © 2011 Ankit A. Bhurane % All Rights Reserved. % ------------------------------------------------------------------------- % Permission to use, copy, modify, distribute or sell this software and its % documentation for educational and research purposes only and without fee % is hereby granted, provided that this copyright notice and the original % author's name appear on all copies and supporting documentation. This % program shall not be used, rewritten, or adapted as the basis of a % commercial software or hardware product without first obtaining % permission of the author. The author make no representations about the % suitability of this software for any purpose. It is provided "as is" % without express or implied warranty. % ------------------------------------------------------------------------- % You can get this code from http://www.ankitbhurane.co.nr % or with a request to: [email protected] % =============================== START ===================================
%% Clear previous Data (if required)
clc; clear all; close all;
%% Specifications
F = 0:5:45; % Frequency varied from 0 to 45 Hz Fs = 50; % Sampling Frequency n = 0:Fs; % Sampling instant f = -Fs/2:Fs/2; % Frequency Axis (Symmetric)
%% Calculate & Plot desired Signals
for k = 1:size(F,2) y = cos(2*pi*F(k)/Fs*n); figure(1); subplot(5,2,k); plot(n,y); xlabel('Time(s)'); title(strcat('F =',num2str(F(k)),'Hz, Fs = 50 Hz')); figure(2); subplot(5,2,k); stem(f,abs(fftshift(fft(y)))); xlabel('Frequency(Hz)'); end
% ================================ END ====================================
S.G.G.S.I..E.T., Nanded. D.S.P. Lab Book
Ankit A. Bhurane 10
Output:
S.G.G.S.I..E.T., Nanded. D.S.P. Lab Book
Ankit A. Bhurane 11
(b)
%% Copyright Notice
% ========================================================================= % DSP Lab 4(b): Maltab code to demostrate sampling theorem( Nyquist Rate ). % Code By: Ankit A. Bhurane ( [email protected] ) % Last Updated: 25 April 2011 % ========================================================================= % Copyright © 2011 Ankit A. Bhurane % All Rights Reserved. % ------------------------------------------------------------------------- % Permission to use, copy, modify, distribute or sell this software and its % documentation for educational and research purposes only and without fee % is hereby granted, provided that this copyright notice and the original % author's name appear on all copies and supporting documentation. This % program shall not be used, rewritten, or adapted as the basis of a % commercial software or hardware product without first obtaining % permission of the author. The author make no representations about the % suitability of this software for any purpose. It is provided "as is" % without express or implied warranty. % ------------------------------------------------------------------------- % You can get this code from http://www.ankitbhurane.co.nr % or with a request to: [email protected] % =============================== START ===================================
%% Clear previous Data (if required)
clc; clear all; close all;
%% Specifications
Fc = 1; Fs1 = 100*Fc; Fs2 = 1.2*Fc; Fs3 = 2*Fc; Fs4 = 10*Fc; t1 = 0:1/Fs1:10; t2 = 0:1/Fs2:10; t3 = 0:1/Fs3:10; t4 = 0:1/Fs4:10; % 10Sec
%% Signals
s1 = cos(2*pi*Fc*t1); % Original signal sampled at very high rate (100*Fs) s2 = cos(2*pi*Fc*t2); % Original signal sampled at SR < 2*Fc (SR = Fc) s3 = cos(2*pi*Fc*t3); % Original signal sampled at SR = 2*Fc s4 = cos(2*pi*Fc*t4); % Original signal sampled at SR > 2*Fc (SR = 10*Fc)
%% Plot desired Signals
subplot(311); plot(t1,s1,'r',t2,s2,'b*-'); title('For SR < 2*Fc'); subplot(312); plot(t1,s1,'r',t3,s3,'b*-'); title('For SR = 2*Fc'); subplot(313); plot(t1,s1,'r',t4,s4,'b*-'); title('For SR > 2*Fc');
% ================================ END ====================================
S.G.G.S.I..E.T., Nanded. D.S.P. Lab Book
Ankit A. Bhurane 12
Output:
S.G.G.S.I..E.T., Nanded. D.S.P. Lab Book
Ankit A. Bhurane 13
Lab 5: Convolution.
(c) 1-D linear convolution & deconvolution using Formula.
(d) 1-D linear and circular convolution & deconvolution using Matrix Multiplication.
(e) 1-D circular convolution from linear convolution.
Matlab code:
(a)
%% Copyright Notice
% ========================================================================= % DSP Lab 5(a): Matlab code for 1-D linear convolution & deconvolution % (using formula). % Code By: Ankit A. Bhurane ( [email protected] ) % Last Updated: 25 April 2011 % ========================================================================= % Copyright © 2011 Ankit A. Bhurane % All Rights Reserved. % ------------------------------------------------------------------------- % Permission to use, copy, modify, distribute or sell this software and its % documentation for educational and research purposes only and without fee % is hereby granted, provided that this copyright notice and the original % author's name appear on all copies and supporting documentation. This % program shall not be used, rewritten, or adapted as the basis of a % commercial software or hardware product without first obtaining % permission of the author. The author make no representations about the % suitability of this software for any purpose. It is provided "as is" % without express or implied warranty. % ------------------------------------------------------------------------- % You can get this code from http://www.ankitbhurane.co.nr % or with a request to: [email protected] % =============================== START ===================================
%% Clear previous Data (if required)
clc; clear all; close all;
%% Parameters
x = [1 2 3 4]; % First Signal h = [5 6 7 8]; % Second Signal
%% Calculate Linear Convolution Length
l1 = length(x); l2 = length(h); l = l1+l2-1;
x = [x zeros(1,l2-1)]; % Zero Padding h = [h zeros(1,l1-1)]; % Zero Padding y = zeros(1,l);
%% Linear Convolution
for i = 1:1:l for k = 1:1:i y(i) = y(i)+x(k)*h(i+1-k);
S.G.G.S.I..E.T., Nanded. D.S.P. Lab Book
Ankit A. Bhurane 14
end end disp(['Convolution: ', num2str(y)]);
%% Linear Deconvolution
x_r = zeros(1,l1); x_r(1) = y(1)/h(1); for i = 2:1:l1 for k = 1:1:i-1 x_r(i) = x_r(i)+x(k)*h(i+1-k); end x_r(i) = (y(i)-x_r(i))/h(1); end disp(['Deconvolution: ', num2str(x_r)]);
% ================================ END ====================================
Output:
Convolution: 5 16 34 60 61 52 32
Deconvolution: 1 2 3 4
(b)
%% Copyright Notice
% ========================================================================= % DSP Lab 5(b): Matlab code for 1-D linear & circular convolution ( using % matrix multiplication method ). % Code By: Ankit A. Bhurane ( [email protected] ) % Last Updated: 25 April 2011 % ========================================================================= % Copyright © 2011 Ankit A. Bhurane % All Rights Reserved. % ------------------------------------------------------------------------- % Permission to use, copy, modify, distribute or sell this software and its % documentation for educational and research purposes only and without fee % is hereby granted, provided that this copyright notice and the original % author's name appear on all copies and supporting documentation. This % program shall not be used, rewritten, or adapted as the basis of a % commercial software or hardware product without first obtaining % permission of the author. The author make no representations about the % suitability of this software for any purpose. It is provided "as is" % without express or implied warranty. % ------------------------------------------------------------------------- % You can get this code from http://www.ankitbhurane.co.nr % or with a request to: [email protected] % =============================== START ===================================
%% Clear previous Data (if required)
clc; clear all; close all;
%% Parameters
S.G.G.S.I..E.T., Nanded. D.S.P. Lab Book
Ankit A. Bhurane 15
x = [1 2 3 4]; % First Signal h = [5 6 7 8]; % Second Signal
%% Calculate Convolution Length
l1 = length(x); l2 = length(h); l = l1+l2-1; m = max(l1,l2);
%% Circular Convolution
x1 = [x zeros(1,m-l1)]; h1 = [h zeros(1,m-l2)];
p1 = zeros(m,m); for i = 1:m p1(:,i) = circshift(x1',i-1); end cir = p1*h1'; disp(['The circular convolution is: ', num2str(cir')]);
%% Linear Convolution
x2 = [x zeros(1,l-l1)]; h2 = [h zeros(1,l-l2)];
p2 = zeros(l,l); for i = 1:l p2(:,i) = circshift(x2',i-1); end lin = p2*h2'; disp(['The linear convolution is: ', num2str(lin')]);
% ================================ END ====================================
Output:
The circular convolution is: 66 68 66 60
The linear convolution is: 5 16 34 60 61 52 32
(c) %% Copyright Notice
% ========================================================================= % DSP Lab 5(c): Matlab code for getting circular convolution result from % linear convolution using pad-add method. % Code By: Ankit A. Bhurane ( [email protected] ) % Last Updated: 25 April 2011 % ========================================================================= % Copyright © 2011 Ankit A. Bhurane % All Rights Reserved. % ------------------------------------------------------------------------- % Permission to use, copy, modify, distribute or sell this software and its % documentation for educational and research purposes only and without fee % is hereby granted, provided that this copyright notice and the original
S.G.G.S.I..E.T., Nanded. D.S.P. Lab Book
Ankit A. Bhurane 16
% author's name appear on all copies and supporting documentation. This % program shall not be used, rewritten, or adapted as the basis of a % commercial software or hardware product without first obtaining % permission of the author. The author make no representations about the % suitability of this software for any purpose. It is provided "as is" % without express or implied warranty. % ------------------------------------------------------------------------- % You can get this code from http://www.ankitbhurane.co.nr % or with a request to: [email protected] % =============================== START ===================================
%% Clear previous Data (if required)
clc; clear all; close all;
%% Parameters
x = [1 2 3 4]; % First Signal h = [5 6 7 8]; % Second Signal
%% Calculate Convolution Length
l1 = length(x); l2 = length(h); m = max(l1,l2);
%% Linear Convolution
lin_c = conv(x,h); disp(['The linear convolution is: ', num2str(lin_c)]);
%% Padd-Add
temp = [lin_c zeros(1,2*m-(l1+l2-1))]; % Zero Padd circ_c = temp(1,1:m)+temp(1,m+1:end); % Add disp(['The circular convolution is: ', num2str(circ_c)]);
% ================================ END ====================================
Output:
The linear convolution is: 5 16 34 60 61 52 32
The circular convolution is: 66 68 66 60
S.G.G.S.I..E.T., Nanded. D.S.P. Lab Book
Ankit A. Bhurane 17
Lab 6: Discrete Fourier Transform.
(a) N-point Discrete Fourier Transform (DFT) & Inverse Discrete Fourier Transform
(IDFT).
(b) Getting IDFT from DFT.
Matlab code:
(a)
%% Copyright Notice
% ========================================================================= % DSP Lab 6(a): Matlab code for N-point DFT & IDFT. % Code By: Ankit A. Bhurane ( [email protected] ) % Last Updated: 25 April 2011 % ========================================================================= % Copyright © 2011 Ankit A. Bhurane % All Rights Reserved. % ------------------------------------------------------------------------- % Permission to use, copy, modify, distribute or sell this software and its % documentation for educational and research purposes only and without fee % is hereby granted, provided that this copyright notice and the original % author's name appear on all copies and supporting documentation. This % program shall not be used, rewritten, or adapted as the basis of a % commercial software or hardware product without first obtaining % permission of the author. The author make no representations about the % suitability of this software for any purpose. It is provided "as is" % without express or implied warranty. % ------------------------------------------------------------------------- % You can get this code from http://www.ankitbhurane.co.nr % or with a request to: [email protected] % =============================== START ===================================
%% Clear previous Data (if required)
clc; clear all; close all;
%% Parameters
x = [1 2 3 4]; % Input Signal in Time Domain N = 5; % Desired Length of DFT l = length(x); x = [x zeros(1,N-l)];
%% Discrete Fourier Transform
f = zeros(1,N); k = 0:N-1; for n = 0:N-1 f(k+1) = f(k+1) + x(n+1)*exp(-1j*2*pi*k*n/N); end disp('DFT is: '); disp(f);
%% Inverse Discrete Fourier Transform
r = zeros(1,N); n = 0:N-1; for k = 0:N-1
S.G.G.S.I..E.T., Nanded. D.S.P. Lab Book
Ankit A. Bhurane 18
r(n+1) = r(n+1) + f(k+1)*exp(1j*2*pi*k*n/N); end r(n+1) = r(n+1)/N; disp('IDFT is: '); disp(r);
% ================================ END ====================================
Output:
DFT is:
10.0000 -4.0451 - 1.3143i 1.5451 - 2.1266i
1.5451 + 2.1266i -4.0451 + 1.3143i
IDFT is:
1.0000 - 0.0000i 2.0000 3.0000 + 0.0000i
4.0000 + 0.0000i 0.0000 + 0.0000i
(b) %% Copyright Notice
% ========================================================================= % DSP Lab 6(b): Maltab code to find IDFT using DFT. % Code By: Ankit A. Bhurane ( [email protected] ) % Last Updated: 25 April 2011 % ========================================================================= % Copyright © 2011 Ankit A. Bhurane % All Rights Reserved. % ------------------------------------------------------------------------- % Permission to use, copy, modify, distribute or sell this software and its % documentation for educational and research purposes only and without fee % is hereby granted, provided that this copyright notice and the original % author's name appear on all copies and supporting documentation. This % program shall not be used, rewritten, or adapted as the basis of a % commercial software or hardware product without first obtaining % permission of the author. The author make no representations about the % suitability of this software for any purpose. It is provided "as is" % without express or implied warranty. % ------------------------------------------------------------------------- % You can get this code from http://www.ankitbhurane.co.nr % or with a request to: [email protected] % =============================== START ===================================
%% Clear previous Data (if required)
clc; clear all; close all;
%% Calculations
x = [1 2 3 4]; % Signal in Time Domain dft = fft(x); disp(['The DFT is: ', num2str(dft)]); idft = (1/size(x,2))*conj(fft(conj(dft))); % Algorithm disp(['The IDFT using DFT is: ', num2str(idft)]);
S.G.G.S.I..E.T., Nanded. D.S.P. Lab Book
Ankit A. Bhurane 19
% ================================ END ====================================
Output: The DFT is: 10+0i -2+2i -2+0i -2-2i
The IDFT using DFT is: 1 2 3 4
S.G.G.S.I..E.T., Nanded. D.S.P. Lab Book
Ankit A. Bhurane 20
Lab 7: Convolution Theorem.
Matlab code: %% Copyright Notice
% ========================================================================= % DSP Lab 7: Maltab code to verify convolution theorem. % Code By: Ankit A. Bhurane ( [email protected] ) % Last Updated: 25 April 2011 % ========================================================================= % Copyright © 2011 Ankit A. Bhurane % All Rights Reserved. % ------------------------------------------------------------------------- % Permission to use, copy, modify, distribute or sell this software and its % documentation for educational and research purposes only and without fee % is hereby granted, provided that this copyright notice and the original % author's name appear on all copies and supporting documentation. This % program shall not be used, rewritten, or adapted as the basis of a % commercial software or hardware product without first obtaining % permission of the author. The author make no representations about the % suitability of this software for any purpose. It is provided "as is" % without express or implied warranty. % ------------------------------------------------------------------------- % You can get this code from http://www.ankitbhurane.co.nr % or with a request to: [email protected] % =============================== START ===================================
%% Clear previous Data (if required)
clc; clear all; close all;
%% Calculations
x1 = [1 2 3 4]; % First Signal x2 = [5 6 7 8]; % Second Signal f1 = fft(x1); f2 = fft(x2); disp(['Conv in TD: ',num2str(cconv(x1,x2,max(length(x1),length(x2))))]); disp(['Inverse of their multiplication in FD: ',num2str(ifft(f1.*f2))]);
% ================================ END ====================================
Output: Conv in TD: 66 68 66 60
Inverse of their multiplication in FD: 66 68 66 60
S.G.G.S.I..E.T., Nanded. D.S.P. Lab Book
Ankit A. Bhurane 21
Lab 8: Impulse response & frequency response of an IIR & FIR systems.
Matlab code: %% Copyright Notice
% ========================================================================= % DSP Lab 8: Matlab code to find Impulse Responce & Frequency Responce of % an IIR & FIR systems. % Code By: Ankit A. Bhurane ( [email protected] ) % Last Updated: 25 April 2011 % ========================================================================= % Copyright © 2011 Ankit A. Bhurane % All Rights Reserved. % ------------------------------------------------------------------------- % Permission to use, copy, modify, distribute or sell this software and its % documentation for educational and research purposes only and without fee % is hereby granted, provided that this copyright notice and the original % author's name appear on all copies and supporting documentation. This % program shall not be used, rewritten, or adapted as the basis of a % commercial software or hardware product without first obtaining % permission of the author. The author make no representations about the % suitability of this software for any purpose. It is provided "as is" % without express or implied warranty. % ------------------------------------------------------------------------- % You can get this code from http://www.ankitbhurane.co.nr % or with a request to: [email protected] % =============================== START ===================================
%% Clear previous Data (if required)
clc; clear all; close all;
%% IIR
num_iir = [0.5 1 0.5]; den_iir = [1 0 0.5]; impz(num_iir,den_iir,50); title('Impulse Responce of IIR filter'); figure; freqz(num_iir,den_iir); title('Frequency Responce of IIR filter');
%% FIR
num_fir = [0.5 0.5 0.5]; den_fir = 1; figure; impz(num_fir,den_fir,50); title('Impulse Responce of FIR filter'); figure; freqz(num_fir,den_fir); title('Frequency Responce of FIR filter');
% ================================ END ====================================
S.G.G.S.I..E.T., Nanded. D.S.P. Lab Book
Ankit A. Bhurane 22
Output:
S.G.G.S.I..E.T., Nanded. D.S.P. Lab Book
Ankit A. Bhurane 23
Lab 9: Gibbs Phenomenon.
Matlab code:
%% Copyright Notice
% ========================================================================= % DSP Lab 9: Matlab code to demonstrate Gibbs phenomenon. % Code By: Ankit A. Bhurane ( [email protected] ) % Last Updated: 25 April 2011 % ========================================================================= % Copyright © 2011 Ankit A. Bhurane % All Rights Reserved. % ------------------------------------------------------------------------- % Permission to use, copy, modify, distribute or sell this software and its % documentation for educational and research purposes only and without fee % is hereby granted, provided that this copyright notice and the original % author's name appear on all copies and supporting documentation. This % program shall not be used, rewritten, or adapted as the basis of a % commercial software or hardware product without first obtaining % permission of the author. The author make no representations about the % suitability of this software for any purpose. It is provided "as is" % without express or implied warranty. % ------------------------------------------------------------------------- % You can get this code from http://www.ankitbhurane.co.nr % or with a request to: [email protected] % =============================== START ===================================
%% Clear previous Data (if required)
clc; clear all; close all;
%% Frequency and Time Domain of Ideal Low Pass Filter
H = zeros(1,101); H(1,48:54) = 1; % Frequency Domain h = abs(fftshift(ifft(H))); % Time Domain
%% Frequency and Time Domain of Ideal Rectangular Window
w = zeros(1,101); w(1,36:66) = 1; W = abs(fftshift(fft(w)));
m = h.*w; M = abs(fftshift((fft(m))));
%% Plot the Desired Signals
subplot(321); stem(-50:50,h); title('Its TD representation'); subplot(322); stem(-50:50,H); title('Ideal LP frequency Responce'); subplot(323); stem(-50:50,w); title('Ideal Rectangular window in TD'); subplot(324); stem(-50:50,W); title('Its FD representation'); subplot(325); stem(-50:50,m); title('Windowed Fourier coefficients in TD'); subplot(326); stem(-50:50,M); title('Its FD representation');
% ================================ END ====================================
S.G.G.S.I..E.T., Nanded. D.S.P. Lab Book
Ankit A. Bhurane 24
Output:
S.G.G.S.I..E.T., Nanded. D.S.P. Lab Book
Ankit A. Bhurane 25
Lab 10: FIR filter design using windowing.
Matlab code:
%% Copyright Notice
% ========================================================================= % DSP Lab 10: Matlab code to design FIR filter using windowing. % Code By: Ankit A. Bhurane ( [email protected] ) % Last Updated: 25 April 2011 % ========================================================================= % Copyright © 2011 Ankit A. Bhurane % All Rights Reserved. % ------------------------------------------------------------------------- % Permission to use, copy, modify, distribute or sell this software and its % documentation for educational and research purposes only and without fee % is hereby granted, provided that this copyright notice and the original % author's name appear on all copies and supporting documentation. This % program shall not be used, rewritten, or adapted as the basis of a % commercial software or hardware product without first obtaining % permission of the author. The author make no representations about the % suitability of this software for any purpose. It is provided "as is" % without express or implied warranty. % ------------------------------------------------------------------------- % You can get this code from http://www.ankitbhurane.co.nr % or with a request to: [email protected] % =============================== START ===================================
%% Clear previous Data (if required)
clc; clear all; close all;
L=11; % Length of Filter Wc = 0.5; % Normalized cutoff frequency
%% Calculating desired Filter Coefficients
b1 = fir1(L-1, Wc, 'low', rectwin(L)); b2 = fir1(L-1, Wc, 'low', bartlett(L)); b3 = fir1(L-1, Wc, 'low', hamming(L)); b4 = fir1(L-1, Wc, 'low', hanning(L)); b5 = fir1(L-1, Wc, 'low', blackman(L));
%% Calculate their Frequency Responce
[h1,w] = freqz(b1); h2 = freqz(b2); h3 = freqz(b3); h4 = freqz(b4); h5 = freqz(b5);
%% Plot desired Signals
w = w/pi; % Normalize Frequency Axis
plot(w, 20*log(abs(h1)), 'b'); hold on; plot(w, 20*log(abs(h2)), 'g');
S.G.G.S.I..E.T., Nanded. D.S.P. Lab Book
Ankit A. Bhurane 26
plot(w, 20*log(abs(h3)), 'r'); plot(w, 20*log(abs(h4)), 'c'); plot(w, 20*log(abs(h5)), 'k');
legend('Rectwin','Bartlett','Hamming','Hanning','Blackman'); xlabel('Normalized Frequency'); ylabel('Magnitude (dB)'); grid on;
% ================================ END ====================================
Output:
S.G.G.S.I..E.T., Nanded. D.S.P. Lab Book
Ankit A. Bhurane 27
Lab 11: Butterworth IIR filter design using bilinear transform.
Matlab code: %% Copyright Notice
% ========================================================================= % DSP Lab 11: Matlab code to design Butterworth IIR filter using Biliner % Transform. % Code By: Ankit A. Bhurane ( [email protected] ) % Last Updated: 25 April 2011 % ========================================================================= % Copyright © 2011 Ankit A. Bhurane % All Rights Reserved. % ------------------------------------------------------------------------- % Permission to use, copy, modify, distribute or sell this software and its % documentation for educational and research purposes only and without fee % is hereby granted, provided that this copyright notice and the original % author's name appear on all copies and supporting documentation. This % program shall not be used, rewritten, or adapted as the basis of a % commercial software or hardware product without first obtaining % permission of the author. The author make no representations about the % suitability of this software for any purpose. It is provided "as is" % without express or implied warranty. % ------------------------------------------------------------------------- % You can get this code from http://www.ankitbhurane.co.nr % or with a request to: [email protected] % =============================== START ===================================
%% Clear previous Data (if required)
clc; clear all; close all;
%% Filter Specifications
Fp = 1000; % Passband Frequency in Hertz Fs = 350; % Stopband Frequency in Hertz Ap = 3; % Passband Attenuation in dB As = 10; % Stopband Attenuation in dB SR = 5000; % Sampling Rate in Samples/Sec ftype = 'high'; % Filter Type Wp = 2*Fp/SR; Ws = 2*Fs/SR; % Passband & Stopband Frequencies (pi not reqd)
%% Design required Filter
[n,Wn] = buttord(Wp,Ws,Ap,As); [b,a] = butter(n,Wn,ftype);
%% Plot its Frequency Responce
freqz(b,a); title('Frequency Resonce');
% ================================ END ====================================
S.G.G.S.I..E.T., Nanded. D.S.P. Lab Book
Ankit A. Bhurane 28
Output:
S.G.G.S.I..E.T., Nanded. D.S.P. Lab Book
Ankit A. Bhurane 29
Lab 12: Filtering of a signal using FIR filter.
Matlab code: %% Copyright Notice
% ========================================================================= % DSP Lab 12: Matlab code for filtering of a signal using FIR filter. % Code By: Ankit A. Bhurane ( [email protected] ) % Last Updated: 25 April 2011 % ========================================================================= % Copyright © 2011 Ankit A. Bhurane % All Rights Reserved. % ------------------------------------------------------------------------- % Permission to use, copy, modify, distribute or sell this software and its % documentation for educational and research purposes only and without fee % is hereby granted, provided that this copyright notice and the original % author's name appear on all copies and supporting documentation. This % program shall not be used, rewritten, or adapted as the basis of a % commercial software or hardware product without first obtaining % permission of the author. The author make no representations about the % suitability of this software for any purpose. It is provided "as is" % without express or implied warranty. % ------------------------------------------------------------------------- % You can get this code from http://www.ankitbhurane.co.nr % or with a request to: [email protected] % =============================== START ===================================
%% Clear previous Data (if required)
clc; clear all; close all;
%% Given Specifications
SR = 500; % If the sampling freq is changed, change the order accordingly n = 0:SR-1; F1 = 5; F2 = 50; % Frequencies of two signals Filt_Ord = 20; % Preferably Even (because all filter can be implemented) Filt_Len = Filt_Ord + 1;
x1 = cos(2*pi*F1/SR*n); % First sinusoid signal x2 = cos(2*pi*F2/SR*n); % Second sinusoid signal x = x1+x2; % Added signal
F = (F1 + F2)/2; % Average frequency for cutoff c = F/SR; % Cutoff Frequency
%% Filter Design
bl = fir1(Filt_Ord,c,'low',rectwin(Filt_Len)); % Low pass filtered signal bh = fir1(Filt_Ord,c,'high',rectwin(Filt_Len)); % High pass filtered signal
%% Filtering (Convolving)
fl = cconv(bl,x,SR); fh = cconv(bh,x,SR);
S.G.G.S.I..E.T., Nanded. D.S.P. Lab Book
Ankit A. Bhurane 30
% Alternate Method 2 % fl = filter(bl,1,x); % fh = filter(bh,1,x);
%% Plot Desired Signals
subplot(511); plot(n/SR,x1); title('10 Hz Signal'); subplot(512); plot(n/SR,x2); title('50 Hz Signal'); subplot(513); plot(n/SR,x); title('10 Hz + 50 Hz Signal'); subplot(514); plot(n/SR,fl); axis([0 1 -1 1]); title('Low pass FIR filtered signal (10 Hz)'); subplot(515); plot(n/SR,fh); axis([0 1 -1 1]); title('High pass FIR filtered Signal (50 Hz)');
% ================================ END ====================================
Output:
S.G.G.S.I..E.T., Nanded. D.S.P. Lab Book
Ankit A. Bhurane 31
Lab 13: Filtering of signal using IIR filter
Matlab code: %% Copyright Notice
% ========================================================================= % DSP Lab 13: Matlab code for filtering of a signal using IIR filter. % Code By: Ankit A. Bhurane ( [email protected] ) % Last Updated: 25 April 2011 % ========================================================================= % Copyright © 2011 Ankit A. Bhurane % All Rights Reserved. % ------------------------------------------------------------------------- % Permission to use, copy, modify, distribute or sell this software and its % documentation for educational and research purposes only and without fee % is hereby granted, provided that this copyright notice and the original % author's name appear on all copies and supporting documentation. This % program shall not be used, rewritten, or adapted as the basis of a % commercial software or hardware product without first obtaining % permission of the author. The author make no representations about the % suitability of this software for any purpose. It is provided "as is" % without express or implied warranty. % ------------------------------------------------------------------------- % You can get this code from http://www.ankitbhurane.co.nr % or with a request to: [email protected] % =============================== START ===================================
%% Clear previous Data (if required)
clc; clear all; close all;
%% Given Specifications
SR = 500; % If the sampling freq is changed, change the order accordingly n = 0:SR-1; F1 = 5; F2 = 50; % Frequencies of two signals Filt_Ord = 20; % Filter Order Preferably Even (coz all filters can be
implemented) Filt_Len = Filt_Ord + 1; % Filter Length
x1 = cos(2*pi*F1/SR*n); % First sinusoid signal x2 = cos(2*pi*F2/SR*n); % Second sinusoid signal x = x1+x2; % Added signal
F = (F1 + F2)/2; % Average frequency for cutoff c = F/SR; % Cutoff Frequency
%% Filter Design
[o,Wn] = buttord((2*F1/SR),(2*F2/SR),3,30); [bl,al] = butter(o,Wn,'low'); [bh,ah] = butter(o,Wn,'high');
%% Filtering (Convolving)
fl = cconv(impz(bl,al,300),x',SR); % f = deconv(conv(x,b),a); % filter uses
this logic.
S.G.G.S.I..E.T., Nanded. D.S.P. Lab Book
Ankit A. Bhurane 32
fh = cconv(impz(bh,ah,300),x',SR);
% Alternate Method 2 % fl = filter(bl,al,x); % fh = filter(bh,ah,x);
% Alternate Method 3 % fl = deconv(conv(x,bl),al); % 'filter' function uses this logic. % fh = deconv(conv(x,bh),ah); % 'filter' function uses this logic.
%% Plot Desired Signals
subplot(511); plot(n/SR,x1); title('10 Hz Signal'); subplot(512); plot(n/SR,x2); title('50 Hz Signal'); subplot(513); plot(n/SR,x); title('10 Hz + 50 Hz Signal'); subplot(514); plot(n/SR,fl); axis([0 1 -1 1]); title('Low pass FIR filtered signal (10 Hz)'); subplot(515); plot(n/SR,fh); axis([0 1 -1 1]); title('High pass FIR filtered Signal (50 Hz)');
% ================================ END ====================================
Output:
APPENDIX
+ Plus
- Minus
* Matrix multiplication
.* Array multiplication
^ Matrix power
.^ Array power
kron Kronecker tensor product
\ Backslash or left division
/ Slash or right division
./ and .\ Array division, right and left
: Colon
( ) Parentheses
[ ] Brackets
{} Curly braces
. Decimal point
... Continuation
, Comma
; Semicolon
% Comment
! Exclamation point
‘ Transpose and quote
.’ Nonconjugated transpose
= Assignment
== Equality
< > Relational operators
& Logical and
| Logical or
~ Logical not
xor Logical exclusive or
abs Absolute value (magnitude).
angle Phase angle.
clc Clear Command Window
conv Convolution and polynomial multiplication.
deconv Deconvolution and polynomial division.
freqz Compute the frequency response of digital filters.
impz Compute the impulse response of digital filters.
fir1 Design a window-based finite impulse response filter.
butter Butterworth analog and digital filter design.
cheby1 Chebyshev type I filter design (passband ripple).
cheby2 Chebyshev type II filter design (stopband ripple).
ellip Elliptic (Cauer) filter design.
buttord Calculate the order and cutoff frequency for a
Butterworth filter.
cheb1ord Calculate the order for a Chebyshev type I filter.
cheb2ord Calculate the order for a Chebyshev type II filter.
ellipord Calculate the minimum order for elliptic filters.
besself Bessel analog filter design.
butter Butterworth analog and digital filter design.
cheby1 Chebyshev type I filter design (passband ripple).
cheby2 Chebyshev type II filter design (stopband ripple).
ellip Elliptic (Cauer) filter design.
bilinear Bilinear transformation method for analog-to-digital
filter conversion.
impinvar Impulse invariance method for analog-to-digital filter
conversion.
input Request user input
lp2bp Transform lowpass analog filters to bandpass.
lp2bs Transform lowpass analog filters to bandstop.
lp2hp Transform lowpass analog filters to highpass.
lp2lp Change the cut -off frequency for a lowpass analog filter.
bartlett Compute a Bartlett window.
blackman Compute a Blackman window.
boxcar Compute a rectangular window.
chebwin Compute a Chebyshev window.
hamming Compute a Hamming window.
hann Compute the Hann (Hanning) window.
kaiser Compute a Kaiser window.
triang Compute a triangular window.
fft Compute the one-dimensional fast Fourier transform.
fftshift Rearrange the outputs of the FFT functions.
ifft One-dimensional inverse fast Fourier transform.
ifft2 Two-dimensional inverse fast Fourier transform.
ans The most recent answer
computer Identify the computer on which MATLAB is running
eps Floating-point relative accuracy
i Imaginary unit
Inf Infinity
j Imaginary unit
NaN Not-a-Number
pi Ratio of a circle’s circumference to its diameter,
abs Absolute value and complex magnitude
real Real part of complex number
strcat String concatenation
strcmp Compare strings
det Matrix determinant
norm Vector and matrix norms
null Null space of a matrix