/
fitIRF.m
323 lines (288 loc) · 12.7 KB
/
fitIRF.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
function fitIRF(XYZimage)
% fitIRF fits an exponentially modified Gaussian to the IRF
% A measurement of the instrument response function is done using the SPAD
% camera. Use a pulsed laser, quenched fluorescein, and accumulate many
% frames into a single histogram map.
%
% Syntax: fitIRF(XYZimage)
%
% Inputs:
% The function requires a 3D array, where 1st and 2nd dimensions
% represent th pixels od the SPAD array and the 3rd dimension is the time
% domain with the histogram bins of the TDC.
% The function also requires the global variable struct 'correction' with
% the fields holding the parameters of the correction.
%
% Outputs:
% The global variable struct 'correction' is populated with new fields:
%
% correction.fit Fit parameters of the exponentially modified
% Gaussian in each pixel
% correction.fit.h Exp-mod Gaussian amplitude
% correction.fit.mu Exp-mod Gaussian peak position
% correction.fit.sigma Exp-mod Gaussian standard deviation
% correction.fit.tau Exp-mod Gaussian exponential decay constant
% correction.fit.offset Exp-mod Gaussian exponential zero offset
% correction.exitFlag 1 for successsful fit, 0 for failed fit
% correction.goodfit Matrix of good fits. This is generated based
% on the similarity of the results, not
% meeting fit stopping conditions. The value
% is 1, i.e. good fit, if sigma is between
% 0.5x and 1.5x median sigma, tau is between
% 0.5x and 1.5x median tau, and offset is not
% an outlier according to 'isoutlier'.
% correction.peak The peak, maximum, rising- and falling-edge
% positions, and full-width half-maximum of the
% fitted data. These values are calculated by
% iterrative fitting, as I could not find the
% analytical solution to the problem. It needs
% looking at in the future, as the solution
% must exist.
% correction.peak.Pos Exp-mod Gaussian peak position
% correction.peak.Max Exp-mod Gaussian peak maximum
% correction.peak.risingHM Exp-mod Gaussian peak rising edge position
% correction.peak.fallingHM Exp-mod Gaussian peak falling edge position
% correction.peak.FWHM Exp-mod Gaussian peak full-width half maximum
% correction.peak.XXinterp Below are further fields that interpolate the
% above values, where the goodfit is not '1'
% correction.peak.FWHMinterp
% correction.peak.PosInterp
% correction.peak.risingHMinterp
% correction.peak.fallingHMinterp
%
% Examples:
% fitIRF(XYZimage)
%
% Toolbox requirement: Optimization Toolbox
% Other m-files required: extractHistogramData, exgfit, exGauss
% Subfunctions: none
% MAT-files required: none, but global variable 'correction' is needed
%
% See also: analyzeCDMmap, analyzeCDMhist, loadCDMdata
% Jakub Nedbal
% King's College London
% Aug 2018
% Last Revision: 08-May-2020 - Moved data import code into 'loadCDMdata'
% Revision: 02-Feb-2020 - Added support for repeated CDM measurements.
% Revision: 13-Aug-2018
%
% Copyright 2018-2020 Jakub Nedbal
%
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions are
% met:
%
% 1. Redistributions of source code must retain the above copyright notice,
% this list of conditions and the following disclaimer.
%
% 2. Redistributions in binary form must reproduce the above copyright
% notice, this list of conditions and the following disclaimer in the
% documentation and/or other materials provided with the distribution.
%
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
% "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
% TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
% PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
% OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
% EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
% PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
% PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
% LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
% NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
% SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
%% Define global variables
global correction
global pixHist
global pixIndex
%% Create a waitbar
hwaitbar = waitbar(0, '', ...
'Name', 'Fitting IRF with exGaussian function...');
% Make it a big higher
hwaitbar.Position(4) = hwaitbar.Position(4) + 10;
%% Run a routine that extract all important aspects of the linearization
% and the input histogram data
extractHistogramData;
%% Find the position of the peak in each pixel
fit.h = zeros(inputSize([1, 2]));
fit.mu = fit.h;
fit.sigma = fit.h;
fit.tau = fit.h;
fit.offset = fit.h;
fit.exitFlag = fit.h;
peak.Pos = nan(inputSize([1,2]));
peak.Max = peak.Pos;
peak.risingHM = peak.Pos;
peak.fallingHM = peak.Pos;
tic
numberPixels = double(numberPixels); %#ok<NODEF>
% loaded by extractHistogramData
%% Go through the image pixel by pixel
for i = 1 : numberPixels
% update the waitbar
if mod(i, 100) == 0
if ishandle(hwaitbar)
% Estimated time to completion
eta = numberPixels / i * toc / 86400;
if isinf(eta); eta = 0; end
% Update the waitbar
waitbar(i / numberPixels, ...
hwaitbar, ...
sprintf('%d of %d\n%s (%s ETA)', ...
i, ...
numberPixels, ...
datestr(toc / 86400, 'HH:MM:SS'), ...
datestr(eta, 'HH:MM:SS')));
end
end
% Extract the histogram for pixel
pixIndex = double(1 : (lastCalBin(i) - firstCalBin(i) + 1))';
%pixHist = double(inputHist(firstCalBin(i) : lastCalBin(i), i));
pixHist = double(XYZimage(pixIndex, i));
% Find the maximum in each pixel
[h0, mu0] = max(pixHist);
sigma0 = 3;
tau0 = 8;
offset0 = median(pixHist);
% These are the initial guesses for the fit
param0 = [h0 * tau0 * exp(1), mu0, sigma0, tau0, offset0];
% Run the fit
[fit.h(i), ...
fit.mu(i), ...
fit.sigma(i), ...
fit.tau(i), ...
fit.offset(i), ...
fit.exitFlag(i)] = exgfit(param0);
%fprintf('%d\n', i);
end
% Reshape outputs into matrices of the same size as the image sensor
%h = reshape(h, inputSize([1, 2]));
%mu = reshape(mu, inputSize([1, 2]));
%sigma = reshape(sigma, inputSize([1, 2]));
%tau = reshape(tau, inputSize([1, 2]));
%exitFlag = reshape(exitFlag, inputSize([1, 2]));
%% Calculate the well fitted bins
% These are ones that give results similar to the median
fit.goodfit = (fit.sigma < 1.5 * median(fit.sigma(:))) & ...
(fit.sigma > 0.5 * median(fit.sigma(:))) & ...
(~isoutlier(fit.offset)) & ...
(fit.tau < 1.5 * median(fit.tau(:))) & ...
(fit.tau > 0.5 * median(fit.tau(:)));
% The first and last row are alway poor on my SPAD, never use them
fit.goodfit([1, end], :) = false;
%% Calculate the fit parameters
tic
% Set options for the minimization function
options = optimoptions('fmincon', 'Display', 'none');
for i = find(fit.goodfit)'
if mod(i, 100) == 0
if ishandle(hwaitbar)
% Estimated time to completion
eta = numberPixels / i * toc / 86400;
if isinf(eta); eta = 0; end
% Update the waitbar
waitbar(i / numberPixels, ...
hwaitbar, ...
sprintf('%d of %d\n%s (%s ETA)', ...
i, ...
numberPixels, ...
datestr(toc / 86400, 'HH:MM:SS'), ...
datestr(eta, 'HH:MM:SS')));
end
end
% Find the position of the peak, and the the maximum of the peak
% Minimization function
mFun = @(x) (-(exGauss(x, ...
fit.h(i), ...
fit.mu(i), ...
fit.sigma(i), ...
fit.tau(i), ...
fit.offset(i))));
% Run minimization routine to find the peak position
peak.Pos(i) = fminsearch(mFun, fit.mu(i));
% Evaluate the exGauss function to get the value of the peak
peak.Max(i) = exGauss(peak.Pos(i), ...
fit.h(i), ...
fit.mu(i), ...
fit.sigma(i), ...
fit.tau(i), ...
fit.offset(i));
% Create a minimization function for half-maximum
mFun = @(x) abs(peak.Max(i) / 2 - ...
exGauss(x, fit.h(i), fit.mu(i), fit.sigma(i), ...
fit.tau(i), fit.offset(i)) + ...
fit.offset(i) / 2);
% Run minimization routine to find the left half-maximum
peak.risingHM(i) = fmincon(mFun, ...
peak.Pos(i) - fit.sigma(i), ...
[], [], [], [], ...
-Inf, ...
peak.Pos(i), ...
[], options);
% Run minimization routine to find the right half-maximum
peak.fallingHM(i) = fmincon(mFun, ...
peak.Pos(i) + fit.tau(i), ...
[], [], [], [], ...
peak.Pos(i), ...
Inf, ...
[], options);
end
% Calculate the full width at half-maximum
peak.FWHM = peak.fallingHM - peak.risingHM;
%% Run interpolations for the data that was not properly fitted
% interpolate the FWHM for points that are not fitted well
% Create a matrix of points
[X, Y] = meshgrid(1 : size(peak.FWHM, 2), 1 : size(peak.FWHM, 1));
Xv = X(~fit.goodfit);
Yv = Y(~fit.goodfit);
X = X(fit.goodfit);
Y = Y(fit.goodfit);
% Create an interpolant object that can be called
% Use the natural neighbor interpolation method and
% nearest neightbor extrapolation method
% to estimate the full-width half maximum for the missing pixels
F = scatteredInterpolant(X, Y, peak.FWHM(fit.goodfit), ...
'natural', 'nearest');
% Create a new matrix, which contains the estimated and the interpolated
% values of the full-width half-maximum
peak.FWHMinterp = peak.FWHM;
peak.FWHMinterp(~fit.goodfit) = F(Xv, Yv);
% Interpolate the peak position for points that are not fitted well
% Create an interpolant object that can be called
% Use the natural neighbor interpolation method and
% nearest neightbor extrapolation method
% to estimate the peak position for the missing pixels
F = scatteredInterpolant(X, Y, peak.Pos(fit.goodfit), ...
'natural', 'nearest');
% Create a new matrix, which contains the estimated and the interpolated
% values of the full-width half-maximum
peak.PosInterp = peak.Pos;
peak.PosInterp(~fit.goodfit) = F(Xv, Yv);
% Interpolate the rising edge half-maximum for pixels not fitted well
% Create an interpolant object that can be called
% Use the natural neighbor interpolation method and
% nearest neightbor extrapolation method
% to estimate the peak position for the missing pixels
F = scatteredInterpolant(X, Y, peak.risingHM(fit.goodfit), ...
'natural', 'nearest');
% Create a new matrix, which contains the estimated and the interpolated
% values of the full-width half-maximum
peak.risingHMinterp = peak.risingHM;
peak.risingHMinterp(~fit.goodfit) = F(Xv, Yv);
% Interpolate the falling edge half-maximum for pixels not fitted well
% Create an interpolant object that can be called
% Use the natural neighbor interpolation method and
% nearest neightbor extrapolation method
% to estimate the peak position for the missing pixels
F = scatteredInterpolant(X, Y, peak.fallingHM(fit.goodfit), ...
'natural', 'nearest');
% Create a new matrix, which contains the estimated and the interpolated
% values of the full-width half-maximum
peak.fallingHMinterp = peak.fallingHM;
peak.fallingHMinterp(~fit.goodfit) = F(Xv, Yv);
% Close the waitbar
delete(hwaitbar)
% Assign results to the main structure for future use
correction.IRF.fit = fit;
correction.IRF.peak = peak;
%% Run a routine to correct the IRF offset
correction.IRF.corrected = resampleHistogramPar(correction.IRF.raw);