Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

Digital Signal Processing

Lab - 1
Name :​ Charmil Gandhi
Roll No : ​1741059

Aim: ​To understand the concepts of signal and systems, signal operations and sampling
theorem using MATLAB
-------------------------------------------------------------------------------------------------------------------------------
1. Generate continuous signal having equation x(t) = 3t/(4 + t​2​) and discrete time signal
having equation x(n) = 3n/(4 + n​2​).
Code:
t = [​-10​:​0.1​:​10​]; ​% sample points with increment of 0.01
x1 = ​3.​*t./(​4​+t.^​2​); ​ continuous time signal
%
subplot(​2​,​1​,​1​);
plot(t,x1); ​% Create plot with blue line
xlabel(​'t (sec)'​);
ylabel(​'x(t)'​); ​% Label axis

n = [​-10​:​1​:​10​]; ​% sample index from 0 to 20


x2 = ​3.​*n./(​4​+n.^​2​); ​ discrete time signal
%
subplot(​2​,​1​,​2​);
Hs = stem(n,x2); ​% Stem-plot with handle Hs
xlabel(​'n'​);
ylabel(​'x(n)'​); ​% Label axis

First, I have defined an array ​t​ which is an array of time ranging from -10 to +10 with an
increment of 0.1. This array signifies the time range for continuous signal. Then, I have defined
the signal ​x1​ which is the continuous signal. Similarly, I have defined an array ​n​ with the same
range as array ​t​ and a signal ​x2​ which is the discrete time signal. To plot both the graphs in the
same figure, I have used the subplot​ ​command. Syntax of the subplot command is
subplot(Number of rows, Number of Columns, Row Number).

To plot the continuous time signal, I have used ​plot ​command which has ​t ​and ​x1 ​as input
parameters and for discrete time signal, I have used ​stem​ command which has ​n ​and ​x2​ has
input parameters
2. Plot the continuous and discrete time sinusoidal wave for given amplitude, frequency,
phase and sampling frequency.
Code:
clear all;
close all;

a=input(​'Enter the amplitude: '​); ​%taking user input amplitude


f=input(​'Enter the frequency: '​); ​%taking user input frequency
p=input(​'Enter the phase: '​); ​%taking user input phase
fs=input(​'Enter the sampling frequency: '​); ​%taking user input frequency

t= ​-1​:​1​/fs:​1​; ​%range of time

x= a*​sin​(​2​*​pi​*f*t + p); ​%sine function generator


subplot(​2,1,1​);
plot(t,x);
title(​'Continuous Time Sinusoidal wave'​);
subplot(​2,1,2​);
stem(t,x);
title(​'Discrete Time Sinusoidal wave'​);

To plot the continuous time and discrete time sinusoidal wave, I first took the value of
amplitude(a),​ ​frequency(f)​, p ​ hase(p)​ and s
​ ampling frequency(fs)​ from the user using the
input​ command. Then, I have defined the time array ​t​ ranging from -1 to +1 with the increment
of 1/fs. ​x​ is the sinusoidal signal represented as ​x = a*sin(2*pi*f*t + p)​. To represent both
continuous time and discrete time signals in the same figure, I have used the ​subplot
command. To plot the continuous time signal, I have used ​plot c​ ommand and for discrete time
signal, I have used ​stem​ command which has ​t ​and ​x​ has input parameters.

3. Generate the function for signal addition. Add two sequences x1(n) ={1,​-1​,2,5,1,5,-1} and
x2(n) ={-2,-8,9,4,2,3,5}.
Code: Addition Function
function​ ​out​ = ​add​(x1,x2,n1,n2,n)
y1 = ​zeros​(​1​,​length​(n));
y2 = y1; ​% initialization
y1(​find​((n>=min(n1))&(n<=max(n1))==​1​))=x1; ​ % x1 with duration of y1
y2(​find​((n>=min(n2))&(n<=max(n2))==​1​))=x2; ​ % x2 with duration of y2
out = y1+y2; ​ % sequence addition
end

The above mentioned code is of ​add​ function which takes 2 signals ​x1​ and ​x2​ and their i​ ndex
ranges​ and ​total length​ as input parameters. First signal ​y1​ is initialized with zeros and
passing the same values to signal ​y2.​ Then I have used ​find​ command to point to non-zero
elements. The result of addition of both y1 and y2 is returned as a variable ​out.​

Code: Main File


