File: hdrl_correlation.c

package info (click to toggle)
cpl-plugin-amber 4.4.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 9,340 kB
  • sloc: ansic: 89,588; sh: 4,337; makefile: 620; python: 295
file content (483 lines) | stat: -rw-r--r-- 17,120 bytes parent folder | download | duplicates (12)
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/*
 * This file is part of the HDRL
 * Copyright (C) 2017 European Southern Observatory
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

/*-----------------------------------------------------------------------------
                                   Includes
 -----------------------------------------------------------------------------*/
#include "hdrl_correlation.h"

#include <math.h>


/*-----------------------------------------------------------------------------
                        Data structures used internally
 -----------------------------------------------------------------------------*/
typedef struct{
    double mean;
    double stdev;
}mean_and_stdev;

/*-----------------------------------------------------------------------------
                               Private Functions
 -----------------------------------------------------------------------------*/

static inline
double calculate_xcorr_sample(const cpl_size shift, const cpl_array * arr1,
        const cpl_array * arr2, const double mean1, const double mean2,
        const double stdev1, const double stdev2);

static inline
mean_and_stdev calculate_mean_and_stdev(const cpl_array * arr1);

static inline cpl_error_code
hdrl_compute_xcorrelation_refine(hdrl_xcorrelation_result* xcorr_res,
        const double bin, const double wrange);

/**
 * @addtogroup hdrl_correlation
 * @{
 */
/*-----------------------------------------------------------------------------
                                   Functions
 -----------------------------------------------------------------------------*/

/* ---------------------------------------------------------------------------*/
/**
 * @brief Constructor for hdrl_xcorrelation_result
 * @param x_corr      Cross correlation. x_corr becomes owned by the returned value,
 *                    do not free x_corr after the wrapping.
 * @param max_idx     Index where the cross correlation reaches its maximum
 * @param half_window Half window used for the cross-correlation calculation
 *
 * @return the constructed object. NULL in case of error. Errors are triggered if
 * data are not self consistent, e.g. if max_idx is greated than the length of
 * x_corr.
 */
/* ---------------------------------------------------------------------------*/

hdrl_xcorrelation_result *
hdrl_xcorrelation_result_wrap(cpl_array * x_corr,  const cpl_size max_idx,
        const cpl_size half_window){

    cpl_ensure(x_corr != NULL, CPL_ERROR_NULL_INPUT, NULL);
    cpl_ensure(max_idx >= 0, CPL_ERROR_ILLEGAL_INPUT, NULL);
    cpl_ensure(max_idx < cpl_array_get_size(x_corr),
            CPL_ERROR_ILLEGAL_INPUT, NULL);

    hdrl_xcorrelation_result *  to_ret = cpl_calloc(1, sizeof(*to_ret));

    to_ret->xcorr = x_corr;
    to_ret->pix_peakpos = max_idx;
    to_ret->half_window = half_window;
    return to_ret;
}

/* ---------------------------------------------------------------------------*/
/**
 * @brief Destructor for hdrl_xcorrelation_result
 * @param self      hdrl_xcorrelation_result to be deleted
 *
 * @return nothing
 */
/* ---------------------------------------------------------------------------*/
void hdrl_xcorrelation_result_delete(hdrl_xcorrelation_result * self){
    if(self == NULL) return;

    cpl_array_delete(self->xcorr);
    cpl_free(self);
}

/* ---------------------------------------------------------------------------*/
/**
 * @brief Get the index where the cross correlation reaches its maximum
 * @param self          hdrl_xcorrelation_result the getter will extract the
 *                      data from
 *
 * @return the index where the cross correlation reaches its maximum
 */
/* ---------------------------------------------------------------------------*/
cpl_size
hdrl_xcorrelation_result_get_peak_pixel(const hdrl_xcorrelation_result * self){

    cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, 0);

    return self->pix_peakpos;
}

/* ---------------------------------------------------------------------------*/
/**
 * @brief Get the index where the cross correlation reaches its maximum, with
 * sub-pixel precision
 * @param self          hdrl_xcorrelation_result the getter will extract the
 *                      data from
 *
 * @return the index where the cross correlation reaches its maximum, with
 * sub-pixel precision
 */
