You are on page 1of 5

LAB1

Title: Discrete-Time Signals and Operations

Student Name:
Reg. No. :
Date :
Signature :

Objectives:

1) Representing discrete time signals in Matlab.


2) Writing Matlab functions for elementary, impulse & step functions.
3) Applying Signal operations.

Preparation:

A) Create a new folder called LAB1

B) Open Matlab Program and switch the current directory to LAB1

Procedure:

Section 1: Types of sequences

Step 1: Unit sample sequence,


Open a new script file and write

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


n = [n1:n2];
x = [(n-n0) == 0];
end

Save the file in current folder, LAB1, with the name impseq.m

Try the following commands in command window and check the results:
impseq(3,0,10)
impseq(0,0,10)

Q) Write down the output, What do n0, n1, and n2 represent?

Ref: Ingle and Proakis, Digital Signal Processing using Matlab, p.7 Page: 1
Step2: Unit step sequence

Open a new script file and write


function [x,n] = stepseq(n0,n1,n2)
n = [n1:n2];
x = [(n-n0) >= 0];
end

Save the file in current folder, LAB1, with the name stepseq.m

Try the following commands in command window and check the results:

stepseq(3,0,10)
stepseq(5,0,10)

Q) Write down the output

Step3: Sinusoidal sequences


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

Q) Draw the output.

Step4: Random sequences


There are two famous types of random sequences

1) Uniform random sequence, we use Matlab function rand(1,N) which generates N samples between [0,1] of
equal probability.

2) Gaussian random sequence, we use Matlab function randn(1,N) which generates N samples of mean zero
and variance 1.

Generate a Gaussian random sequence of mean = 10, and variance1 = 2, variance2 = 6 , number of
samples = 100

n= [-20:30];
SD1 = sqrt(2); % Standard Deviation
SD2 = sqrt(6); % Standard Deviation
M = 10;
X1 = M + SD1 * randn(1,100);
X2 = M + SD2 * randn(1,100);
Y1= normpdf(n,M,SD1);
Y2= normpdf(n,M,SD2);
subplot(2,2,1); stem(X1);
subplot(2,2,2); stem(X2);
subplot(2,2,3); stem(n,Y1);
subplot(2,2,4); stem(n,Y2);

Ref: Ingle and Proakis, Digital Signal Processing using Matlab, p.7 Page: 2
Q) Compare the figures and give us your note.

Section 2: Sequence Operations

Step1: Signal addition, this is sample by sample addition


Open a new script file and write

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

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


y1 = zeros(1,length(n)); % Initialization of y1
y2 = zeros(1,length(n)); % Initialization of y2
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; % Add x1 and x2

end

Save the file in current folder, LAB1, with the name sigadd.m

Try the following commands in command window and check the results:
x1 = [4 5 3 1 2]
n1 = [-1 0 1 2 3]
x2 = [1 3 2 7 5 2 3]
n2 = [0 1 2 3 4 5 6]
[y,n] = sigadd(x1,n1,x2,n2)

Q) Write down the values of y and n, what do they represent?

Step2: Signal Multiplication, this is sample by sample

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

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


y1 = zeros(1,length(n)); % Initialization of y1
y2 = zeros(1,length(n)); % Initialization of y2
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; % Element Multiply x1 and x2

end

Save the file in current folder, LAB1, with the name sigmult.m

you can apply the same commands in the command window to check the function.

Ref: Ingle and Proakis, Digital Signal Processing using Matlab, p.7 Page: 3
Step3: Signal shifting y(n) = x(n-k) .
In this operation each sample in x(n) is shifted by amount k to obtain shifted sequence y(n)
Please, note that the shifting has no effect on x vector, but the vector n is changed by adding k to each element.

Open a new script file and write


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

end

Save the file in current folder, LAB1, with the name sigshift.m

Try the following commands in command window:

n = 0:100;
Fs = 10;
t = n * (1/fs);
x = cos(2.*pi.*t);
subplot(2,1,1);
stem(n,x);
[y,n] = sigshift(x,n,5); % shift by k = 5
subplot(2,1,2);
stem(n,y);

Q) Describe y(n) with respect to x(n) in time domain?

Step4: Signal Folding, in this operation each sample of x(n) is flipped around n = 0 to obtain a folded sequence
y(n).

Open a new script file and write


function [y,n] = sigfold(x,n)
y = fliplr(x);
n = -fliplr(n);
end

Save the file in current folder, LAB1, with the name sigfold.m

Try the following commands in command window:

x = [0 1 2 3 4 5];
n = [0 1 2 3 4 5];
stem(n,x);
hold on;
[y,n] = sigfold(x,n);
stem(n,y)
axis([-5 5 0 10]);

Ref: Ingle and Proakis, Digital Signal Processing using Matlab, p.7 Page: 4
Q) Describe y(n) with respect to x(n) in time domain?

Conclusion:

Homework:
After you have completed LAB1, you had the following Matlab function in LAB1 folder:
(impseq, stepseq, sigadd, sigmult, sigshift, and sigfold), use them to obtain the following signals:

Let x(n) = [1,2,3,4,5,6,7,6,5,4,3,2,1] , determine and plot the following sequences

a) x1(n) = 2 x(n – 5) – 3 x(n + 4)

b) x2(n) = 3 x(3 – n) + x(n) x(n – 2)

Ref: Ingle and Proakis, Digital Signal Processing using Matlab, p.7 Page: 5

You might also like