0% found this document useful (0 votes)
56 views15 pages

Digital Signal Processing Laboratory: Experiment No.3

The document describes an experiment using MATLAB to plot and perform basic operations on discrete time signals. It defines common discrete time signals like impulse, step, exponential and sinusoidal sequences. It also discusses representing sequences in MATLAB and functions for generating periodic and random signals. The document provides MATLAB programs to add, multiply, scale, shift and fold discrete time signals. Screenshots of sample outputs from the programs are included with a discussion.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views15 pages

Digital Signal Processing Laboratory: Experiment No.3

The document describes an experiment using MATLAB to plot and perform basic operations on discrete time signals. It defines common discrete time signals like impulse, step, exponential and sinusoidal sequences. It also discusses representing sequences in MATLAB and functions for generating periodic and random signals. The document provides MATLAB programs to add, multiply, scale, shift and fold discrete time signals. Screenshots of sample outputs from the programs are included with a discussion.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Digital Signal Processing Laboratory

Plotting and Basic Operations of Discrete Time Signals using MATLAB


Experiment No.3

Score

Exceeds Meets Needs


CRITERIA Unsatisfactory
Expectations Expectations Improvement
Completeness
20 15 10 5
Accuracy of
20 15 10 5
Results
Observation /
20 15 10 5
Conclusion
Spelling and
20 15 10 5
Grammar
Presentation and
20 15 10 5
Format

Submitted by
Disa, Mar Jose P.
10:30-1:30 PM / Fri / CL14

Submitted to
Engr. Giel G. Mad
Instructor

Date Performed
February 7, 2020

Date Submitted
February 14, 2020

Experiment No. 3
Plotting and Basic Operations of Discrete Time Signals using MATLAB
I. OBJECTIVES
The objectives of this experiment, using the MATLAB software, are for the students:
1. To learn how to plot the standard discrete time signals.
2. To plot periodic and aperiodic sinusoidal discrete time signals.
3. To perform basic operations on discrete time signals.

II. MATERIALS REQUIRED


Computer With MATLAB installed

III. DISCUSSION

In MATLAB we can represent a finite-duration sequence be a row vector of appropriate values.


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. For example, a sequence
x(n) = {2, 1, -1, 0, 1, 4, 3, 7} can be represented in MATLAB by

>> n = [ -3, -2, -1, 0, 1, 2, 3, 4]
>> x = [2, 1, -1, 0, 1, 4, 3, 7]
Generally, we will use the x-vector representation alone when the sample position information is
not required or when such information is trivial (e.g. when the sequence begins at n = 0). An
arbitrary infinite-duration sequence cannot be represented in MATLAB due to the finite memory
limitations.

BASIC SEQUENCES
We use several elementary sequences in digital signal processing for analysis purposes. Those are
given below.
(1) Unit sample sequence
 1, n  0 ..., 0, 0, 1, 0, 0, ...
 ( n)    
0, n0   
In MATLAB the function zeros(1,N) generates a row vector of N zeros, which can be used to
implement δ(n)overafiniteinterval.How- ever, the logical relation n==0 is an elegant way of
implementing δ(n).
(2) Unit step sequence
 1, n  0 ..., 0, 0, 1, 1, 1, ...
u (n )    
0, n0   
In MATLAB the function ones(1,N) generates a row vector of N ones. It can be used to generate u(n)
overafiniteinterval.Onceagainan elegant approach is to use the logical relation n>=0.
(3) Real-valued exponential sequence

In MATLAB an array operator “.^” is required to implement a real exponential sequence.


(4) Complex-valued exponential sequence

x(n)  e (  j0 ) n , n

where σ produces an attenuation (if <0) or amplification (if >0) and ω0 is the frequency in radians.
A MATLAB function exp is used to generate exponential sequences.

OWEN 2
(5) Sinusoidal sequence
x( n)  cos( 0 n   ), n

