Sunteți pe pagina 1din 9

Digital Signal Processing Lab

Instructor:
Semester:
Session:
Handout:
Student Name:
Date:

Ghulam Abbas
Fall 2014
EE-10
3

Discrete-time Signals in Time-Domain


1-Introduction

Signals are broadly classified into analog and discrete signals. An analog is denoted by xa(t),
where t is time is seconds. A discrete-time signal is represented as a sequence of numbers, called
samples. These samples are denoted by x(n), where n is integer-valued and represents discrete
instances in time. An example of a discrete time signal is:
x(n) = {2,1,-1,0,1,4,3,5}

Where the up row indicates the sample at n=0.


In MATLAB, a finite duration sequence is represented by a row vector. However, such a vector
does not have any information about sample position n. Therefore a correct representation of x(n)
would require two vectors, one each for x and n .
The above given sequence x(n) can be represented in MATLAB by
>> x = [2,1,-1,0,1,4,3,5];
>> n=[-3,-2,-1,0,1,2,3,4];
>> stem(n,x)
An arbitrary infinite-duration sequence cannot be represented in MATLAB due to the finite
memory limitations.
2- Elementary Sequences
Unit Sample Sequence
A unit sample sequence is defined by
=

1
0

=0
= ,0,0,0,1,0,0,0,
0

In MATLAB, the function zeros(1,N) is used to generate unit sample sequence over a finite
interval of length N.
u=[1 zeros(1,N-1)];
A unit sample sequence of length N and delayed by samples M, where M < N, can be generated by
ud=[zeros(1,M) 1 zeros(1,N-M-1)];
Program 1:
Generate unit sample sequence for -10 n 20.
>> n=-10:20;
>> u=[zeros(1,10) 1 zeros(1,20)];
>> stem(n,u)
Unit Step Sequence
A unit step sequence is defined by
u(n) =

1 0
= ,0,0,0,1,1,1,1,
0 <0

In MATLAB, the function ones(1,N) is used to generate unit step sequence over a finite interval
of length N.
S=[ones(1,N)];
A unit step sequence of length N delayed by samples M, where M < N, can be generated by
sd=[zeros(1,M) ones(1,N-M)]
Program 2:
Generate unit step sequence u(n) for -10 n 20.
>> n=-10:20;
>> u=[zeros(1,10) ones(1,21)];
>> stem(n,u)
Exponential Signals
Real-valued exponential sequence is given by
x(n)= an, n
In MATLAB an array operator .^ is required to implement a real exponential sequence.
Program 3:
Generate a real-valued exponential sequence x(n)= an , a=1.2 for 0 n 20.
>> n=0:20;
>> a=1.2;
>> x=a.^n;
>> stem(n,x)

Complex-valued exponential sequence is given by


x(n)= (+ 0 ) ,
Where is called attenuation and 0 is the frequency in radians. A MATLAB function exp is used to
generate exponential sequences.
Program 4:
1

Generate a Complex valued exponential sequence x(n)= ( 2+ 6 ) for 0 n 40.


>> n=0:40;
>> c=-(1/2) + (pi/6)*i;
>> x=exp(c*n);
>> subplot(2,1,1);
>> stem(n,real(x))
>> subplot(2,1,2);
>> stem(n,imag(x))
Sinusoidal Sequence
Sinusoidal sequence is given by
x(n)=cos(0 + ) ,
Where is the phase in radians. A MATLAB function cos or sin is used to generate sinusoidal
sequences.
Program 5:

Generate a Sinusoidal sequence x(n)=cos(0.1 + 3 ) for 0 n 10.


>> n=0:10;
>> x=cos(0.1*pi*n + pi/3);
>> stem(n,x)
Random Signals
A random signal of length N with samples uniformly distributed in the interval (0,1) can be
generated by using MATLAB command
x=rand(1,N);
Similarly, a random signal of length N with samples normally distributed with zero mean and
unity variance can be generated by
x=randn(1,N)

3-Operations on Signal Sequence


Signal Addition
It is a sample by sample addition given by
{x1(n)}+{x2(n)}={x1(n)+x2(n)}
In MATLAB it is implemented by the arithmetic operator +. However the lengths of x1(n) and x2(n)
must be the same. If the sequences are of unequal length or if the sample positions are different for
equal-length sequences, then the + operator cannot be directly used. The two sequences x1(n) and
x2(n) must be first aligned by taking into account the MATLAB indexing operations.
The following MATLAB routine demonstrated these operations
% implement y(n)= x1(n)+x2(n)
n=min(min(n1),min(n2)):max(max(n1),max(n2)); %duration of y(n)
y1=zeros(1,length(n));
%initialization
y2= zeros(1,length(n));
y1(find((n>=min(n1))&(n<=max(n1))==1))=x1;
% x1 with duration of y(n)
y2(find((n>=min(n2))&(n<=max(n2))==1))=x2;
% x2 with duration of y(n)
y=y1+y2;
% sequence addition
Program 6:
Find y(n) = x1(n1) + x2(n2)
where

x1(n1)= [-2,-6,-7,5,-1,10,11]
x2(n2)=[0,4,5,6,1,-1,-10,9,5,4,3,2,12]

>> x1=[-2,-6,-7,5,-1,10,11];
>> x2=[0,4,5,6,1,-1,-10,9,5,4,3,2,12];
>> n1=-3:3;
>> n2=-6:6;
>> n=min(min(n1),min(n2)):max(max(n1),max(n2));
>> y1=zeros(1,length(n));
>> y2= zeros(1,length(n));
>> y1(find((n>=min(n1))&(n<=max(n1))==1))=x1;
>> y2(find((n>=min(n2))&(n<=max(n2))==1))=x2;
>> y=y1+y2;
>> subplot(311);
>> stem(n1,x1)
>> subplot(312)
>> stem(n2,x2)
>> subplot(313)
4