/* ---------------------------------------------------------------------------*/
double  hdrl_xcorrelation_result_get_peak_subpixel
                                        (const hdrl_xcorrelation_result * self){

    cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, 0);

    return self->peakpos;
}

/* ---------------------------------------------------------------------------*/
/**
 * @brief Get the half_window used to calculate the cross-correlation.
 * @param self          hdrl_xcorrelation_result the getter will extract the
 *                      data from
 *
 * @return the half_window used to calculate the cross-correlation.
 */
/* ---------------------------------------------------------------------------*/
cpl_size hdrl_xcorrelation_result_get_half_window
                                        (const hdrl_xcorrelation_result * self){

    cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, 0);

    return self->half_window;
}

/* ---------------------------------------------------------------------------*/
/**
 * @brief Get the estimated standard deviation of the correlation
 * @param self          hdrl_xcorrelation_result the getter will extract the
 *                      data from
 *
 * @return the estimated standard deviation of the correlation
 */
/* ---------------------------------------------------------------------------*/
double
hdrl_xcorrelation_result_get_sigma(const hdrl_xcorrelation_result * self){

    cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, 0);

    return self->sigma;
}

/* ---------------------------------------------------------------------------*/
/**
 * @brief Getter for the cross correlation
 * @param self          hdrl_xcorrelation_result the getter will extract the
 *                      data from
 *
 * @return the cross correlation
 */
/* ---------------------------------------------------------------------------*/
const cpl_array *
hdrl_xcorrelation_result_get_correlation(const hdrl_xcorrelation_result * self){

    cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);

    return self->xcorr;
}

/* ---------------------------------------------------------------------------*/
/**
 * @brief Calculate cross-correlation
 * @param arr1        First array
 * @param arr2        Second array
 * @param half_window half search window where the correlation is calculated
 * @param normalize   CPL_TRUE normalize correlation in mean and rms
 *
 * @return cross correlation and index where the peak is. NULL in case of error.
 *
 * @note: elements marked as invalid in arr1 or arr2 will be treated as they were
 * out-of-boudary pixels.
 */
/* ---------------------------------------------------------------------------*/
hdrl_xcorrelation_result * hdrl_compute_xcorrelation(
                    const cpl_array * arr1, const cpl_array * arr2,
                    const cpl_size half_window, const cpl_boolean normalize){

    cpl_ensure(half_window > 1, CPL_ERROR_INCOMPATIBLE_INPUT, NULL);

    const mean_and_stdev not_normalized = {0.0, 1.0};

    cpl_ensure(arr1 != NULL && arr2 != NULL, CPL_ERROR_NULL_INPUT, NULL);

    cpl_size xcorr_length = 2 * half_window + 1;
    cpl_array * corr = cpl_array_new(xcorr_length, HDRL_TYPE_DATA);

    const mean_and_stdev d1 = normalize ?
            calculate_mean_and_stdev(arr1) : not_normalized;
    const mean_and_stdev d2 = normalize ?
            calculate_mean_and_stdev(arr2) : not_normalized;

    cpl_size max_idx = -1;
    double max_corr = 0.0;

    for(cpl_size i = -half_window; i <= half_window; ++i){
        const double cr = calculate_xcorr_sample(i, arr1, arr2, d1.mean, d2.mean,
                d1.stdev, d2.stdev);

        const cpl_size idx = i + half_window;
        cpl_array_set(corr, idx, cr);

        if(isnan(cr)) continue;

        if(cr >= max_corr || max_idx < 0){
            max_corr = cr;
            max_idx = idx;
        }
    }

    return hdrl_xcorrelation_result_wrap(corr, max_idx, half_window);
}

static inline
cpl_error_code check_if_bad(const hdrl_xcorrelation_result * gfit,
        const cpl_boolean check_refine){

    cpl_ensure_code(gfit != NULL, CPL_ERROR_ILLEGAL_OUTPUT);
    cpl_ensure_code(hdrl_xcorrelation_result_get_peak_pixel(gfit) >=0,
            CPL_ERROR_ILLEGAL_OUTPUT);

    if(check_refine){

        const double px = hdrl_xcorrelation_result_get_peak_subpixel(gfit);
        cpl_ensure_code(px >= 0.0 && !isnan(px), CPL_ERROR_ILLEGAL_OUTPUT);

        const double sigma = hdrl_xcorrelation_result_get_sigma(gfit);
        cpl_ensure_code( sigma > 0.0 && !isnan(sigma),CPL_ERROR_ILLEGAL_OUTPUT);
    }

    return CPL_ERROR_NONE;
}

