Digital Signal Processing: Assignment 2
1. Pre-Lab
1.4.a Get Test Images: 326x426 lighthouse image
% zurgaa variable bolgono
img = load('lighthouse.mat');
a=whos('img.xx'); %eniig ashiglaj chadqvveen
% colormap(gray(256));
% img dotor chin array baigaa 1.barb 2.echart 4.xx 5.zone
% lighthouse image:
show_img(img.xx);
1.4.b 1-D discrete-time signal: After comparing image and plot, I say 1 represents white and
0 represents black.
img = load('lighthouse.mat');
% turn 200th row into time signal
xx200=img.xx(200,:);
row=img.xx(200,:);
figure;
plot(1:426,row);
2. Warm-up
2.1 Synthesize a Test Image
2.1.a Test image/ all columns are identical using: xpix= ones(256,1)*cos(2*pi*(0:255)/16)
% test image which's all columns are identical
xpix = ones(256,1)*cos(2*pi*(0:255)/16);
show_img(xpix);
-Cosine results numbers range between -1 to 1. This means 1-white, 0-black.
-Total width in pixel=256.
Number of bands=32 /16 white 16 black
Width of single band=256/32=8.
-Above formula has frequency of 1/16 and total pixel is 256. Then the total number of
bands will be 32 and 256/32=8.
2.1.b When xpix value is 1, then color will be white. When it is -1 color will be black.
2.1.c To generate 5 black bands, we have to take frequency as 1/80.
% to generate 5 black bands, we have to take frequency as 1/80
ypix=rot90(ones(400,1)*cos(2*pi*(0:399)/80));
show_img(ypix);
2.2 Printing Multiple Images on One Page using Paint:
2.3 Sampling of Images: White parts are most affected by aliasing.
img = load('lighthouse.mat');
% downsampling
p=2; % down sample factor
small.img.xx = img.xx(1:p:end,1:p:end);
show_img(small.img.xx);
3. Sampling, Aliasing and Reconstruction
3.1.a Some parts of the downsampled image are distorted due to their frequency domain
spectrums intercepting. The high frequency parts of the original image (white colors) are most
affected by aliasing as they are the ones experiencing the interceptions.
3.2.a
- Ceil function round towards plus infinity. Inside the function there is a vector, which
starts from 0.999 and adds 1 by 1 to the previous value. If the vector increases by 1, there
will be 4 repeated values if the array is divided by 4.
-Interpolation factor would be 4 as each element is duplicated to represent 4 values.
img = load('lighthouse.mat');
xr1 = (-2).^(0:6);
L = length(xr1);
nn = ceil((0.999:1:4*L)/4);
xr1hold = xr1(nn);
% y tenhlegiin utguud n -2-n 0s 6 hvrtel zergvvd. nn der utguud 4 davhcaj
% baigaa tul y-n utguud ch 4 davhcana.
plot(xr1hold);
3.2.b xholdrows has stretched 3 times than downsampled image xx3.
img = load('lighthouse.mat');
p=3;
xx3=img.xx(1:p:end,1:p:end);
show_img(xx3); %first image
[a,b]=size(xx3);
n=1:a;
L = length(xx3(n,:));
nn = ceil((0.999:1:3*L)/3); % 0 order idea
xholdrows = xx3(n,nn);
show_img(xholdrows); %second image
3.2.c xhold compared to the original lighthouse image/ zero-order hold:
img = load('lighthouse.mat');
show_img(img.xx); %first image
p=3;
xx3=img.xx(1:p:end,1:p:end);
% show_img(xx3);
[a,b]=size(xx3);
n=1:a;
L = length(xx3(n,:));
nn = ceil((0.999:1:3*L)/3); % 0 order idea
xholdrows = xx3(n,nn);
m=1:426;
L = 109-(1/3);
nn = ceil((0.999:1:3*L)/3);
xhold = xholdrows(nn,m);
show_img(xhold); %second image
3.2.d The interpolation factor when converting xr1 to xr1linear: 10.
3.2.e linear interpolation:
img = load('lighthouse.mat');
p=3;
xx3=img.xx(1:p:end,1:p:end);
[a,b]=size(xx3);
for row=1:a
nold=1:b;
wp1=xx3(row,:);
nnew=(1/3):(1/3):b;
wp1=im2double(wp1);
linear=interp1(nold,wp1,nnew);
xxlinear_rows(row,:) = linear;
end
for column=1:b*3
nold=1:a;
wp2=xxlinear_rows(:,column);
nnew=(1/3):(1/3):a-(1/3);
wp2=im2double(wp2);
linear=interp1(nold,wp2,nnew);
xxlinear(:,column)=linear;
end
show_img(xxlinear);
3.2.f xxlinear compared to the original lighthouse image/ linear interpolation:
3.2.g The quality comparison of the linear interpolation result to the zero-order hold result:
-The zero hold method has sharp color pixel and the linear interpolation has more smooth
transition between pixel color.
-The fence problem is the same in both methods too. The fence problem could not be solved as
the high frequency region is distorted due to aliasing and data there is lost.
% Full code:
clc,clear;
% zurgaa variable bolgono
img = load('lighthouse.mat');
% a=whos('img.xx'); %eniig ashiglaj chadqvveen
% colormap(gray(256));
% 256ruu scaledh?
% img dotor chin array baigaa 1.barb 2.echart 4.xx 5.zone
% lighthouse image:
show_img(img.xx);
% % turn 200th row into time signal
% xx200=img.xx(200,:);
% row=img.xx(200,:);
% figure;
% plot(1:426,row);
% % test image which's all columns are identical
% xpix = ones(256,1)*cos(2*pi*(0:255)/16);
% show_img(xpix);
% % to generate 5 black bands, we have to take frequency as 1/80
% ypix=rot90(ones(400,1)*cos(2*pi*(0:399)/80));
% show_img(ypix);
% % downsampling
% p=2; % down sample factor
% small.img.xx = img.xx(1:p:end,1:p:end);
% show_img(small.img.xx);
% xr1 = (-2).^(0:6);
% L = length(xr1);
% nn = ceil((0.999:1:4*L)/4);
% xr1hold = xr1(nn);
% % y tenhlegiin utguud n -2-n 0s 6 hvrtel zereg. nn der utguud 4 davhcaj
% % baigaa tul y-n utguud ch 4 davhcana.
% plot(xr1hold);
p=3;
xx3=img.xx(1:p:end,1:p:end);
% show_img(xx3);
[a,b]=size(xx3);
n=1:a;
L = length(xx3(n,:));
nn = ceil((0.999:1:3*L)/3); % 0 order idea
xholdrows = xx3(n,nn);
% show_img(xholdrows);
m=1:426;
L = 109-(1/3);
nn = ceil((0.999:1:3*L)/3); %<--Round up to the integer part
xhold = xholdrows(nn,m);
show_img(xhold);
% n1 = 0:6;
% xr1 = (-2).^n1;
% figure;
% c=length(xr1);
% stem((0:c-1),xr1);
% tti = 0:0.1:6; % locations between the n1 indices
% xr1linear = interp1(n1,xr1,tti); %-- function is INTERP-ONE
% figure;
% stem(tti,xr1linear)
% p=3;
% xx3=img.xx(1:p:end,1:p:end);
% [a,b]=size(xx3);
for row=1:a
nold=1:b;
wp1=xx3(row,:);
nnew=(1/3):(1/3):b;
wp1=im2double(wp1);
linear=interp1(nold,wp1,nnew);
xxlinear_rows(row,:) = linear;
end
for column=1:b*3
nold=1:a;
wp2=xxlinear_rows(:,column);
nnew=(1/3):(1/3):a-(1/3);
wp2=im2double(wp2);
linear=interp1(nold,wp2,nnew);
xxlinear(:,column)=linear;
end
show_img(xxlinear);