where A is an amplitude and θ is the phase in radians. A MATLAB function cos (or sin) is used to
generate sinusoidal sequences.
(6) Random signal

Many practical sequences cannot be described by mathematical expressions like above. These
sequences are called random (or stochastic) sequences and are characterized by parameters of the
associated proability density functions or their statistical moments. In MATLAB, the funcion
rand(1, N) generates a length N random sequences whose elements are uniformly distributed
between [0, 1]. The function randn(1, N) generates a length N Gaussian random sequence with
mean 0 and variance 1.
(7) Periodic Sequence

A sequence x(n) is periodic if x(n)=x(n + N), ∀n. The smallest integer N that satisfies this relation is
called the fundamental period. We will use ˜ x(n) to denote a periodic sequence. To generate P
periods of ˜ x(n) from one period{x(n), 0 ≤ n ≤ N−1},we can copy x(n) P times:

But an elegant approach is to use MATLAB’s powerful indexing capabilities. First we generate a
matrix containing P rows of x(n)values. Then we can concatenate P rows into a long row vector
using the construct (:). However, this construct works only on columns. Hence we will have to use
the matrix transposition operator ’ to provide the same effect on rows.

OPERATIONS ON SEQUENCES

1. Signal addition: This is a sample-by-sample addition given by


{x1(n)}+{x2(n)} = {x1(n)+x2(n)}
It is implemented in MATLAB by the arithmetic operator “+”. However, the lengths of x1(n) and
x2(n) must be thesame. If 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 first augment
x1(n) and x2(n) so that they have the same position vector n (and hence the same length). This
requires careful attention to MATLAB’s indexing operations. In particular, logical operation of
intersection “&”, relational operations like “<=” and “==”, and the find function are required to make
x1(n) and x2(n) of equal length.
2. Signal multiplication: This is a sample-by-sample (or “dot”) multiplication) given by
{x1(n)}·{x2(n)} = {x1(n)x2(n)}
It is implemented in MATLAB by the array operator .*. Once again, the similar restrictions apply for
the .* operator as for the + operator. Therefore we have developed the sigmult function, which is
similar to the sigadd function.
3. Scaling: In this operation each sample is multiplied by a scalar α.
α{x(n)} = {αx(n)}
An arithmetic operator (*) is used to implement the scaling operation in MATLAB.

OWEN 3
4. 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 let m = n−k, then n = m+k and the above operation is given by y(m + k)={x(m)} Hence this
operation has no effect on the vector x, but the vector n is changed by adding k to each element.
This is shown in the function sigshift.
5. 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 as shown in the sigfold function.

IV. PROCEDURES
Create the below functions/scripts in MATLAB and provide appropriate input (at least 2) to
view and verify their output.

PROGRAM 1

PROGRAM 2

PROGRAM 3

PROGRAM 4

PROGRAM 5

PROGRAM 6

OWEN 4
PROGRAM 7

PROGRAM 8

V. RESULTS:

PROGRAM 9

PROGRAM 10

Screenshot of Output Discussion

OWEN 5
(code and plot)
Program 1
M-File: Program 1 is a function that
function [x,n] = impseq( n0,n1,n2 )
outputs range of values of a
n = (n1:n2); discrete-time unit impulse
x = ((n-n0) == 0); function. The parameters of the
stem(n,x); function include n0, n1, and n2.
end
n0 pertains to which point the
impulse signal will occur. n1
pertains to the minimum of the
range while n2 pertains to the
maximum of the range

Program 2
M-File:
function [x,n] = stepseq(n0,n1,n2) Program 2 is a function that
n = [n1:n2];
x = [(n-n0) >= 0]; outputs range of values of a
stem (n,x); discrete-time unit step function.
end The parameters of the function
include n0, n1, and n2.
n0 pertains to which point the
step signal will start. n1 pertains
to the minimum of the range
while n2 pertains to the
maximum of the range

