-
Notifications
You must be signed in to change notification settings - Fork 1
/
Process4D_alignDiff.m
62 lines (45 loc) · 1.56 KB
/
Process4D_alignDiff.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
%% DataIO
datadir = '/Users/sung/Desktop/Data/20230207_1T-TaS2_Protochips/acquisition_12';
fname = 'scan_x128_y128.raw';
im4D = ( read_empad( fullfile(datadir,fname),128));
%%
Bragg = [64, 63];
wd = 20;
wd_fit = wd;
[nky, nkx, nsy, nsx] = size(im4D);
im4D_aligned = zeros(size(im4D));
gaus2D = @(x, xdata) x(1)*exp(-0.5*( (xdata(:,:,1)-x(2)).^2+(xdata(:,:,2)-x(3)).^2)/(x(4)^2))+x(5);
%circ = @(x, xdata) x(1)*double( (xdata(:,:,1)-x(2)).^2+(xdata(:,:,2)-x(3)).^2<x(4)^2 ) +x(5);
center = nky/2 + 1;
[xx,yy] = meshgrid( 1:nky, 1:nkx );
xdata = cat(3,xx,yy);
figure
subplot(1,2,1)
im1 = imagesc( zeros(2*wd+1));
axis image
subplot(1,2,2)
im2 = imagesc( zeros(2*wd+1));
axis image
t_h = title( '' );
for sx = 1:nsx
for sy = 1:nsy
diff_cur = im4D( :,:,sy,sx );
xfit = ( Bragg(1)+(-wd_fit:wd_fit) );
yfit = ( Bragg(2)+(-wd_fit:wd_fit) );
xdatafit = xdata( yfit, xfit, :);
subIm = diff_cur( yfit, xfit );
x0 = Bragg(1); y0 = Bragg(2);
A0 = max(subIm(:));
B0 = min(subIm(:));
param_guess = [ A0, x0, y0, 3, B0];
lb = [ A0/2, x0-wd/2, y0-wd/2, 3, B0/2];
ub = [ A0*2, x0+wd/2, y0+wd/2, 3, B0*2];
param_fit = lsqcurvefit( gaus2D, param_guess, xdatafit,subIm, lb, ub );
im_align = imtranslate( diff_cur, -[param_fit(2)-center, param_fit(3)-center],'bilinear');
im4D_aligned(:,:, sy,sx) = im_align;
im1.CData = subIm;
im2.CData = gaus2D( param_fit, xdatafit);
t_h.String = sprintf( '%d %d', sx, sy);
drawnow
end
end