56#define MODULE_ID "XSH_CORREL_GAUSSIANS"
63 {
"debug", required_argument, 0,
DEBUG_OPT},
70 puts (
"Unitary test : Create two Gaussians one shifted to the other of a given quantity, then correlate them to check if correlation returns expected shift");
71 puts(
"Usage : ./tetst_xsh_correl_gaussians [options]");
74 puts(
" --debug=<n> : Level of debug LOW | MEDIUM | HIGH [MEDIUM]" );
75 puts(
" --help : What you see" ) ;
77 puts(
"The input files argument MUST be in this order:" );
78 puts(
" 1. PRE frame");
79 puts(
" 2. SOF a) MODEL : [XSH_MOD_CFG_TAB_UVB]");
80 puts(
" b) POLYNOMIAL: [DISP_TAB, ORDER_TAB_EDGES]");
92 while( (opt = getopt_long( argc, argv,
"debug:help",
96 if ( strcmp( optarg,
"LOW")==0){
99 else if ( strcmp( optarg,
"HIGH")==0){
125 double* correl,
const int length,
const int hsize) {
132 double *profile1 = cpl_calloc(length,
sizeof(
double));
133 double *profile2 = cpl_calloc(length,
sizeof(
double));
134 memset( profile1,0,length);
135 memset( profile2,0,length);
145 for (i = 0; i < length; i++)
146 if (norm < prof1[i]){
150 if (i < length && norm < prof2[i]){
155 for (i = 0; i < length; i++) {
156 profile1[i] = prof1[i]/norm;
157 profile2[i] = prof2[i]/norm;
161 int short_length = length - 2*hsize - 1;
162 xsh_msg(
"mk4 short_len=%d",short_length);
170 int nsteps=step_max-step_min+1;
178 for (i = step_min; i <= step_max; i++) {
181 if (k >= length)
break;
182 for (j = 0; j < length && k+j < length; j++) {
183 value += profile1[k] * profile2[k+j];
194 for (i=0 ; i<nsteps ; i++) {
209 double a=correl[maxpos-1];
210 double b=correl[maxpos+1];
211 double c=correl[maxpos];
212 double fraction=(a-b)/(2.*a+2.*b-4.*c);
214 xsh_msg(
"fraction=%g",fraction);
215 shift=maxpos+fraction;
327 int STEP_MIN=-half_search;
328 int STEP_MAX=half_search;
333 for (i = 0; i < width_i; i++)
334 if (norm < line_i[i]){
339 for (i = 0; i < width_t; i++)
340 if (norm < line_t[i]){
345 for (i = 0; i < width_i; i++) {
350 for (i = 0; i < width_t; i++) {
356 nsteps = (STEP_MAX - STEP_MIN) +1 ;
357 xcorr = cpl_malloc(nsteps *
sizeof(
double));
359 xcorr[
step-STEP_MIN] = 0.00 ;
361 for (i=0 ; i<width_t ; i++) {
363 (i+
step < width_i)) {
364 xcorr[
step-STEP_MIN] += line_t[i] * line_i[i+
step];
368 xcorr[
step-STEP_MIN] /= (double) nval ;
370 xcorr_max = xcorr[0] ;
372 for (i=0 ; i<nsteps ; i++) {
373 if (xcorr[i]>xcorr_max) {
375 xcorr_max = xcorr[i];
378 cpl_vector* vcor=cpl_vector_wrap(nsteps,xcorr);
379 double a=xcorr[maxpos-1];
380 double b=xcorr[maxpos+1];
381 double c=xcorr[maxpos];
382 double fraction=(a-b)/(2.*a+2.*b-4.*c);
383 cpl_vector_save(vcor,
"vcor.fits",CPL_BPP_IEEE_FLOAT,NULL,CPL_IO_DEFAULT);
384 cpl_vector_unwrap(vcor);
386 xsh_msg(
"STEP_MIN=%d maxpos=%d",STEP_MIN,maxpos);
387 (*delta) = (double)STEP_MIN + (
double)maxpos;
388 xsh_msg(
"a=%g b=%g c=%g fraction=%g",a,b,c,fraction);
389 xsh_msg(
"maxpos=%d width_i=%d",maxpos,width_i);
390 xsh_msg(
"fractionary delta=%g",*delta+fraction);
405 double norm=
sigma*sqrt(2*CPL_MATH_PI);
408 for(i=0;i<
size;i++) {
410 data[i]=a*exp(-(
x-center)*(
x-center)*inv_2_c2);
413 return cpl_error_get_code();
429int main(
int argc,
char** argv)
434 cpl_msg_set_level( CPL_MSG_DEBUG);
440 if ( (argc-optind) >= 2) {
449 double* gauss_d1=NULL;
450 double* gauss_d2=NULL;
453 cpl_vector* gauss_v1=cpl_vector_new(
size);
454 cpl_vector* gauss_v2=cpl_vector_new(
size);
456 gauss_d1=cpl_vector_get_data(gauss_v1);
457 gauss_d2=cpl_vector_get_data(gauss_v2);
463 int half_search=(int)(2*gauss_s+1);
472 xsh_msg(
"function1d corerel: shift %g",shift_o);
474 double moses_shift=0;
475 double* moses_correl=NULL;
477 moses_correl = cpl_calloc(len_corr,
sizeof(
double));
479 xsh_msg(
"moses_shift=%g",moses_shift);
480 cpl_vector* mc=cpl_vector_wrap(len_corr,moses_correl);
481 cpl_vector_save(mc,
"moses_correl.fits",CPL_BPP_IEEE_FLOAT,NULL,CPL_IO_DEFAULT);
484 cpl_vector_save(gauss_v1,
"gauss_v1.fits",CPL_BPP_IEEE_FLOAT,NULL,CPL_IO_DEFAULT);
485 cpl_vector_save(gauss_v2,
"gauss_v2.fits",CPL_BPP_IEEE_FLOAT,NULL,CPL_IO_DEFAULT);
492 cpl_vector* correl=cpl_vector_new(len_corr);
493 check(cpl_vector_fill(correl, 0.0));
494 double shift=cpl_vector_correlate(correl,gauss_v1,gauss_v2);
496 cpl_vector_save(correl,
"correl.fits",CPL_BPP_IEEE_FLOAT,NULL,CPL_IO_DEFAULT);
499 int maxpos=(int)(shift-1);
500 xsh_msg(
"correl max=%g",cpl_vector_get(correl,shift));
501 xsh_msg(
"Shift: expected: %g computed: %g accuracy:%g ",
502 shift_i,shift_o,fabs((shift_i-shift_o)/shift_i));
505 check(
max = cpl_vector_get_max(correl));
509 pvec=cpl_vector_get_data(correl);
512 for(i=1;i<len_corr;i++) {
518 xsh_msg(
"maxpos my determination: %d",maxpos);
525 a=cpl_vector_get(correl,maxpos-1);
526 b=cpl_vector_get(correl,maxpos+1);
527 c=cpl_vector_get(correl,maxpos);
528 fraction=(a-b)/(2.*a+2.*b-4.*c);
529 xsh_msg(
"len_corr=%d",len_corr);
530 xsh_msg(
"a=%g b=%g c=%g fraction=%g",a,b,c,fraction);
540 if (cpl_error_get_code() != CPL_ERROR_NONE) {
int main()
Unit test of xsh_bspline_interpol.
static void HandleOptions(int argc, char **argv)
static double xsh_correlate_profiles2(const double *prof1, const double *prof2, double *correl, const int length, const int hsize)
static double xsh_function1d_xcorrelate2(double *line_i, int width_i, double *line_t, int width_t, int half_search, double *delta)
cpl_error_code xsh_gauss_gen(double *data, const double center, const double sigma, const int size)
static struct option LongOptions[]
#define XSH_ASSURE_NOT_NULL_MSG(pointer, msg)
#define xsh_error_dump(level)
#define XSH_ASSURE_NOT_ILLEGAL_MSG(cond, msg)
#define xsh_msg(...)
Print a message on info level.
void xsh_free_vector(cpl_vector **v)
Deallocate a vector and set the pointer to NULL.
int xsh_debug_level_set(int level)
set debug level
#define TESTS_CLEAN_WORKSPACE(DRL_ID)
#define TESTS_INIT_WORKSPACE(DRL_ID)
#define TESTS_INIT(DRL_ID)