OWEN 6
Program 3
M-File:
Program 3 is a function that
function[x,n] = xseq (n0,n1,n2)
n = [0:10];
outputs a range of values of a
x = (0.9).^n; real-exponential discrete signal.
stem(n,x); The plot of this program is a
end downward real exponential
signal.

function[x,n] = xseq (n0,n1,n2)


n = [0:5];
x = (0.5).^n;
stem(n,x);

Program 4
function[x,n] = yseq (n0,n1,n2)
n = [0:10]; Program 4 is a function that
x = exp((2+3j)*n);
stem(n,x); outputs a range of values of a
grid 'ON'; discrete-time complex
end exponential signal function. The
plot of this program is a complex
exponential sequence.
OWEN 7
Program 5
function[x,n] = zseq (n0,n1,n2)
n = [0:10]; Program 5 is a function that
x = 3*cos(0.1*pi*n+pi/3) +
2*sin(0.5*pi*n); outputs a range of values of a
stem(n,x); discrete-time sinusoidal signal
end function. The plot of this program
is a sinusoid signal.

function[x,n] = zseq (n0,n1,n2)


n = [1:5];
x = 7*cos(0.1*pi*n+pi/3) +
5*sin(0.5*pi*n);
stem(n,x);
end

OWEN 8
Program 6
n=[0:10]; x=(n.^3)/3; Program 6 is has a variable xtilde
>> xtilde = x' * ones(1,5); which holds a sequence of a
xtilde = xtilde(:);
xtilde = xtilde'; discrete-time periodic signal. The
stem(xtilde) second parameter of the function
‘ones()’ determine the number of
periods in the signal

>> xtilde = x' * ones(1,8);


xtilde = xtilde(:);
xtilde = xtilde';
>> stem(xtilde)

Program 7
function[y,n] = sigadd(x1,n1,x2,n2)
n = Program 7 shows the sequence of
min(min(n1),min(n2)):max(max(n1),max(n2));
y1 = zeros(1,length(n)); y2 = y1; a discrete-time function of a step-
y1(find((n>=min(n1))&(n<=max(n1))==1))=x1; by-step signal addition. x1and x2
y2(find((n>=min(n2))&(n<=max(n2))==1))=x2; pertain to the array elements to
y = y1+y2; be inputted into the function
end
while n1 and n2 pertains to the
lengths of x1 and x2 respectively.

OWEN 9
>> n1=[1:50]; x1=n1;
n2=[1:50]; x2=sin(n2);
>> stem(sigadd(x1,n1,x2,n2))

>> n1=[1:50]; x1=log(n1)/10;


>> n2=[1:50]; x2=-(0.9).^n2;
>> stem(sigadd(x1,n1,x2,n2))
Program 8
function[y,n] = sigmult(x1,n1,x2,n2) Program 8 shows the sequence of
n =
min(min(n1),min(n2)):max(max(n1),max(n2));
a discrete-time function of a step-
y1 = zeros(1,length(n)); y2 = y1; by-step signal multiplication.
y1(find((n>=min(n1))&(n<=max(n1))==1))=x1; x1and x2 pertain to the array
y2(find((n>=min(n2))&(n<=max(n2))==1))=x2; elements to be inputted into the
y = y1.*y2;
function while n1 and n2 pertains
end
to the lengths of x1 and x2
>> n1=[1:50]; x1=n1; respectively.
>> n2=[1:50]; x2=sin(n2); Both functions sigadd and
>> stem(sigmult(x1,n1,x2,n2)) sigmult operate per step.
However, sigmult does not mean
multiplication as per expression
or convolution. Given two similar
equations given as ‘x’, the sigmult
of the functions is not x2. Only the
individual values of the steps of
each respective functions is
multiplied.

OWEN 10
>> [x1,n1] = stepseq(25,0,50);
>> n2=[0:50]; x2=n2;
>> stem(sigmult(x1,n1,x2,n2))