x1 = input(​'Enter the signal #1: '​); ​%taking input from user for array
n1 = input(​'Enter index range: '​); ​%taking index range from users
x2 = input(​'Enter the signal #2: '​); ​%taking input from user for array
n2 = input(​'Enter index range: '​); ​%taking index range from users
n = min(min(n1),min(n2)):max(max(n1),max(n2)); ​% duration of y(n)
y = add(x1,x2,n1,n2,n);
stem(n,y) ​% resultant discrete signal
ylabel(​'x(n)'​);
xlabel(​'n'​);
First, I am taking both the sequences ​x1​ and ​x2​ and their respective index range ​n1​ and ​n2
from the user. The next step is to find the length ​(n)​ of the output sequence as both the
sequences have origin at different indices which is done by finding the minimum and maximum
index of both sequences. Then, the addition is performed by calling the user made function of
addition:​ add​. The resultant discrete time signal is plotted using ​stem​ command.

The above result is in addition of 2 signals: x1(n) ={1,​-1​,2,5,1,5,-1} and x2(n) ={-2,-8,9,4,2,3,5}.

4. Generate the function for signal multiplication. For Two sequences


x1(n)={1,-1,2,5,1,5,-1} and x2(n) ={-2,-8,9,4,2,3,5}.
Code: Multiplication Function
function​ ​out​ = ​multiply​(x1,x2,n1,n2,n)
y1 = ​zeros​(​1​,​length​(n));
y2 = y1; ​ initialization
%
y1(​find​((n>=min(n1))&(n<=max(n1))==​1​))=x1; %​ x1 with duration of y1
y2(​find​((n>=min(n2))&(n<=max(n2))==​1​))=x2; % ​ x2 with duration of y2
out = y1.*y2;
end

The above code is of ​multiply function which takes 2 signals ​x1 and ​x2 and their ​index ranges
and ​total length as input parameters. First signal ​y1 is initialized with zeros and passing the
same values to signal ​y2.​ Then I have used ​find command to point to non-zero elements. The
result of multiplication of both y1 and y2 is returned as a variable ​out​.
Code: Main File
x1 = input(​'Enter the signal #1: '​); ​%taking input from user for array
n1 = input(​'Enter index range: '​); ​%taking index range from users
x2 = input(​'Enter the signal #2: '​); ​%taking input from user for array
n2 = input(​'Enter index range: '​); ​%taking index range from users
n = min(min(n1),min(n2)):max(max(n1),max(n2)); ​% duration of y(n)
y = multiply(x1,x2,n1,n2,n); ​%
sequence multiplication
stem(n,y) ​% resultant discrete
signal
ylabel(​'x(n)'​);
xlabel(​'n'​);
title(​'Multiplication of discrete signals'​);

First, I am taking both the sequences ​x1 and ​x2 and their respective index range ​n1 and ​n2
from the user. The next step is to find the length ​(n) of the output sequence as both the
sequences have origin at different indices which is done by finding the minimum and maximum
index of both sequences. Then, the multiplication is performed by calling the user made function
of addition:​ multiply.​ The resultant discrete time signal is plotted using ​stem​ command.

The above result is in multiplication of 2 signals: x1(n) ={1,​-1​,2,5,1,5,-1} and x2(n) =


{-2,-8,9,4,2,3,5}.

