Dsp Lab Report

Post on 23-Dec-2016

33 views 1 download

transcript

USIT and IGITGuru Gobind Singh Indraprastha University

Kashmere Gate, Delhi – 110006.

D EP A R T M E N T O F E L EC T R ON I CS & C O M M U N I CAT I ON E N G I N E E RI N G

Digital Signal Processing Lab

Sub Code: ECW

Sem: II

B y

Mr. Pramod.S

T a b l e o f C o n t en t s

1. Verification of Sampling theorem. 1

2. Impulse response of a given system 4

3. Linear convolution of two given sequences. 7

4. Circular convolution of two given sequences 11

5. Computation of N point DFT of a given sequence and to plotagnitude and phase spectrum

6. Linear convolution of two sequences using DFT andIDFT

7. Circular convolution of two given sequences using DFTand IDFT

8. Design and implementation of FIR filter to meet given specifications.

9. Design and implementation of IIR filter to meet given specifications.

P R O G R A M 1

V E R I F I C A TION OF S A M P LI NG THEO R EM

A i m : To write the MATLAB code for verifying Sampling Theorem.

Generate a sinusoidal wave of 1kHz. Calculate the Nyquist frequency, and verify Sampling Theorem, showing output waveforms for undersampled, oversampled and right sampled cases.

MA TL AB C O D E :

% Experiment 1 : Sampling Theorem Verification

clear all; close all; clc;

% Signal Parametersf = 1000; % Signal Frequency = 1kHzT = 1/f; % Signal Periodt = 0:0.01*T:2*T; % Time index

% Generate the original signal and plot it:x = cos(2*pi*t*f); % Signal : 2*pi*f*tsubplot(2,2,1);plot(t,x); title('Continuous signal'); xlabel('t');ylabel('x(t)');

%Oversampling Condition:fs1 = 10*f; % Oversampling (fs > 2f)n1 = 0:1/fs1:2*T; % Time scalex1 = cos(2*pi*f*n1); % Generating sampled signalsubplot(2,2,2);stem(n1,x1); hold on; plot(n1,x1,'r'); hold off;title('Oversampling Condition : Fs = 10F');xlabel('n');ylabel('x(n)');

% Right Sampling Condition:fs2 = 2*f; % Nyquist Rate Sampling (fs = 2f)n2 = 0:1/fs2:2*T;x2 = cos(2*pi*f*n2);subplot(2,2,3);stem(n2,x2);hold on; plot(n2,x2,'r'); hold off;title('Sampling at Nyquist Frequency : Fs = 2F'); xlabel('n');

ylabel('x(n)');

% Under Sampling Condition:fs3 = 1.5*f; % Undersampling (fs < 2f)n3 = 0:1/fs3:2*T;x3 = cos(2*pi*f*n3); subplot(2,2,4);stem(n3,x3); hold on; plot(n3,x3,'r'); hold off;title('Undersampling Condition : Fs = 1.5 f');xlabel('n');ylabel('x(n)');

O U T P U T :

P R O G R A M 2

I MPU LSE R ES P O N SE OF A G I V EN S Y STEM

A i m : To write the MATLAB code to find the impulse response of a given second-order system whose difference equation representation is given.

Assume a second-order system represented by the following difference equation:

(𝑛) = 𝑏0π‘₯(𝑛) + 𝑏1π‘₯(π‘›βˆ’ 1) + 𝑏20π‘₯(π‘›βˆ’ 2) + π‘Ž1𝑦(π‘›βˆ’ 1) + π‘Ž2𝑦(π‘›βˆ’ 2)MA TL AB C O D E :

% Experiment 2 : Impulse Response of a Given Second-Order System

clear all; close all; clc;

% Accept Input and Output signal Co-efficients:b = input('Enter the coefficients of x(n) in 1-D Matrix Form: '); a = input('Enter the coefficients of y(n) in 1-D Matrix Form: ');N = input('Enter the number of samples of impulse response desired: ');

% Calculate Impulse Response using IMPZ function:% [H,T] = IMPZ(B,A,N) computes N samples of the impulse response, using% coefficients B and A from difference equation representation.

[h,t] = impz(b,a,N);

%Plot and Display impulse response co-efficients:stem(t,h);title('Impulse Response Plot');ylabel('h(n)'); xlabel('n'); disp('Impulse Response Coefficients:'); disp(h);

O U T P U T :

Enter the coefficients of x(n) in 1-D Matrix Form: [1 0.2 -

1.5] Enter the coefficients of y(n) in 1-D Matrix Form: [1 3 -

0.12] Enter the number of samples of impulse response

desired: 5

