Eeng420 DSP Lab Experiment 2
Eeng420 DSP Lab Experiment 2
LAB EXPERIMENT 2
Objectives:
The objective of this lab is to be familiar with how to implement discrete signals such
as Step signals, impulse signals in MATLAB. Finally, calculate the signal power and
energy in MATLAB.
Signals can be classified as; analog signals, x(t), where t represents time and
discrete signals, x(n), where n represents integer and sample position.
Ex:
x(n)= {2,1,-1,0,1,4,3,7}
In matlab
>> n=[-3,-2,-1,0,1,2,3,4];
>> x=[2,1,-1,0,1,4,3,7];
>> stem(n,x)
Note: When the sequence begins at n=0, we do not need to use n vector.
2. TYPES OF SEQUENCES
In matlab (M-file)
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];
Ex:
% n= -3 -2 -1 0 1 2 3 4 5
>> impseq(3,-3,5)
ans =
0 0 0 0 0 0 1 0 0
% n= -6 -5 -4 -3 -2 -1 0 1 2
>> impseq(-1,-6,2)
ans =
0 0 0 0 0 1 0 0 0
1
2.2 Unit Step Sequence
In matlab (M-file)
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];
Ex:
% n= -4 -3 -2 -1 0 1 2 3 4
>> stepseq(2,-4,4)
ans =
0 0 0 0 0 0 1 1 1
% n= -2 -1 0 1 2 3
>> stepseq(-1,-2,3)
ans =
0 1 1 1 1 1
Ex:
x(n)=(0.9)^n for 0 <=n <= 10
In matlab
>> n=[0:10];
>> x=(0.9).^n;
>> stem(n,x)
x(n)= exp(a-jw0)n
Ex:
x(n)=exp[(2+j3)n] for 0 <=n <= 10
In matlab
>> n=[0:10];
>> x=exp((2+3*j).*n);
>> stem(n,x)
2
2.5 Sinusoidal Sequence
x(n)=Acos(w0n+θ)
Ex:
x(n)=3cos(0.1πn+π/3)+2sin(0.5πn) for 0 <=n <= 10
In matlab
>>n=[0:10];
>>x=3*cos(0.1*pi*n+pi/3)+2*sin(0.5*pi*n);
>>stem(n,x)
In matlab
>> x=[6 9 0]
>> xtilde=x’ *ones(1,4);
>> xtilde=xtilde(:);
>> xtilde=xtilde’ ;
3. OPERATIONS ON SEQUENCES
x1(n)+x2(n)
Ex:
The lengths of y1 and y2 are same.
>> n=[1 2 3 4 5];
>> y1=[1 0 1 0 1];
>> y2=[0 2 2 1 0];
>> z=y1+y2
z= 1 2 3 1 1
3
However, if the sequences are of unequal lengths, or if the sample positions are
different for equal-length sequences, then we cannot directly use the operator
(+). We have to use ‘‘sigadd’’ function.
M-file
function [y,n]=sigadd(x1,n1,x2,n2)
n=min(min(n1),min(n2)): max(max(n1),max(n2));
y1=zeros(1,length(n)); y2=y1;
y1(find((n>=min(n1))&(n<=max(n1))==1))=x1;
y2(find((n>=min(n2))&(n<=max(n2))==1))=x2;
y=y1+y2;
Ex:
>> x1=[2 2 2 2];
>> n1=[2 3 4 5];
>> x2=[2 2 2 2];
>> n2=[1 2 3 4];
>> sigadd(x1,n1,x2,n2)
ans= 2 4 4 4 2
Ex:
>> x1=[2 2 2 2];
>> n1=[2 3 4 5];
>> x2=[2 2 2 2];
>> n2=[1 2 3 4];
>> sigmult(x1,n1,x2,n2)
ans= 0 4 4 4 0
3.3 Scaling :
Each example is multiplied by a scalar a. ( a*x(n) )
Ex:
>> A=[3 5 8]
>>B=3*A
3.4 Shifting
M-file
function [y,n]=sigshift(x,m,n0)
n=m+n0; y=x;
4
Ex:
>> x=[2 0 1 3 5];
>> n=[-3 -2 -1 0 1];
>>[y1,n1]=sigshift(x,n,4
)
Ex:
>> x=[5 4 8];
>> n=1:3;
>>[y,n1]=sigshift(x,n,-2)
3.5 Folding
y(n)=x(-n)
M-file
function [y,n]=sigfold(x,n)
y=fliplr(x); n=-fliplr(n);
Ex:
>> x=[1 2 3 4];
>> n=2:5;
>>[y,n]=sigfold(a,n)
Ex:
>> x=[1 2 1 6 1 0 1];
>> n=-3:3;
>>[y,n]=sigfold(x,n)
In matlab
>> sum(x(n1:n2))
5
Ex:
>> x=[1 1 4 2 6 3];
>> sum(x(2:4))
In matlab
>> prod(x(n1:n2))
Ex:
>> x=[8 9 7 3];
>> prod(x(1:4))
In matlab
>> Px=sum(abs(x).^2)/N % x is a sequence
Ex:
x=[9 6 4 7] Find Px for N=2and N=3.
>> x=[9 6 4 7]
>> Px=sum(abs(x).^2)/2
>> Px=sum(abs(x).^2)/3