You are on page 1of 9

University of Technology

Department of Communication Engineering


Optical Communication Systems Engineering Branch

Basic Operations on Discrete Time Signals

Experiment No.2
Digital Signal Processing Laboratory I

Mousa Saad Luaibi Fourth year

Morning Study Group B


q

Wednesday, October 26, 2022

Tuesday, November 1, 2022

Iraq, Baghdad

coe.19.003@student.uotechnology.edu.iq
Digital Signal Processing Laboratory I 2023-2022 EXP.NO.2: Basic operations on discrete time signals

➢ Aim
Write a program in MATLAB to study the basic operations on the Discrete - time signals.

➢ Theory
1. Signal addition: This is a sample-by-sample addition given by:
{𝑥1 (𝑛)} + {𝑥2 (𝑛)} = {𝑥1 (𝑛) + 𝑥2 (𝑛)}

2. Signal multiplication: This is a sample-by-sample (or “dot”) multiplication) given by:


{𝑥1 (𝑛)} . {𝑥2 (𝑛)} = {𝑥1 (𝑛)𝑥2 (𝑛)}

3. Shifting: In this operation, each sample of 𝑥(𝑛) is shifted by an amount k to obtain a


shifted sequence:
𝑦(𝑛) . 𝑦(𝑛) = {𝑥(𝑛 − 𝑘)}
If we let 𝑚 = 𝑛 − 𝑘, then 𝑛 = 𝑚 + 𝑘 and the above operation is given by:
𝑦(𝑚 + 𝑘) = {𝑥(𝑛 − 𝑘)}

4. Folding: In this operation each sample of 𝑥(𝑛) is flipped around 𝑛 = 0 to obtain a folded
sequence 𝑦(𝑛):
𝑦(𝑛) = {𝑥(−𝑛)}

➢ Procedure
Write the following MATLAB Code in the PC.
1. Signal addition:

n=0:1:3; subplot(221);
x1=[1 2 3 4]; stem(n,x1);
legend('x1(n) signal'); xlabel('Samples(n)');
ylabel('Amplitude(V)'); grid; ylim([0 5]);
subplot(222); x2=[1 1 1 1];
stem(n,x2,'b'); legend('x2(n) signal');
xlabel('Samples(n)'); ylim([0 2]);
ylabel('Amplitude(V)'); grid;
subplot(2,2,[3 4]); y=x1+x2;
stem(n,y,'r'); legend('x2(n) signal');
xlabel('Samples(n)'); ylim([0 6]);
ylabel('Amplitude(V)'); grid;
Digital Signal Processing Laboratory I 2023-2022 EXP.NO.2: Basic operations on discrete time signals

2. Signal multiplication

n1=-2:1:1; subplot(2,2,1); x1=[1 2 3 4]; stem(n1,x1);


legend('x1(n) signal'); xlabel('Samples(n)');
ylabel('Amplitude(V)'); grid; axis([-3 2 0 5 ]);

n2=0:1:3; subplot(2,2,2); x2=[1 1 1 1]; stem(n2,x2);


legend('x2(n) signal'); xlabel('Samples(n)');
ylabel('Amplitude(V)'); grid; axis([-1 4 0 2]);

n3=min(min(n1),min(n2)):1:max(max(n1),max(n2)); %finding the


duration of output signal
subplot(2,2,[3 4]); s1=zeros(1,length(n3)); s2=s1;
s1(find((n3>=min(n1))&(n3<=max(n1))))=x1; % signal x with the
duration of output signal 'y'
s2(find((n3>=min(n2))&(n3<=max(n2))))=x2; % signal y with the
duration of output signal 'y'
y=s1.*s2; %multiplication
stem(n3,y,'r'); legend('y(n) signal'); xlabel('Samples(n)');
ylabel('Amplitude(V)'); grid; axis([-3 4 0 5 ]);
Digital Signal Processing Laboratory I 2023-2022 EXP.NO.2: Basic operations on discrete time signals

3. Shifting

function[y,n]=sigshift(x,m,n0);
n=m+n0; y=x;
end

Input Output
x=[1 2 3 4]; y=[1 2 3 4]
m=0:1:3; n=[2 3 4 5]
n0=2;
[y,n]=sigshift(x,m,n0)
Digital Signal Processing Laboratory I 2023-2022 EXP.NO.2: Basic operations on discrete time signals

4. Folding

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