5. Generate the function for timing shifting. For sequences x(n) ={1,​-1​,2,5,1,5,-1}.
Code: time_shifting function
​ ij ]​ = ​time_shifting​( ii, n )
function​ [
ij = n + ii;
end
The above code is a ​time_shifting function which takes index and the amount by which we
want to shift as input parameters.

Code: Main File


x = input(​'Enter the signal #1: '​); ​%taking input from user for array
n = input(​'Enter index range: '​); ​%taking index range from users
ii = input(​'Enter by how much you want to shift: '​); ​%taking index range from users
subplot(​2​,​1​,​1​);
stem(n,x); ​%displaying the discrete signal
title(​'x(n) Original signal'​);
xlabel(​'n'​);
ylabel(​'x(n)'​);

y=time_shifting(ii,n);
subplot(​2​,​1​,​2​);
stem(m,y); ​%displaying the shifted discrete signal
title(​'y(n)=x(n-2) signal'​);
xlabel(​'n'​);
ylabel(​'y(n)'​);
title(​'Shifted Signal'​);

​ nd index range ​n a
First, I am taking the signal ​x a ​ s input from the user. Secondly, I am taking ​ii
which represents the amount by which we want to perform time shift on the signal. Then, I have
called the function ​time_shifting ​to shift the signal by ​ii. T
​ o compare both, original signal and
shifted signal, I have used ​subplot c​ ommand so that both the signals can be plotted in the
same figure.
6. Generate the function for signal folding. Fold the sequence x(n) ={1,​-1​,2,5,1,5,-1}.
Code: signal_folding function
​ ij ]​ = ​signal_folding​( ii)
function​ [
ij = -ii;
end
By signal folding, we mean that the signal will be reversed. The above code is ​signal_folding
function which takes index ​ii ​as input parameter and to reverse that, I have just passed the
negative value of that index.

Code: Main File


x = input(​'Enter the signal: '​); ​% taking input array from user
ii = input(​'Enter index range: '​); ​% defining the array index
subplot(​2​,​1​,​1​);
stem(ii,x);
xlabel(​'n'​);
ylabel(​'x(n)'​);
title(​'Original Signal'​);
subplot(​2​,​1​,​2​);
ij = signal_folding(ii); ​% function call
stem(ij, x) ​% display folded discrete
function
xlabel(​'n'​);
ylabel(​'x(n)'​);
title(​'Folded Signal'​);

​ nd index range ​ii as user input. Then to fold the signal, I have called
First, I am taking signal ​x a
the user made ​signal_folding function. To compare the output of original signal ​x ​and folded
signal ​y, I​ have used ​subplot c​ ommand to plot both the graphs in the same figure.
7. Generate the function for time multiplication. Use it for sequence x1(n) ={1,​-1​,2,5,1,5,-1}.
Code: time_multiplication
​ ij ]​ = ​time_multiplication​( ii, num)
function​ [
ij = ii*(​1​/num);
end

The above code is of ​time_multiplication​ which takes index ​ii​ and the number ​num​ which we
want to multiply.

Code: Main File


x = input(​'Enter the signal: '​); ​% taking user input signal
ii = input(​'Enter index range: '​); ​% taking index range
num = input(​'Enter the number to multiply with time: '​);
subplot(​2​,​1​,​1​);
stem(ii,x); ​%displaying the discrete signal
xlabel(​'n'​);
ylabel(​'x(n)'​);
title(​'Original Signal'​);
subplot(​2​,​1​,​2​);
ij = time_multiplication(ii, num); ​%function called
stem(ij, x); ​%displaying the new
discrete signal
xlabel(​'n'​);
ylabel(​'x(n)'​);
title(​'New Signal'​);

First, I am taking signal ​x​ and index range ​ii​ from user input and also the number ​num w
​ ith
which we want to multiply the signal. To compare both the signals, I have used subplot
command to plot both the graphs in the same figure.
8. Generate function for unit sample signal 𝛿(n). Also plot 𝛿(n-1) and 𝛿(n+1 ).
Code:
t = [​-1.5​:​0.01​:​1.5​];
impulse = t==​0​; ​%impulse function
subplot(​3​,​1​,​1​);
impulse_plusone = t==​1​; ​%impulse function at n=1
stem(t,impulse);
xlabel(​'n'​);
ylabel(​'x(n)'​);

subplot(​3​,​1​,​2​);
stem(t,impulse_plusone);
xlabel(​'n'​);
ylabel(​'x(n)'​);

subplot(​3​,​1​,​3​);
impulse_minusone = t==​-1​; ​%impulse function at n=-1
stem(t,impulse_minusone);
xlabel(​'n'​);
ylabel(​'x(n)'​);

First I have defined an array ​t​ which ranges from -1.5 to +1.5 with increment of 0.01. Then, I
have defined impulse function​ impulse = t==0. T ​ his impulse is at 0​th​ index. Similarly impulse at
+1(​impulse = t== -1​) and -1(​impulse = t== 1​) index can be generated. To plot all the graphs in
the same figure, I am using ​subplot​ command.
9. Generate function for unit step signal u(n). Also plot u(n-1) and u(n+1).
Code:
clear all;
close all;

t = (​-5​:​0.1​:​5​)'; ​% defining time range


unitstep = t>=​0​; ​ generating unit step signal
%
unitstep_plus1 = t>=​1​; ​ shifting of unit step signal
%
to right by 1
unitstep_minus1 = t<=​-1​; ​% shifting of unit step signal
to left by 1

subplot(​3​,​1​,​1​);
stem(t,unitstep); ​% plotting unit step signal

subplot(​3​,​1​,​2​);
stem(t,unitstep_plus1); ​% plotting right shifted unit
step signal

subplot(​3​,​1​,​3​);
stem(t,unitstep_minus1); ​% plotting left shifted unit
step signal

The above code is of unit step signal. First I have defined an array ​t ​which ranges from -5 to +5
​ imilarly unit
with the increment of 0.1. The unit step function is defined as ​unitstep = t>=0 . S
steps with shifted origins can also be defined by ​unitstep_plus1 = t>=1 ​and
​ o plot all the 3 signals in single figure, I have used the ​subplot
unitstep_minus1 = t<= -1. T
command.

10. Generate function for unit ramp signal u(n). Also plot ur(n-1) and ur(n+1).
Code:
clear all;
close all;
t = (​-10​:​0.1​:​10​)'; ​% defining the time range
unitstep = t>=​0​; ​% generating unit step
signal
unitstep_plus1 = t>=​1​; ​% generating right shifted
unit step signal
unitstep_minus1 = t<=​-1​; ​% generating left shifted
unit step signal
ramp = t.*unitstep; ​% generating ramp signal
ramp_plus1 = t.*unitstep_plus1; ​% generating right shifted
ramp signal
ramp_minus1 = (t).*(​1​-unitstep_minus1); ​% generating left
shifted ramp signal
subplot(​3​,​1​,​1​);
stem(t,ramp);
subplot(​3​,​1​,​2​);
stem(t,ramp_plus1);
subplot(​3​,​1​,​3​);
stem(t,ramp_minus1);
First, I have defined an array ​t ​ ranging from -10 to +10 incrementing by 0.1. We can create
ramp signal using unit step signal. We have to just multiply the ​unit step signal​ with values of ​t
to get the ​ramp signal.​ The same thing has been done in the code to obtain the ramp signal
with origin at 0 and at 1. To obtain the ramp signal with origin shifted to -1 position, I did
ramp_minus1 = (t).*(1-unitstep_minus1) ​where unitstep_minus1 =
unitstep_minus1 = t<=-1.

11. Plot all the given signals and comment on their output for periodicity writing common
MATLAB code.
a. X(n) = cos(0.002𝝅n)
b. X(n) = sin(30𝝅n/105)
c. X(n) = sin(5n)
d. X(n) = cos(32𝝅n/10)
e. X(n) =10cos(7n𝝅/6)
f. X(n) = 2e​j(n-𝝅)
Code:
clear all;
close all;
% To calculate the period of sinusodal waves, we just divide 2*pi by
% co-efficient of n

n = ​-10​:​0.01​:​10​; ​% defining range of n


x1 = ​cos​(​0.002​*​pi​.*n); ​ % signal 1
x2 = ​sin​(​30​*​pi​.*(n./​105​)); ​ % signal 2
x3 = ​sin​(​5.​*n); ​ % signal 3
x4 = ​cos​(​32​*​pi​.*(n./​10​)); ​ % signal 4
x5 = ​10.​*​cos​(​7.​*n + ​pi​/​6​); ​ % signal 5
x6 = ​2.​*(​exp​(​i​*(n-​pi​))); ​ % signal 6

subplot(​3​,​1​,​1​);
plot(n,x1);
title(​'Signal 1 with period of 1000'​); ​% signal with period of 1000
subplot(​3​,​1​,​2​);
plot(n,x2);
title(​'Signal 2 with period 7'​); ​% signal with period 7

subplot(​3​,​1​,​3​);
plot(n,x3); ​% signal with period 2*pi/5
title(​'Signal 3 with period 2*pi/5'​);
figure

subplot(​3​,​1​,​1​);
plot(n,x4); ​% signal with period 5/8
title(​'Signal 4 with period 5/8'​);

subplot(​3​,​1​,​2​);
plot(n,x5); ​% Not Periodic
title(​'Signal 5 with period 2*pi/7'​);

subplot(​3​,​1​,​3​);
plot(n,x6); ​% signal with period 2*pi
title(​'Signal 6 with period 2*pi'​);

First, I have defined an array ​n​ which ranges from -10 to +10 with an increment of 0.01.
Secondly, I defined all the signals which are given in the question. To know the period of the
signal, we just divide the coefficient of 2𝝅 by ​n.​ By this we came to know the periods of all the
signals and these periods are maintained in the comments just beside their plotting. To plot
multiple signals in one figure, I have used the subplot​ ​command. By using this, I have made 2
figures, each with 3-3 plots.
12. Plot all the given signals and comment on their output for periodicity writing common
MATLAB code.
1) x(n) = 3cos(n𝝅/6) + 5cos(3n𝝅/4)
2) x(n) = cos(n/7)cos(n𝝅/7)
3) x(n) = cos(n𝝅/6)cos(n𝝅/9)
4) x(n) = 2cos(n𝝅/4) - sin(n𝝅/6) + 3cos(n𝝅/8 + 𝝅/3)
Code:
close all;
clear all;
% To find periods of complex signals, we find periods of individual
% components and take LCM to find period of whole components

