0% found this document useful (0 votes)
21 views19 pages

Lesson 11111

notes

Uploaded by

Maria Kapiya
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)
21 views19 pages

Lesson 11111

notes

Uploaded by

Maria Kapiya
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/ 19

Discrete-Time Signals and Systems

MATLAB
T.N.Kaputu

1
How to Represent a Discrete Time Signal in MATLAB

• The correct representation of a discrete time signal in


Matlab takes 2 vectors

• One vector is used to indicate the location of the time samples

• The other vector is used to indicate the amplitude (value) of


the signal at the corresponding temporal locations

Example: 𝑥 𝑛 = {2,1, −1, 0, 1,4,3,7}

>> n=[-3,-2,-1,0,1,2,3,4];

x=[2,1,-1,0,1,4,3,7];
2
TYPES OF SEQUENCES

Unit sample sequence:

n = -10:10; % create a vector of sample indices


x = (n==0); % creates a ’1’ where n=0, and a ’0’ elsewhere
stem( n, x ); % display using a stem diagram
xlabel( ’n’ ); % mark the x-axis
ylabel( ’x[n]’ ); % mark the y-axis

3
TYPES OF SEQUENCES

Unit sample sequence:

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];

4
TYPES OF SEQUENCES

Unit step sequence:

n = -10:10; % create a vector of sample indices


x = (n>=0); % creates a ’1’ where n>=0, and a ’0’ elsewhere
stem( n, x ); % display using a stem diagram
xlabel( ’n’ ); % mark the x-axis
ylabel( ’x[n]’ ); % mark the y-axis

5
TYPES OF SEQUENCES

Unit step sequence:

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];

6
TYPES OF SEQUENCES

Real-valued exponential sequence:

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

7
TYPES OF SEQUENCES

Sinusoidal sequence:

example

3 cos 0.1 𝜋𝑛 + 𝜋/3 + 2sin(0.5𝜋𝑛)

n = [0:10];
x = 3*cos(0.1*pi*n+pi/3) + 2*sin(0.5*pi*n);
stem(n,x)

8
OPERATIONS ON SEQUENCES

Another basic operation is to add up a sequence of consecutive samples. For


instance, we could add samples with time indices ranging from 𝑛1 𝑎𝑛𝑑 𝑛2 as
follows

𝑛2

𝑦 𝑛 = ෍ 𝑥 𝑛1 + 𝑥 𝑛1 + 1 + ⋯ + 𝑥 𝑛2
𝑛=𝑛1

Note that the result of this type of sample summation is a scalar value, not a
signal (i.e. the time index is lost since we’ve summed over it). Summation is
performed in Matlab using the “sum” function. For instance

y=sum(x)

will give the sum of all the samples stored in the vector x

9
OPERATIONS ON SEQUENCES
Signal addition:
This is a sample-by-sample addition given by
𝑥1 𝑛 + 𝑥2 𝑛 = 𝑥1 𝑛 + 𝑥2 𝑛
function [y,n] = sigadd(x1,n1,x2,n2)

% implements y(n) = x1(n)+x2(n)


% [y,n] = sigadd(x1,n1,x2,n2)
% y = sum sequence over n, which includes n1 and n2
% x1 = first sequence over n1
% x2 = second sequence over n2 (n2 can be different from n1)

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
y2(find((n>=min(n2))&(n<=max(n2))==1))=x2; % x2 with duration of y
y = y1+y2; % sequence addition
10
OPERATIONS ON SEQUENCES

It is implemented in MATLAB by the


Signal multiplication
array operator .*.
𝑥1 𝑛 ∙ 𝑥2 𝑛 = 𝑥1 𝑛 ∙ 𝑥2 𝑛

function [y,n] = sigmult(x1,n1,x2,n2)

% implements y(n) = x1(n)*x2(n)


% [y,n] = sigmult(x1,n1,x2,n2)
% y = product sequence over n, which includes n1 and n2
% x1 = first sequence over n1
% x2 = second sequence over n2 (n2 can be different from n1)

n = min(min(n1),min(n2)):max(max(n1),max(n2)); % duration of y(n)


y1 = zeros(1,length(n));
y2 = y1;
y1(find((n>=min(n1))&(n<=max(n1))==1))=x1; % x1 with duration of y
y2(find((n>=min(n2))&(n<=max(n2))==1))=x2; % x2 with duration of y
y = y1 .* y2; % sequence multiplication
11
OPERATIONS ON SEQUENCES

Scaling:
In this operation each sample is multiplied by a scalar α.
𝛼 𝑥𝑛 = 𝛼{𝑥(𝑛)}

12
OPERATIONS ON SEQUENCES
Shifting
In this operation, each sample of 𝑥(𝑛) is shifted by an amount 𝑘 to obtain a
shifted sequence 𝑦(𝑛).

𝑦[𝑛] = {𝑥(𝑛 − 𝑘)}

If we let 𝑚 = 𝑛 − 𝑘, then 𝑛 = 𝑚 + 𝑘 and the above operation is given by

𝑦 𝑚 + 𝑘 = {𝑥(𝑚)}

function [y,n] = sigshift(x,m,k)

% implements y(n) = x(n-k)


% [y,n] = sigshift(x,m,k)

n = m+k;
y = x;
13
OPERATIONS ON SEQUENCES

Folding:
In this operation each sample of x(n) is flipped around n = 0
to obtain a folded sequence y(n)

𝑦 𝑛 = {𝑥(−𝑛)}

In MATLAB this operation is implemented by fliplr(x) function for sample values


and by -fliplr(n) function for sample positions as shown in the sigfold function

function [y,n] = sigfold(x,n)


% implements y(n) = x(-n)
% -----------------------
% [y,n] = sigfold(x,n)
%
y = fliplr(x); n = -fliplr(n);

14
Exercise 1

Generate and plot each of the following sequences over the indicated interval.

15
Exercise 2

16
Homework

17
Homework

18
19

You might also like