static inline
cpl_boolean check_and_delete_if_bad(hdrl_xcorrelation_result ** gfit_arg,
        const cpl_boolean check_refine){

    cpl_error_code fail = check_if_bad(*gfit_arg, check_refine);

    if(fail){
        hdrl_xcorrelation_result_delete(*gfit_arg);
        *gfit_arg = NULL;
    }

    return fail ? CPL_FALSE : CPL_TRUE;
}

/* ---------------------------------------------------------------------------*/
/**
 * @brief Calculate gaussian fit on cross-correlation, does a second fitting for
 * refinement.
 * @param arr1        First array
 * @param arr2        Second array
 * @param half_win    half search window where the correlation is calculated
 * @param normalize   CPL_TRUE normalize correlation in mean and rms
 * @param bin         wavelength bin
 * @param wrange      half window wavelength range where the fit is going to be
 *                    done
 *
 * @return gaussian fit for cross correlation.
 */
/* ---------------------------------------------------------------------------*/
hdrl_xcorrelation_result * hdrl_compute_offset_gaussian(
        const cpl_array * arr1,
        const cpl_array * arr2,
        const cpl_size  half_win, const cpl_boolean normalize,
        const double bin, const double wrange){

    cpl_ensure(half_win > 1, CPL_ERROR_ILLEGAL_INPUT, NULL);
    cpl_ensure(arr1 != NULL, CPL_ERROR_NULL_INPUT, NULL);
    cpl_ensure(arr2 != NULL, CPL_ERROR_NULL_INPUT, NULL);

    hdrl_xcorrelation_result * gfit =
            hdrl_compute_offset_gaussian_internal(arr1, arr2, half_win,
                    normalize, bin, wrange);

    cpl_ensure(gfit != NULL, CPL_ERROR_ILLEGAL_OUTPUT, NULL);

    const cpl_size half_win2 =
            (cpl_size)(3. * CPL_MATH_FWHM_SIG * gfit->sigma / bin);

    hdrl_xcorrelation_result_delete(gfit);

    gfit = hdrl_compute_offset_gaussian_internal(arr1, arr2, half_win2,
                    normalize, bin, wrange);

    return gfit;
}

/* ---------------------------------------------------------------------------*/
/**
 * @brief Calculate gaussian fit on cross-correlation
 * @param arr1        First array
 * @param arr2        Second array
 * @param half_win    half search window where the correlation is calculated
 * @param normalize   CPL_TRUE normalize correlation in mean and rms
 * @param bin         wavelength bin
 * @param wrange      half window wavelength range where the fit is going to be
 *                    done
 *
 * @return gaussian fit for cross correlation.
 */
/* ---------------------------------------------------------------------------*/
hdrl_xcorrelation_result * hdrl_compute_offset_gaussian_internal(
        const cpl_array * arr1, const cpl_array * arr2,
        const cpl_size half_win, const cpl_boolean normalize,
        const double bin, const double wrange){

    hdrl_xcorrelation_result * res =
            hdrl_compute_xcorrelation(arr1, arr2, half_win, normalize);

    if(!check_and_delete_if_bad(&res, CPL_FALSE)) return  NULL;

    cpl_error_code fail =
            hdrl_compute_xcorrelation_refine(res, bin, wrange);

    if(fail){
        hdrl_xcorrelation_result_delete(res);
        return NULL;
    }

    if(!check_and_delete_if_bad(&res, CPL_TRUE)) return  NULL;

    return  res;
}

/* ---------------------------------------------------------------------------*/
/**
 * @brief Refine a hdrl_xcorrelation_result using gaussian fit
 * @param xcorr_res   hdrl_xcorrelation_result calculated with pixel precision
 * @param bin         wavelength bin
 * @param wrange      half window wavelength range where the fit is going to be
 *                    done
 *
 * @return gaussian fit for cross correlation.
 */