Impulse Response Coefficients: 1.0000-2.80007.0200

-21.396065.0304

P R O G R A M 3

LI N E AR C O NV OL U TION OF TWO G I V EN SEQ U E NC ES

A i m : To write the MATLAB code to perform Linear Convolution upon two given discrete time signals.

MA TL AB C O D E :

1. Using β€œconv” function:

%% Linear Convolution using CONV commandclear all; close all; clc;

% Accept input signal sequencesx1 = input('Enter Input Sequence for Signal x1(n): ');x2 = input('Enter Input Sequence for Signal x2(n): ');

%Perform Linear Convolution using CONV commandy=conv(x1,x2);

%Plot Input and Convolved Signalssubplot(3,1,1); stem(x1);title('Input Signal x1(n)');xlabel('n'); ylabel('x1(n)');

subplot(3,1,2); stem(x2);title('Input Signal x2(n)');xlabel('n'); ylabel('x2(n)');

subplot(3,1,3); stem(y);title('Convolved Signal y(n) = x1(n)*x2(n)');xlabel('n'); ylabel('y(n)');

% Display the convolved Sequence in Command Windowdisp('Convolved sequence:'); disp(y);

2. U sing C onvolu t ion Sum f o r m ula :

%% Linear Convolution without using CONV commandclear all; close all; clc;

x1 = input('Enter Input Sequence for Signal x1(n): '); n1 = length(x1);

x2 = input('Enter Input Sequence for Signal x2(n): '); n2=length(x2);

N = n1+n2-1; %Length of Convolved SequenceT = 1:N; % Create Time Index

%Zero padding to make sequences of length Nx1=[x1 zeros(1,N-n1)];x2=[x2 zeros(1,N-n2)];

%Initializing Output sequence of zeros.y = zeros(1,N);

%Performing Linear Convolution:

for n = 1:N% y(n) = 0R;

for k = 1:ny(n)=y(n)+x1(k)*x2(n-k+1);

endend

% Plot Input and Output Sequences:subplot(3,1,1);stem(T,x1);title('Input Signal x1(n)'); xlabel('n'); ylabel('x1(n)');

subplot(3,1,2);stem(T,x2);title('Input Signal x2(n)');xlabel('n'); ylabel('x2(n)');

subplot(3,1,3);stem(T,y);title('Convolved Signal y(n) = x1(n)*x2(n)');xlabel('n'); ylabel('y(n)');

% Display the convolved Sequence in Command Windowdisp('Convolved sequence:'); disp(y);

O U T P U T :

Enter Input Sequence for Signal x1(n): [1 2 -1 3]Enter Input Sequence for Signal x2(n): [2 3 -2] Convolved sequence:

2 7 2 -1 11 -6

P R O G R A M 4

C I RCU L AR C O NV O L U TION OF TWO G I V EN SEQ U E NC ES

A i m : To write the MATLAB code to perform Circular Convolution upon two given discrete time signals.

M A TL AB C O D E :

1. U sing C onvolu t ion Sum F o r m ul a :

%% Circular Convolution using Formula

clear all; close all; clc;

x1 = input('Enter Input Sequence for Signal x1(n): '); n1 = length(x1);

x2 = input('Enter Input Sequence for Signal x2(n): '); n2=length(x2);

N = max(n1,n2); % Length of Convolved SequenceT = 1:N; % Create Time Index

%Zero padding to make sequences of length Nx1=[x1 zeros(1,N-n1)];x2=[x2 zeros(1,N-n2)];

%Initializing Output sequence of zeros.y = zeros(1,N);

%Performing Linear Convolution:for m=1:N

for n=1:Ni=m-n+1; %(m-n+1) since we're taking index

from 1 if(i<=0)

i=N+i;

endend

endy(m)=y(m)+x1(n)*x2(i); %Convolution Sum Formula

% Plot Input and Output Sequences:subplot(3,1,1);stem(T,x1);title('Input Signal x1(n)');xlabel('n'); ylabel('x1(n)');

subplot(3,1,2);stem(T,x2);

title('Input Signal x2(n)'); xlabel('n'); ylabel('x2(n)');

subplot(3,1,3);stem(T,y);title('Convolved Signal y(n) = x1(n)*x2(n)'); xlabel('n'); ylabel('y(n)');

% Display the convolved Sequence in Command Windowdisp('Convolved sequence:');disp(y);

2. Using β€œcconv” function.

%% Circular Convolution using CCONV commandclear all; close all; clc;

% Accept input signal sequencesx1 = input('Enter Input Sequence for Signal x1(n): '); x2 = input('Enter Input Sequence for Signal x2(n): ');n=max(length(x1),length(x2));

