You are on page 1of 26

USIT and IGIT

Guru Gobind Singh Indraprastha University


Kashmere Gate, Delhi 110006.



DEPARTMENT OF ELECTRONICS & COMMUNICATION
ENGINEERING





Digital Signal Processing Lab
Sub Code: ECW
Sem: II






By
Mr. Pramod.S



Table of Contents






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 plot
agnitude and phase spectrum

6. Linear convolution of two sequences using DFT and
IDFT

7. Circular convolution of two given sequences using DFT
and IDFT

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

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



PROGRAM 1

VERIFICATION OF SAMPLING THEOREM

Aim: 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.




MATLAB CODE:

% Experiment 1 : Sampling Theorem Verification

clear all; close all; clc;

% Signal Parameters
f = 1000; % Signal Frequency = 1kHz
T = 1/f; % Signal Period
t = 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*t
subplot(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 scale
x1 = cos(2*pi*f*n1); % Generating sampled signal
subplot(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)');



OUTPUT:





PROGRAM 2

IMPULSE RESPONSE OF A GIVEN SYSTEM

Aim: 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)

MATLAB CODE:

% 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);



OUTPUT:

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.8000
7.0200
-21.3960
65.0304









PROGRAM 3

LINEAR CONVOLUTION OF TWO GIVEN SEQUENCES

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


MATLAB CODE:

1. Using conv function:

%% Linear Convolution using CONV command
clear all; close all; clc;

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

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

%Plot Input and Convolved Signals
subplot(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 Window
disp('Convolved sequence:');
disp(y);


2. Using Convolution Sum formula:

%% Linear Convolution without using CONV command
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 = n1+n2-1; %Length of Convolved Sequence
T = 1:N; % Create Time Index



%Zero padding to make sequences of length N
x1=[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:n
y(n)=y(n)+x1(k)*x2(n-k+1);

end
end

% 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 Window
disp('Convolved sequence:');
disp(y);







OUTPUT:

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
























PROGRAM 4

CIRCULAR CONVOLUTION OF TWO GIVEN SEQUENCES

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

MATLAB CODE:

1. Using Convolution Sum Formula:

%% 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 Sequence
T = 1:N; % Create Time Index

%Zero padding to make sequences of length N
x1=[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:N
i=m-n+1; %(m-n+1) since we're taking index
from 1
if(i<=0)
i=N+i;



end


end


end
y(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 Window
disp('Convolved sequence:');
disp(y);

2. Using cconv function.

%% Circular Convolution using CCONV command
clear all; close all; clc;

% Accept input signal sequences
x1 = 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 command
y=cconv(x1,x2,n);

%Plot Input and Convolved Signals
subplot(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 Window
disp('Convolved sequence:');
disp(y);

OUTPUT:

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




PROGRAM 5

COMPUTATION OF N- POINT DFT

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


MATLAB CODE:

%Experiment 5 : N-Point DFT
clear all; close all; clc;

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

%Calculate DFT using formula
n = 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 Signals
n = 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 Plot
subplot (2,2,3);
stem(n, abs(Xk));
grid on;
title('Magnitude Plot of DFT : |X(k)|');
xlabel('n');ylabel('|X(k)|');

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


OUTPUT:

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






PROGRAM 6

LINEAR CONVOLUTION USING DFT AND IDFT

Aim: To calculate the Linear Convolution of two sequences using DFT and IDFT


MATLAB CODE

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

%Accept input sequences
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 sequence
T = 1:N;





%Calculate N-point DFT and IDFT.
y1=fft(x1,N); % N-point DFT of x1
y2=fft(x2,N); % N-point DFT of x2
y3=y1.*y2; % Multiplication in time domain
y=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 Window
disp('Convolved sequence:');
disp(y);


OUTPUT

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







PROGRAM 7

CIRCULAR CONVOLUTION USING DFT AND IDFT

Aim: To calculate the Circular Convolution of two sequences using DFT and IDFT


MATLAB CODE

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

%Accept input sequences
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 sequence
T = 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 x1
y2=fft(x2,N); % N-point DFT of x2
y3=y1.*y2; % Multiplication in time domain
y=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 Window
disp('Convolved sequence:');
disp(y);

OUTPUT

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







PROGRAM 8

DESIGN AND IMPLEMENTATION OF FIR FILTER

Aim: To design and implement a FIR Filter for the given specifications.



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

MATLAB CODE

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

close all; clear all; clc;

% Accept Filter Parameters from User
N = 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 Frequency
Wh = hamming(N); %Create a N-point symmetric Hamming
window

% Generate a FIR filter based on Hamming Window created
b = 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 Values
disp('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');


OUTPUT

Enter the window length N : 27
Enter 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 21
0.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.0800
0.0934
0.1327
0.1957
0.2787
0.3769
0.4846
0.5954
0.7031
0.8013


0.8843
0.9473
0.9866
1.0000
0.9866
0.9473
0.8843
0.8013
0.7031
0.5954
0.4846
0.3769
0.2787
0.1957
0.1327
0.0934
0.0800


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








PROGRAM 9

DESIGN AND IMPLEMENTATION OF IIR FILTER

Aim: To design and implement a IIR Filter for the given specifications.





PROBLEM 1: BUTTERWORTH FILTER:

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.

MATLAB CODE :

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

% Accept Input Parameters from user
Rp = 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 values
Wp=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 Filter
freqz(z,p);
title('Butterworth frequency response');







OUTPUT:

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
























PROBLEM 2: CHEBYSHEV FILTER:

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.





DESIGN:
W1 = (2*pi* F1 )/ Fs = 2*pi*100)/4000 = 0.05 rad
W2 = (2*pi* F2 )/ Fs = 2*pi*500)/4000 =0.25 rad


Prewarp:
T=1sec
1 = 2/T tan (w1/2) = 0.157 rad/sec
2 = 2/T tan (w2/2) = 0.828 rad/sec


Order:



= 10

10
1
= 0.765
A= 10
-As/20
, A = 10
20/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.388
Therefore n= 2. Cut-
off Frequency:
c = p = 1 = 0.157 rad\sec
Normalized Transfer Function:
H(s)=[bo / 1+
2
] / [ s
2
+b1s+b0]
= 0.505/[ s
2
+0.8s+0.036]


Denormalized Transfer Function:



H(s)= Hn(s) |s-s/c H(s)= Hn(s) |s-s/0.157
H(s) = 0.0125 / [s
2
+0.125s+0.057]


Apply BLT:
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







MATLAB CODE:

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

% Accept Input Parameters from user
Rp = 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 values
Wp=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');

OUTPUT:

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

You might also like