30#ifndef HDRL_USE_PRIVATE
32#define HDRL_USE_PRIVATE
35#include "hdrl_fringe.h"
36#include "hdrl_prototyping.h"
37#include "hdrl_random.h"
51void test_hermite_sum(
void);
52void hdrl_fringe_compute_test_input(
void);
53void hdrl_fringe_correct_test_input(
void);
54void hdrl_fringe_hermite_test(
void);
55void hdrl_mime_fringe_amplitudes_test(
void);
56void hdrl_mime_fringe_amplitudes_ls_test(
void);
58static cpl_matrix * hdrl_mime_hermite_functions_create(
int n,
double center,
59 double scale,
const cpl_matrix * x);
62void hdrl_fringe_hermite_test(
void)
94 w = (b - a) / (n - 1);
97 f = cpl_matrix_new(n, 1);
99 for (i = 0; i < n; i++)
101 double xi = (cpl_matrix_get_data(x))[i];
102 (cpl_matrix_get_data(f))[i] = (1.0 + xi) * exp(-0.5 * xi * xi);
106 funs = hdrl_mime_hermite_functions_create(nfun, center, scale, NULL);
108 cpl_test_error(CPL_ERROR_NULL_INPUT);
109 funs = hdrl_mime_hermite_functions_create(0, center, scale, x);
111 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
112 funs = hdrl_mime_hermite_functions_create(nfun, center, 0.0, x);
114 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
115 funs = hdrl_mime_hermite_functions_create(nfun, center, -1.0, x);
117 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
120 funs = hdrl_mime_hermite_functions_create(nfun, center, scale, x);
121 cpl_test_nonnull(funs);
122 cpl_test_error(CPL_ERROR_NONE);
126 cpl_test_nonnull(coeffs);
127 cpl_test_error(CPL_ERROR_NONE);
128 cpl_test_eq(cpl_matrix_multiply_scalar(coeffs, w), CPL_ERROR_NONE);
132 hseries = hdrl_mime_hermite_series_create(nfun, center, scale, NULL, x);
133 cpl_test_null(hseries);
134 cpl_test_error(CPL_ERROR_NULL_INPUT);
135 hseries = hdrl_mime_hermite_series_create(nfun, center, scale, coeffs, NULL);
136 cpl_test_null(hseries);
137 cpl_test_error(CPL_ERROR_NULL_INPUT);
138 hseries = hdrl_mime_hermite_series_create(nfun, center, scale, NULL, NULL);
139 cpl_test_null(hseries);
140 cpl_test_error(CPL_ERROR_NULL_INPUT);
141 hseries = hdrl_mime_hermite_series_create(0, center, scale, coeffs, x);
142 cpl_test_null(hseries);
143 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
144 hseries = hdrl_mime_hermite_series_create(nfun, center, -1.0, coeffs, x);
145 cpl_test_null(hseries);
146 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
147 hseries = hdrl_mime_hermite_series_create(nfun, center, 0.0, coeffs, x);
148 cpl_test_null(hseries);
149 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
152 hseries = hdrl_mime_hermite_series_create(nfun, center, scale, coeffs, x);
153 cpl_test_nonnull(hseries);
154 cpl_test_error(CPL_ERROR_NONE);
155 cpl_test_eq(cpl_matrix_subtract(hseries, f), CPL_ERROR_NONE);
158 cpl_matrix_delete(x);
159 cpl_matrix_delete(f);
160 cpl_matrix_delete(funs);
161 cpl_matrix_delete(coeffs);
162 cpl_matrix_delete(hseries);
166void test_fringe_mime_gmx1(
void)
176 params[2] = sqrt(0.5);
180 params[5] = sqrt(0.5);
184 hdrl_mime_gmix1(x, params, &y);
185 cpl_test_leq(fabs(y - 3.678794411714423e-01), 1e-15);
188 hdrl_mime_gmix1(x, params, &y);
189 cpl_test_leq(fabs(y - 1.0), 1e-15);
192 hdrl_mime_gmix1(x, params, &y);
193 cpl_test_leq(fabs(y - 3.678794411714423e-01), 1e-15);
195 hdrl_mime_gmix_derivs1(x, params, result);
196 cpl_test_leq(fabs(result[0] - 3.678794411714423e-01), 1e-15);
197 cpl_test_leq(fabs(result[1] - 7.357588823428847e-01), 1e-15);
198 cpl_test_leq(fabs(result[2] - 1.040520190045778e+00), 1e-15);
199 cpl_test_leq(fabs(result[3] - 3.678794411714423e-01), 1e-15);
200 cpl_test_zero(result[4]);
201 cpl_test_zero(result[5]);
206 params[2] = sqrt(0.5);
210 params[5] = sqrt(0.5);
214 hdrl_mime_gmix1(x, params, &y);
215 cpl_test_leq(fabs(y - 3.678794411714423e-01), 1e-15);
218 hdrl_mime_gmix1(x, params, &y);
219 cpl_test_leq(fabs(y - 1.0), 1e-15);
222 hdrl_mime_gmix1(x, params, &y);
223 cpl_test_leq(fabs(y - 3.678794411714423e-01), 1e-15);
225 hdrl_mime_gmix_derivs1(x, params, result);
226 cpl_test_leq(fabs(result[0] - 3.678794411714423e-01), 1e-15);
227 cpl_test_zero(result[1]);
228 cpl_test_zero(result[2]);
229 cpl_test_leq(fabs(result[3] - 3.678794411714423e-01), 1e-15);
230 cpl_test_leq(fabs(result[4] - 7.357588823428847e-01), 1e-15);
231 cpl_test_leq(fabs(result[5] - 1.040520190045778e+00), 1e-15);
254static cpl_matrix * hdrl_mime_hermite_functions_create(
int n,
double center,
255 double scale,
const cpl_matrix * x)
265 cpl_ensure(x, CPL_ERROR_NULL_INPUT, NULL);
267 if (n < 1 || scale <= 0.0)
269 cpl_error_set(cpl_func, CPL_ERROR_ILLEGAL_INPUT);
274 nr = cpl_matrix_get_nrow(x) * cpl_matrix_get_ncol(x);
277 funs = cpl_matrix_new(nr, n);
280 rt = 1.0 / sqrt(sqrt(CPL_MATH_PI));
283 m = cpl_matrix_get_data(funs);
284 mx = cpl_matrix_get_data_const(x);
285 for (i = 0; i < nr; i++, m += n)
287 xi = (mx[i] - center) / scale;
288 m[0] = rt * exp(-0.5 * xi * xi);
292 m = cpl_matrix_get_data(funs);
293 mx = cpl_matrix_get_data_const(x);
295 for (i = 0; i < nr; i++, m += n)
297 xi = (mx[i] - center) / scale;
298 m[1] = rt * sqrt(2.0) * xi * exp(-0.5 * xi * xi);
302 m = cpl_matrix_get_data(funs);
303 for (i = 0; i < nr; i++, m += n)
305 xi = (mx[i] - center) / scale;
306 for (j = 2; j < n; j++)
308 m[j] = sqrt(2.0) * xi * m[j - 1] - sqrt(j - 1) * m[j - 2];
309 m[j] = m[j] / sqrt(j);
314 cpl_matrix_multiply_scalar(funs, 1 / sqrt(scale));
320void test_hermite_sum(
void)
326 double center, scale;
349 sums = hdrl_mime_hermite_functions_sums_create(nfun, center, scale, NULL);
351 cpl_test_error(CPL_ERROR_NULL_INPUT);
352 sums = hdrl_mime_hermite_functions_sums_create(0, center, scale, x);
354 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
355 sums = hdrl_mime_hermite_functions_sums_create(nfun, center, 0.0, x);
357 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
358 sums = hdrl_mime_hermite_functions_sums_create(nfun, center, -1.0, x);
360 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
363 sums = hdrl_mime_hermite_functions_sums_create(nfun, center, scale, x);
364 cpl_test_nonnull(sums);
365 cpl_test_error(CPL_ERROR_NONE);
367 cpl_test_leq(fabs(cpl_matrix_get(sums, 0, 0) - 1.685081590066050e+00),
369 cpl_test_leq(fabs(cpl_matrix_get(sums, 1, 0) - 9.093843925414908e-01),
371 cpl_test_leq(fabs(cpl_matrix_get(sums, 2, 0) - 4.521636055506448e-01),
373 cpl_test_leq(fabs(cpl_matrix_get(sums, 3, 0) - 8.130124958110769e-01),
375 cpl_test_leq(fabs(cpl_matrix_get(sums, 4, 0) - 8.013868548156017e-01),
379 cpl_matrix_delete(x);
380 cpl_matrix_delete(sums);
386void hdrl_fringe_compute_test_input(
void)
390 hdrl_parameter * collapse_params;
394 cpl_imagelist * ilist_obj = cpl_imagelist_new();
395 cpl_mask * stat_mask = cpl_mask_new(nx, ny);
398 cpl_image * contrib_map;
405 cpl_image * cima1 = cpl_image_new(nx, ny, CPL_TYPE_FLOAT);
406 cpl_image * cima2 = cpl_image_new(nx, ny, CPL_TYPE_FLOAT);
410 cpl_image_add_scalar(cima1, 1.);
411 cpl_image_add_scalar(cima2, 10.);
415 &master, &contrib_map, NULL);
416 cpl_test_error(CPL_ERROR_NULL_INPUT);
417 cpl_test_null(master);
418 cpl_test_null(contrib_map);
420 cpl_table *qctable = (cpl_table*)0xbadbeef;
422 &master, &contrib_map, &qctable);
423 cpl_test_error(CPL_ERROR_NULL_INPUT);
424 cpl_test_null(master);
425 cpl_test_null(contrib_map);
428 &master, &contrib_map, NULL);
429 cpl_test_error(CPL_ERROR_NULL_INPUT);
430 cpl_test_null(master);
431 cpl_test_null(contrib_map);
435 cpl_imagelist_set(ilist_obj, cima1, 0);
437 &master, &contrib_map, NULL);
438 cpl_test_error(CPL_ERROR_INCOMPATIBLE_INPUT);
439 cpl_test_null(master);
440 cpl_test_null(contrib_map);
441 cpl_imagelist_set(ilist_obj, cima2, 1);
447 &master, &contrib_map, NULL);
448 cpl_test_error(CPL_ERROR_NULL_INPUT);
449 cpl_test_null(master);
450 cpl_test_null(contrib_map);
457 &master, &contrib_map, NULL);
458 cpl_test_error(CPL_ERROR_INCOMPATIBLE_INPUT);
459 cpl_test_null(master);
460 cpl_test_null(contrib_map);
486 &master, &contrib_map, NULL);
487 cpl_test_error(CPL_ERROR_NONE);
488 cpl_test_nonnull(master);
489 cpl_test_nonnull(contrib_map);
491 cpl_image_delete(contrib_map);
497 cpl_imagelist_delete(ilist_obj);
499 cpl_mask_delete(stat_mask);
503void hdrl_fringe_correct_test_input(
void)
509 cpl_imagelist * ilist_obj = cpl_imagelist_new();
510 cpl_mask * stat_mask = cpl_mask_new(nx, ny);
521 cpl_image * cima1 = cpl_image_new(nx, ny, CPL_TYPE_FLOAT);
522 cpl_image * cima2 = cpl_image_new(nx, ny, CPL_TYPE_FLOAT);
526 cpl_image_add_scalar(cima1, 1.);
527 cpl_image_add_scalar(cima2, 10.);
531 cpl_test_error(CPL_ERROR_NULL_INPUT);
535 cpl_imagelist_set(ilist_obj, cima1, 0);
537 cpl_test_error(CPL_ERROR_INCOMPATIBLE_INPUT);
538 cpl_imagelist_set(ilist_obj, cima2, 1);
544 cpl_test_error(CPL_ERROR_NULL_INPUT);
551 cpl_test_error(CPL_ERROR_INCOMPATIBLE_INPUT);
560 cpl_test_error(CPL_ERROR_NONE);
563 cpl_test_error(CPL_ERROR_NONE);
566 cpl_test_error(CPL_ERROR_NONE);
572 hdrl_random_state * rng = hdrl_random_state_new(1, NULL);
577 for(cpl_size i = 1; i <= sizex; i++) {
578 for(cpl_size j = 1; j <= sizey; j++) {
579 cpl_image_set(cplima1, i, j, (
double)hdrl_random_poisson(rng, 100.));
580 cpl_image_set(cplima2, i, j, (
double)hdrl_random_poisson(rng, 200.));
584 hdrl_random_state_delete(rng);
585 float* pobj = cpl_image_get_data_float(cima1);
586 int sx = cpl_image_get_size_x(cima1);
587 int sy = cpl_image_get_size_y(cima1);
588 int i_min = 0.25 * sx;
589 int i_max = 0.75 * sx;
590 int j_min = 0.25 * sy;
591 int j_max = 0.75 * sy;
593 for(
int j = j_min; j < j_max; j++) {
594 for(
int i = i_min; i < i_max; i++) {
599 pobj = cpl_image_get_data_float(cima2);
600 for(
int j = j_min; j < j_max; j++) {
601 for(
int i = i_min; i < i_max; i++) {
610 cpl_binary* pmask = cpl_mask_get_data(stat_mask);
612 for(
int j = j_min; j < j_max; j++) {
613 for(
int i = i_min; i < i_max; i++) {
619 cpl_image* contrib_map=NULL;
620 hdrl_parameter * collapse_params;
624 &masterfringe, &contrib_map, NULL);
626 cpl_test_error(CPL_ERROR_NONE);
632 cpl_imagelist_delete(ilist_obj);
634 cpl_image_delete(contrib_map);
636 cpl_mask_delete(stat_mask);
642void hdrl_mime_fringe_amplitudes_test(
void)
645 cpl_image * image_double = cpl_image_new(10, 10, CPL_TYPE_DOUBLE);
646 cpl_image * image_float = cpl_image_new(10, 10, CPL_TYPE_FLOAT);
647 cpl_mask * mask = cpl_mask_new(10, 10);
651 matrix = hdrl_mime_fringe_amplitudes(NULL, NULL);
652 cpl_test_error(CPL_ERROR_NULL_INPUT);
653 cpl_test_null(matrix);
655 matrix = hdrl_mime_fringe_amplitudes(image_double, NULL);
656 cpl_test_error(CPL_ERROR_NULL_INPUT);
657 cpl_test_null(matrix);
659 matrix = hdrl_mime_fringe_amplitudes(NULL, mask);
660 cpl_test_error(CPL_ERROR_NULL_INPUT);
661 cpl_test_null(matrix);
664 matrix = hdrl_mime_fringe_amplitudes(image_float, mask);
665 cpl_test_error(CPL_ERROR_INVALID_TYPE);
666 cpl_test_null(matrix);
670 cpl_test_error(CPL_ERROR_NONE);
671 matrix = hdrl_mime_fringe_amplitudes(image_double, mask);
672 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
673 cpl_test_null(matrix);
675 cpl_mask_delete(mask);
676 cpl_image_delete(image_float);
677 cpl_image_delete(image_double);
681void hdrl_mime_fringe_amplitudes_ls_test(
void)
684 cpl_image * image_double1 = cpl_image_new(10, 10, CPL_TYPE_DOUBLE);
685 cpl_image * image_double2 = cpl_image_new(10, 10, CPL_TYPE_DOUBLE);
686 cpl_image * image_float = cpl_image_new(10, 10, CPL_TYPE_FLOAT);
687 cpl_mask * mask = cpl_mask_new(10, 10);
691 matrix = hdrl_mime_fringe_amplitudes_ls(NULL, NULL, NULL);
692 cpl_test_error(CPL_ERROR_NULL_INPUT);
693 cpl_test_null(matrix);
695 matrix = hdrl_mime_fringe_amplitudes_ls(NULL, mask, image_double2);
696 cpl_test_error(CPL_ERROR_NULL_INPUT);
697 cpl_test_null(matrix);
699 matrix = hdrl_mime_fringe_amplitudes_ls(image_double1, NULL, image_double2);
700 cpl_test_error(CPL_ERROR_NULL_INPUT);
701 cpl_test_null(matrix);
703 matrix = hdrl_mime_fringe_amplitudes_ls(image_double1, mask, NULL);
704 cpl_test_error(CPL_ERROR_NULL_INPUT);
705 cpl_test_null(matrix);
708 matrix = hdrl_mime_fringe_amplitudes_ls(image_float, mask, image_double2);
709 cpl_test_error(CPL_ERROR_INVALID_TYPE);
710 cpl_test_null(matrix);
712 matrix = hdrl_mime_fringe_amplitudes_ls(image_double1, mask, image_float);
713 cpl_test_error(CPL_ERROR_INVALID_TYPE);
714 cpl_test_null(matrix);
718 cpl_test_error(CPL_ERROR_NONE);
719 matrix = hdrl_mime_fringe_amplitudes_ls(image_double1, mask, image_double2);
720 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
721 cpl_test_null(matrix);
723 cpl_mask_delete(mask);
724 cpl_image_delete(image_float);
725 cpl_image_delete(image_double2);
726 cpl_image_delete(image_double1);
738 cpl_test_init(PACKAGE_BUGREPORT, CPL_MSG_WARNING);
740 hdrl_fringe_hermite_test();
741 test_fringe_mime_gmx1();
743 hdrl_fringe_compute_test_input();
744 hdrl_fringe_correct_test_input();
745 hdrl_mime_fringe_amplitudes_test();
746 hdrl_mime_fringe_amplitudes_ls_test();
749 return cpl_test_end(0);
hdrl_parameter * hdrl_collapse_mean_parameter_create(void)
create a parameter object for mean
cpl_error_code hdrl_fringe_correct(hdrl_imagelist *ilist_fringe, const cpl_imagelist *ilist_obj, const cpl_mask *stat_mask, const hdrl_image *masterfringe, cpl_table **qctable)
Scales and subtracts the master fringe from the images.
cpl_error_code hdrl_fringe_compute(hdrl_imagelist *ilist_fringe, const cpl_imagelist *ilist_obj, const cpl_mask *stat_mask, const hdrl_parameter *collapse_params, hdrl_image **master, cpl_image **contrib_map, cpl_table **qctable)
Calculates the master fringe and contribution map based on the.
cpl_error_code hdrl_image_add_scalar(hdrl_image *self, hdrl_value value)
Elementwise addition of a scalar to an image.
cpl_size hdrl_image_get_size_y(const hdrl_image *self)
return size of Y dimension of image
cpl_size hdrl_image_get_size_x(const hdrl_image *self)
return size of X dimension of image
cpl_image * hdrl_image_get_image(hdrl_image *himg)
get data as cpl image
hdrl_image * hdrl_image_new(cpl_size nx, cpl_size ny)
create new zero filled hdrl image
void hdrl_image_delete(hdrl_image *himg)
delete hdrl_image
hdrl_image * hdrl_imagelist_unset(hdrl_imagelist *himlist, cpl_size pos)
Remove an image from an imagelist.
cpl_error_code hdrl_imagelist_set(hdrl_imagelist *himlist, hdrl_image *himg, cpl_size pos)
Insert an image into an imagelist.
void hdrl_imagelist_delete(hdrl_imagelist *himlist)
Free all memory used by a hdrl_imagelist object including the images.
hdrl_imagelist * hdrl_imagelist_new(void)
Create an empty imagelist.
void hdrl_parameter_delete(hdrl_parameter *obj)
shallow delete of a parameter
cpl_matrix * hdrl_mime_matrix_product_left_transpose_create(const cpl_matrix *mat1, const cpl_matrix *mat2)
Create the product of the transpose of a matrix with another matrix.
cpl_matrix * hdrl_mime_matrix_linspace_create(int n, double a, double b)
Create equally spaced nodes.