00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifdef HAVE_CONFIG_H
00027 # include <config.h>
00028 #endif
00029
00030
00036
00039
00040
00041
00042
00043
00044 #include <xsh_error.h>
00045 #include <xsh_msg.h>
00046 #include <xsh_dfs.h>
00047 #include <tests.h>
00048 #include <cpl.h>
00049 #include <math.h>
00050 #include <getopt.h>
00051 #include <string.h>
00052
00053
00054
00055 #define MODULE_ID "XSH_CORRELATE_GAUSSIANS"
00056
00057 enum {
00058 DEBUG_OPT, HELP_OPT
00059 };
00060
00061 static struct option LongOptions[] = {
00062 {"debug", required_argument, 0, DEBUG_OPT},
00063 {"help", 0, 0, HELP_OPT},
00064 {NULL, 0, 0, 0}
00065 };
00066
00067 static void Help( void )
00068 {
00069 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");
00070 puts( "Usage : ./tetst_xsh_correl_gaussians [options]");
00071
00072 puts( "Options" ) ;
00073 puts( " --debug=<n> : Level of debug LOW | MEDIUM | HIGH [MEDIUM]" );
00074 puts( " --help : What you see" ) ;
00075
00076 puts( "The input files argument MUST be in this order:" );
00077 puts( " 1. PRE frame");
00078 puts( " 2. SOF a) MODEL : [XSH_MOD_CFG_TAB_UVB]");
00079 puts( " b) POLYNOMIAL: [DISP_TAB, ORDER_TAB_EDGES]");
00080
00081 TEST_END();
00082 exit(0);
00083 }
00084
00085 static void HandleOptions( int argc, char ** argv)
00086 {
00087 int opt ;
00088 int option_index = 0;
00089
00090 while( (opt = getopt_long( argc, argv, "debug:help",
00091 LongOptions, &option_index )) != EOF){
00092 switch( opt ) {
00093 case DEBUG_OPT:
00094 if ( strcmp( optarg, "LOW")==0){
00095 xsh_debug_level_set( XSH_DEBUG_LEVEL_LOW);
00096 }
00097 else if ( strcmp( optarg, "HIGH")==0){
00098 xsh_debug_level_set( XSH_DEBUG_LEVEL_HIGH);
00099 }
00100 break;
00101 case HELP_OPT:
00102 Help();
00103 break;
00104 default:
00105 break;
00106 }
00107 }
00108 }
00109
00110 cpl_error_code
00111 xsh_gauss_gen(double* data,const double center,const double sigma, const int size)
00112 {
00113
00114 int i=0;
00115 double x=0;
00116 double inv_2_c2=0.5/sigma/sigma;
00117 double norm=sigma*sqrt(2*CPL_MATH_PI);
00118 double a=1./norm;
00119
00120 for(i=0;i<size;i++) {
00121 x=i;
00122 data[i]=a*exp(-(x-center)*(x-center)*inv_2_c2);
00123 }
00124
00125 return cpl_error_get_code();
00126 }
00127
00128
00129
00130
00131
00132
00139
00140
00141 int main( int argc, char** argv)
00142 {
00143
00144 TESTS_INIT( MODULE_ID);
00145 cpl_msg_set_level( CPL_MSG_DEBUG);
00146 xsh_debug_level_set( XSH_DEBUG_LEVEL_MEDIUM) ;
00147
00148 HandleOptions( argc, argv);
00149
00150
00151 if ( (argc-optind) >= 2) {
00152 Help();
00153 }
00154
00155 int ret=0;
00156 int size=100;
00157 double shift_i=5.15;
00158 double shift_o=0;
00159 double gauss_c=0.5*size;
00160 double gauss_s=10.;
00161 double* gauss_d1=NULL;
00162 double* gauss_d2=NULL;
00163
00164
00165 cpl_vector* gauss_v1=cpl_vector_new(size);
00166 cpl_vector* gauss_v2=cpl_vector_new(size);
00167
00168 gauss_d1=cpl_vector_get_data(gauss_v1);
00169 gauss_d2=cpl_vector_get_data(gauss_v2);
00170
00171 check(xsh_gauss_gen(gauss_d1,gauss_c,gauss_s,size));
00172 check(xsh_gauss_gen(gauss_d2,gauss_c+shift_i,gauss_s,size));
00173
00174
00175 int half_search=(int)(2*gauss_s+1);
00176 int len_corr=2*size-1;
00177
00178
00179
00180
00181 cpl_vector* correl=cpl_vector_new(len_corr);
00182 double shift=cpl_vector_correlate(correl,gauss_v1,gauss_v2);
00183
00184 cpl_vector_save(correl,"correl.fits",CPL_BPP_IEEE_FLOAT,NULL,CPL_IO_DEFAULT);
00185 xsh_msg("shift=%g",shift);
00186
00187 double max=0;
00188 int maxpos=0;
00189 double* pvec=NULL;
00190
00191 pvec=cpl_vector_get_data(correl);
00192 max=-100;
00193 int i=0;
00194 for(i=1;i<len_corr;i++) {
00195 if(max<pvec[i] ) {
00196 max=pvec[i];
00197 maxpos=i;
00198 }
00199 }
00200 xsh_msg("maxpos my determination: %d",maxpos);
00201
00202 double a=0;
00203 double b=0;
00204 double c=0;
00205 double fraction=0;
00206
00207 a=cpl_vector_get(correl,maxpos-1);
00208 b=cpl_vector_get(correl,maxpos+1);
00209 c=cpl_vector_get(correl,maxpos);
00210 fraction=(a-b)/(2.*a+2.*b-4.*c);
00211 xsh_msg("len_corr=%d",len_corr);
00212 xsh_msg("a=%g b=%g c=%g fraction=%g",a,b,c,fraction);
00213 xsh_msg("shift - (size-1)=%g",shift -(size-1));
00214 xsh_msg("fraction shift - (size-1)=%g",shift -(size-1)+fraction);
00215
00216
00217 cleanup:
00218 xsh_free_vector(&gauss_v1);
00219 xsh_free_vector(&gauss_v2);
00220 xsh_free_vector(&correl);
00221
00222
00223 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00224 xsh_error_dump( CPL_MSG_ERROR);
00225 ret=1;
00226 }
00227 TEST_END();
00228 return ret;
00229 }
00230