>> stem(n,y)
Signal Multiplication
This is a sample by sample multiplication (dot multiplication) given by
{x1(n)}.{x2(n)}={x1(n).x2(n)}
In MATLAB it is implemented by array operator .*. The same restrictions apply for signal multiplication
as for signal addition.
The following MATLAB routine demonstrated these operations
% implement y(n)= x1(n).x2(n)
n=min(min(n1),min(n2)):max(max(n1),max(n2)); %duration of y(n)
y1=zeros(1,length(n)); y2=y1;
%initialization
y1(find((n>=min(n1))&(n<=max(n1))==1))=x1;
% x1 with duration of y(n)
y2(find((n>=min(n2))&(n<=max(n2))==1))=x2;
% x2 with duration of y(n)
y=y1.*y2;
% sequence multiplication
Program 7:
Find

y(n) = x1(n1) * x2(n2)

where

x1(n1)= [-2,-6,-7,5,-1,10,11]
x2(n2)=[0,4,5,6,1,-1,-10,9,5,4,3,2,12]

>> x1=[-2,-6,-7,5,-1,10,11];
>> x2=[0,4,5,6,1,-1,-10,9,5,4,3,2,12];
>> n1=-3:3;
>> n2=-6:6;
>> 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;
>> subplot(311);
>> stem(n1,x1)
>> subplot(312)
>> stem(n2,x2)
>> subplot(313)
>> stem(n,y)

Signal Folding
In this operation each sample of x(n) is flipped around n=0 to obtain a folded sequence
y(n)
y(n) = {x(-n)}
In MATLAB this operation is implemented by fliplr(x) function for sample values and by fliplr(n)
function for sample positions.
Program 8:
Find x(-n) where x(n) = [1,2,5,4,1].
>> n=-1:3;
>> x=[1,2,5,4,1];
>> y=fliplr(x);
>> n1=-fliplr(n);
>> subplot(211);
>> stem(n,x);
>> subplot(212);
>> stem(n1,y);
Signal Scaling
In this operation each sample is multiplied by a scalar .
= {()}
An arithmetic operator * is used to implement the scaling operation in MATLAB.
Program 9
Find 2x(n) where x(n)=[1,2,5,4,1].
>> n=-1:3;
>> x=[1,2,5,4,1];
>> y=2*x;
>> stem(n,y)
Signal Shifting
In this operation each sample of x(n) is shifted by an amount k to obtain a shifted
sequence y(n).
y(n) = {x(n-k)}
If we take m=n-k then n=m+k and the above operation is given by
y(m+k) = {x(m)}
So there is no effect on the vector x, but the vector n is changed by adding k to each element.

Program 10
Find x(n-2) where x(n)=[1,2,5,4,1].
>> n=-1:3;
>> x=[1,2,5,4,1];
>> n1=n+2;
>> subplot(211);
>> stem(n,x)
>> subplot(212);
>> stem(n1,x)
Sample Summation
It adds all sample values of x(n) between n1 and n2.
2

= 1 + (2 )
=1

It is implemented by the sum(x(n1:n2)) function.


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

() = 1 (2 )
1

It is implemented by the prod(x(n1:n2)) function.


Signal Energy
The energy of the sequence x(n) is given by

() =

Where superscript * denotes the operation of complex conjugation. The energy of a finiteduration sequence x(n) can be computed in MATLAB using the following
Program 11
Find energy of x(n) = [0,1,2,2,2].
>> x=[0,1,2,2,2];
>> E=sum(abs(x).^2)

Signal Power
The average power of a periodic sequence with fundamental period N is given by
1
=

Program 12
Find power of x(n) = *,1,0.5,0,1,0.5,0,1,0.5,0,+ having period N=3.
>> x=[1,0.5,0];
>> N=3;
>> P=(sum(abs(x).^2))/N
Even and Odd Signals
A real valued sequence () is called even if
() = ()
Similarly a real valued sequence () is called odd if
= ()
Any arbitrary real valued sequence x(n) can be decomposed into its even and odd components
() = + ()
Where the even and odd parts are given by
=

1
[ + ()]
2

1
[ ()]
2

And

Program 13
Find even and odd parts of x(n)=u(n) for -10 n 10.
>> x1=[zeros(1,10) ones(1,11)];
>> n1=-10:10;
>> x2=fliplr(x1);
>> n2=-fliplr(n1);
>> n=min(min(n1),min(n2)):max(max(n1),max(n2));
>> y1=zeros(1,length(n));
>> y2=zeros(1,length(n));
>> y1(find((n>=min(n1))&(n<=max(n1))==1))=x1;
8

>> y2(find((n>=min(n2))&(n<=max(n2))==1))=x2;
>> ye=(y1+y2)/2;
>> y0=(y1-y2)/2;
>> subplot(311);
>> stem(n,x1)
>> subplot(312)
>> stem(n,ye)
>> subplot(313)
>> stem(n,y0)
Program 14: Let x=[1,2,3,1,4]
Generate and plot x(1-n).
x=[1,2,3,1,4];
n=-2:2;
x1=fliplr(x);
n1=-fliplr(n);
n2=n1+1;
subplot(311);
stem(n,x);
subplot(312);
stem(n1,x1);
subplot(313);
stem(n2,x1);

S-ar putea să vă placă și