1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
/*
* This file is part of the HDRL
* Copyright (C) 2017 European Southern Observatory
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
/*-----------------------------------------------------------------------------
Includes
-----------------------------------------------------------------------------*/
#include "hdrl_spectrum_shift.h"
#include <math.h>
#include <cpl.h>
/*----------------------------------------------------------------------------*/
/**
* @defgroup hdrl_spectrum1d_shift test, the xcorrelation methods are already
* covered by the correlation tests.
*/
/*----------------------------------------------------------------------------*/
double calc_gauss(double mean, double sigma, double x){
double exponent = - pow((x- mean), 2.0) / (2.0 * sigma * sigma);
double v = 1.0/(2.0 * CPL_MATH_PI * sigma * sigma) * exp(exponent);
return v;
}
hdrl_value gauss_func(hdrl_data_t lambda){
hdrl_data_t mean = 1500;
hdrl_data_t sigma = 250;
double v = calc_gauss(mean, sigma, lambda);
return (hdrl_value){v, 0.0};
}
hdrl_value absorption1_func(hdrl_data_t lambda){
hdrl_data_t mean = 1754;
hdrl_data_t sigma = .75;
double v = calc_gauss(mean, sigma, lambda);
return (hdrl_value){ exp(-v), 0.0};
}
hdrl_value absorption2_func(hdrl_data_t lambda){
hdrl_data_t mean = 1504;
hdrl_data_t sigma = .75;
double v = calc_gauss(mean, sigma, lambda);
return (hdrl_value){ exp(-v), 0.0};
}
cpl_array * get_wlengths(double start, double stop, double step){
cpl_size sz = (cpl_size)floor((stop - start) / step);
cpl_array * arr = cpl_array_new(sz, HDRL_TYPE_DATA);
for(cpl_size i = 0; i < sz; i++){
cpl_array_set(arr, i, start);
start += step;
}
return arr;
}
void test_on_slope(void){
cpl_array * wlenghts = get_wlengths(1e3, 2e3, 1.0);
hdrl_spectrum1D * gaussian =
hdrl_spectrum1D_create_analytic(gauss_func, wlenghts, hdrl_spectrum1D_wave_scale_linear);
hdrl_spectrum1D * absorption =
hdrl_spectrum1D_create_analytic(absorption1_func, wlenghts, hdrl_spectrum1D_wave_scale_linear);
hdrl_spectrum1D * gaussian_with_abs =
hdrl_spectrum1D_mul_spectrum_create(gaussian, absorption);
hdrl_parameter * par =
hdrl_spectrum1D_shift_fit_parameter_create(1750, 1730, 1770, 1740, 1760, 20);
hdrl_data_t offset = hdrl_spectrum1D_compute_shift_fit(gaussian_with_abs, par);
cpl_test_rel((1.0 + offset) * 1750., 1754., 1e-3);
hdrl_spectrum1D_delete(&gaussian);
hdrl_spectrum1D_delete(&absorption);
hdrl_spectrum1D_delete(&gaussian_with_abs);
cpl_array_delete(wlenghts);
hdrl_parameter_delete(par);
}
void test_on_peak(void){
cpl_array * wlenghts = get_wlengths(1e3, 2e3, 1.0);
hdrl_spectrum1D * gaussian =
hdrl_spectrum1D_create_analytic(gauss_func, wlenghts, hdrl_spectrum1D_wave_scale_linear);
hdrl_spectrum1D * absorption =
hdrl_spectrum1D_create_analytic(absorption2_func, wlenghts, hdrl_spectrum1D_wave_scale_linear);
hdrl_spectrum1D * gaussian_with_abs =
hdrl_spectrum1D_mul_spectrum_create(gaussian, absorption);
hdrl_parameter * par =
hdrl_spectrum1D_shift_fit_parameter_create(1500, 1480, 1520, 1490, 1510, 20);
hdrl_data_t offset = hdrl_spectrum1D_compute_shift_fit(gaussian_with_abs, par);
cpl_test_rel((1.0 + offset) * 1500., 1504., 1e-3);
hdrl_spectrum1D_delete(&gaussian);
hdrl_spectrum1D_delete(&absorption);
hdrl_spectrum1D_delete(&gaussian_with_abs);
cpl_array_delete(wlenghts);
hdrl_parameter_delete(par);
}
void test_compute_shift_xcorrelation(void)
{
cpl_array * wlenghts = get_wlengths(1e3, 2e3, 1.0);
hdrl_spectrum1D * gaussian = hdrl_spectrum1D_create_analytic(
gauss_func, wlenghts, hdrl_spectrum1D_wave_scale_linear);
hdrl_spectrum1D * absorption = hdrl_spectrum1D_create_analytic(
absorption2_func, wlenghts, hdrl_spectrum1D_wave_scale_linear);
cpl_size half_win = 1;
/* Test nulls */
hdrl_spectrum1D_compute_shift_xcorrelation(
NULL, NULL, half_win, CPL_FALSE);
cpl_test_error(CPL_ERROR_NULL_INPUT);
hdrl_spectrum1D_compute_shift_xcorrelation(
NULL, absorption, half_win, CPL_FALSE);
cpl_test_error(CPL_ERROR_NULL_INPUT);
hdrl_spectrum1D_compute_shift_xcorrelation(
gaussian, NULL, half_win, CPL_FALSE);
cpl_test_error(CPL_ERROR_NULL_INPUT);
hdrl_spectrum1D_compute_shift_xcorrelation(
gaussian, absorption, half_win, CPL_FALSE);
cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
hdrl_spectrum1D_delete(&gaussian);
hdrl_spectrum1D_delete(&absorption);
cpl_array_delete(wlenghts);
}
/*----------------------------------------------------------------------------*/
/**
@brief Unit tests of spectrum shift estimation module
**/
/*----------------------------------------------------------------------------*/
int main(void)
{
cpl_test_init(PACKAGE_BUGREPORT, CPL_MSG_WARNING);
test_on_slope();
test_on_peak();
test_compute_shift_xcorrelation();
return cpl_test_end(0);
}
|