P. 1
DSP_Lab_Manual_Matlab_Ankit

DSP_Lab_Manual_Matlab_Ankit

|Views: 35|Likes:
Published by Ankit Bhurane
Digital, Signal, Processing, DSP, Lab, Manual, Matlab, Ankit
Digital, Signal, Processing, DSP, Lab, Manual, Matlab, Ankit

More info:

Published by: Ankit Bhurane on Dec 14, 2012
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

01/02/2013

pdf

text

original

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. 2. 3. 4. Basic discrete-time sequences……………………………………………………... 1 Decomposition of real signal into even & odd components………………………. 5 Range of digital frequency of a sinusoid…………………………………………... 7 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

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 ( ankit.bhurane@gmail.com ) 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: ankit.bhurane@gmail.com =============================== START ===================================

%% Clear previous Data (if required) clc; close all; clear all; %% Define desired Signals x = -10:10; % Time axis y1 y2 y3 y4 y5 y6 = = = = = = zeros(1,21); y1(1,11) = 1; % Unit Impulse zeros(1,21); y2(1,11:21) = 1; % Unit Step 0:10; % Unit Ramp exp(0:2*pi/10:2*pi); % Exponential (Increasing) exp(-(0:2*pi/10:2*pi)); % Exponential (Decreasing) sin(0:2*pi/10:2*pi); % Sinusoid

%% Plot desired signals subplot(3,2,1); subplot(3,2,2); subplot(3,2,3); subplot(3,2,4); subplot(3,2,5); subplot(3,2,6); stem(x,y1); title('Unit Impulse'); stem(x,y2); title('Unit Step'); stem(y3,y3); title('Unit Ramp'); stem(y3,y4); title('Exponential Increasing'); stem(y3,y5); title('Exponential Decreasing'); stem(y3,y6); title('Sinusoid');

% ================================ END ====================================

Ankit A. Bhurane

3

S.G.G.S.I..E.T., Nanded.

D.S.P. Lab Book

Output:

Lab 2: Decomposition of real signal into even & odd components. Matlab code:
%% Copyright Notice

Ankit A. Bhurane

4

S.G.G.S.I..E.T., Nanded.

D.S.P. Lab Book

% ========================================================================= % DSP Lab 2: Matlab code for decomposition of real signal into even & odd. % Code By: Ankit A. Bhurane ( ankit.bhurane@gmail.com ) % 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: ankit.bhurane@gmail.com % =============================== START =================================== %% Clear previous Data (if required) clc; close all; clear all; x y e o = = = = -10:10; % Time axis zeros(1,21); y(1,11:21) = 1; % Step Signal (y + fliplr(y))/2; % Even decomposition (y - fliplr(y))/2; % Odd decomposition

%% Plot desired Signals subplot(5,1,1); subplot(5,1,2); subplot(5,1,3); subplot(5,1,4); subplot(5,1,5); stem(x,y); title('Original signal'); % Plot original signal stem(x,fliplr(y)); title('Fliped signal'); % Its Fliped ver stem(x,e); title('Even Decomposition'); stem(x,o); title('Odd Decomposition'); stem(x,e+o); title('Reconstructed/ Synthesized signal');

% ================================ END ====================================

Output:

Ankit A. Bhurane

5

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

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 ( ankit.bhurane@gmail.com ) 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: ankit.bhurane@gmail.com =============================== START ===================================

%% Clear previous Data (if required) clc; clear all; close all;

%% Define required Parameters & Plot Signals w = n = t = for end % ================================ END ==================================== 0:pi/4:2*pi; % 0:49; % Number 0; % Theta k = 1:9 subplot(3,3,k); title(strcat('w Omega ( For unique sequences maximium value is pi ) of samples = 50 stem(cos(w(k)*n + t)); =',num2str(w(k))));

Ankit A. Bhurane

7

S.G.G.S.I..E.T., Nanded.

D.S.P. Lab Book

Output:

Ankit A. Bhurane

8

S.G.G.S.I..E.T., Nanded.

D.S.P. Lab Book

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 ( ankit.bhurane@gmail.com ) 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: ankit.bhurane@gmail.com =============================== START ===================================

