-
Notifications
You must be signed in to change notification settings - Fork 0
/
SPAD-corrections.cpp
331 lines (251 loc) · 8.57 KB
/
SPAD-corrections.cpp
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
324
325
326
327
328
329
330
331
#include <windows.h>
#include "SPAD-correct.h"
#include "SPAD-correct_internal.h"
#include <random>
#include <cmath>
extern double* gBinWidthFactors;
extern int gnBinWidthFactors;
extern double* gTimebaseShifts;
extern int gnTimebaseShifts;
extern double* gTimebaseScales;
extern int gnTimebaseScales;
// MSVC Binomial random number generation, 39 ms for 16x16
static std::random_device rd;
static std::mt19937_64 gen(rd());
using BinomialDist = std::binomial_distribution<>;
int MSVC_rbinom(int n, double p)
{
if (p <= 0.0) return(0);
if (p >= 1.0) return(n);
BinomialDist rand_binom(n, p);
return rand_binom(gen);
}
// Brute force photon by photon, 17 ms for 16x16
// Speed depends on n with large n slower
///*
int rbinom(int n, double p)
{
if (p <= 0.0) return(0);
if (p >= 1.0) return(n);
// if (n > 5) return(MSVC_rbinom(n, p)); ???? Makes it 2x slower with my data
if (n > 100) return(MSVC_rbinom(n, p));
int x = 0;
int P = (int)(p * RAND_MAX);
for (int i = 0; i < n; i++) {
if (rand() < P) {
x++;
}
}
return (x);
}
//*/
// Non random alternative for comparison with redistribution of photons fractionally
// Should return n*p
// But result needs to remain integer for saving back to uint16
// To use, uncomment and comment the real version.
// This is better tested in R.
/*
int rbinom(int n, double p)
{
return ((int)((double)n * p));
}
*/
/*
Set functions for bin_width_factors (timebins * detectors) and time shifts (per detector)
*/
/*
i = in bin
j = out bin
f = amount of time to fill in bin j
t = time available in bin i
n = photons available in bin i
N = photons passing i to j
*/
USHORT* combined_correction(USHORT* trans, int nbins,
double bin_borders[], int bin_jindexes[])
{
USHORT* new_Int;
new_Int = (USHORT*)calloc(nbins, sizeof(USHORT));
if (new_Int == NULL) return NULL;
for (int i = 0; i < nbins; i++) {
int j, N;
double f, p;
double b1 = bin_borders[i];
double b2 = bin_borders[i + 1]; // by design it has nbins+1 values
double t = b2 - b1;
if (t <= 0.0) continue; // bin i has no width (!)
int n = (int)trans[i];
if (n <= 0) continue; // bin i has no photons
int bj1 = bin_jindexes[i];
int bj2 = bin_jindexes[i+1]; // by design it has nbins+1 values
j = bj1;
f = min(1 - (b1 - bj1), t); // pre, not more than what is available
p = f / t;
N = rbinom(n, p);
if(j>=0 && j<nbins)
new_Int[j] += N;
j = j + 1;
t = t - f;
n = n - N;
if (bj1 < nbins) { // if lower bin limit is after the transient there is nothing to do
if (bj2 >= 0) { // if upper bin limit is before the transient there is nothing to do
while (j < bj2) {
f = 1; // whole
p = f / t;
N = rbinom(n, p);
if (j >= 0 && j < nbins)
new_Int[j] += N;
j = j + 1;
t = t - f;
n = n - N;
}
}
}
//p = 1; # remainder
N = n;
if (j >= 0 && j < nbins)
new_Int[j] += N;
}
return(new_Int);
}
int correct_transient(USHORT* trans, int nbins, double bin_borders[], int bin_jindexes[])
{
if (trans == NULL) return(-1);
USHORT* signal = combined_correction(trans, nbins, bin_borders, bin_jindexes);
if (signal == NULL) {
return(-2);
}
memcpy(trans, signal, nbins * sizeof(USHORT));
free(signal);
return(0);
}
void calc_bin_borders(double* bin_width_factors, int nbins, double shift, double scale, double **times, int **jvals)
{
int nvals = nbins + 1;
double* vals = (double*)malloc(nvals * sizeof(double));
int* jval = (int*)malloc(nvals * sizeof(int));
double t = -shift;
for (int i = 0; i < nvals; i++) {
vals[i] = t;
jval[i] = (int)ceil(t) - 1;
t += bin_width_factors[i] * scale;
}
*times = vals;
*jvals = jval;
}
/// Struct to hold info for each thread for thread_correct
typedef struct
{
USHORT* image;
int width;
int height;
int timebins;
int start_row, stop_row;
} thread_correct_info;
void thread_correct(void* param)
{
USHORT* trans = NULL;
double* bin_width_factors = NULL;
thread_correct_info* info = (thread_correct_info*)param;
USHORT* image = info->image;
int width = info->width;
int height = info->height;
int timebins = info->timebins;
int start = info->start_row;
int stop = info->stop_row;
trans = &(image[start * width * timebins]); // init to first transient
bin_width_factors = &(gBinWidthFactors[start * timebins]); // init to factors for first pixel
int k = start * width; // index into gTimebaseShifts and gTimebaseScales
for (int i = start; i < stop; i++) {
for (int j = 0; j < width; j++) {
// calculate the bin borders for transient in this pixel
double* bin_borders;
int* bin_jindexes;
calc_bin_borders(bin_width_factors, timebins, gTimebaseShifts[k], gTimebaseScales[k], &bin_borders, &bin_jindexes);
correct_transient(trans, timebins, bin_borders, bin_jindexes);
trans += timebins; // next transient
k++;
free(bin_borders);
free(bin_jindexes);
bin_width_factors += timebins; // factors for next pixel
}
}
}
/*
Correct for INL and DNL
image must be a sorted 3D time resolved image.
uses global bin width factors etc.
*/
int SPAD_CorrectTransients(USHORT* image, int width, int height, int timebins)
{
const int nThreads = 16;
HANDLE hThread[16];
thread_correct_info info[16];
int rows_per_thread = height / nThreads;
int i;
// DEBUG with single thread
//return (SPAD_CorrectTransients_SingleThread(image, width, height, timebins));
if (!gBinWidthFactors && !gTimebaseShifts && !gTimebaseScales) {
printf("Warning: No calibration set, nothing to do!\n");
return (0);
}
if (!gBinWidthFactors) SPAD_reset_bin_width_factors(width, height, timebins);
if (!gTimebaseShifts) SPAD_reset_timebase_shifts(width, height, timebins);
if (!gTimebaseScales) SPAD_reset_timebase_scales(width, height);
// Seed random number generation
srand((unsigned int)time(NULL));
clock_t tStart = clock();
printf("Starting %d threads\n", nThreads);
for (i = 0; i < nThreads - 1; i++) {
info[i].image = image;
info[i].width = width;
info[i].height = height;
info[i].timebins = timebins;
info[i].start_row = rows_per_thread * i;
info[i].stop_row = info[i].start_row + rows_per_thread;
hThread[i] = (HANDLE)_beginthread(thread_correct, 0, &info[i]);
}
// Last thread gets remaining rows
info[i].image = image;
info[i].width = width;
info[i].height = height;
info[i].timebins = timebins;
info[i].start_row = rows_per_thread * i;
info[i].stop_row = height;
hThread[i] = (HANDLE)_beginthread(thread_correct, 0, &info[i]);
// Wait for threads to end
DWORD ret = WaitForMultipleObjects(nThreads, hThread, TRUE, ULONG_MAX); // wait for as long as we can
if (ret == WAIT_TIMEOUT) {
printf("ERROR: THREAD TIMEOUT\n");
return(-1);
}
printf("Finished threads: %.2fs\n", ((double)clock() - (double)tStart) / CLOCKS_PER_SEC);
return(0);
}
int SPAD_CorrectTransients_SingleThread(USHORT* image, int width, int height, int timebins)
{
USHORT* trans = NULL;
double* bin_width_factors = NULL;
if (!gBinWidthFactors) SPAD_reset_bin_width_factors(width, height, timebins);
if (!gTimebaseShifts) SPAD_reset_timebase_shifts(width, height, timebins);
if (!gTimebaseScales) SPAD_reset_timebase_scales(width, height);
trans = image; // init to first transient
bin_width_factors = gBinWidthFactors; // init to factors for first pixel
// Seed random number generation
srand((unsigned int)time(NULL));
int k = 0; // index into gTimebaseShifts and gTimebaseScales
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
double* bin_borders;
int* bin_jindexes;
calc_bin_borders(bin_width_factors, timebins, gTimebaseShifts[k], gTimebaseScales[k], &bin_borders, &bin_jindexes);
correct_transient(trans, timebins, bin_borders, bin_jindexes);
trans += timebins; // next transient
k++;
free(bin_borders);
free(bin_jindexes);
bin_width_factors += timebins; // factors for next pixel
}
}
return(0);
}