%Perform Linear Convolution using CONV commandy=cconv(x1,x2,n);

%Plot Input and Convolved Signalssubplot(3,1,1); stem(x1);title('Input Signal x1(n)');xlabel('n'); ylabel('x1(n)');

subplot(3,1,2); stem(x2);title('Input Signal x2(n)');xlabel('n'); ylabel('x2(n)');

subplot(3,1,3); stem(y);title('Convolved Signal y(n) = x1(n)*x2(n)');xlabel('n'); ylabel('y(n)');

% Display the convolved Sequence in Command Windowdisp('Convolved sequence:'); disp(y);

O U T P U T :

Enter Input Sequence for Signal x1(n): [2 1 2 1]

Enter Input Sequence for Signal x2(n): [1 2 3 4] Convolved sequence:

14 16 14 16

P R O G R A M 5

C O MPU T A TION OF N - P OI N T D FT

A i m : Computation of N point DFT of a given sequence and to plot magnitude and phase spectrum.

MA TL AB C O D E:

%Experiment 5 : N-Point DFTclear all; close all; clc;

%Accept Input sequence from userxn = input ('Enter the sequence x(n) : ');xn=xn';N = length(xn);Xk = zeros(N, 1); %Initialize zero matrix for DFT sequence

%Calculate DFT using formulan = 0:N-1;for k = 0:N-1

Xk(k+1) = exp(-j*2*pi*k*n/N)*xn;end

%Display DFT Sequence disp('DSP Sequence : X(k) :'); disp(Xk);

%Plot Signalsn = 0:N-1; %Time base

% Input Sequence subplot (2,2,[1:2]); stem(n, xn);title('Input Sequence x(n)');xlabel('n');ylabel('x(n)');

% Output Magnitude Plotsubplot (2,2,3);stem(n, abs(Xk));grid on;title('Magnitude Plot of DFT : |X(k)|'); xlabel('n');ylabel('|X(k)|');

