0% found this document useful (0 votes)
17 views7 pages

Eeng420 DSP Lab Experiment 2

The document outlines a lab experiment for EENG420 Digital Signal Processing, focusing on implementing discrete signals such as step and impulse signals in MATLAB. It covers various types of discrete time signals, operations on sequences, and methods to calculate signal energy and power. The document includes MATLAB code examples for generating and manipulating these signals.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views7 pages

Eeng420 DSP Lab Experiment 2

The document outlines a lab experiment for EENG420 Digital Signal Processing, focusing on implementing discrete signals such as step and impulse signals in MATLAB. It covers various types of discrete time signals, operations on sequences, and methods to calculate signal energy and power. The document includes MATLAB code examples for generating and manipulating these signals.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

EENG420 Digital Signal Processing

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.

DISCRETE TIME SIGNALS

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.

1. VECTOR AND MATRIX OPERATIONS:

x(n)= {…, x(-1), x(0), x(1),…}


Representation of x(n) would require two vectors : x and n

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

2.1 Unit Sample Sequence

1 , n=0 and 1 , n=n0


δ (n) = δ (n − n0) =
0 , otherwise shifted by n0 0,otherwise

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

1,n≥0 and 1 ,n≥0


u ( n) = 0 ,n<0 u ( n − n0) = 0 ,n<0
shifted by n0

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

2.3 Real-Valued Exponential Sequence

x(n)=a^n real exponential function

Ex:
x(n)=(0.9)^n for 0 <=n <= 10

In matlab
>> n=[0:10];
>> x=(0.9).^n;
>> stem(n,x)

2.4 Complex-Valued Exponential Sequence

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)

2.6 Random Sequence

rand(1,N) generates a length N random sequence whose elements are


uniformly distributed between [0,1].
randn(1,N) generates a length N Gaussian random sequence with mean 0
and variance 1.

2.7 Periodic Sequence

x(n) is periodic if x(n)=x(n+N)


>> xtilde=[x,x,…,x];

In matlab
>> x=[6 9 0]
>> xtilde=x’ *ones(1,4);
>> xtilde=xtilde(:);
>> xtilde=xtilde’ ;

3. OPERATIONS ON SEQUENCES

3.1 Signal Addition

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

3.2 Signal Multiplication

x1(n)*x2(n) Only change (+) to (.*) and sigadd to sigmult.

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

y(n)=x(n-k) m=n-k and n=m+k

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)

3.6 Sample Summation

It adds all sample values of x(n) between n1 and n2.


n2
∑ x(n)= x(n1)+……+ x(n2)
n=n1

In matlab
>> sum(x(n1:n2))

5
Ex:
>> x=[1 1 4 2 6 3];
>> sum(x(2:4))

3.7 Sample Product

It multiplies all sample values of x(n) between n1 and n2.


n2
Π x(n)= x(n1)*……* x(n2)
n=n1

In matlab
>> prod(x(n1:n2))

Ex:
>> x=[8 9 7 3];
>> prod(x(1:4))

3.8 Signal Energy

The energy of a sequence


∞ ∞
Єx=∑ x(n)x*(n)= ∑|x(n)|²
-∞ -∞
In matlab
>> Ex= sum(x.*conj(x)) % x is a sequence
>> Ex=sum(abs(x).^2)
Try for x=[2 4 5]
Both of the answers will be same

3.9 Signal Power


The average power of a periodic sequence
N-1
Px= (1/N)* ∑|x(n)|²
0

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

You might also like