DSP Experiments: Experiment 1

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 20

DSP EXPERIMENTS

SUBMITTED BY: Sameer Raj


1802462
ECE ( Z 1)

EXPERIMENT 1
OBJECTIVE:
To develop elementary signal function modules (m-files) for unit sample, unit step, unit
ramp and exponential sequences.
PROGRAM:
clc;
clear all;
% Unit Impulse signal
t= -5:1:5; % t variable stores the values of range for example range from -5 to 5 with a
step size 1.
y=[zeros(1,5),ones(1,1),zeros(1,5)]; % y variable stores the values of output
subplot(2,2,1); % subplot command dividing figure window into four positions
stem(t,y); % plot of values of t and y in discrete form
ylabel('Values of y'); xlabel('Values of t'); % labeling of y and x axis
title('Unit Impulse Signal'); % assigning title to the figure

clc;
% Unit Step signal
t1= 0:1:5;
y1=ones(1,6);
subplot(2,2,2);
stem(t1,y1);
ylabel('Values of y1'); xlabel('Values of t1');
title('Unit Step Signal');

clc;
% Unit Ramp signal
n1=input(‘Enter the value for the end of ramp sequence:’;); % declaration of variable n1
for entering input at the time of execution
t2=0:n1;
y2=t2;
subplot(2,2,3);
stem(t2,y2);
ylabel('Values of y2'); xlabel('Values of t2');
title('Ramp sequence');

clc;
% Exponential signal
n2=input('Enter the length of exponential sequence:'); % declaration of variable n2 for
entering input at the time of execution
t3=0:n2;
a=input('Enter the amplitude:'); % declaration of variable a for entering the value of
amplitude of exponential signal
y3=exp(a*t3); % calculate the exponential value of (3a’ multiplied by ’t’)
subplot(2,2,4);
stem(t3,y3);
ylabel('Values of y3'); xlabel('Values of t3');
title('Exponential Sequence');

disp('Unit Impulse Signal');y % disp command for displaying the values stored in output
variable y
disp('Unit Step Signal');y1
disp('Ramp Signal');y2
disp('Exponential Sequence');y3
OUTPUT:
Enter the value for the end of ramp sequence: 7
Enter the length of exponential sequence: 10
Enter the amplitude: 2

EXPERIMENT 2
OBJECTIVE:
Write a program in MATLAB to generate standard sequences.
PROGRAM:
clc;
clear all;
%unit step sequence
n=0:1:9; % n variable stores the values of range for example range from 0 to 9 with a
step size 1
un = ones(1,10); %use of ones command for creating an array of ones
subplot(2,1,1); % subplot command dividing figure window into two positions
stem(n,un,'k'); % plot of values of n and un in discrete form in black color using k
xlabel('values of n');
ylabel('values of un');
title('STEP SEQUENCE');
grid on; % grid lines to the current axis (as on graphing paper)