% Output Phase Plotsubplot(2,2,4);stem(n, angle(Xk)'); grid on;title('Phase Plot of DFT : angle(X(k))');xlabel('n');ylabel('Angle');

O U T P U T :

Enter the sequence x(n) : [0 1 2 3]

DSP Sequence : X(k) :

6.0000

-2.0000 + 2.0000i

-2.0000 - 0.0000i

-2.0000 - 2.0000i

P R O G R A M 6

LI N E AR C O NV OL U TION U SI NG D F T AND I D F T

A i m : To calculate the Linear Convolution of two sequences using DFT and IDFT

MA TL AB C O DE

% Experiment 6 : Linear Convolution using Fourier Transformsclear all; close all; clc;

%Accept input sequencesx1 = input('Enter Input Sequence for Signal x1(n): ');n1 = length(x1);x2 = input('Enter Input Sequence for Signal x2(n): '); n2=length(x2);

N = n1+n2-1; % Length of convolved sequenceT = 1:N;

%Calculate N-point DFT and IDFT.y1=fft(x1,N); % N-point DFT of x1y2=fft(x2,N); % N-point DFT of x2y3=y1.*y2; % Multiplication in time domainy=ifft(y3,N); % N-point IDFT of y to recover result

% Plot Input and Output Sequences:subplot(3,1,1);stem(T,x1);title('Input Signal x1(n)'); xlabel('n'); ylabel('x1(n)');

subplot(3,1,2);stem(T,x2);title('Input Signal x2(n)'); xlabel('n'); ylabel('x2(n)');

subplot(3,1,3);stem(T,y);title('Convolved Signal y(n) = x1(n)*x2(n)');xlabel('n'); ylabel('y(n)');

% Display the convolved Sequence in Command Windowdisp('Convolved sequence:');disp(y);

O U T P UT

Enter Input Sequence for Signal x1(n): [1 2] Enter Input Sequence for Signal x2(n): [1 2 3] Convolved sequence:1 4 7 6

P R O G R A M 7

C I RCU L AR C O NV O L U TION U SI NG D F T A ND I D F T

A i m : To calculate the Circular Convolution of two sequences using DFT and IDFT

MA TL AB C O DE

% Experiment 7 - Circular Convolution using Fourier Transformsclear all; close all; clc;

%Accept input sequencesx1 = input('Enter Input Sequence for Signal x1(n): ');n1 = length(x1);x2 = input('Enter Input Sequence for Signal x2(n): ');n2=length(x2);

N=max(n1,n2); % Length of convolved sequenceT = 1:N;

x1=[x1 zeros(1,N-n1)];x2=[x2 zeros(1,N-n2)];

%Calculate N-point DFT and IDFT.y1=fft(x1,N); % N-point DFT of x1y2=fft(x2,N); % N-point DFT of x2y3=y1.*y2; % Multiplication in time domainy=ifft(y3,N); % N-point IDFT of y to recover result

% Plot Input and Output Sequences:subplot(3,1,1);stem(T,x1);title('Input Signal x1(n)');xlabel('n'); ylabel('x1(n)');

subplot(3,1,2);stem(T,x2);title('Input Signal x2(n)');xlabel('n'); ylabel('x2(n)');

subplot(3,1,3);stem(T,y);title('Convolved Signal y(n) = x1(n)*x2(n)');xlabel('n'); ylabel('y(n)'); grid on;

% Display the convolved Sequence in Command Windowdisp('Convolved sequence:');disp(y);

O U T P UT

Enter Input Sequence for Signal x1(n): [1 2 3 4]Enter Input Sequence for Signal x2(n): [4 3 2] Convolved sequence:

22 19 20 29

P R O G R A M 8

D ESI G N AND I M P L E M E N T A TION O F F IR F ILTER

A i m : To design and implement a FIR Filter for the given specifications.

P r obl e m : Using MATLAB design an FIR filter to meet the following specifications choosing Hamming window:

β€’ Window length, N = 27β€’ Stop band attenuation = 50dBβ€’ Cut-off frequency = 100 Hzβ€’ Sampling frequency = 1000 Hz

MA TL AB C O DE

% Experiment 8 : Designing a FIR Filter (Hamming Window)

close all; clear all; clc;

% Accept Filter Parameters from UserN = input('Enter the window length N : ');fc = input('Enter the cut-off frequency fc (Hz) : ');Fs = input('Enter the sampling frequency Fs (Hz) : ');

Wc = 2*fc/ Fs; %Nyquist FrequencyWh = hamming(N); %Create a N-point symmetric Hamming window

% Generate a FIR filter based on Hamming Window createdb = fir1(N-1, Wc ,Wh);

% Calculate Frequency Response of Filter designed.% h - Frequency Response values w = Frequencies[h,w] = freqz(b,1,256);mag = 20*log10(abs(h)); %Magnitude of Response

% Display Valuesdisp('Hamming Window Co-efficients : ');disp(Wh);disp('Unit Sample Response of FIR Filter h(n) : ');disp(b);

% Plot Frequency response of Butterworth Filter.freqz(b);title('Hamming Filter Frequency Response');

O U T P UT

Enter the window length N : 27Enter the cut-off frequency fc (Hz) : 100 Enter the sampling frequency Fs (Hz) : 1000

0.0019 0.0023 0.0022

Columns 8 through 14

-0.0000 -0.0058 -0.0142 -0.0209

-0.0185 0.0000 0.0374 0.0890 0.1429 0.1840 0.1994

Columns 15 through 210.1840 0.1429 0.0890 0.0374 0.0000 -0.0185 -0.0209

Columns 22 through 27-0.0142 -0.0058 -0.0000 0.0022 0.0023 0.0019

Hamming Window Co-efficients : 0.08000.09340.13270.19570.27870.37690.48460.59540.70310.8013

0.88430.94730.98661.00000.98660.94730.88430.80130.70310.59540.48460.37690.27870.19570.13270.09340.0800

Unit Sample Response of FIR Filter h(n) : Columns 1 through 7

P R O G R A M 9

D ESI G N AND I M P L E M E N T A TION OF IIR F ILTER

A i m : To design and implement a IIR Filter for the given specifications.

P R OBLEM 1: B U TTE R WO R TH F ILTE R :

Using MATLAB design an IIR filter with passband edge frequency 1500Hz and stop band edge at 2000Hz for a sampling frequency of 8000Hz, variation of gain within pass band 1db and stopband attenuation of 15 db. Use Butterworth prototype design and Bilinear Transformation.

MA TL AB C O D E :

% Experiment 9 : Design of IIR Filter (Butterworth Filter)clear all; close all; clc;

% Accept Input Parameters from userRp = input('Enter Passband Attenuation in dB : '); Rs = input('Enter Stopband Attenuation in dB : '); fp = input('Enter Passband Frequency in Hz : '); fs = input('Enter Stopband Frequency in Hz : '); Fs = input('Enter Sampling Frequency in Hz : ');

% Calculate Sampled Frequency valuesWp=2* fp / Fs ;Ws=2* fs / Fs ;

% Calculate Butterworth filter order and cutoff frequency:% N = Minimum order of Filter Wn = Cutoff Frequencies[N,Wn] = buttord(Wp,Ws,Rp,Rs);

% Butterworth Filter Design (z = zeros p = poles)[z,p] = butter(N,Wn);

% Display Filter parameters : disp('Order of Butterworth Filter : '); disp(N);disp('Butterworth Window Cutoff Frequency : ');disp(Wn);

% Plot Frequency Response of Filterfreqz(z,p);title('Butterworth frequency response');

O U T P U T :

Enter Passband Attenuation in dB : 1 Enter Stopband Attenuation in dB : 15 Enter Passband Frequency in Hz : 1500 Enter Stopband Frequency in Hz : 2000 Enter Sampling Frequency in Hz : 8000

Order of Butterworth Filter :6

Butterworth Window Co-efficient : 0.4104

οΏ½

οΏ½

P R OBLEM 2: C HEB Y SHEV F ILTE R :

Using MATLAB design an IIR filter with passband edge frequency 1500Hz and stop band edge at 2000Hz for a sampling frequency of 8000Hz, variation of gain within pass band 1 db and stopband attenuation of 15 db. Use Chebyshev prototype design and Bilinear Transformation.

D ESI G N :W1 = (2*pi* F1 )/ Fs = 2*pi*100)/4000 = 0.05Ξ  rad W2 = (2*pi* F2 )/ Fs = 2*pi*500)/4000 =0.25Ξ  rad

