Professional Documents
Culture Documents
Dspli 2
Dspli 2
Experiment No.2
Digital Signal Processing Laboratory I
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 (𝑛)}
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
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
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
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]);