clc;
%ramp sequence
n1=input('Enter the value for the end of ramp sequence:');
t1=0:n1;
a=input('Enter the amplitude:');
y1=a*t1;
subplot(2,2,3);
stem(t1,y1);
title('RAMP SEQUENCE');
xlabel('values of y1');
ylabel('values of t1');
grid on;
clc;
% Unit Ramp signal
n2=input('Enter the value for the end sequence:'); % declaration of variable n1 for
entering input at the time of execution
t2=0:n2;
y2=t2;
subplot(2,2,3);
stem(t2,y2);
ylabel('values of y2'); xlabel('values of t2');
title('Unit Ramp sequence');
grid on;
OUTPUT:
Enter the value for the end of ramp sequence: 6
Enter the amplitude: 2
Enter the value for the end of sequence: 3
EXPERIMENT 3
OBJECTIVE:
To develop program for linear convolution.
PROGRAM:
clc;
clear all;
x=input('Enter the first sequence:'); % declaration of variable x for entering input
sequence at the time of execution
h=input('Enter the second sequence:'); % declaration of variable h for entering input
sequence at the time of execution
y=conv(x,h); %declaration of output variable y storing the value of convolution of x and h
subplot(1,3,1); % subplot command dividing figure window into three positions
stem(x); % plot of values of x in discrete form
ylabel('values of x');
xlabel('random values');
title('sequence x');
subplot(1,3,2);
stem(h);
ylabel('values of h');
xlabel('random values');
title('sequence h');
subplot(1,3,3);
stem(y);
ylabel('values of y');
xlabel('random values');
title('Convolution of x and h');
disp('The resultant signal is:');
OUTPUT:
Enter the first sequence: [4 7 2 8]
Enter the second sequence: [1 5 3]
The resultant signal is: [4 27 49 39 46 24]
Experiment 4
OBJECTIVE:
To develop program modules based on operation on sequences like signal shifting,
signal folding, signal addition and signal multiplication.
PROGRAM:
clc;
clear all;
%Signal Shifting
t=-2:1:2; % t variable stores the values of range for example range from -2 to 2 with a
step size 1.
x=[1 2 3 4 5]; % declare the amplitude of sequence to be represented
subplot(2,1,1); % subplot command dividing figure window into two positions
stem(t,x); % plot of values of t and y in discrete form
title('Input signal');
ylabel('Values of x');
xlabel('Values of t');
n=input('Enter value of n'); % declaration of variable n for storing value by which signal
should be shifted
t1=t+n; % shifting the original signal by n
subplot(2,1,2);
stem(t1,x);
title('Shifted signal');
ylabel('Values of x');
xlabel('Values of t1');
OUTPUT:
Enter value of n 4
EXPERIMENT 5
OBJECTIVE:
To develop program for circular convolution.
PROGRAM:
clc;
clear all;
x=input('Enter the first sequence:'); % declaration of variable x for entering input
sequence at the time of execution
h=input('Enter the second sequence:'); % declaration of variable h for entering input
sequence at the time of execution
y=cconv(x,h); %declaration of output variable y storing the value of convolution of x and
h
subplot(1,3,1); % subplot command dividing figure window into three positions
stem(x); % plot of values of x in discrete form
ylabel('Values of x');
xlabel('Random values');
title('Sequence x');
subplot(1,3,2);
stem(h);
ylabel('Values of h');
xlabel('Random values');
title('Sequence h');
subplot(1,3,3);
stem(y);
ylabel('Values of y');
xlabel('Random values');
title('Convolution of x and h');
disp('The resultant signal is:');
OUTPUT:
Enter the first sequence [1,4,5,6]
Enter the second sequence [4,6,7,8]
The resultant signal is [4,22,50,90,102,80,48]
Experiment 6
OBJECTIVE:
To develop program for finding the magnitude and phase response of LTI system
described by system function H(z).
PROGRAM:
clc;
clear all;
x=input ('Enter the coefficients of x(n) ');
y=input (' Enter the coefficients of y(n)');
[h,f]=freqz(x,y); % to find frequency response
subplot(2,1,1);
plot(f ./pi, abs(h)); % plot magnitude response
ylabel('magnitude');
xlabel('numerated frequency');
subplot(2,1,2);
plot(f./pi, angle(h)); % plot phase response
xlabel('normalized frequency');
ylabel('phase in radians');
OUTPUT:
Enter the coefficients of x(n) 2
Enter the coefficients of y(n) 3
EXPERIMENT-7
OBJECTIVE:
To develop program for finding the response of the LTI system by difference equation.
PROGRAM:
clc;
clear all;
n=40;
num=[0.9 -0.45 0.35 0.002]; % numerator coefficients
den=[1 0.71 -0.46 -0.62]; % denominator coefficients
y=impz(num,den,n); % use if impz command for finding impulse response
stem(y);
xlabel('time index n');
ylabel('amplitude');
title('impulse response');
grid;
OUTPUT:
EXPERIMENT-8
OBJECTIVE:
To develop program for computing inverse z-transform.
PROGRAM:
clc;
clear all;
num=[18]; % numerator coefficients
den=[18 3 -4 -1]; % denominator coefficients
[r,p,k]=residuez(num,den) % use of residuez command for finding poles and zeros
[r,p,k]=zplane(num,den) % use of zplane command for show the poles and zeros on
graph
OUTPUT:
EXPERIMENT-9
OBJECTIVE:
To develop program for computing discrete Fourier Transform (DFT) and inverse
discrete Fourier Transfer (IDFT).
PROGRAM:
% DFT
clc;
clear all;
x=input('enter the sequence');
n=input('enter the length of Fourier Transform');
X=fft(x,n); % fft command for finding fourier transform
subplot(2,1,1);
stem(X);
ylabel('values of X');
xlabel('real axis');
title('discrete fourier transform');
%IDFT
X=[2,1,4,5,6,-1,7,3];
N=8;
x=ifft(X,N);
subplot(2,1,2);
stem(x);
ylabel('values of x');
xlabel('real axis');
title('Inverse discrete fourier transform');
OUTPUT:

