-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdemo_resize.m
136 lines (113 loc) · 2.93 KB
/
demo_resize.m
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
%% Downsize an array using different methods
clear;
% load an RBG image: 135-by-198-by-3 array
I = imread('onion.png');
disp(size(I));
figure; imagesc(I);
%%
% Set target dimension
targetdim = round([100 150 3]);
disp(targetdim);
%%
% Downsize using interpolation
I_interp = array_resize(I, targetdim); %<-- default method is interpolate
disp(size(I_interp));
figure; imagesc(I_interp);
%%
% Downsize using discrete cosine transform (DCT)
I_dct = array_resize(I, targetdim, 'method', 'dct');
disp(size(I_dct));
figure; imagesc(I_dct);
%%
% Downsize to PCA scores using HOSVD. PC scores loose original
% interpretation
I_hosvd = array_resize(I, targetdim, 'method', 'hosvd');
disp(size(I_hosvd));
figure; imagesc(I_hosvd);
%%
% Downsize to PCA scores using marginal SVD. PC scores loose original
% interpretation
I_2dsvd = array_resize(I, targetdim, 'method', '2dsvd');
disp(size(I_2dsvd));
figure; imagesc(I_2dsvd);
%% Upsize an array using different methods
%%
% Set target dimension
targetdim = round([200 300 3]);
disp(targetdim);
%%
% Upsize using interpolation
I_interp = array_resize(I, targetdim); %<-- default method is interpolate
disp(size(I_interp));
figure; imagesc(I_interp);
%%
% Upsize using discrete cosine transform (DCT)
I_dct = array_resize(I, targetdim, 'method', 'dct');
disp(size(I_dct));
figure; imagesc(I_dct);
%% Downsizing-analysis-upsizing
clear;
% reset random seed
s = RandStream('mt19937ar','Seed',2);
RandStream.setGlobalStream(s);
%%
% True coefficients for regular (non-array) covariates
p0 = 5;
b0 = ones(p0,1);
%%
% 2D true signal: 64-by-64 cross
shape = imread('cross.gif');
shape = array_resize(shape,[32,32]); % 32-by-32
b = zeros(2*size(shape));
b((size(b,1)/4):(size(b,1)/4)+size(shape,1)-1, ...
(size(b,2)/4):(size(b,2)/4)+size(shape,2)-1) = shape;
[p1,p2] = size(b);
disp(size(b));
figure; imagesc(-b);
colormap(gray);
title('True Signal');
axis equal;
axis tight;
%%
% Simulate covariates
n = 500; % sample size
X = randn(n,p0); % n-by-p0 regular design matrix
M = tensor(randn(p1,p2,n)); % p1-by-p2-by-n matrix variates
disp(size(M));
%%
% Simulate responses
mu = X*b0 + double(ttt(tensor(b), M, 1:2));
sigma = 1; % noise level
y = mu + sigma*randn(n,1);
%%
% Rank 2 Kruskal regression with 64-by-64 covariates
tic;
[~,beta1,glmstats1] = kruskal_reg(X,M,y,2,'normal');
toc;
disp(size(beta1));
figure;
imagesc(-double(beta1));
colormap(gray);
title('Estimate using 64x64 covariates');
axis equal;
axis tight;
%%
% Resize 2D covariates to 32-by-32
M_reduce = array_resize(double(M), [32 32 n], 'method', 'dct');
disp(size(M_reduce));
%%
% Rank 2 Kruskal regression with 32-by-32 covariates
tic;
[~,beta2,glmstats2] = kruskal_reg(X,M_reduce,y,2,'normal');
toc;
disp(size(beta2));
%%
% Resize estimate and disp in original size
beta2 = array_resize(double(beta2), [64 64], 'method', 'dct');
disp(size(beta2));
figure;
imagesc(-double(beta2));
colormap(gray);
title('Estimate using 32x32 covariates');
axis equal;
axis tight;