P r e w a r p: T=1secΞ©1 = 2/T tan (w1/2) = 0.157 rad/sec Ξ©2 = 2/T tan (w2/2) = 0.828 rad/sec

O r d er:

𝐴𝑝έ = 10βˆ’ 10 βˆ’ 1Ξ­ = 0.765A= 10-As/20 , A = 1020/20 , A=10 g= (A2 - 1) / Ξ­ , g = 13.01Ξ©r= Ξ©2 / Ξ©1 Ξ©r=0.828/0.157 = 5.27 rad\sec

n = log10 𝐠+οΏ½ οΏ½(π πŸβˆ’ 𝟏) οΏ½ / log10{Ξ©r + √(πœ΄πŸβ€“ 𝟏) }n= 1.388Therefore n= 2. Cut -

o f f F r e qu e n c y :

Ξ©c = Ξ©p = Ξ©1 = 0.157 rad\sec

N o r m a li z e d Tra ns f e r F u n c tion:

H(s)=[bo / 1+ Ξ­2 ] / [ s2+b1s+b0]= 0.505/[ s2+0.8s+0.036]

De no r m a li z e d Tra ns f e r Fun c tion:

H(s)= Hn(s) |s-s/Ξ©c H(s)= Hn(s) |s-s/0.157

H(s) = 0.0125 / [s2+0.125s+0.057]

A pp l y B L T : H(Z) = H(s)|s=(2/T)[(1-z-1)/(1+z-1)]

H(Z) = 0.0125+0.025Z-1 + 0.0125 Z-

2 4.2769-7.96Z-1 + 3.76Z-2

H(Z) = 0.0029+0.0052Z-1 + 0.0029 Z-

2 1-1.86Z-1 + 0.88Z-2

MA TL AB C O D E :

% Experiment 9B : Design of IIR Filter (Chebyshev Filter)clear all; close all; clc;

% Accept Input Parameters from userRp = input('Enter Passband Attenuation in dB : '); Rs = input('Enter Stopband Attenuation in dB : '); fp = input('Enter Passband Frequency in Hz : '); fs = input('Enter Stopband Frequency in Hz : '); Fs = input('Enter Sampling Frequency in Hz : ');

% Calculate Sampled Frequency valuesWp=2* fp / Fs ;Ws=2* fs / Fs ;

% Calculate Chebyshev filter order and cutoff Frequency:% N = Minimum order of Filter Wn = Cutoff Frequencies[N,Wn]=cheb1ord(Wp,Ws,Rp,Rs);

% Chebyshev Filter Design (z = zeros p = poles)[z,p]=cheby1(N,Rp,Wn);

% Display Filter parameters : disp('Order of Chebyshev Filter : '); disp(N);disp('Chebyshev Window Cutoff Frequency : ');disp(Wn);

% Plot Frequency Response of Filter :freqz(z,p);title('Chebyshev Frequency response');

O U T P U T :

Enter Passband Attenuation in dB : 1

Enter Stopband Attenuation in dB : 15 Enter Passband Frequency in Hz : 1500 Enter Stopband Frequency in Hz : 2000 Enter Sampling Frequency in Hz : 8000

Order of Chebyshev Filter : 4

Chebyshev Window Cutoff Frequency : 0.3750