You are on page 1of 14

qwertyuiopasdfghjklzxcvbnmqwe

rtyuiopasdfghjklzxcvbnmqwertyui
opasdfghjklzxcvbnmqwertyuiopa
sdfghjklzxcvbnmqwertyuiopasdfg
M.Bilal Junaid
hjklzxcvbnmqwertyuiopasdfghjklz
Assignment :2
xcvbnmqwertyuiopasdfghjklzxcvb
nmqwertyuiopasdfghjklzxcvbnmq
wertyuiopasdfghjklzxcvbnmqwert
yuiopasdfghjklzxcvbnmqwertyuio
pasdfghjklzxcvbnmqwertyuiopas
dfghjklzxcvbnmqwertyuiopasdfgh
jklzxcvbnmqwertyuiopasdfghjklzx
cvbnmqwertyuiopasdfghjklzxcvb
nmqwertyuiopasdfghjklzxcvbnmq
wertyuiopasdfghjklzxcvbnmqwert
yuiopasdfghjklzxcvbnmrtyuiopas
MS(EE)-SP014-014

Advance Digital Signal Processing

Answer 1
function q1()
A = [ 0.05 0.10 0.15 0.20 0.25 0.30 0.35 0.40 0.45 0.50 0.55 0.60 0.65 0.70 0.75 0.80 0.85 0.90 0.95 1];
B = [ 0.309 0.587 0.809 0.951 1 0.951 0.809 0.587 0.309 1.224e-16 -0.309 -0.587 -0.809 -0.951 -1 -0.951 -0.809 -0.587 -0.309
-2.449e-16];
%personal Convolution Function-------------------------A_new=A
B_new=B
m=length(A_new);
n=length(B_new);
A_new=[A_new,zeros(1,n)];
B_new=[B_new,zeros(1,m)];
for i=1:n+m-1
y(i)=0;
for j=1:m
if(i-j+1>0)
y(i)=y(i)+A_new(j)*B_new(i-j+1);
end
end
end
%-----------------------------------------------------------%Matlab Convolution Function--------------------c=conv(A,B)
%-----------------------------------------------figure(1)
subplot(2,2,1)
plot(A)
title('Singal A')
subplot(2,2,2)
plot(B)
title('Singal B')
% Showing My convolution and Matlab convolution results are same.
subplot(2,2,3)
plot(y)
title('Singal C=A*B Using Personal Convolution Function')
subplot(2,2,4)
plot(y)
title('Singal C=A*B Using Matlab Convolution Function')
%-------------------------------------------------------N=128;
Fsmax=20 %assiming sampling at 20Mhz
%taking fft with window size 128
ffta=fft(A,N);
fftb=fft(B,N);
fftc=fft(y,N);
&--------------------------------%output of Frequency Response
figure(2)
subplot(2,2,1)
plot([-N/2:N/2-1]*Fsmax/N,fftshift(abs(ffta)))
title('Frequency Response of Singal A')

subplot(2,2,2)
plot([-N/2:N/2-1]*Fsmax/N,fftshift(abs(fftb)))
title('Frequency Response of Signal B')
subplot(2,2,[3 4])
plot([-N/2:N/2-1]*Fsmax/N,fftshift(abs(fftc)))
title('Frequency Response of Singal C')
%----------------------------------end

Output of Question 1

Answer 2

%loading both images--------------load image


load image_pattern
%----------------------------------%plotting image Pattern and Image--------figure(1)
subplot(1,2,1)
imshow(image_pattern)
title('Image Pattern','FontSize',12,'Color','r')
subplot(1,2,2)
imshow(image)
title('Image','FontSize',12,'Color','r')
%-------------------------------------%cross correlation between image pattern and image to find where image
%pattern is most likely to exist in image
c = normxcorr2(image_pattern,image);
%-----------------------------------------% finding top left coordinates of rectangular image pattern in image
[max_c, imax] = max(abs(c(:)));
[ypeak, xpeak] = ind2sub(size(c),imax(1));
corr_offset = [(xpeak-size(image_pattern,2))
(ypeak-size(image_pattern,1))];
xoffset = corr_offset(1);
yoffset = corr_offset(2);
xbegin = xoffset+1;
xend = xoffset+ size(image_pattern,2);
ybegin = yoffset+1;
yend = yoffset+size(image_pattern,1);
%-----------------------------------------------------------------------% extract image pattern from image from the results of cross corelation and compare to already provided image pattern
extracted_image = image(ybegin:yend,xbegin:xend,:);
if isequal(image_pattern,extracted_image)
disp('Huraah!! Success:: Image Pattern was extracted from Image')
%top Left cooridnates of image pattern
X_coordinate_topleft_of_pattern = xoffset;
Y_coordinate_topleft_of_patten = yoffset;
%------------------------------------------%Center Coordiantes of image Pattern
X_coordinate_center_of_pattern = xoffset + size(image_pattern,2)/2;
Y_coordinate_center_of_pattern = yoffset + size(image_pattern,1)/2;
%-------------------------------------------------------------------

