EE321
DIGITAL SIGNAL PROCESSING LAB
REPORT
Experiment-1
Familiarization with Kits and MATLAB
NAME : Kanishk Giri
ROLL NO: 2001EE24
Aim 1: To generate basic signals like unit impulse, unit step, unit
ramp signal and exponential signals.
Q1. WAP to generate the following signals using MATLAB:
a) Unit step Sequence
MATLAB code:
N = (-2 : 1 : 2);
step = [];
for n = N
if(n >= 0)
step(end+1) = 1;
else
step(end+1) = 0;
end
end
stem(N,step);
xlim([-3 3]);
ylim([-2 2]);
grid ON;
xlabel('Time');
ylabel('Amplitude');
title('Question 1(a) : Unit Step Response');
Output:
b) Ramp sequence
MATLAB code:
N = (-2 : 1 : 2);
ramp = []
for n = N
if(n < 0)
ramp(end+1) = 0
else
ramp(end+1) = n
end
end
stem(N,ramp);
xlim([-3 3]);
ylim([-2 3]);
grid ON;
xlabel('Time');
ylabel('Amplitude');
title('Question 1(b) : Unit ramp sequence');
Output:
c) Exponential sequence
MATLAB code:
t = (-50 : 0.1 : 50);
x = 0.4;
exponential = x.^t;
plot(t,exponential);
grid ON;
xlabel('Time');
ylabel('Amplitude');
title('Question 1(c) : Exponential Sequence');
Output:
Functions used in the following question :
(i) Delta function:
MATLAB code:
function output = impulse_signal(input)
if(input == 0)
output = 1;
else
output = 0;
end
end
(ii) Unit Step function:
MATLAB code:
function output = step_signal(input)
if(input >= 0)
output = 1;
else
output = 0;
end
end
d) Generate and plot each of the following sequences over the indicated
interval.
MATLAB code:
N = (-5 : 1 : 5);
x = []
for n = N
x(end+1) = (2*impulse_signal(n+1))-(impulse_signal(n-4));
end
stem(N,x);
grid ON;
xlabel('Number of samples (n)');
ylabel('Amplitude');
xlim([-6 6]);
ylim([-3 3]);
title('Question 1(d)(a)');
Output:
MATLAB code:
N = (0 : 1 : 20);
X = [];
for n = N
X(end+1) = n*(step_signal(n)-step_signal(n-10)) + 10*exp(-
0.3*(n-10))*(step_signal(n-10)-step_signal(n-20));
end
stem(N,X);
grid ON;
xlabel('Number of samples (n)');
ylabel('Amplitude');
xlim([0 20]);
ylim([0 10]);
title('Question 1(d)(b)');
Output:
MATLAB code:
N = (0 : 1 : 50);
X = [];
for n = N
X(end+1) = cos(0.04*pi*n) + 0.2*rand(size(n));
end
stem(N,X);
grid ON;
xlabel('Number of samples (n)');
ylabel('Amplitude');
xlim([0 50]);
ylim([-2 2]);
title('Question 1(d)(c)');
Output:
MATLAB code:
N = (-10: 1 : 9);
A = [5,4,3,2,1];
B = ones(1,4);
z = A'*B;
X = reshape(z,1,20);
stem(N,X);
grid ON;
ylim([0 6]);
xlabel('Number of samples (n)');
ylabel('Amplitude');
title('Question 1 (d)(d)');
Output:
e) Generate the complex-valued signal
MATLAB code:
N = (-10 : 1 : 10);
X = [];
for n = N
X(end+1) = exp((-0.1+j*0.3)*n);
end
stem(N,abs(X));
grid ON;
xlabel('Number of samples (n)');
ylabel('Amplitude');
title('Question 1 (e)');
stem(N,angle(X));
grid ON;
xlabel('Number of samples (n)');
ylabel('Angle (in radian)');
title('Question 1 (e)');
Output:
f) Let x(n) = u(n) − u(n − 10). Decompose x(n) into
even and odd components.
MATLAB code:
N = (-20 : 1 : 20);
Xeven = [];
Xodd = [];
for n = N
Xeven(end+1) = 0.5*(step_signal(n)-step_signal(n-10)
+step_signal(-n) - step_signal(-n-10));
Xodd(end+1) = 0.5*(step_signal(n)-step_signal(n-10)
-(step_signal(-n) - step_signal(-n-10)));
end
stem(N,Xeven);
grid ON;
xlabel('Number of samples (n)');
ylabel('Amplitude of Xeven');
title('Question 1 (f)');
stem(N,Xodd);
grid ON;
xlabel('Number of samples (n)');
ylabel('Amplitude of Xodd');
title('Question 1 (f)');
Output:
Q2. Given the following difference equation
y(n) − y(n − 1) + 0.9y(n − 2) = x(n); ∀n
a. Calculate and plot the impulse response h(n) at n = −20, . . . , 100.
MATLAB code:
N = (-20 : 1 : 100);
a = [1 -1 0.9];
b = [1]
X = [];
for n = N
X(end+1) = impulse_signal(n);
end
Y = filter(b,a,X);
stem(N,Y);
grid ON;
xlabel('Number of samples (n)');
ylabel('Amplitude');
title('Q2 (a)');
Output:
b. Calculate and plot the unit step response s(n) at n =
−20, . . . , 100.
MATLAB code:
N = (-20 : 1 : 100);
a = [1 -1 0.9];
b = [1]
X = [];
for n = N
X(end+1) = step_signal(n);
end
Y = filter(b,a,X);
stem(N,Y);
grid ON;
xlabel('Number of samples (n)');
ylabel('Amplitude');
title('Q2 (b)');
Output:
(c) Is the system specified by h(n) stable?
Yes
—-------------------------------------------------------------
Aim 2: To find the frequency response of a given
system given in (Transfer Function/ Differential
equation form).
Q3. Find and plot the frequency response (amplitude
and phase) of a given system
y[n]-0.25y[n-1] +0.45y[n-2] =1.55x[n]+1.95x[n-1] + 2.15x[n]
MATLAB code:
N = (-20 : 1 : 100);
a = [1 -0.25 0.45];
b = [1.55 1.95 2.15];
[h,w] = freqz(b,a,'whole',360);
stem(abs(h));
grid ON;
ylabel('Amplitude response');
stem(angle(h));
grid ON;
ylabel('Anlge response');
Output:
—-------------------------------------------------------------
Aim 3: To write a MATLAB program to evaluate the
impulse response of the system
y(n) = x(n)+0.5x(n-1)+0.85x(n-2)+y(n-1)+y(n-2).
MATLAB code:
N = (-15: 1 : 30);
a = [1 -1 -1];
b = [1 0.5 0.85];
X = [];
for n = N
X(end+1) = impulse_signal(n);
end
Y = filter(b,a,X);
stem(N,Y);
grid ON;
ylabel('Amplitude');
xlabel('Number of samples');
title('Aim 3 question');
Output:
—-------------------------------------------------------------------------------------------------------------
Aim 4: To write a "C" program to generate various wave forms (Sine wave,
Square wave) graphically with using Code Composer Studio (CCS-3.1) IDE
and the DSP TMS 320C6713 DSK Kit.
a) Sine wave:
C Program:
#include <stdio.h>
#include <math.h>
float j;
int i;
float y[360];
void main()
{
for( i = 0; i < 360; i++)
{
j= i;
y[i] = (float)sin(j*3.142/180.0);
}
}
Output:
b) Square Wave:
C Program:
#include <stdio.h>
#include <math.h>
int i;
int y[500];
void main()
{
for(i =0; i<500; i++)
{
if((i/50)%2 == 0)
y[i] = 5;
else
y[i] = -5;
}
}
Output:
—-------------------------------------------------------------