You are on page 1of 7

NAME AMJAD ALI

STUDENT ID 9449

DSP FINAL LAB

QUESTION NO 1:

Matlab code;

Part a:
%task 1 convolution between two signals
clc;
clear all;
close all;
n=-10:10;
x=(2.^n).*(((n+3)>=0)-((n-7)>=0));%input signal x[n]
h=(3.^n).*(((-n+3)>=0)-((-n-3)>=0));%input signal h[n]
y=conv(x,h)%convolution to find y[n]
length(y);%length of y
n1=0:length(y)-1;
subplot(3,1,1)
stem(n,x)
title('input x[n]')
subplot(3,1,2)
stem(n,h)
title('input h[n]')
subplot(3,1,3)
stem(n1,y)
title('convolved signal
y[n]')
xlabel('time index n')
ylabel('amplitude')
question 1 part b:
%question 1 (b) cross coraltion between two signals
clc;
clear all;
close all;
n=-10:10;
x=(2.^n).*(((n+3)>=0)-((n-7)>=0));%input signal x[n]
h=(3.^n).*(((-n+3)>=0)-((-n-3)>=0));%input signal h[n]
y=xcorr(x,h)%cross corelation
length(y);%length of y
n1=0:length(y)-1;
subplot(3,1,1)
stem(n,x)
title('input x[n]')
subplot(3,1,2)
stem(n,h)
title('input h[n]')
subplot(3,1,3)
stem(n1,y)
title('signal y[n]')
xlabel('time index n')
ylabel('amplitude')
QUESTION NO 2:
%QUESTION 2 PART A
%Using MATLAB find out the “Linearity” of the system.
clc;
n=0:40;
a1=2;
a2=-3;
x1=cos(2*pi*.1*n);%SIGNAL 1
x2=cos(2*pi*.5*n);%SIGNAL 2
x=(a1*x1)+(a2.*x2);
num = [0.2 -0.9 0.1];%Cofficient of x
den = [1 0.4 0.1];%cofficient of y
y1 = filter(num,den,x1); % Compute the output y1[n]
y2 = filter(num,den,x2); % Compute the output y2[n]
yt = a1*y1 + a2*y2;
y = filter(num,den,x); % Compute the output y[n]
d = y - yt; % Compute the difference output d[n]
% Plot the outputs and the difference signal
subplot(3,1,1)
stem(n,y);
ylabel('Amplitude');
title('Output Due to
Weighted Input');
subplot(3,1,2)
stem(n,yt);
ylabel('Amplitude');
title('Weighted
Output');subplot(3,1,3)
stem(n,d);
xlabel('Time index n');
ylabel('Amplitude');
title('Difference
Signal')
QUESTION 2 PART B;
%QUESTION 2 PART B
%Using MATLAB find out the “Time-Invariance” of the system.
clc;
n=0:40;
a1=2;
x1=cos(2*pi*.1*n);%sinal
x2=a1*x1.*(n>=10);%delayed signal
num = [0.2 -0.9 0.1];%Cofficient of x
den = [1 0.4 0.1];%cofficient of y
y1 = filter(num,den,x1);
% Plot the outputs and the difference signal
subplot(2,1,1)
stem(n,y1);
ylabel('Amplitude');
title('Output y[n]');
subplot(2,1,2)
stem(n,y);
xlabel('Time index n');
ylabel('Amplitude');
title('output due to delayed input');
QUESTION NO 3:
%Apply Bit plane slicing on the following image and then recover
the original image.
clc;
clear all;
close all;
% read an image
I= imread('image.png');
a=rgb2gray(I);
% Read image size
[m,n] = size(a);
% convert the image class from "uint8" to double
b = double(a);
% convert each pixel into binary using matlab command "de2bi"
c = de2bi(b);
% calling the LSB Bit of each pixel
c1 = c(:,1);
% "reshape"
r1 = reshape(c1,m,n);
% 2nd Bit plane
c2 = c(:,2);
r2 = reshape(c2,m,n);
% 3rd Bit Plane
c3 = c(:,3);
r3 = reshape(c3,m,n);
% 4th Bit Plane
c4 = c(:,4);
r4 = reshape(c4,m,n);
% 5th Bit Plane
c5 = c(:,5);
r5 = reshape(c5,m,n);
% 6th Bit Plane
c6 = c(:,6);
r6 = reshape(c6,m,n);
% 7th Bit Plane
c7 = c(:,7);
r7 = reshape(c7,m,n);
% 8th Bit Plane
c8 = c(:,8);
r8 = reshape(c8,m,n);
% Displaying all the Bit Planes
subplot(341);
imshow(r1);title('LSB Bit Plane');
subplot(342);
imshow(r2);title('2nd Bit Plane');
subplot(343);
imshow(r3);title('3rd Bit Plane');
subplot(344);
imshow(r4);title('4th Bit Plane');
subplot(345);
imshow(r5);title('5th Bit Plane');
subplot(346);
imshow(r6);title('6th Bit Plane');
subplot(347);
imshow(r7);title('7th Bit Plane');
subplot(348);
imshow(r8);title('MSB Bit Plane');
subplot(349);
imshow(I);title('ORIGINAL image');
subplot(3,4,10);
imshow(a);title('Gray image');
QUESTION NO 4:
clc;
clear all;
close all;
f=50;%frequency
nCyl=10;
t=0:0.0000002:nCyl/f;
x=3*cos(25*pi*t)+10*sin(30*pi*t)-cos(100*pi*t);
plot(t,x,'lineWidth',2)
hold on
fs1=2*f;%sampled frequency
t1=0:1/fs1:nCyl/f;
xn=3*cos(25*pi*t1)+10*sin(30*pi*t1)-cos(100*pi*t1);
stem(t1,xn,'r');
title('frequency=50Hz & sampled Frequency=100Hz');
xlabel('Time');
ylabel('Amplitude');

You might also like