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
00027 #ifdef HAVE_CONFIG_H
00028 #include <config.h>
00029 #endif
00030
00031
00032
00033
00034 #include "xsh_utils_vector.h"
00035 #include "xsh_utils_wrappers.h"
00036 #include "xsh_utils.h"
00037 #include "xsh_msg.h"
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050 cpl_vector*
00051 xsh_vector_extract_range(cpl_vector* vin,const cpl_size pos, const int hsize)
00052 {
00053 cpl_vector* result;
00054 int i=0;
00055 int k=0;
00056 int size_i=0;
00057 int size_o=2*hsize+1;
00058 int ref=(int)pos;
00059 double* data_i=NULL;
00060 double* data_o=NULL;
00061
00062 cpl_ensure(vin != NULL, CPL_ERROR_NULL_INPUT,NULL);
00063 cpl_ensure(hsize > 0, CPL_ERROR_ILLEGAL_INPUT,NULL);
00064 cpl_ensure(pos>hsize, CPL_ERROR_ILLEGAL_INPUT,NULL);
00065 cpl_ensure(hsize > 0, CPL_ERROR_ILLEGAL_INPUT,NULL);
00066 size_i=cpl_vector_get_size(vin);
00067 cpl_ensure(pos+hsize < size_i, CPL_ERROR_ILLEGAL_INPUT,NULL);
00068
00069 result=cpl_vector_new(size_o);
00070
00071 data_i=cpl_vector_get_data(vin);
00072 data_o=cpl_vector_get_data(result);
00073
00074 for(i=-hsize;i<=hsize;i++){
00075 data_o[k]=data_i[ref+i];
00076 k++;
00077 }
00078 return result;
00079 }
00080
00081
00082
00083
00084
00085
00086
00087 cpl_vector*
00088 xsh_vector_upsample(cpl_vector* vin,const int factor)
00089 {
00090 cpl_vector* result;
00091 int i=0;
00092 int j=0;
00093 int size_i=0;
00094 int size_o=0;
00095 double* data_i=NULL;
00096 double* data_o=NULL;
00097 double y1=0;
00098 double y2=0;
00099 double x1=0;
00100 double x2=0;
00101 double m=0;
00102
00103 cpl_ensure(vin != NULL, CPL_ERROR_NULL_INPUT,NULL);
00104 cpl_ensure(factor > 0, CPL_ERROR_ILLEGAL_INPUT,NULL);
00105
00106 size_i=cpl_vector_get_size(vin);
00107 size_o=(size_i-1)*factor+1;
00108
00109 result=cpl_vector_new(size_o);
00110
00111 data_i=cpl_vector_get_data(vin);
00112 data_o=cpl_vector_get_data(result);
00113
00114 for(i=0;i<size_i-1;i++){
00115 y1=data_i[i];
00116 y2=data_i[i+1];
00117 x1=i;
00118 x2=i+1;
00119 m=(y2-y1)/factor;
00120 for(j=0;j<factor;j++){
00121 data_o[i*factor+j]= y1 + m * j;
00122 }
00123 }
00124 data_o[size_o-1]=data_i[size_i-1];
00125
00126 return result;
00127 }
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138 cpl_vector*
00139 xsh_vector_fit_slope(cpl_vector* vec_wave,cpl_vector* vec_flux,const double wmin_max,const double wmax_min,const int degree)
00140 {
00141 cpl_vector* vec_slope=NULL;
00142 int i=0;
00143 int size=0;
00144 int k=0;
00145 double* pwave=NULL;
00146 double* pflux=NULL;
00147 double* pslope=NULL;
00148 double* pvec_x=NULL;
00149 double* pvec_y=NULL;
00150 double mse=0;
00151 cpl_polynomial* pfit=NULL;
00152
00153 cpl_vector* vec_x=NULL;
00154 cpl_vector* vec_y=NULL;
00155
00156
00157 cpl_ensure(vec_wave,CPL_ERROR_NULL_INPUT,NULL);
00158 cpl_ensure(vec_flux,CPL_ERROR_NULL_INPUT,NULL);
00159 cpl_ensure(wmin_max<wmax_min,CPL_ERROR_INCOMPATIBLE_INPUT,NULL);
00160 cpl_ensure(degree>0 && degree < 3,CPL_ERROR_INCOMPATIBLE_INPUT,NULL);
00161
00162
00163 size=cpl_vector_get_size(vec_flux);
00164 vec_x=cpl_vector_new(size);
00165 vec_y=cpl_vector_new(size);
00166 pwave=cpl_vector_get_data(vec_wave);
00167 pflux=cpl_vector_get_data(vec_flux);
00168 pvec_x=cpl_vector_get_data(vec_x);
00169 pvec_y=cpl_vector_get_data(vec_y);
00170
00171 for(i=0;i<size;i++) {
00172 if( pwave[i] <= wmin_max || pwave[i] >= wmax_min ) {
00173 pvec_x[k]=pwave[i];
00174 pvec_y[k]=pflux[i];
00175 k++;
00176 }
00177 }
00178
00179 cpl_vector_set_size(vec_x,k);
00180 cpl_vector_set_size(vec_y,k);
00181
00182
00183 pfit=xsh_polynomial_fit_1d_create(vec_x,vec_y,degree,&mse);
00184
00185
00186 vec_slope=cpl_vector_new(size);
00187 pslope=cpl_vector_get_data(vec_slope);
00188 for(i=0;i<size;i++) {
00189 pslope[i] = cpl_polynomial_eval_1d( pfit, pwave[i], NULL);
00190 }
00191 xsh_free_vector(&vec_x);
00192 xsh_free_vector(&vec_y);
00193 xsh_free_polynomial(&pfit);
00194 return vec_slope;
00195 }
00196
00197