EXPERIMENT-10
OBJECTIVE:
To develop program for cascade realization of FIR and IIR filters.
PROGRAM:
1. Cascade realization of FIR filter
clc;
clear all;
% Conversion of a rational transfer function to its factored form
%MODIFIED to make the numerator and denominator coefficient vectors the same
length for
calling tf2zp
num = input('Numerator coefficient vector = ');
den = input('Denominator coefficient vector = ');
[b,a] = eqtflength(num,den); % make lengths equal
[z,p,k] = tf2zp(b,a); % for Transfer function to zero-pole conversion
sos = zp2sos(z,p,k) % for Zero-pole-gain to second-order sections model conversion
2. Cascade realization of IIR filter
num = input('Numerator coefficient vector = ');
den = input('Denominator coefficient vector = ');
[r1,p1,k1] = residuez(num,den); % for finding the residues, poles and direct terms of the
partialfraction expansion
[r2,p2,k2] = residuez(num,den);
disp('Cascade form I')
disp('Residues are');disp(r1);
disp('Poles are at');disp(p1);
disp('Constant value');disp(k1);
disp('Cascade form II')
disp('Residues are');disp(r2);
disp('Poles are at');disp(p2);
disp('Constant value');disp(k2);
OUTPUT:
EXPERIMENT-11
OBJECTIVE:
To develop program for designing IIR filter
PROGRAM:
clc;
clear all;
wp=input('Enter the pass band frequency'); % pass band frequency
ws=input('Enter the stop band frequency'); % stop band frequency
rp=input(' Enter the pass band ripple'); % pass band ripples
rs=input('Enter the stop band ripple'); % stop band ripples
fs=input(' Enter the sampling frequency'); % sampling frequency
w1=2*wp/fs;
w2=2*ws/fs;
[n,wn]=buttord(w1,w2,rp,rs); % butterworth filter coefficients
[b,a]=butter(n,wn);
w=0:0.01:pi;
[h,om]=freqz(b,a,w);
m=20*log10(abs(h)); % magnitude values
an=angle(h); % phase values
subplot(2,1,1);
plot(om/pi,m);
ylabel('normalized frequency');
xlabel('magnitude values');
title('magnitude response of IIR filter');
subplot(2,1,2);
plot(om/pi,an);
ylabel('normalized frequency');
xlabel('phase values');
title('phase response of IIR filter');
OUTPUT:
Enter the pass band frequency 0.02
Enter the stop band frequency 0.03
Enter the pass band ripple 0.05
Enter the stop band ripple 0.08
Enter the sampling frequency 2000

EXPERIMENT-12
OBJECTIVE:
To write a MATLAB programs for pole-zero plot, amplitude, phase response and impulse
response from the given transfer function of a discrete-time causal system.
PROGRAM:
%pole- zero plot
clc;
clear all;
num=[18];
den=[18 3 -4 -1];
[r,p,k]=residuez(num,den)
figure(1)
zplane(r,p,k) % for plotting the poles and zeros
%impulse response
y=impz(num,den);
figure(2)
stem(y);
xlabel('time index n');
ylabel('amplitude');
title('impulse response');
%magnitude and phase response
[h,f]=freqz(num,den); % to find frequency response
figure (3)
subplot (2,1,1);
plot (f ./pi, abs(h)); % plot magnitude response
ylabel('magnitude');
xlabel('numerated frequency');
title ('magnitude response');
subplot (2,1,2);
plot(f./pi, angle(h)); % plot phase response
xlabel('normalized frequency');
ylabel('phase in radians');
title ('phase response');
OUTPUT:
r=
0.3600
0.2400
0.4000
p=
0.5000
-0.3333
-0.3333
k=
[]

You might also like