DIGITAL SIGNAL PROCESSING
LAB Tutorial-01
Prepared by: Mekonen A.
DDIT, SECE 2019 G.C
Out lines
ou
Basics of MATLAB and its Operations
Basics of Discrete Time Signals
Operations on Discrete Time Signals
Discrete Time Systems (Linear Convolution)
What is MatLab?
MatLab stands for = Matrix Laboratory
Is an interactive, matrix-based system for scientific and engineering
numeric computation and visualization
It integrates numerical analysis, matrix computation, signal processing and
graphics in an easy-to-use
MatLab has a number of add-on software modules, called toolboxes, that
perform more specialized computations.
Used for Signal processing and analysis
Modelling of dynamical systems
Solving partial differential equations
Simulation of engineering systems
Matrix computations and linear algebra
MatLab is case sensitive (‘A’ is a different variable from ‘a’)
MatLab Desktop Interfaces/windows
Command window : is where we type the commands
and instructions that cause MatLab to evaluate, compute,
draw and perform all the other wonderful results
Command History: contains a running history of the
commands that you type into the Command Window
Current Folder : displays contents of current working
directory or access your files
Work space : shows the variables you have created
Editor Window :use to edit individual entries
in a vector or matrix
Some Useful MatLab Commands
what: List all m-files in current directory
dir: List all files in current directory
ls: Same as dir.
pwd: Show current directory
whos: gives a complete listing of all variables currently defined and the size of those variables
Who: simply lists all variable names currently in use
clear all: clear all variables from workspace window
clear a,b: will simply clear variables a and b
close all: used to close all figure windows
clc: used to clear command windows
edit: used to open editor window
exit/quit: used to close matlab
Simple MatLab Commands for Plotting
Plot: plotting in a linear coordinate as continuous function.
Stem: plot in a linear coordinate as discrete time.
hold on :Allows multiple plots to be superimposed on the same
axes.
Figure: Create a figure for plotting
hold off: Release hold on current plot
subplot(a,b,c): Create an a × b matrix of plots with c the current
figure
grid: Adds a grid to the plot
xlabel: Labels x-axis ylabel: Labels y-axis
Some Built-in Math Functions
MATLAB comes with a large number of built-in functions that operate on
matrices on an element-by element basis
sin: sine
cos: cosine
tan: tangent
asin: inverse sine
acos: inverse cosine
atan: inverse tangent
exp: exponential (e=2.7183)
log: natural logarithm
log10: common logarithm
sqrt: square root
abs: absolute value
Matrix Operations
MatLab contains a number of arithmetic, relational, and logical operations on
matrices
+ addition
- subtraction
* multiplication
/ right division
\ left division
^ exponentiation (power)
’ conjugate (transpose)
< less than
<= less than or equal to
> greater than
>= greater than or equal to
== equal to
~= not equal to
Graphing a Signal Using MatLab
Consider a discrete sine wave signal defined as follows
Cont’d
y(k) = 5sin(0.2k), k = 0, 1, 2, …. , 50
This signal takes on values every 0.2 seconds starting at 0 seconds
and ending at 10 seconds
%MatLab program for graphing the signal
• k=0:1:50; %k=[0, 1, 2, . . . 50]
• t=0.2*k; %t=[0, 0.2, 0.4, . . . 10]
• y=5*sin(0.2*k); subplot(1,2,1)
• plot(t,y,'b’); title('y=5*sin(0.2*k)’); xlabel('time in (sec)');
• ylabel('Amplitude’); grid on; subplot(1,2,2)
• stem(t,y,'r’); title('y=5*sin(0.2*k)in discrete form');
• xlabel('n sample number’); ylabel('Amplitude'); grid on
Cont’d
Product of Sinusoidal Signals
•% MatLab Program for Multiplication of Two signals
•t=0:0.01:2;
•y=sin(2*pi*t).*sin(2*pi*4*t);
•subplot(2,1,1)
•plot(t,y);title('multiplication of two signals' );
•xlabel('time in (sec)' ); ylabel('Aplitude'); grid on
•subplot(2,1,2)
•stem(t,y,'k’);
•title('multiplication of two signals' );
•xlabel('number of sapling’ ); ylabel('Aplitude’);
•grid on
Basic Discrete Time Signals
Unit Impulse Signal (Unit Sampling Sequence)
Unit Step Signal (Unit Step Sequence)
Generation of exponential sequence
Real-valued Exponential Sequence
Complex-valued Exponential Sequence
Unit Impulse Signal(Unit Sample Sequence)
(n)=1, for only n=0, and else in everywhere it has the
amplitude value zero (for n 0).
(n-n0)=1, for n=n0, and else in everywhere it has the
amplitude zero for (n n0).
program on MatLab
function[x,n]=impseq(n0,n1,n2)
%Generates x(n)=delta(n-n0);n1<=n<=n2;
%[x,n]=impseq(n0,n1,n2)
n=(n1:n2);
x=((n-n0)==0);
Unit step signal
u(n) = 1 for n ≥ 0 ,
= 0 for n < 0
U(n-n0)= 1 for n ≥ n0, for a shifted signal by an n0
0 for n < n0
program on matlab
function[x,n]=stepseq(n0,n1,n2)
%Generates x(n)=u(n-n0);n1<=n<=n2
%[x,n]=stepseq(n0,n1,n2);
n=(n1:n2);
x=((n-n0)>=0);
Generation of Exponential Sequences
%Generation of exponential sequences
n=input('Enter the length of
exponential sequence');
t=0:n;
a=input('Enter the value of a');
y=exp(a.*t);
stem(t,y,'r');
xlabel(‘Index number');
ylabel('Amplitude');
title('Generation of exponential
sequence');
Real-valued Exponential Sequence
x(n) = ∀n; a ∈ R
example: generate x(n) = , for 0 ≤ n ≤ 10
generate x(n) = , for 0 ≤ n ≤ 10
programing on matlab >> y=exp((1.11).^n);
>> n=[0:10];
>> subplot(2,2,2)
>> x=exp((0.9).^n);
xlabel('n sequences')
>> subplot(2,2,1)
>> xlabel('n sequences')
ylabel(‘y=exp((1.11).^n
>> ylabel(‘x=exp((0.9).^n’)) ’))
>> title('real-valued sequence') title('real valued sequence')
>> stem(n,x) stem(n,y,'r')
Complex-valued Exponential Sequence
n=[0:10];
x=exp((2+3i)*n); subplot(1,2,1);
stem(n,real(x)); xlabel(‘(a) n sequences');
ylabel('Amplitude of x');
title('plotting the real part')
Subplot(1,2,2);stem(n,imag(x));
xlabel(‘(b) n sequences');
ylabel(‘Amplitude of imaginary sequence')
title('plotting the imaginary part')
subplot(1,2,3); stem(n,angle(x)*(180/pi));
xlabel(‘(c) n sequences');
ylabel('angle in degrees')
title('plotting the angles in(degrees)')
Basic Discrete Time Signal Operations
Signal Addition
Signal Multiplication
Signal Scaling (Constant Multiplier)
Time Shifting
Signal Folding (Time Reversal)
Signal Addition
>> n1=0:6;
>> n2=2:8;
>> x1=rand(size(n1));
>> x2=rand(size(n2));subplot(2,2,1);
>> stem(n1,x1,’k’);title(‘The first
sequence’);
>> Subplot(2,2,2);
>> stem(n2,x2,'r');title(‘The second
sequence’);
>> [y,n]=sigadd(x1,n1,x2,n2);
>> stem(n,y,’b’)
>> title('signal addition');
>> xlabel('number of sequences');
>> ylabel('Amplitude of signals');
Cont’d
x=input('ENTER THE FIRST SEQUENCE:');
subplot(3,1,1);
stem(x);
title('X=The 1st signal');
y=input('ENTER THE SECOND SEQUENCE:');
subplot(3,1,2);
stem(y);
title('Y=The 2nd signal');
z=x+y;
disp(z)
subplot(3,1,3);
stem(z);
title('The addedd signal is(Z=X+Y)');
Signal Multiplication
n1=1:6;
>> n2=0:5;
>> x1=[1 2 1 2 2 1];
>> x2=[2 4 5 6 4 2];
>> subplot(2,2,1); stem(n1,x1,'b');
>> title('plotting the first sequence');
>> subplot(2,2,2); stem(n2,x2,'k');
>> title('plotting the second sequence');
>> subplot(2,2,3)
>> [y,n]=sigmult(x1,n1,x2,n2); stem(n,y,'r');
>> title('signal multiplication');
>> xlabel('n sequences of two signals');
>> ylabel('The result of multiplied signal')
Signal Scaling
>> x=[2 3 4 5 6];
>> n=0:4;
>> subplot(2,2,1)
>> stem(n,x); xlabel('n index')
>> ylabel('Amplitude of the signal')
>> title('The first signal');
>> y=-1*x; subplot(2,2,2); stem(n,y,'r’)
>> xlabel('n index');
>> ylabel('The Amplitude of the scaled
signal');
>>title('scaling with a constant number')
>> grid on
Time Shifting
function [y,n] = sigshift(x,m,k)
% implements y(n) = x(n-k)
% [y,n] = sigshift(x,m,k)
n = m+k; y = x;
On command window write the following
>> m=0:4;
>> x=[1 2 4 6 2 4];
>> [y,n]=sigshift(x,m,2);
>> stem(m,x,'r')
>> [y1,m]=sigshift(x,m,2);
>> hold on
>> stem(m,y1,’b’)
Folding Signal (Time Reversal)
Programming on MatLab
>> x=[2 6 4 2];
n=[0:3];
stem(n,x,'k')
hold on
[y,n]=sigfold(x,n);
stem(n,y,'r');
>>legend('1st signal','folded signal');
>> title(Time Reversed signal');
>> xlabel('n index number');
>> ylabel(‘magnitude the of the signal')
Cont’d
• x=input('ENTER THE SEQUENCE x(n)=');
• N=input('Enter length of the sequence
N=');
• n=0:N-1;
• subplot(2,1,1); stem(n,x,'k'); grid on
• title('Signal x(n)');
• y=fliplr(x);
• m=fliplr(-n);
• disp('FOLDED SEQUENCE')
• disp(y)
• subplot(2,1,2);
• stem(m,y,'r'); grid on
• title('Reversed Signal x(-n)') ;
Linear Convolution (Convolution Sum)
x=input('ENTER THE INPUT SEQUENCE of x(n)=');
nx=length(x);n1=0:1:nx-1;
h=input('ENTER THE IMPULSE RESPONSE of
h(n)=');
nh=length(h);n2=0:1:nh-1;
y=conv2(x,h); n3=0:1:nx+nh-2; subplot(2,2,1);
stem(n1,x,'b'); xlabel('(a) n-index');
ylabel('Amplitude of x(n)');subplot(2,2,2);
stem(n2,h,'k'); xlabel('(b) n-index');
ylabel('Amplitude of h(n)');subplot(2,2,3);
stem(n3,y,'r'); xlabel('n sampling
sequences');
ylabel('Amplitude of y(n)');
title('convolution of x(n) with h(n)');
disp('The resultant signal is');y;
Cont’d
x=[1 2 3 4 3 2 1];
nx=length(x); n=0:1:nx-1;
subplot(2,2,1); xlabel('(a)n– index’);
ylabel(x(n)');
title('input sequence of x(n)');
stem(n,x'k');
h=[4 2 3 1 3 2]; nh=length(h); m=0:1:nh-1;
subplot(2,2,2); xlabel('(b)n index’);
ylabel('h(n)');
title('impulse response of h(n)');
stem(m,h,'b');
y=conv2(x,h); ny=0:1:nx+nh-2;
subplot(2,1,2); xlabel('(c)n--index');
ylabel('y(n)’);
title('convolution of two sequences x(n)and h(n)');
stem(ny,y,'r');
disp('The resultant signal is ');y;
Cont’d
• x=[4 3 5 5];
• nx=length(x); n=0:1:nx-1;
• subplot(2,2,1); stem(n,x,'k');grid on
• xlabel('(a)n– index'); ylabel('x(n)’);
• title('input sequence of x(n)');
• h=[-1 1 2]; nh=length(h); m=0:1:nh-1;
• subplot(2,2,2); stem(m,h,'b');grid on
• xlabel('(b)n-- index');ylabel('h(n)')
• title('impulse response of h(n)');
• y=conv2(x,h);ny=0:1:nx+nh-2;
• subplot(2,1,2); stem(ny,y,'r');grid on
• xlabel('(c)n--index');
• ylabel('y(n)’);
• title('convolution of two sequences x(n)and h(n)');
• disp('The resultant signal is ');y;
THANK YOU!