Program 9
function [y,n] = sigshift(x,m,k)
n = m+k; y=x; Program 9 exhibits a function
end
that shifts the signal by the value
>> n=[0:10]; x=[2:12]; of a constant ‘k’ given that the
>> [y1,n1] = sigshift(x,n,4); signal x matches with the interval
>> stem(n1,y1) sample n. A positive k moves the
sample intervals to the right by k
while shifts to the left if k is
negative.

OWEN 11
>> n=[0:7]; x=[0 5 5 0 5 0 5 0];
>> [y1,n1] = sigshift(x,n,-10);
>> stem(n1,y1)

Program 10
function [y,n] = sigfold(x,n)
y = fliplr(x); n=-fliplr(n); Program 10 exhibits a function
end
>> n=[0:20]; x=[0:20]; that flips/folds the domain of the
>> [y,n1]=sigfold(x,n); discrete-time signal sample from
>> stem(n,x) the origin to the maxima. Such
that a function starting from 0 to
n will be flipped / folded to a
domain of 0 to n

OWEN 12
>> stem(n1,y)

VI. SUPPLEMENTARY PROBLEMS

Program Code Screenshot Output Screenshot and Plot

a.

>> n = [-5:5];
>> x = 2*impseq(-2,-5,5)-impseq(4,-
5,5);
>>stem(n,x);
>> xlabel('n');
>> ylabel('x(n)')

OWEN 13
b.

n = [0:20];
>> x1 = n.*(stepseq(0, 0,20)-
stepseq(20,0,20));
>> x2 = 10*exp(-0.3*(n-
10)).*(stepseq(10,0,20)stepseq(20,0,
20));
>> x = x1+x2;
>> stem(n,x);
>> xlabel('n');
>>ylabel('x(n)')

c.
>> n = [-10:9];
>> x = [5,4,3,2,1];
>> xtilde=x'*ones(1,4);
>> xtilde=xtilde(:);
>> stem(n,xtilde);
>> xlabel('n');
>> ylabel('xtilde(n)');

d.

>> n = [-2:10];
>> x=[1:7,6:-1:1];
>> [xa,na]=sigshift(x,n,5);
>> [xb,nb]=sigshift(x,n,-4);
>> [x1,n1]=sigadd(2*xa,na,-3*xb,nb);
>> stem(n1,x1);
>> xlabel('n');
>> ylabel('x1(n)');

e.

>> n = [-2:10];
>> x=[1:7,6:-1:1];
>> [xa,na]=sigshift(x,n,5);
>> [xb,nb]=sigshift(x,n,-4);
>> [x1,n1]=sigadd(2*xa,na,-3*xb,nb);
>> stem(n1,x1);
>> xlabel('n');
>> ylabel('x1(n)');
>> n = [-2:10];
>> x= [1:7,6:-1:1];
>> [xa,na]=sigfold(x,n);
>> [xa,na]=sigshift(xa,na,3);
>> [xb,nb]=sigshift(x,n,2);
>> [xb,nb]=sigmult(x,n,xb,nb);
>> [x2,n2]=sigadd(xa,na,xb,nb);

OWEN 14
>> stem(n2,x2);
>> xlabel('n');
>> ylabel('x2(n)');

VII. ANALYSIS / OBSERVATION:


There are several applications in this experiment that can be performed for
discrete-time signal plotting in MATLAB. Various signal operations can be performed
and interpreted using functions in the time domain. Such as unit-impulse definition and
step-sequences; real and complex exponential signals; sinusoids; and periodic signals.
Time domain equation operations such as signal addition can also be performed.

VIII. CONCLUSION:
The syntax for creating discrete-time models is similar to that for continuous-
time models, except that you must also provide a sample time. using discrete time
functions and various operations, MATLAB can be used to express, evaluate, and
manipulate time-domain functions through signal analysis. The syntax for
creating discrete-time models is similar to that for continuous-time models, except that
you must also provide a sample time 

OWEN 15

You might also like