Input Output
x=[1 2 3 4]; y=[4 3 2 1]
n=0:1:3; n=[-3 -2 -1 0]
[y,n]=sigfold(x,n)

5. Unit impulse

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

Input Output
n1=0; x=[0 0 1 0]
n2=3; n=[0 1 2 3]
n0=2;
[x,n]=imseq(n0,n1,n2)

6. Unit step

function[y,n]=stepseq(n0,n1,n2);
n = m+n0; y = x;
end

Input Output
n1=0; y=[0 0 1 1]
n2=3; n=[0 1 2 3]
n0=2;
[y,n]=stepseq(n0,n1,n2)
Digital Signal Processing Laboratory I 2023-2022 EXP.NO.2: Basic operations on discrete time signals

7. Let x(n) = {1, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, 2, 1}. Determine and plot the following sequences.

a. 𝑥1 (𝑛) = 2𝑥(𝑛 − 5) − 3𝑥(𝑛 + 4).
b. 𝑥2 (𝑛) = 𝑥(3 − 𝑛) + 𝑥(𝑛) 𝑥(𝑛 − 2).

clear all
clc
n=-2:1:10;
x=[1:7,6:-1:1];
[xl1,nl1] = sigshift(x,n,5) ;
[x12,n12] = sigshift(x,n,-4) ;
[x1,n1] = sigadd(2*xl1,nl1,-3*x12,n12);
subplot(2, 1, 1) ;
stem(n1 ,x1)
title('Sequence in Example a')
xlabel('n');
ylabel('x1(n)');
[x21,n21] = sigfold(x,n) ;
[x21,n21] = sigshift(x21,n21,3) ;
[x22,n22] = sigshift(x,n,2) ;
[x22,n22] = sigmult(x,n,x22,n22) ;
[x2,n2] = sigadd(x21,n21,x22,n22);
subplot(2,1,2);
stem(n2,x2) ;
title('Sequence in Example b')
xlabel('n');
ylabel('x2(n)');

➢ Discussion
Generate and plot each of the following sequences over the indicated interval:
1. 𝑥(𝑛) = 2𝛿(𝑛 + 2) − 𝛿(𝑛 − 4), −5 ≤ 𝑛 ≤ 5.

n=-5:1:5; x=2*((n+2)==0)-((n-4)==0);
stem(n,x,'r'); legend('x(n) signal');
xlabel('Samples(n)'); ylim([-1.5 2.5]);
ylabel('Amplitude(V)'); grid;
Digital Signal Processing Laboratory I 2023-2022 EXP.NO.2: Basic operations on discrete time signals

2. 𝑥(𝑛) = 𝑛[𝑢(𝑛) − 𝑢(𝑛 − 10)] + 10𝑒 − 0.3(𝑛 − 10)[𝑢(𝑛 − 10) − 𝑢(𝑛 − 20)], 0 ≤
𝑛 ≤ 20.

n=0:1:20;
x=n.*[(n>=0)-((n-10)>=0)]+10*exp(-0.3*(n-10)).*[((n-10)>=0)-((n-20)>=0)];
stem(n,x); legend('x(n) signal');
xlabel('Samples(n)'); ylim([0 11]);
ylabel('Amplitude(V)'); grid;
Digital Signal Processing Laboratory I 2023-2022 EXP.NO.2: Basic operations on discrete time signals

3. 𝑥(𝑛) = cos(0.04𝜋𝑛) + 0.2𝑤(𝑛), 0 ≤ 𝑛 ≤ 50. Where 𝑤(𝑛) is a Gaussian random


sequence with zero mean and unit variance.

n=0:1:50;
x=cos(0.04*pi*n) +[0.2*randn(1,length(n))];
stem(n,x,'r'); legend('x(n) signal');
xlabel('Samples(n)');
ylabel('Amplitude(V)'); grid;
Digital Signal Processing Laboratory I 2023-2022 EXP.NO.2: Basic operations on discrete time signals

4. 𝑥 ~ (𝑛) = {… ,5, 4, 3, 2 ,1, 5, 4, 3, 2, 1, 5, 4, 3, 2, 1, … }, −10 ≤ 𝑛 ≤ 9.


n=-10:1:9; m=[5,4,3,2,1];
a=m'*ones(1,4); x=(a(:))';
stem(n,x); legend('x(n) signal');
xlabel('Samples(n)');
ylabel('Amplitude(V)'); grid;
axis([-11 10 0 6]);

You might also like