%plotting already provided image pattern and Recovered image to check


%Image match was correct or not

figure,
subplot(1,2,1)
imshow(image_pattern)%image pattern provided
title('Image pattern provided','FontSize',12,'Color','r')
subplot(1,2,2)
imshow(extracted_image) %recovered image from cross corelation
title('Image pattern recovered image from cross corelation','FontSize',12,'Color','r')
%---------------------------------------------------------------end
%showing extracted image pattern over Orignal Image.
recovered_image = uint8(zeros(size(image)));
recovered_image(ybegin:yend,xbegin:xend,:) = image_pattern;
[m,n,p] = size(image);
mask = ones(m,n);
i = find(recovered_image(:,:,1)==0);
mask(i) = .6; % adding Transperancy
% overlay images with transparency
figure, imshow(image(:,:,1))
hold on
h = imshow(recovered_image);
title('Masked Recovered image over orignal image','FontSize',12,'Color','r')
set(h,'AlphaData',mask)
%-----------------------------------------%Displaying Coordiantes---------------------------------fprintf('Coordinate are : \n\n')
fprintf('X_coordinate(Top-left) = %f\n',X_coordinate_topleft_of_pattern);
fprintf('Y_coordinate(Top-left) = %f\n',Y_coordinate_topleft_of_patten);
fprintf('X_coordinate(Center) = %f\n',X_coordinate_center_of_pattern);
fprintf('Y_coordinate(Center) = %f\n',Y_coordinate_center_of_pattern);
%----------------------------------------------------------------

Output Of Question 2

Answer 3
load shifted_ocr
%View from Imview to find exact pixels and size to extract one character
%from OCR diagram.
imview(shifted_ocr)
%cropping character 'C' from original image
shifter_ocr_block = imcrop(shifted_ocr,[48 36 15 18]);
%showing cropped image
figure,imshow(shifter_ocr_block)
title('Extracted Block of character from Orignal Image','Color','r')
%cross corelation with extracted block and orignal image
cc = normxcorr2(shifter_ocr_block,shifted_ocr);
%finding coordinates of co-relation
[max_cc, imax] = max(abs(cc(:)));
[ypeak, xpeak] = ind2sub(size(cc),imax(1));
corr_offset = [ (ypeak-size(shifter_ocr_block,1)) (xpeak-size(shifter_ocr_block,2)) ];
xoffset = corr_offset(1);
yoffset = corr_offset(2);
xbegin = xoffset+1;
xend = xoffset+ size(shifter_ocr_block,2);
ybegin = yoffset+1;
yend = yoffset+size(shifter_ocr_block,1);
%-----------------------------------------------------------------------% extract image pattern from image from the results of cross corelation and compare to already provided image pattern
extracted_image = shifted_ocr(xbegin:xend,ybegin:yend,:);
figure,imshow(extracted_image)
title('Image Recovered After Cross-Corelation','Color','r')
%find scaled offset of extracted character
x=[36 48]-corr_offset;
fprintf('Image is offset by : \n %d %d \n',x(1),x(2))

Output Of Question 3

Answer 4
load shifted_ocr
%REsults with SNR=-20DB
%View from Imview to find exact pixels and size to extract one character
%from OCR diagram.
imshow(shifted_ocr)
shifted_ocr=im2double(shifted_ocr);
shifted_ocr_new=AWGN(shifted_ocr,-20,'measured');
figure,imshow(shifted_ocr_new)
%cropping character 'C' from original image
shifter_ocr_block = imcrop(shifted_ocr,[48 36 15 18]);
%showing cropped image
figure,imshow(shifter_ocr_block)
title('Extracted Block of character from Orignal Image','Color','r')
%cross corelation with extracted block and orignal image
cc = normxcorr2(shifter_ocr_block,shifted_ocr_new);
%finding coordinates of co-relation
[max_cc, imax] = max(abs(cc(:)));
[ypeak, xpeak] = ind2sub(size(cc),imax(1));
corr_offset = [ (ypeak-size(shifter_ocr_block,1)) (xpeak-size(shifter_ocr_block,2)) ];
xoffset = corr_offset(1);
yoffset = corr_offset(2);
xbegin = xoffset+1;
xend = xoffset+ size(shifter_ocr_block,2);
ybegin = yoffset+1;
yend = yoffset+size(shifter_ocr_block,1);
%-----------------------------------------------------------------------% extract image pattern from image from the results of cross corelation and compare to already provided image pattern
extracted_image = shifted_ocr_new(xbegin:xend,ybegin:yend);
figure,imshow(extracted_image)
title('Image Recovered After Cross-Corelation','Color','r')
%find scaled offset of extracted character
x=[36 48]-corr_offset;
fprintf('Image is offset with SNR=-20: \n %d %d \n',x(1),x(2))
%REsults with SNR=-10DB
%View from Imview to find exact pixels and size to extract one character
%from OCR diagram.
imshow(shifted_ocr)
shifted_ocr=im2double(shifted_ocr);
shifted_ocr_new=AWGN(shifted_ocr,-10,'measured');
figure,imshow(shifted_ocr_new)
%cropping character 'C' from original image
shifter_ocr_block = imcrop(shifted_ocr,[48 36 15 18]);
%showing cropped image
figure,imshow(shifter_ocr_block)
title('Extracted Block of character from Orignal Image','Color','r')
%cross corelation with extracted block and orignal image
cc = normxcorr2(shifter_ocr_block,shifted_ocr_new);
%finding coordinates of co-relation
[max_cc, imax] = max(abs(cc(:)));
[ypeak, xpeak] = ind2sub(size(cc),imax(1));
corr_offset = [ (ypeak-size(shifter_ocr_block,1)) (xpeak-size(shifter_ocr_block,2)) ];

