Digital Signal Processing Laboratory: Experiment No.3
Digital Signal Processing Laboratory: Experiment No.3
Score
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.
III. DISCUSSION
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, n0
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, n0
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
x(n) e ( j0 ) 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
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
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.
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.
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
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))
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)
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)');
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