%% Clear previous Data (if required) clc; clear 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 ==================================== close all;

Ankit A. Bhurane

9

S.G.G.S.I..E.T., Nanded.

D.S.P. Lab Book

Output:

Ankit A. Bhurane

10

S.G.G.S.I..E.T., Nanded.

D.S.P. Lab Book

(b)
%% Copyright Notice % % % % % % % % % % % % % % % % % % % % % ========================================================================= DSP Lab 4(b): Maltab code to demostrate sampling theorem( Nyquist Rate ). Code By: Ankit A. Bhurane ( ankit.bhurane@gmail.com ) 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: ankit.bhurane@gmail.com =============================== 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 s2 s3 s4 = = = = cos(2*pi*Fc*t1); cos(2*pi*Fc*t2); cos(2*pi*Fc*t3); cos(2*pi*Fc*t4); % % % % Original Original Original Original signal signal signal signal sampled sampled sampled sampled at at at at very SR < SR = SR > high rate (100*Fs) 2*Fc (SR = Fc) 2*Fc 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 ====================================

Ankit A. Bhurane

11

S.G.G.S.I..E.T., Nanded.

D.S.P. Lab Book

Output:

Ankit A. Bhurane

12

S.G.G.S.I..E.T., Nanded.

D.S.P. Lab Book

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 ( ankit.bhurane@gmail.com ) 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: ankit.bhurane@gmail.com =============================== 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);

Ankit A. Bhurane

13

S.G.G.S.I..E.T., Nanded.
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)]);

D.S.P. Lab Book

% ================================ END ====================================

Output: Convolution: 5 16 34 Deconvolution: 1 2 3 (b)
%% Copyright Notice % % % % % % % % % % % % % % % % % % % % % % ========================================================================= DSP Lab 5(b): Matlab code for 1-D linear & circular convolution ( using matrix multiplication method ). Code By: Ankit A. Bhurane ( ankit.bhurane@gmail.com ) 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: ankit.bhurane@gmail.com =============================== START ===================================

60 4

61

52

32

%% Clear previous Data (if required) clc; clear all; close all; %% Parameters

Ankit A. Bhurane

14

S.G.G.S.I..E.T., Nanded.

D.S.P. Lab Book

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 (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 ( ankit.bhurane@gmail.com ) 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

52

32

Ankit A. Bhurane

15

S.G.G.S.I..E.T., Nanded.
% % % % % % % % % %

D.S.P. Lab Book

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: ankit.bhurane@gmail.com =============================== 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 68 60 66 61 60 52 32

The circular convolution is: 66

Ankit A. Bhurane

16

S.G.G.S.I..E.T., Nanded.

D.S.P. Lab Book

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 ( ankit.bhurane@gmail.com ) 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: ankit.bhurane@gmail.com =============================== START ===================================

%% Clear previous Data (if required) clc; clear all; close all; %% Parameters x N l x = = = = [1 2 3 4]; % Input Signal in Time Domain 5; % Desired Length of DFT length(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

Ankit A. Bhurane

17

S.G.G.S.I..E.T., Nanded.
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);

D.S.P. Lab Book

% ================================ END ====================================

Output: DFT is: 10.0000 1.5451 + 2.1266i -4.0451 - 1.3143i -4.0451 + 1.3143i 1.5451 - 2.1266i

IDFT is: 1.0000 - 0.0000i 2.0000 4.0000 + 0.0000i 0.0000 + 0.0000i (b)
%% Copyright Notice % % % % % % % % % % % % % % % % % % % % %

3.0000 + 0.0000i

========================================================================= DSP Lab 6(b): Maltab code to find IDFT using DFT. Code By: Ankit A. Bhurane ( ankit.bhurane@gmail.com ) 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: ankit.bhurane@gmail.com =============================== 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)]);

Ankit A. Bhurane

18

S.G.G.S.I..E.T., Nanded.

D.S.P. Lab Book

% ================================ END ====================================

Output: The DFT is: 10+0i -2+2i 2 3 -2+0i 4 -2-2i

The IDFT using DFT is: 1

Ankit A. Bhurane

19

S.G.G.S.I..E.T., Nanded.

D.S.P. Lab Book