xoffset = corr_offset(1);
yoffset = corr_offset(2);
xbegin = xoffset+1;
xend = xoffset+ size(shifter_ocr_block,2);
ybegin = yoffset+1;
yend = yoffset+size(shifter_ocr_block,1);
%-----------------------------------------------------------------------% extract image pattern from image from the results of cross corelation and compare to already provided image pattern
extracted_image = shifted_ocr_new(xbegin:xend,ybegin:yend);
figure,imshow(extracted_image)
title('Image Recovered After Cross-Corelation','Color','r')
%find scaled offset of extracted character
x=[36 48]-corr_offset;
fprintf('Image is offset with SNR=-10: \n %d %d \n',x(1),x(2))

%REsults with SNR=0DB


%View from Imview to find exact pixels and size to extract one character
%from OCR diagram.
imshow(shifted_ocr)
shifted_ocr=im2double(shifted_ocr);
shifted_ocr_new=AWGN(shifted_ocr,0,'measured');
figure,imshow(shifted_ocr_new)
%cropping character 'C' from original image
shifter_ocr_block = imcrop(shifted_ocr,[48 36 15 18]);
%showing cropped image
figure,imshow(shifter_ocr_block)
title('Extracted Block of character from Orignal Image','Color','r')
%cross corelation with extracted block and orignal image
cc = normxcorr2(shifter_ocr_block,shifted_ocr_new);
%finding coordinates of co-relation
[max_cc, imax] = max(abs(cc(:)));
[ypeak, xpeak] = ind2sub(size(cc),imax(1));
corr_offset = [ (ypeak-size(shifter_ocr_block,1)) (xpeak-size(shifter_ocr_block,2)) ];
xoffset = corr_offset(1);
yoffset = corr_offset(2);
xbegin = xoffset+1;
xend = xoffset+ size(shifter_ocr_block,2);
ybegin = yoffset+1;
yend = yoffset+size(shifter_ocr_block,1);
%-----------------------------------------------------------------------% extract image pattern from image from the results of cross corelation and compare to already provided image pattern
extracted_image = shifted_ocr_new(xbegin:xend,ybegin:yend);
figure,imshow(extracted_image)
title('Image Recovered After Cross-Corelation','Color','r')
%find scaled offset of extracted character
x=[36 48]-corr_offset;
fprintf('Image is offset with SNR=0: \n %d %d \n',x(1),x(2))

%REsults with SNR=10DB


%View from Imview to find exact pixels and size to extract one character
%from OCR diagram.
imshow(shifted_ocr)
shifted_ocr=im2double(shifted_ocr);
shifted_ocr_new=AWGN(shifted_ocr,10,'measured');
figure,imshow(shifted_ocr_new)

%cropping character 'C' from original image


shifter_ocr_block = imcrop(shifted_ocr,[48 36 15 18]);
%showing cropped image
figure,imshow(shifter_ocr_block)
title('Extracted Block of character from Orignal Image','Color','r')
%cross corelation with extracted block and orignal image
cc = normxcorr2(shifter_ocr_block,shifted_ocr_new);
%finding coordinates of co-relation
[max_cc, imax] = max(abs(cc(:)));
[ypeak, xpeak] = ind2sub(size(cc),imax(1));
corr_offset = [ (ypeak-size(shifter_ocr_block,1)) (xpeak-size(shifter_ocr_block,2)) ];
xoffset = corr_offset(1);
yoffset = corr_offset(2);
xbegin = xoffset+1;
xend = xoffset+ size(shifter_ocr_block,2);
ybegin = yoffset+1;
yend = yoffset+size(shifter_ocr_block,1);
%-----------------------------------------------------------------------% extract image pattern from image from the results of cross corelation and compare to already provided image pattern
extracted_image = shifted_ocr_new(xbegin:xend,ybegin:yend);
figure,imshow(extracted_image)
title('Image Recovered After Cross-Corelation','Color','r')
%find scaled offset of extracted character
x=[36 48]-corr_offset;
fprintf('Image is offset with SNR=10: \n %d %d \n',x(1),x(2))

Output Question 4

As the value of SNR degrades its become more and more difficult for co-relation to
find exact match of block over entire image hence accuracy of shifting factor also
decreased.

You might also like