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