Lab 7: Convolution Theorem. Matlab code:
%% Copyright Notice % % % % % % % % % % % % % % % % % % % % % ========================================================================= DSP Lab 7: Maltab code to verify convolution theorem. Code By: Ankit A. Bhurane ( ankit.bhurane@gmail.com ) 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: ankit.bhurane@gmail.com =============================== 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 68 66 60

Inverse of their multiplication in FD: 66

Ankit A. Bhurane

20

S.G.G.S.I..E.T., Nanded.

D.S.P. Lab Book

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 ( ankit.bhurane@gmail.com ) 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: ankit.bhurane@gmail.com =============================== 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 ====================================

Ankit A. Bhurane

21

S.G.G.S.I..E.T., Nanded.

D.S.P. Lab Book

Output:

Ankit A. Bhurane

22

S.G.G.S.I..E.T., Nanded.

D.S.P. Lab Book

Lab 9: Gibbs Phenomenon. Matlab code:
%% Copyright Notice % % % % % % % % % % % % % % % % % % % % % ========================================================================= DSP Lab 9: Matlab code to demonstrate Gibbs phenomenon. Code By: Ankit A. Bhurane ( ankit.bhurane@gmail.com ) 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: ankit.bhurane@gmail.com =============================== 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); subplot(322); subplot(323); subplot(324); subplot(325); subplot(326); stem(-50:50,h); stem(-50:50,H); stem(-50:50,w); stem(-50:50,W); stem(-50:50,m); stem(-50:50,M); title('Its TD representation'); title('Ideal LP frequency Responce'); title('Ideal Rectangular window in TD'); title('Its FD representation'); title('Windowed Fourier coefficients in TD'); title('Its FD representation');

% ================================ END ====================================

Ankit A. Bhurane

23

S.G.G.S.I..E.T., Nanded.

D.S.P. Lab Book

Output:

Ankit A. Bhurane

24

S.G.G.S.I..E.T., Nanded.

D.S.P. Lab Book

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 ( ankit.bhurane@gmail.com ) 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: ankit.bhurane@gmail.com =============================== 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 b2 b3 b4 b5 = = = = = fir1(L-1, fir1(L-1, fir1(L-1, fir1(L-1, fir1(L-1, Wc, Wc, Wc, Wc, Wc, 'low', 'low', 'low', 'low', 'low', rectwin(L)); bartlett(L)); hamming(L)); hanning(L)); 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');

Ankit A. Bhurane

25

S.G.G.S.I..E.T., Nanded.
plot(w, 20*log(abs(h3)), 'r'); plot(w, 20*log(abs(h4)), 'c'); plot(w, 20*log(abs(h5)), 'k');

D.S.P. Lab Book

legend('Rectwin','Bartlett','Hamming','Hanning','Blackman'); xlabel('Normalized Frequency'); ylabel('Magnitude (dB)'); grid on; % ================================ END ====================================

Output:

Ankit A. Bhurane

26

S.G.G.S.I..E.T., Nanded.

D.S.P. Lab Book

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 ( ankit.bhurane@gmail.com ) 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: ankit.bhurane@gmail.com =============================== 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 ====================================

Ankit A. Bhurane

27

S.G.G.S.I..E.T., Nanded.

D.S.P. Lab Book

Output:

Ankit A. Bhurane

28

S.G.G.S.I..E.T., Nanded.

D.S.P. Lab Book

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 ( ankit.bhurane@gmail.com ) 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: ankit.bhurane@gmail.com =============================== 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);

Ankit A. Bhurane

29

S.G.G.S.I..E.T., Nanded.
% 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)');

D.S.P. Lab Book

% ================================ END ====================================

Output:

Ankit A. Bhurane

30

S.G.G.S.I..E.T., Nanded.

D.S.P. Lab Book

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 ( ankit.bhurane@gmail.com ) 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: ankit.bhurane@gmail.com =============================== 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.

Ankit A. Bhurane

31

S.G.G.S.I..E.T., Nanded.
fh = cconv(impz(bh,ah,300),x',SR); % Alternate Method 2 % fl = filter(bl,al,x); % fh = filter(bh,ah,x);

D.S.P. Lab Book

% 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:

Ankit A. Bhurane

32

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

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->