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_vector*
00111 xsh_vector_supersample(cpl_vector* v, const int factor)
00112 {
00113 cpl_vector* result=NULL;
00114 int size=cpl_vector_get_size(v);
00115 double* dv=NULL;
00116 double* dr=NULL;
00117 int size_new=size*factor;
00118 double dfactor=factor;
00119 int i=0,j=0;
00120 double m=0;
00121
00122 xsh_msg("rk1 size_new=%d",size_new);
00123 result=cpl_vector_new(size_new);
00124 xsh_msg("rk2");
00125 dv=cpl_vector_get_data(v);
00126 xsh_msg("rk3");
00127 dr=cpl_vector_get_data(result);
00128 xsh_msg("rk4");
00129 xsh_msg("0-val=%g",dv[0]);
00130 xsh_msg("1-val=%g",dv[1]);
00131
00132 for(i=0;i<size-1;i++) {
00133 m=(dv[i+1]-dv[i])/dfactor;
00134
00135 for(j=0;j<factor;j++) {
00136 dr[i*factor+j]=dv[i]+m*j;
00137
00138 }
00139 }
00140
00141 return result;
00142 }
00143
00144
00145
00146
00147
00154
00155
00156 int main( int argc, char** argv)
00157 {
00158
00159 TESTS_INIT( MODULE_ID);
00160 cpl_msg_set_level( CPL_MSG_DEBUG);
00161 xsh_debug_level_set( XSH_DEBUG_LEVEL_MEDIUM) ;
00162
00163 HandleOptions( argc, argv);
00164
00165
00166 if ( (argc-optind) >= 2) {
00167 Help();
00168 }
00169
00170 int ret=0;
00171 int size=100;
00172 int size_obs=0;
00173 int size_ref=0;
00174
00175 double shift_i=5.15;
00176 double shift_o=0;
00177 double gauss_c=0.5*size;
00178 double gauss_s=10.;
00179 double* dobs=NULL;
00180 double* dref=NULL;
00181 const int file_size=256;
00182 char dir[file_size];
00183 char file_obs[file_size];
00184 char file_ref[file_size];
00185
00186 sprintf(dir,"%s","/media/SCRATCH_BACKUP/data7/xsh/spr/RESPONSE_SLIT_NOD_UVB_GD71/");
00187 sprintf(file_obs,"%svof.fits",dir);
00188 sprintf(file_ref,"%svrf.fits",dir);
00189 xsh_msg("file_obs=%s",file_obs);
00190 xsh_msg("file_ref=%s",file_ref);
00191
00192 cpl_vector* vobs=NULL;
00193 cpl_vector* vref=NULL;
00194 cpl_vector* vobs_res=NULL;
00195 cpl_vector* vref_res=NULL;
00196 check(vobs=cpl_vector_load(file_obs,0));
00197 check(vref=cpl_vector_load(file_ref,0));
00198 size_obs=cpl_vector_get_size(vobs);
00199 size_ref=cpl_vector_get_size(vref);
00200 int factor=1000;
00201 xsh_msg("ok1");
00202 dobs=cpl_vector_get_data(vobs);
00203 dref=cpl_vector_get_data(vref);
00204
00205 xsh_msg("vk1");
00206 check(vobs_res=xsh_vector_supersample(vobs,factor));
00207 xsh_msg("vk2");
00208 check(vref_res=xsh_vector_supersample(vref,factor));
00209 xsh_msg("vk3");
00210
00211
00212
00213
00214 xsh_msg("ok1 size_obs=%d size_ref=%d",size_obs,size_ref);
00215
00216 int len_corr=factor*(size_obs+size_ref)-5;
00217 xsh_msg("len_corr=%d",len_corr);
00218
00219 cpl_vector* correl=cpl_vector_new(len_corr);
00220
00221
00222 cpl_vector* filter=NULL;
00223 double radius=2;
00224 cpl_vector_save(vobs,"vobs.fits",CPL_BPP_IEEE_FLOAT,NULL,CPL_IO_DEFAULT);
00225 cpl_vector_save(vref,"vref.fits",CPL_BPP_IEEE_FLOAT,NULL,CPL_IO_DEFAULT);
00226
00227 cpl_vector_save(vobs_res,"vobs_res.fits",CPL_BPP_IEEE_FLOAT,NULL,CPL_IO_DEFAULT);
00228 cpl_vector_save(vref_res,"vref_res.fits",CPL_BPP_IEEE_FLOAT,NULL,CPL_IO_DEFAULT);
00229
00230 double shift=cpl_vector_correlate(correl,vobs_res,vref_res);
00231
00232
00233 filter=cpl_vector_new(size_obs);
00234
00235
00236
00237
00238
00239
00240
00241 xsh_msg("ok4");
00242 cpl_vector_save(vobs,"vobs_filt.fits",CPL_BPP_IEEE_FLOAT,NULL,CPL_IO_DEFAULT);
00243 xsh_msg("ok5");
00244 cpl_vector_save(vref,"vref_filt.fits",CPL_BPP_IEEE_FLOAT,NULL,CPL_IO_DEFAULT);
00245 xsh_msg("ok6");
00246
00247 cpl_vector_save(correl,"correl.fits",CPL_BPP_IEEE_FLOAT,NULL,CPL_IO_DEFAULT);
00248 xsh_msg("ok1");
00249 xsh_msg("shift=%g",shift);
00250
00251 double max=0;
00252 int maxpos=0;
00253 double* pvec=NULL;
00254
00255 pvec=cpl_vector_get_data(correl);
00256 max=-100;
00257 int i=0;
00258 for(i=0;i<len_corr;i++) {
00259 if(max<pvec[i] ) {
00260 max=pvec[i];
00261 maxpos=i;
00262 }
00263 }
00264 xsh_msg("maxpos my determination: %d",maxpos);
00265 xsh_msg("ok1");
00266
00267 double a=0;
00268 double b=0;
00269 double c=0;
00270 double fraction=0;
00271
00272 a=cpl_vector_get(correl,maxpos-1);
00273 b=cpl_vector_get(correl,maxpos+1);
00274 c=cpl_vector_get(correl,maxpos);
00275 fraction=(a-b)/(2.*a+2.*b-4.*c);
00276 xsh_msg("len_corr=%d",len_corr);
00277 xsh_msg("a=%g b=%g c=%g fraction=%g",a,b,c,fraction);
00278 xsh_msg("shift - (size-1)=%g",shift -(size-1));
00279 xsh_msg("fraction shift - (size-1)=%g",
00280 (shift -(size*factor-1)+fraction)/factor);
00281 xsh_msg("ok1");
00282
00283
00284 cleanup:
00285 xsh_free_vector(&vobs);
00286 xsh_free_vector(&vref);
00287 xsh_free_vector(&vobs_res);
00288 xsh_free_vector(&vref_res);
00289 xsh_free_vector(&correl);
00290 xsh_free_vector(&filter);
00291
00292
00293 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00294 xsh_error_dump( CPL_MSG_ERROR);
00295 ret=1;
00296 }
00297 TEST_END();
00298 return ret;
00299 }
00300