0% found this document useful (0 votes)
226 views

Digital Signal Processing

This document contains MATLAB code that generates and plots various sequences. It includes: 1. Code to generate and plot sequences a-e by manipulating impulse and step sequences. 2. Code that shifts, adds, and multiplies sequences to generate new sequences a-d for problem 2.2. 3. Code to plot periodic and non-periodic cosine sequences for problem 3. 4. Code that shifts, adds, and decomposes into even and odd parts for sequences a-e in problem 4. 5. Code to decompose a complex sequence into real even, real odd, imaginary even, and imaginary odd parts. 6. Code that downsamples sequences and plots the original and

Uploaded by

Engr. Abdullah
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
226 views

Digital Signal Processing

This document contains MATLAB code that generates and plots various sequences. It includes: 1. Code to generate and plot sequences a-e by manipulating impulse and step sequences. 2. Code that shifts, adds, and multiplies sequences to generate new sequences a-d for problem 2.2. 3. Code to plot periodic and non-periodic cosine sequences for problem 3. 4. Code that shifts, adds, and decomposes into even and odd parts for sequences a-e in problem 4. 5. Code to decompose a complex sequence into real even, real odd, imaginary even, and imaginary odd parts. 6. Code that downsamples sequences and plots the original and

Uploaded by

Engr. Abdullah
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Problems ch#02

P 1 :generate and plot the samples using matlab


(a)
n = [0:25];
x0 = zeros(1,26);
for m=0:10,
x0 = x0+ (m-1).*(impseq(2.*m,0,25)-impseq(2.*m+1,0,25));
end
subplot(3,2,1);
stem(n,x0);
title('Sequence a')
output wave form

(b)
n = [-25:25];
x1 = n.^2.*(stepseq(-5,-25,25)-stepseq(6,-25,25)) + 10*impseq(0,-25,25) +
20*0.5.^n.*(stepseq(4,-25,25)-stepseq(10,-25,25));
subplot(3,2,2); stem(n,x1);
title('Sequence b')
outpur waveform:

(c)
n = [0:20];
x2 = 0.9.^n.*cos(0.2*pi.*n + pi/3);
subplot(3,2,3);
stem(n,x2);
title('Sequence c')

output waveform:

(d)
n = [0:100];
x3= 10.*cos(0.008*pi.*n.^2) + randn(size(n));
subplot(3,2,4);
stem(n,x3);
title('Sequence d')

output waveform

(e)
X4 = [2,1,2,3];
X4= x4' * ones(1,5);
X4 = (x4(:))';
n = [0:size(x4)];
subplot(3,2,5);
stem(x4);
title('Sequence e')

output waveform

P 2.2
u = [1,-2,4,6,-5,8,10];
n = [-4:2];
subplot(3,2,1);
stem(n,u);
title('Original Sequence');

output waveform

(a)
[y11,n11] = sigshift(y,n,-2);
[y12,n12] = sigshift(y,n,4);
[y1,n1] = sigadd(3*y11,n11,y12,n12);
[y1,n1] = sigadd(y1,n1,-2*y,n);
subplot(3,2,2);
stem(n1,y1);
title('Sequence a');

output waveform

(b)
[v21,n21] = sigshift(v,n,-5);
[v22,n22] = sigshift(v,n,-4);
[v2,n2] = sigadd(5*v21,n21,4*v22,n22);
[v2,n2] = sigadd(v2,n2,3*v,n);
subplot(3,2,3);
stem(n2,v2);
title('Sequence b');

output waveform

(c)
[m31,n31] = sigshift(m,n,-4);
[m32,n32] = sigshift(m,n,1);
[m33,n33] = sigmult(m31,n31,m32,n32);
[m31,n31] = sigfold(m,n);
[m31,n31] = sigshift(m31,n31,2);
[m32,n32] = sigmult(m31,n31,m,n);
[m3,n3] = sigadd(m33,n33,m32,n32);
subplot(3,2,4);
stem(n3,m3);
title('Sequence c');

output waveform