% for signal 1
n1 = ​0​:​50​;
x1 = ​3​*​cos​(n1*​pi​/​6​) + ​5​*​cos​(​3​*n1*​pi​/​4​);
subplot(​2​,​1​,​1​);
stem(n1,x1); ​% periodic signal with period of 24
title(​'Signal #1'​);

% for signal 2
n2 = ​0​:​50​;
x2 = ​cos​(n2/​7​).*​cos​(n2*​pi​/​7​);
subplot(​2​,​1​,​2​);
stem(n2,x2); ​% non-periodic signal
title(​'Signal #2'​);
figure;

% for signal 3
n3 = ​0​:​80​;
x3 = ​cos​(n3*​pi​/​6​).*​cos​(n3*​pi​/​9​);
subplot(​2​,​1​,​1​);
stem(n3,x3); ​% periodic signal with period of 36
title(​'Signal #3'​);

% for signal 4
n4 = ​0​:​100​;
x4 = ​2​*​cos​(n4*​pi​/​4​)-​sin​(n4*​pi​/​6​)+​3​*​cos​((n4*​pi​/​8​) + (​pi​/​3​));
subplot(​2​,​1​,​2​);
stem(n4,x4); ​% periodic signal with period of 48
title(​'Signal #4'​);
figure;
To find the periods of complex signals, we find periods of individual signals, take LCM to find the
period of the entire signal. In the above mentioned code, first for each and every signal, I have
defined ranges of ​n ​and then defined the signal. I have used ​subplot ​command to plot multiple
graphs in the same figure. I have made 2 figures each with 2-2 plots in it.

