X-shooter Pipeline Reference Manual 3.8.15
test-xsh_cpl_vector_correl_gaussians.c
Go to the documentation of this file.
1/* *
2 * This file is part of the ESO X-shooter Pipeline *
3 * Copyright (C) 2006 European Southern Observatory *
4 * *
5 * This library is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software *
17 * Foundation, 51 Franklin St, Fifth Floor, Boston, MA 02111-1307 USA *
18 * */
19
20/*
21 * $Author: amodigli $
22 * $Date: 2012-07-21 08:32:42 $
23 * $Revision: 1.1 $
24 * $Name: not supported by cvs2svn $
25 */
26#ifdef HAVE_CONFIG_H
27# include <config.h>
28#endif
29
30/*--------------------------------------------------------------------------*/
36/*--------------------------------------------------------------------------*/
39/*---------------------------------------------------------------------------
40 Includes
41 ---------------------------------------------------------------------------*/
42
43
44#include <xsh_error.h>
45#include <xsh_msg.h>
46#include <xsh_dfs.h>
47#include <tests.h>
48#include <cpl.h>
49#include <math.h>
50#include <getopt.h>
51#include <string.h>
52/*---------------------------------------------------------------------------
53 Defines
54 ---------------------------------------------------------------------------*/
55#define MODULE_ID "XSH_CPL_VECTOR_CORREL_GAUSSIANS"
56
57enum {
59};
60
61static struct option LongOptions[] = {
62 {"debug", required_argument, 0, DEBUG_OPT},
63 {"help", 0, 0, HELP_OPT},
64 {NULL, 0, 0, 0}
65};
66
67static void Help( void )
68{
69 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");
70 puts( "Usage : ./tetst_xsh_correl_gaussians [options]");
71
72 puts( "Options" ) ;
73 puts( " --debug=<n> : Level of debug LOW | MEDIUM | HIGH [MEDIUM]" );
74 puts( " --help : What you see" ) ;
75
76 puts( "The input files argument MUST be in this order:" );
77 puts( " 1. PRE frame");
78 puts( " 2. SOF a) MODEL : [XSH_MOD_CFG_TAB_UVB]");
79 puts( " b) POLYNOMIAL: [DISP_TAB, ORDER_TAB_EDGES]");
80
82 TEST_END();
83 exit(0);
84}
85
86static void HandleOptions( int argc, char ** argv)
87{
88 int opt ;
89 int option_index = 0;
90
91 while( (opt = getopt_long( argc, argv, "debug:help",
92 LongOptions, &option_index )) != EOF){
93 switch( opt ) {
94 case DEBUG_OPT:
95 if ( strcmp( optarg, "LOW")==0){
97 }
98 else if ( strcmp( optarg, "HIGH")==0){
100 }
101 break;
102 case HELP_OPT:
103 Help();
104 break;
105 default:
106 break;
107 }
108 }
109}
110
111cpl_error_code
112xsh_gauss_gen(double* data,const double center,const double sigma, const int size)
113{
114
115 int i=0;
116 double x=0;
117 double inv_2_c2=0.5/sigma/sigma;
118 double norm=sigma*sqrt(2*CPL_MATH_PI);
119 double a=1./norm;
120
121 for(i=0;i<size;i++) {
122 x=i;
123 data[i]=a*exp(-(x-center)*(x-center)*inv_2_c2);
124 }
125
126 return cpl_error_get_code();
127}
128
129/*---------------------------------------------------------------------------
130 Functions prototypes
131 ---------------------------------------------------------------------------*/
132
133/*--------------------------------------------------------------------------*/
140/*--------------------------------------------------------------------------*/
141
142int main( int argc, char** argv)
143{
144 /* Initialize libraries */
147 cpl_msg_set_level( CPL_MSG_DEBUG);
149
150 HandleOptions( argc, argv);
151
152 /* Analyse parameters */
153 if ( (argc-optind) >= 2) {
154 Help();
155 }
156
157 int ret=0;
158 int size=100;
159 double shift_i=5.15;
160 //double shift_o=0;
161 double gauss_c=0.5*size;
162 double gauss_s=10.;
163 double* gauss_d1=NULL;
164 double* gauss_d2=NULL;
165
166
167 cpl_vector* gauss_v1=cpl_vector_new(size);
168 cpl_vector* gauss_v2=cpl_vector_new(size);
169
170 gauss_d1=cpl_vector_get_data(gauss_v1);
171 gauss_d2=cpl_vector_get_data(gauss_v2);
172
173 check(xsh_gauss_gen(gauss_d1,gauss_c,gauss_s,size));
174 check(xsh_gauss_gen(gauss_d2,gauss_c+shift_i,gauss_s,size));
175
176
177 //int half_search=(int)(2*gauss_s+1);
178 int len_corr=2*size-1;
179
180
181 /* CPL VECTOR */
182
183 cpl_vector* correl=cpl_vector_new(len_corr);
184 check(cpl_vector_fill(correl, 0.0));
185 double shift=cpl_vector_correlate(correl,gauss_v1,gauss_v2);
186
187 cpl_vector_save(correl,"correl.fits",CPL_BPP_IEEE_FLOAT,NULL,CPL_IO_DEFAULT);
188 xsh_msg("shift=%g",shift);
189 //cpl_vector_dump(correl,stdout);
190 double max=0;
191 int maxpos=0;
192 double* pvec=NULL;
193 /* get maxpos */
194 pvec=cpl_vector_get_data(correl);
195 max=-100;
196 int i=0;
197 for(i=1;i<len_corr;i++) {
198 if(max<pvec[i] ) {
199 max=pvec[i];
200 maxpos=i;
201 }
202 }
203 xsh_msg("maxpos my determination: %d",maxpos);
204
205 double a=0;
206 double b=0;
207 double c=0;
208 double fraction=0;
209 //maxpos +=1;
210 a=cpl_vector_get(correl,maxpos-1);
211 b=cpl_vector_get(correl,maxpos+1);
212 c=cpl_vector_get(correl,maxpos);
213 fraction=(a-b)/(2.*a+2.*b-4.*c);
214 xsh_msg("len_corr=%d",len_corr);
215 xsh_msg("a=%g b=%g c=%g fraction=%g",a,b,c,fraction);
216 xsh_msg("shift - (size-1)=%g",shift -(size-1));
217 xsh_msg("fraction shift - (size-1)=%g",shift -(size-1)+fraction);
218
219
220 cleanup:
221 xsh_free_vector(&gauss_v1);
222 xsh_free_vector(&gauss_v2);
223 xsh_free_vector(&correl);
224
225
226 if (cpl_error_get_code() != CPL_ERROR_NONE) {
227 xsh_error_dump( CPL_MSG_ERROR);
228 ret=1;
229 }
231 TEST_END();
232 return ret;
233}
234
int main()
Unit test of xsh_bspline_interpol.
static double sigma
cpl_error_code xsh_gauss_gen(double *data, const double center, const double sigma, const int size)
static struct option LongOptions[]
#define check(COMMAND)
Definition: xsh_error.h:71
#define xsh_error_dump(level)
Definition: xsh_error.h:92
int size
int * x
#define xsh_msg(...)
Print a message on info level.
Definition: xsh_msg.h:121
void xsh_free_vector(cpl_vector **v)
Deallocate a vector and set the pointer to NULL.
Definition: xsh_utils.c:2284
int xsh_debug_level_set(int level)
set debug level
Definition: xsh_utils.c:3125
static void HandleOptions(int argc, char **argv)
static void Help(void)
#define TESTS_CLEAN_WORKSPACE(DRL_ID)
Definition: tests.h:139
#define TESTS_INIT_WORKSPACE(DRL_ID)
Definition: tests.h:133
#define TEST_END()
Definition: tests.h:111
#define TESTS_INIT(DRL_ID)
Definition: tests.h:105
#define max(a, b)
@ XSH_DEBUG_LEVEL_HIGH
Definition: xsh_utils.h:138
@ XSH_DEBUG_LEVEL_LOW
Definition: xsh_utils.h:137
@ XSH_DEBUG_LEVEL_MEDIUM
Definition: xsh_utils.h:138