(d)
[l41,n41] = sigshift(l,n,-2);
l42 = cos(0.1*pi*n);
[l42,n42] = sigmult(l42,n,l41,n41);
l43 = 2*exp(0.5*n);
[l41,n41] = sigmult(l43,n,l,n);
[l4,n4] = sigadd(l42,n42,l41,n41);
subplot(3,2,5);
stem(n4,l4);
title('Sequence d');
output waveform:

(e)
t5 = zeros(size(n));
for k = 1:5,
[t51,n51] = sigshift(t,n,k);
t52 = n.*t51;
t5 = sigadd(t5,n51,t52,n51);
end
subplot(3,2,6);
stem(n51,t5);
title('Sequence e');
output waveform:

Problem 3
(a)
In order for a sequence to be periodic f0 has to be a rational multiple
of 2p, i.e. f0 = (K/N)2p

(b)
n = -20:20;
s= cos (0.3*pi*n);
subplot(2,1,1);
plot(n,s,'ko');
title('cos(0.3\pin)');
xlabel('n');
output waveform

(c)
x 0= cos (0.3*n); % Non-Periodic Sequence
subplot(2,1,2);
plot(n,x0,'ko');
title('cos(0.3n)');
xlabel('n');
output waveform

Problem no:4
t = [1,-2,4,6,-5,8,10];
n = [-4:2];
(a)
[t11,n11] = sigshift(t,n,-2);
[t12,n12] = sigshift(t,n,4);
[t1,n1] = sigadd(3*t11,n11,t12,n12);
[t1,n1] = sigadd(t1,n1,-2*t,n);
[te,to,m] = evenodd(t1,n1);
subplot(2,2,1);
stem(n1,t1);
title('Original Sequence')
xlabel('n');
ylabel('x(n)');
subplot(2,2,2);
stem(m,te);

original signal

title('Even Part')
xlabel('n');
ylabel('te(n)');
subplot(2,2,4);
stem(m,to);
title('Odd Part')
xlabel('n');
ylabel('to(n)');
even part
output waveform

(b)
[x21,n21] = sigshift(x,n,-5);
[x22,n22] = sigshift(x,n,-4);
[x2,n2] = sigadd(5*x21,n21,4*x22,n22);
[x2,n2] = sigadd(x2,n2,3*x,n);
[xe,xo,m] = evenodd(x2,n2);
subplot(2,2,1);
stem(n2,x2);
title('Original Sequence')
xlabel('n');
ylabel('x(n)');
subplot(2,2,2);
stem(m,xe);
title('Even Part')
xlabel('n');
ylabel('xe(n)');
subplot(2,2,4);
stem(m,xo); title('Odd Part')
xlabel('n');
ylabel('xo(n)');

output waveform
original signal:

Even Part:

(c)
[x31,n31] = sigshift(x,n,-4);
[x32,n32] = sigshift(x,n,1);
[x33,n33] = sigmult(x31,n31,x32,n32);
[x31,n31] = sigfold(x,n);
[x31,n31] = sigshift(x31,n31,2);
[x32,n32] = sigmult(x31,n31,x,n);
[x3,n3] = sigadd(x33,n33,x32,n32);
[xe,xo,m] = evenodd(x3,n3);
subplot(2,2,1);
stem(n3,x3);
title('Original Sequence')
xlabel('n');
ylabel('x(n)');
subplot(2,2,2);
stem(m,xe);
title('Even Part')
xlabel('n');
ylabel('xe(n)');
subplot(2,2,4);
stem(m,xo);

Odd part:

