-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_weighting.m
180 lines (122 loc) · 4.63 KB
/
test_weighting.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
% This file is to compare two approches to compute covariance matrix for
% dimensionality reduction in different scenarios
% Approch 1: F * Ipsf * Dirac2D, gridded natural weights in practice
% Approch 2: matrix probing, very very slow
close all
clear variables
clc
addpath data
addpath data/images
addpath lib/
addpath fouRed/
% addpath src
addpath irt/
try
setup;
catch ME
error('NUFFT library not found in location src/irt');
end
rng('shuffle');
Nx = 64;
Ny = 64;
serialise = @(x) x(:);
%% Gridded visibilities
ratio = 0.1;
M = round(Nx*Ny*ratio);
u = [];
v = [];
ox = 1;
oy = 1;
sigma = 5;
while length(u) < M
u = round(sigma * randn(M, 1) + Nx/2);
v = round(sigma * randn(M, 1) + Ny/2);
sfu = find((u<=Nx) & (u>=1));
sfv = find((v<=Ny) & (v>=1));
sf = intersect(sfu, sfv);
u = u(sf);
v = v(sf);
end
figure(), plot(u,v,'.'), title('Grided uv-coverage')
mu = [Nx/2*ox Ny/2*oy];
Sigma = [20*ox 0; 0 20*oy];
x1 = 1:(Nx*ox); x2 = 1:(Ny*oy);
[X1,X2] = meshgrid(x1,x2);
F = mvnpdf([X1(:) X2(:)],mu,Sigma);
F = reshape(F,length(x2),length(x1));
F = 10*F/max(F(:));
figure(), surf(x1,x2,F), title('Simulated weighting')
W = zeros(ox*Nx, oy*Ny);
W(ox*u,oy*v) = F(ox*u,oy*v);
figure(), surf(x1,x2,W), title('Simulated weighted sampling')
Phi = @(x) serialise(W.*fftshift(fft2(ifftshift(reshape(full(x), Nx, Ny)), ox*Nx, oy*Ny)));
Phit = @(x) serialise(fftshift(ifft2(ifftshift(W'.*reshape(full(x), ox*Nx, oy*Ny)), Nx, Ny)));
Ipsf = @(x) Phit(Phi(x));
im = zeros(Nx, Ny);
im(ceil((Nx+1)/2), ceil((Ny+1)/2)) = 1;
PSF = reshape(Ipsf(im), Nx, Ny);
FPSF = fftshift(fft2(ifftshift(PSF)));
figure(),imagesc(abs(FPSF)),colorbar(), title('Sigma matrix via PSF')
covoperator = @(x) fftshift(fft2(ifftshift(reshape(Phit(Phi(fftshift(ifft2(ifftshift(reshape(full(x), Nx, Ny)))))), Nx, Ny))));
covariancemat = guessmatrix_test2(1, covoperator, Ny*Nx, Ny*Nx);
d = diag(covariancemat);
figure(),imagesc(reshape(abs(d), Nx, Ny)),colorbar(), title('Sigma matrix via matrix probing')
diff = FPSF - reshape(d, Nx, Ny);
figure(),imagesc(abs(diff)),colorbar(), title('Different between two methods (gridded vis)')
%% Continuous visibilities
N = Nx * Ny;
visibSize = 5 * Ny * Nx;
ox = 2; % oversampling factors for nufft
oy = 2; % oversampling factors for nufft
Kx = 8; % number of neighbours for nufft
Ky = 8; % number of neighbours for nufft
sampling_pattern = 'gaussian';
util_gen_sampling_pattern_config; % Set all parameters
sparam.N = N; % number of pixels in the image
sparam.Nox = ox*Nx; % number of pixels in the image
sparam.Noy = oy*Ny; % number of pixels in the image
sparam.p = ceil(visibSize/N);
[~, ~, uw, vw, ~] = util_gen_sampling_pattern(sampling_pattern, sparam);
uw = [uw; -uw];
vw = [vw; -vw];
% uw = linspace(-pi, pi, Nx);
% vw = linspace(-pi, pi, Ny);
% uw = uw(u);
% vw = vw(v);
% uw = uw(:);
% vw = vw(:);
% u = linspace(-pi, pi, Nx);
% v = linspace(-pi, pi, Ny);
% [uw,vw] = meshgrid(u,v);
% uw = uw(:);
% vw = vw(:);
figure(),plot(uw,vw,'.'),title('continuous uv-coverage')
fprintf('Initializing the NUFFT operator\n\n');
tstart = tic;
[A, At, Gw, scale] = op_nufft([uw vw], [Nx Ny], [Kx Ky], [oy*Nx ox*Ny], [Nx/2 Ny/2], 0);
tend = toc(tstart);
fprintf('Initialization runtime: %ds\n\n', ceil(tend));
Phi = @(x) Gw*serialise(A(reshape(full(x), Nx, Ny))); % Phi: vect -> vect
Phit = @(x) serialise((At(Gw'*x(:))));
Ipsf = @(x) Phit(Phi(x));
dirac2D = zeros(Nx, Ny);
dirac2D(ceil((Nx+1)/2), ceil((Ny+1)/2)) = 1;
PSF = reshape(Ipsf(dirac2D), Nx, Ny);
PSF1 = zeros(size(PSF));
% PSF1(ceil((Nx+1)/2)-8:ceil((Nx+1)/2)+8, ceil((Ny+1)/2)-8:ceil((Ny+1)/2)+8) = PSF(ceil((Nx+1)/2)-8:ceil((Nx+1)/2)+8, ceil((Ny+1)/2)-8:ceil((Ny+1)/2)+8);
FPSF = fftshift(fft2(ifftshift(PSF)));
figure(),imagesc(abs(FPSF)),colorbar(), title('Sigma matrix via PSF')
covoperator = @(x) fftshift(fft2(ifftshift(reshape(Phit(Phi(fftshift(ifft2(ifftshift(reshape(full(x), Nx, Ny)))))), Nx, Ny))));
covariancemat = guessmatrix_test2(1, covoperator, Ny*Nx, Ny*Nx);
d = diag(covariancemat);
figure(),imagesc(reshape(abs(d), Nx, Ny)),colorbar(), title('Sigma matrix via matrix probing')
diff = FPSF - reshape(d, Nx, Ny);
figure(),imagesc(abs(diff)),colorbar(), title('Different between two methods (continuous vis)')
%% Phi^T Phi matrix probing
figure(),imagesc(abs(PSF)),colorbar(), title('Phi^T Phi via PSF')
covariancemat2 = guessmatrix_test2(1, Ipsf, Ny*Nx, Ny*Nx);
d2 = diag(covariancemat2);
% d3 = fftshift(fft2(ifftshift(reshape(full(d2), Nx, Ny))));
figure(),imagesc(reshape(abs(d2), Nx, Ny)),colorbar(), title('Phi^T Phi probing via matrix probing')
diff2 = PSF - reshape(d2, Nx, Ny);
figure(),imagesc(abs(diff2)),colorbar(), title('Different between two methods')