-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBEC_1D_fit_ver5.m
204 lines (170 loc) · 8.09 KB
/
BEC_1D_fit_ver5.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
function funcOut = BEC_1D_fit_ver4(analyVar, indivDataset)
%% Analysis folder name as generated by the AnalysisVariables.m
path = analyVar.analyOutDir;
batchfileAtom = [analyVar.dataDir char(analyVar.basenamevectorAtom(1)) '.batch']; % current atom batchfile name
batchLineFormat = '%q%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%{yyyy.MM.dd-HH:mm:ss}D';
indivBatchAtomData = textscan(fopen(batchfileAtom), batchLineFormat,'commentstyle','%');
%% option to choose atom filelist which are not commented out lines in batch
%filelist =indivDataset{1,1}.fileAtom; %%% filelist of all the batch lines
filelist = indivBatchAtomData{1,1}; %%% filelist of all the 'uncommented' batch lines in batch file.
scanvariable = indivBatchAtomData(2); %%% getting the second column in batch files, i.e, scan variable
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% READ ME %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% Need to run imagefit_Analysis_Runner to generate the *imagebatch.txt
%%%%% files in the analysis folder
%%%%% Listing all the filenames with parameteres in the data folder
%%
if length(filelist)==0
fprintf('no files to read, Check path/ or nothing exists!');
end
numoftiles = floor(sqrt(length(filelist)))+1;
for i=1:length(filelist)
fid=fopen(strcat(fullfile(path,filelist{i}),'ODimagebatch.txt'),'r');
dataset = dlmread(strcat(fullfile(path,filelist{i}),'ODimagebatch.txt'),'\t',0,0);
xdata(:,i) = sum(dataset,2);
ydata(:,i) = sum(dataset);
Axis = [1:length(ydata)];
figure(1);
subplot(numoftiles,numoftiles,i);
plot(Axis',ydata(:,i),'s','MarkerEdgeColor','b','MarkerFaceColor','b');
hold on
xlim([Axis(1) Axis(end)]);
%%%%%%%%%%%%%%%%%%%%%%% fitting for ydata %%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% find the moments to establish initial guess
mean = sum(Axis'.*abs(ydata(:,i)))/(sum(abs(ydata(:,i))));
variance = sum(Axis'.*Axis'.*abs(ydata(:,i)))/(sum(abs(ydata(:,i))));
stddev = sqrt(variance-mean^2);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%% play with initial params %%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[~,idx] = max(ydata(:,i));
Con_init = [max(ydata(:,i))/2/pi,idx,stddev/4];
Therm_init = [max(ydata(:,i))/4/pi,idx,stddev/2];
offset_init = ydata(1,i);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%disp(offset_init)
fitx = linspace(Axis(1),Axis(end),1000)';
plot(fitx,offset_init+Thermal_func(fitx,Therm_init(1),Therm_init(2),Therm_init(3)),'-.')
%%%%%%% Fit ydata %%%%%%%%
fitType = fittype(@(offsety,my,Cfacy,sigmay,Thfacy,yrtf,y0,X) offsety + my.*X ...
+ BEC_func(X,Cfacy,y0,yrtf) + Thermal_func(X,Thfacy,y0,sigmay),'independent','X');
fitOptions=fitoptions(fitType);
fitOptions.StartPoint=[offset_init,0,Con_init(1),Therm_init(3),Therm_init(1),Con_init(3),idx];
fitOptions.Upper=[Inf,Inf,Inf,Inf,Inf,Inf,Axis(end)];
fitOptions.Lower=[-Inf,-Inf,0,0,0,0,Axis(1)];
[fitobject,gof]=fit([1:length(ydata)]',ydata(:,i),fitType,fitOptions);
paramy(:,i) = coeffvalues(fitobject);
%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% save params in variables to use %%%%%
yoffset = paramy(1,i);
ybkgslope = paramy(2,i);
thermalfacy = paramy(5,i);
sigy = paramy(4,i);
condenfacy = paramy(3,i);
rtfy = paramy(6,i);
ycenter = paramy(7,i);
%disp(yoffset)
%%%%% plot the fit %%%
fitplot = plot(fitobject);
set(fitplot,'Linewidth',2)
axis tight;
legend off;
if i==length(filelist)
legend('ydata','ThermGuess','Fit');
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%% Fit for other axis %%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure(2);
Axis = [1:length(xdata)];
subplot(numoftiles,numoftiles,i);
plot(Axis',xdata(:,i),'o','MarkerEdgeColor','b','MarkerFaceColor','b');
hold on
xlim([Axis(1) Axis(end)]);
%%%%%%%%%%%%%%%%%%%%%%% fitting for xdata %%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% find the moments to establish initial guess
mean = sum(Axis'.*abs(xdata(:,i)))/(sum(abs(xdata(:,i))));
variance = sum(Axis'.*Axis'.*abs(xdata(:,i)))/(sum(abs(xdata(:,i))));
stddev = sqrt(variance-mean^2);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%% play with initial params %%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[~,idx] = max(xdata(:,i));
Con_init = [max(xdata(:,i))/2/pi,idx,stddev/4];
Therm_init = [max(xdata(:,i))/4/pi,idx,stddev/2];
offset_init = xdata(1,i);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%disp(offset_init)
fitx = linspace(Axis(1),Axis(end),1000)';
plot(fitx,offset_init+Thermal_func(fitx,Therm_init(1),Therm_init(2),Therm_init(3)),'k-.')
%%%%% Fit X image
fitType = fittype(@(offsetx,mx,sigmax,xrtf,x0,X) offsetx + mx.*X ...
+ BEC_func(X,condenfacy*rtfy/xrtf,x0,xrtf) + Thermal_func(X,thermalfacy*sigy/sigmax,x0,sigmax),'independent','X');
fitOptions=fitoptions(fitType);
fitOptions.StartPoint=[offset_init,0,Therm_init(3),Con_init(3),idx];
fitOptions.Upper=[Inf,Inf,Inf,Inf,Axis(end)];
fitOptions.Lower=[-Inf,-Inf,0,0,Axis(1)];
[fitobject,gof]=fit([1:length(xdata)]',xdata(:,i),fitType,fitOptions);
paramx(:,i) = coeffvalues(fitobject);
%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% Extract the fitted parameters
xoffset = paramx(1,i);
xbkgslope = paramx(2,i);
rtfx = paramx(4,i);
xcenter = paramx(5,i);
sigx = paramx(3,i);
%disp(yoffset)
%%%%% plot the fit %%%
fitplot = plot(fitobject);
set(fitplot,'Linewidth',2)
axis tight;
legend off;
if i==length(filelist)
legend('xdata','ThermGuess','Fit');
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% making the image mesh %%%%%%%%%%%%%%%%%
xgrid = [1:length(xdata(:,i))];ygrid= [1:length(ydata(:,i))];
c0 = yoffset/length(ydata(:,i)) - xbkgslope;
xbkgslope_2d = xbkgslope/length(xdata(:,i));
ybkgslope_2d = ybkgslope/length(ydata(:,i));
[YY,XX] = meshgrid(xgrid,ygrid);
%%%%% Constructing the mesh/image from fitted parameters above
Therm_mesh = Thermal_func_2D(XX,YY,thermalfacy/sigx,xcenter,ycenter,sigx,sigy);
Therm_mesh = reshape(Therm_mesh,[length(ydata(:,i)),length(xdata(:,i))]);
BEC_mesh = BEC_func_2D(XX,YY,condenfacy/sigx,xcenter,ycenter,rtfx,rtfy);
fitted_dataset = c0 + xbkgslope_2d.*XX+ ybkgslope_2d.*YY + BEC_mesh/2 + Therm_mesh/2;
%%%%% plotting the fit image
figure(3);
subplot(numoftiles,numoftiles,i);
imagesc(fitted_dataset)
title(scanvariable{1}(i))
colorbar
%%%%% plotting the residuals (fit image - original image)
figure(4);
subplot(numoftiles,numoftiles,i);
imagesc(fitted_dataset-dataset)
title(scanvariable{1}(i))
colorbar
%%%%% plotting the original image
figure(5);
subplot(numoftiles,numoftiles,i);
imagesc(dataset)
title(scanvariable{1}(i))
colorbar
condensate_fraction(i) = (sum(sum(BEC_mesh)))/(sum(sum(BEC_mesh))+sum(sum(Therm_mesh)))*100;
end
%%%%% Calculating condensate fraction by adding the corresponding Mesh
%%%%% values from fit and storing it in an array.
figure(6);
plot(scanvariable{1},condensate_fraction)
title('BEC fraction with evaporation')
ylabel('percentage of condensate(%)')
xlabel('time [ms]')
hold off;
% script end
end