title('Odd Part')
xlabel('n');
ylabel('xo(n)');
(d)
[x41,n41] =
sigshift(x,n,-2);
x42 = cos(0.1*pi*n);
[x42,n42] = sigmult(x42,n,x41,n41);
x43 = 2*exp(0.5*n);
[x41,n41] = sigmult(x43,n,x,n);
[x4,n4] = sigadd(x42,n42,x41,n41);
[xe,xo,m] = evenodd(x4,n4);
subplot(2,2,1);
stem(n4,x4);
title('Original
Sequence')
xlabel('n');
ylabel('x(n)');
subplot(2,2,2);
stem(m,xe);
title('Even Part')
xlabel('n');
ylabel('xe(n)');
subplot(2,2,4);
stem(m,xo);
title('Odd Part')
xlabel('n');
ylabel('xo(n)');
(e)
x5 =
zeros(size(n));
for k = 1:5,
[x51,n51] = sigshift(x,n,k);
x52 = n.*x51;
x5 = sigadd(x5,n51,x52,n51);
end
[xe,xo,m] =
evenodd(x5,n51);
subplot(2,2,1);
stem(n51,x5);
title('Original
Sequence')
xlabel('n');

ylabel('x(n)');
subplot(2,2,2);
stem(m,xe);
title('Even Part') =>
xlabel('n');
ylabel('xe(n)');
subplot(2,2,4);
stem(m,xo);
title('Odd Part') =>
xlabel('n');
ylabel('xo(n)');
Problem no 5:
n = 0:10;
x = 10*exp(-j*0.4*pi*n);
[xe, xo, m] = evenoddcomplex(x,n);
subplot(3,1,1);
stem(n,real(x),'ko');
title('Real Original Sequence')
xlabel('n');
ylabel('x(n)');
subplot(3,1,2);
stem(m,real(xe),'ko');
title('Real Even Part')
xlabel('n');
ylabel('xe(n)');
subplot(3,1,3);
stem(m,real(xo),'ko');
title('Real Odd Part')
xlabel('n');
ylabel('xo(n)');
figure(2);
subplot(3,1,1);
stem(n,imag(x),'ko');
title('Imaginary Original Sequence')
xlabel('n');
ylabel('x(n)');
subplot(3,1,2);
stem(m,imag(xe),'ko');
title('Imaginary Even Part')
xlabel('n');
ylabel('xe(n)');
subplot(3,1,3);
stem(m,imag(xo),'ko');
title('Imaginary Odd Part')

xlabel('n');
ylabel('xo(n)');
function [y,m] = dnsample(x,n,M)
index = find (n==0);
x1 = x(1:index);
x1 = fliplr(x1);
y1 = downsample(x1,M);
y1 = fliplr(y1);
temp = size(y1);
m1 = 1:
temp(1,2)-1;
m1 = -fliplr(m1);
x2 = x(index:end);
y2 = downsample(x2,M);
y = [y1(1:end-1),y2];
temp = size(y2);
m2 = 0:
temp(1,2)-1;
m = [m1,m2];

P 2.6
(a)
n = -50:50;
x = sin(0.125*pi*n);
subplot(2,2,1);
plot(n,x,'ko-');
title('Original Signal sin(0.125\pin)');
[y,m] =
dnsample(x,n,4);
subplot(2,2,3);
plot(m,y,'ko-');
title('Down-Sampled Signal by a factor of 4');
axis([m(1),m(end),-1,1]);
(b)
x = sin(0.5*pi*n);
subplot(2,2,2);
plot(n,x,'ko-');
title('Original Signal

sin(0.5\pin)');
[y,m] = dnsample(x,n,4);
subplot(2,2,4);
plot(m,y,'ko-');
title('Down-Sampled Signal by a factor of 4');
axis([m(1),m(end),-1,1]);

Problem no7
clear; clc;
n1 = 0:20;
x1 = 0.9.^n1;
subplot(3,2,1);
stem(n1,x1,'ko');
title('First given sequence x1(n)');
axis([-20 20 0 1]);
[x1f,n1f] = sigfold(x1,n1);
n2 = -20:0;
x2 = 0.8.^(-n2);
subplot(3,2,2);
stem(n2,x2,'ko');
title('Second given sequence x2(n)');
axis([-20 20 0 1]);
[x2f,n2f] = sigfold(x2,n2);
[r,nr] = conv_m(x1,n1,x1f,n1f);
subplot(3,2,3); stem(nr,r,'ko');
title('Auto-Correlation x1(n)*x1(-n)');
[r,nr] = conv_m(x2,n2,x2f,n2f);
subplot(3,2,4);
stem(nr,r,'ko');
title('Auto-Correlation x2(n)*x2(-n)');
[r,nr] = conv_m(x1f,n1f,x2,n2);
subplot(3,1,3);
stem(nr,r,'ko');
title('Cross-Correlation');
first given sequence

Second given sequence

Auto-Correlation x1(n)*x1(-n)

Auto-Correlation x2(n)*x2(-n)

Cross-Correlation

You might also like