13. Sample the sinusoid x = sin(2 𝝅 f t), where f = 2 kHz, and plot the sampled signals over
the continuous-time signal. Let x1 be the signal sampled at 10 kHz. Let x2 be the signal
sampled at 3 kHz. Plot required waveforms and comment on the same by writing
common MATLAB code.
Code:
clear all;
close all;
% Nyquist Criteria : sampling frequency > 2*f
% According to Nyquist Criteria, the signal sampled at 10 kHz is over
% over damping and the signal sampled at 3 kHz is under damping
f = ​2000​;
T = ​1​/f;
tMin = ​0​;
tMax = ​10​*T; ​% plotting for 10 cycles
dt1 = ​1​/​10000​;
dt2 = ​1​/​3000​;

t1 = tMin:dt1:tMax;
t2 = tMin:dt2:tMax;
x1 = ​sin​(​2​*​pi​*f*t1);
x2 = ​sin​(​2​*​pi​*f*t2);

% plotting all waveforms


subplot(​2​,​1​,​1​);
%stem(t1,x1);
plot(t1,x1);
title(​'Samples at 10 kHz'​);

subplot(​2​,​1​,​2​);
%stem(t2,x2);
plot(t2,x2);
title(​'Sampled at 3 kHz'​);

According to Nyquist Criteria, sampling frequency (​fs)​ < 2*frequency(​f)​ . The frequency of the
signal is 2000 Hz. ​x1​ is the signal sampled at 10 kHz and ​x2​ be the signal sampled at 3kHz.
The variables ​dt1 a​ nd ​dt2 ​are 1/10000 and 1/3000. To plot for 10 cycles, I have done 10*T
where T = 1/2000. Then I have declared arrays ​t1​ and ​t2 a​ nd then defined respective signals ​x1
and ​x2.​ Using ​subplot c​ ommand, I have plotted multiple graphs in the same figure.

According to Nyquist Rate, the signal sampled at 10kHz is over damping and the signal
sampled at 2kHz is under damping.

Conclusion:
In this lab we performed the simulation of basics of signals and systems understanding the
generation of various simple signals and also performing various operations on the signals like
addition, multiplication, finding periodicity etc.

You might also like