/* ---------------------------------------------------------------------------*/
static inline cpl_error_code
hdrl_compute_xcorrelation_refine(hdrl_xcorrelation_result* xcorr_res,
        const double bin, const double wrange){

       const cpl_array * xcorr = hdrl_xcorrelation_result_get_correlation(xcorr_res);
       const cpl_size maxpos = hdrl_xcorrelation_result_get_peak_pixel(xcorr_res);

       const cpl_size xcorr_size = cpl_array_get_size(xcorr);
       const cpl_size pre_idx = CPL_MAX(0, maxpos - 1);
       const cpl_size post_idx =  CPL_MIN(maxpos + 1, xcorr_size - 1);

       const double a = cpl_array_get(xcorr, pre_idx, NULL);
       const double b = cpl_array_get(xcorr, post_idx, NULL);
       const double c = cpl_array_get(xcorr, maxpos, NULL);
       /* Find sub-pixel peak initial estimation. Use parabolic assumption */
       const double fraction = (b - a) / ( 4. * c - 2. * a - 2. * b );
       const double subpix_offset = maxpos - fraction;

       xcorr_res->peakpos = subpix_offset * bin;
       xcorr_res->sigma = bin * 10;
       xcorr_res->area = 1.0;

       cpl_size num_elems = 0;

       cpl_vector * wavs_windowed = cpl_vector_new(xcorr_size);
       cpl_vector * corr_windowed = cpl_vector_new(xcorr_size);

       for(cpl_size i = 0; i < xcorr_size; ++i){
           const double w = i * bin;
           int rej = 0;
           const double xcorr_data = cpl_array_get(xcorr, i, &rej);

           if(rej || isnan(xcorr_data)) continue;
           if(w <  xcorr_res->peakpos - wrange
                   || w > xcorr_res->peakpos + wrange) continue;

           cpl_vector_set(corr_windowed, num_elems, xcorr_data);
           cpl_vector_set(wavs_windowed, num_elems, w);

           num_elems++;
       }

       if(num_elems <= 0){
           cpl_vector_delete(wavs_windowed);
           cpl_vector_delete(corr_windowed);
           cpl_ensure_code(CPL_FALSE, CPL_ERROR_ILLEGAL_OUTPUT);
       }

       cpl_vector_set_size(corr_windowed, num_elems);
       cpl_vector_set_size(wavs_windowed, num_elems);

       cpl_error_code code = cpl_vector_fit_gaussian(wavs_windowed, NULL,
               corr_windowed, NULL, CPL_FIT_ALL, &xcorr_res->peakpos, &xcorr_res->sigma,
               &xcorr_res->area, &xcorr_res->offset, &xcorr_res->mse, NULL, NULL);

       /* if the fitting does not converge, CPL_ERROR_CONTINUE is set, the
        * output parameters are calculated using a best-effort approach.
        * See the documentation of cpl_vector_fit_gaussian .*/
       if(code == CPL_ERROR_CONTINUE){
           cpl_error_reset();
       }

       cpl_vector_delete(wavs_windowed);
       cpl_vector_delete(corr_windowed);

       return cpl_error_get_code();
}

static inline
double calculate_xcorr_sample(const cpl_size shift,
        const cpl_array * arr1, const cpl_array * arr2,
        const double mean1, const double mean2,
        const double stdev1, const double stdev2){

    double d = 0.0;
    cpl_size num_el = 0;

    const double norm = 1.00 / sqrt(stdev1 * stdev2);

    const cpl_size l1 = cpl_array_get_size(arr1);
    const cpl_size l2 = cpl_array_get_size(arr2);

    for(cpl_size i = 0; i < l2; i++){
        const cpl_size j = i + shift;
        int rej1 = 0, rej2 = 0;
        if(j < 0) continue;
        if(j >= l1) continue;

        const double v1 = cpl_array_get(arr1, j, &rej1);
        const double v2 = cpl_array_get(arr2, i, &rej2);

        if(rej1 || rej2) continue;

        const double val = norm * (v1 - mean1) * (v2 - mean2);

        d += val;
        num_el++;
    }
    return d / (double)num_el;
}

static inline
mean_and_stdev calculate_mean_and_stdev(const cpl_array * arr1){

    const double mean = cpl_array_get_mean(arr1);
    const double stdev = cpl_array_get_stdev(arr1);

    return (mean_and_stdev){mean, stdev};
}
/**@}*/