0%

ee261中的代码题(2)

本题是ee261中Problem Set 6的一道matlab代码题,主要用到了采样定理和混淆相关的知识。
ee261相关知识点整理博客:https://www.cnblogs.com/TaigaCon/p/5079156.html


结果分析

alpha=0.99

当alpha = 0.99时,linear和nearest两种方法的结果都很不错,毕竟此时采样率还是基本符合采样定理的。

linear

alpha=0.99_linear

nearest

alpha=0.99_nearest

alpha=0.95

alpha=0.95时,可以发现图片已经受到混淆的严重影响了,“nearest”下图片体现出了“像素风”,也就是说图片的精细化程度下降很多;
而“linear”相比于“nearest”,它的边缘要更模糊一些。推测是因为linear相比nearest更加平均,过渡更均匀。

linear

alpha=0.95_linear

nearest

alpha=0.95_nearest

alpha=0.90

混淆的作用在这里比alpha=0.95更要严重,图片已经面目全非。当然,linear依然比nearest模糊。

linear

alpha=0.9_linear

nearest

alpha=0.9_nearest

matlab代码

代码中alpha=0.90,插值类型“nearest”,若要修改这两个参数,修改26行和59行的参数值即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
%https://see.stanford.edu/materials/lsoftaee261/PS-6-2007.pdf
%第五题,matlab编程题
%题目大意:给定一张图片,采样频率的改变、插值方式对原图像的影响(和采样定理有关)

%读取图片
clc;clear;
img = imread('man.gif');
[row,col] = size(img);
% imshow(img);

%第一步,从256*256转化成1*256^2,并将像素值由uint8转化为double,保证归一化
imgVector = [];
for i = 1:row
imgVector = [imgVector,img(i,:)];
end
imgVector = double(imgVector)/256;
vectorLength = length(imgVector);

%第二步,fft,保证0点在中心
imgVectorFren = fftshift(fft(imgVector));
fValues = -vectorLength/2:1:vectorLength/2-1;
zeroPoint = length(fValues)/2+1;
plot(fValues,imgVectorFren);

%第三步:求不同alpha值时的带宽
alpha = 0.90;%0.9、0.95、0.99
energyTotal = 0;%代表信号总能量
energyP = abs(imgVectorFren(zeroPoint))^2;%代表近似带宽内信号总能量

%计算信号总能量
for i = 1:vectorLength
energyTotal = energyTotal + abs(imgVectorFren(i))^2;
end

%计算近似带宽
p = 0;
while(energyP/energyTotal<alpha)
p = p+1;
energyP = energyP+abs(imgVectorFren(p+zeroPoint))^2+abs(imgVectorFren(-p+zeroPoint))^2;
end

%第四步:求得近似带宽后,根据采样定理,以奈奎斯特采样频率取对原时域信号采样
rate = p*2/vectorLength;
interval = floor(1/rate);

indexTotal = 1;
vectorSampled = [];
indexSampled = [];
while(indexTotal<=vectorLength)
indexSampled = [indexSampled,indexTotal];
vectorSampled = [vectorSampled,imgVector(indexTotal)];

indexTotal = indexTotal + interval;
end


%第五步,对采样后的时域信号进行插值,插值方式两种:直线、复制近邻的值。
indexInterped = 1:vectorLength;
vectorInterped = interp1(indexSampled,vectorSampled,indexInterped,'nearest'); %直线:linear;最近邻:nearest

%第六步,将vectorInterped复原为图像,并与原图进行对比
imgInterped = [];
for i = 0:row-1
imgInterped = [imgInterped;vectorInterped(1,i*col+1:(i+1)*col)];
end

subplot(1,2,1);
imshow(img);
title('imgOri');

subplot(1,2,2);
imshow(imgInterped);
title('imgInterped');