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 <xsh_utils.h>
00048 #include <tests.h>
00049 #include <cpl.h>
00050 #include <math.h>
00051 #include <getopt.h>
00052 #include <string.h>
00053
00054
00055
00056
00057 #define MODULE_ID "XSH_CORRELATE_GAUSSIANS"
00058
00059 enum {
00060 DEBUG_OPT, HELP_OPT
00061 };
00062
00063 static struct option LongOptions[] = {
00064 {"debug", required_argument, 0, DEBUG_OPT},
00065 {"help", 0, 0, HELP_OPT},
00066 {NULL, 0, 0, 0}
00067 };
00068
00069 static void Help( void )
00070 {
00071 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");
00072 puts( "Usage : ./tetst_xsh_correl_gaussians [options]");
00073
00074 puts( "Options" ) ;
00075 puts( " --debug=<n> : Level of debug LOW | MEDIUM | HIGH [MEDIUM]" );
00076 puts( " --help : What you see" ) ;
00077
00078 puts( "The input files argument MUST be in this order:" );
00079 puts( " 1. PRE frame");
00080 puts( " 2. SOF a) MODEL : [XSH_MOD_CFG_TAB_UVB]");
00081 puts( " b) POLYNOMIAL: [DISP_TAB, ORDER_TAB_EDGES]");
00082
00083 TEST_END();
00084 exit(0);
00085 }
00086
00087 static void HandleOptions( int argc, char ** argv)
00088 {
00089 int opt ;
00090 int option_index = 0;
00091
00092 while( (opt = getopt_long( argc, argv, "debug:help",
00093 LongOptions, &option_index )) != EOF){
00094 switch( opt ) {
00095 case DEBUG_OPT:
00096 if ( strcmp( optarg, "LOW")==0){
00097 xsh_debug_level_set( XSH_DEBUG_LEVEL_LOW);
00098 }
00099 else if ( strcmp( optarg, "HIGH")==0){
00100 xsh_debug_level_set( XSH_DEBUG_LEVEL_HIGH);
00101 }
00102 break;
00103 case HELP_OPT:
00104 Help();
00105 break;
00106 default:
00107 break;
00108 }
00109 }
00110 }
00111
00112
00113 cpl_error_code
00114 xsh_gauss_gen(double* data,const double center,const double sigma, const int size)
00115 {
00116
00117 int i=0;
00118 double x=0;
00119 double inv_2_c2=0.5/sigma/sigma;
00120 double norm=sigma*sqrt(2*CPL_MATH_PI);
00121 double a=1./norm;
00122
00123 for(i=0;i<size;i++) {
00124 x=i;
00125 data[i]=a*exp(-(x-center)*(x-center)*inv_2_c2);
00126 }
00127
00128 return cpl_error_get_code();
00129 }
00130
00131
00132
00133
00134
00135
00142
00143
00144 int main( int argc, char** argv)
00145 {
00146
00147 TESTS_INIT( MODULE_ID);
00148 cpl_msg_set_level( CPL_MSG_DEBUG);
00149 xsh_debug_level_set( XSH_DEBUG_LEVEL_MEDIUM) ;
00150
00151 HandleOptions( argc, argv);
00152
00153
00154 if ( (argc-optind) >= 2) {
00155 Help();
00156 }
00157
00158 int ret=0;
00159 int size=1000000;
00160 double shift_i=5.15;
00161 double shift_o=0;
00162 double gauss_c=0.5*size;
00163 double gauss_s=10.;
00164 double* gauss_d1=NULL;
00165 double* gauss_d2=NULL;
00166
00167 cpl_vector* gauss_v1=cpl_vector_new(size);
00168 cpl_vector* gauss_v2=cpl_vector_new(size);
00169
00170 gauss_d1=cpl_vector_get_data(gauss_v1);
00171 gauss_d2=cpl_vector_get_data(gauss_v2);
00172
00173 check(xsh_gauss_gen(gauss_d1,gauss_c,gauss_s,size));
00174 check(xsh_gauss_gen(gauss_d2,gauss_c+shift_i,gauss_s,size));
00175
00176 int half_search=(int)(3*gauss_s+1);
00177 int len_corr=2*size-1;
00178
00179 double* xcorr=NULL;
00180
00181 double corr=0;
00182 xcorr=xsh_function1d_xcorrelate(gauss_d1,size,gauss_d2,size,half_search,1,&corr,&shift_o);
00183 cpl_vector_save(gauss_v1,"gauss_v1.fits",CPL_BPP_IEEE_FLOAT,NULL,CPL_IO_DEFAULT);
00184 cpl_vector_save(gauss_v2,"gauss_v2.fits",CPL_BPP_IEEE_FLOAT,NULL,CPL_IO_DEFAULT);
00185 cpl_free(xcorr);
00186
00187 cleanup:
00188 xsh_free_vector(&gauss_v1);
00189 xsh_free_vector(&gauss_v2);
00190
00191
00192 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00193 xsh_error_dump( CPL_MSG_ERROR);
00194 ret=1;
00195 }
00196 TEST_END();
00197 return ret;
00198 }
00199