CR2RE Pipeline Reference Manual 1.6.10
cr2res_pol-test.c
1/*
2 * This file is part of the CR2RES Pipeline
3 * Copyright (C) 2002,2003 European Southern Observatory
4 *
5 * This program 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, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02111-1307 USA
18 */
19
20#ifdef HAVE_CONFIG_H
21#include <config.h>
22#endif
23
24/*-----------------------------------------------------------------------------
25 Includes
26 -----------------------------------------------------------------------------*/
27
28#include <stdlib.h>
29#include <string.h>
30#include <math.h>
31#include <cpl.h>
32#include <hdrl.h>
33#include <cr2res_dfs.h>
34#include <cr2res_pol.h>
35
36#define CR2RES_DETECTOR_SIZE 2048
37
38/*-----------------------------------------------------------------------------
39 Functions prototypes
40 -----------------------------------------------------------------------------*/
41
42
43static void test_cr2res_pol_demod_stokes(void);
44static void test_cr2res_pol_demod_stokes_different_wavelengths(void);
45static void test_cr2res_pol_demod_stokes_zeros(void);
46
47/*----------------------------------------------------------------------------*/
52/*----------------------------------------------------------------------------*/
55/*----------------------------------------------------------------------------*/
70/*----------------------------------------------------------------------------*/
71static void test_cr2res_pol_demod_stokes(){
72 int n = 8; // as it has to be
73 cpl_vector ** wl = cpl_malloc(n * sizeof(cpl_vector*));
74 cpl_vector ** intens = cpl_malloc(n * sizeof(cpl_vector*));
75 cpl_vector ** errors = cpl_malloc(n * sizeof(cpl_vector*));
76 cpl_bivector * pol;
77 double value = 0;
78 double error = 1. / sqrt(8);
79
80 // spec = sigma = 1
81 // -> pol = 0
82 // -> sigma pol = 1 / sqrt(8)
83 cpl_bivector * spec = cpl_bivector_new(CR2RES_DETECTOR_SIZE);
84 for (cpl_size i = 0; i < CR2RES_DETECTOR_SIZE; i++)
85 {
86 cpl_vector_set(cpl_bivector_get_x(spec), i, 1);
87 cpl_vector_set(cpl_bivector_get_y(spec), i, 1);
88 }
89
90 // Set the wavelength
91 // for this test they all have the same wavelength scale
92 double wmin = 2000;
93 double wmax = 3000;
94 double step = (wmax - wmin) / CR2RES_DETECTOR_SIZE;
95 for (int i = 0; i < n; i++){
96 wl[i] = cpl_vector_new(CR2RES_DETECTOR_SIZE);
97 for (int j = 0; j < CR2RES_DETECTOR_SIZE; j++){
98 cpl_vector_set(wl[i], j, wmin + j * step);
99 }
100 }
101
102 // just use the same spectrum 8 times
103 for (int i = 0; i < n; i++)
104 {
105 intens[i] = cpl_bivector_get_x(spec);
106 errors[i] = cpl_bivector_get_y(spec);
107 }
108
109 cpl_test_nonnull(pol = cr2res_pol_demod_stokes(intens, wl, errors, n));
110
111 // Check that the results are as expected
112 for (cpl_size i = 0; i < CR2RES_DETECTOR_SIZE; i++)
113 {
114 cpl_test_abs(value, cpl_vector_get(cpl_bivector_get_x(pol), i),
115 DBL_EPSILON);
116 cpl_test_abs(error, cpl_vector_get(cpl_bivector_get_y(pol), i),
117 DBL_EPSILON);
118 }
119
120 // Clean memory
121 for (int i = 0; i < n; i++){
122 cpl_vector_delete(wl[i]);
123 }
124 cpl_bivector_delete(pol);
125 cpl_bivector_delete(spec);
126 cpl_free(intens);
127 cpl_free(wl);
128 cpl_free(errors);
129}
130
131
132static void test_cr2res_pol_demod_stokes_different_wavelengths(){
133 int n = 8; // as it has to be
134 cpl_vector ** wl = cpl_malloc(n * sizeof(cpl_vector*));
135 cpl_vector ** intens = cpl_malloc(n * sizeof(cpl_vector*));
136 cpl_vector ** errors = cpl_malloc(n * sizeof(cpl_vector*));
137 cpl_bivector * pol;
138 double value = 0;
139 double error = 1. / sqrt(8);
140
141 cpl_size i, j;
142
143 // spec = sigma = 1
144 // -> pol = 0
145 // -> sigma pol = 1 / sqrt(8)
146 cpl_bivector * spec = cpl_bivector_new(CR2RES_DETECTOR_SIZE);
147 for (i = 0; i < CR2RES_DETECTOR_SIZE; i++)
148 {
149 cpl_vector_set(cpl_bivector_get_x(spec), i, 1);
150 cpl_vector_set(cpl_bivector_get_y(spec), i, 1);
151 }
152
153 // Set the wavelength
154 // each spectra has a slightly shifted scale
155 double wmin = 2000;
156 double wmax = 3000;
157 double step = (wmax - wmin) / CR2RES_DETECTOR_SIZE;
158 double diff_between_spectra = 20;
159 for (i = 0; i < n; i++){
160 wl[i] = cpl_vector_new(CR2RES_DETECTOR_SIZE);
161 for (j = 0; j < CR2RES_DETECTOR_SIZE; j++){
162 cpl_vector_set(wl[i], j, wmin + j * step + i * diff_between_spectra);
163 }
164 }
165
166 // just use the same spectrum 8 times
167 // demod should not modify them (it makes copies)
168 for (i = 0; i < n; i++)
169 {
170 intens[i] = cpl_bivector_get_x(spec);
171 errors[i] = cpl_bivector_get_y(spec);
172 }
173
174 cpl_test_nonnull(pol = cr2res_pol_demod_stokes(intens, wl, errors, n));
175
176
177 // These values depend on the chosen wavelength grid
178 // make sure to adjust them appropriately when you change those values
179 cpl_size xmin = 287;
180 cpl_size xmax = 1760;
181 // Check that the results are as expected
182 // First we have some NANs
183 i = 0;
184 for (; i < xmin; i++){
185 cpl_test(isnan(cpl_vector_get(cpl_bivector_get_x(pol), i)));
186 cpl_test(isnan(cpl_vector_get(cpl_bivector_get_y(pol), i)));
187 }
188 // Then 0s as in the same wavelengths case
189 for (; i < xmax + 1; i++)
190 {
191 cpl_test_abs(value, cpl_vector_get(cpl_bivector_get_x(pol), i),
192 DBL_EPSILON);
193 cpl_test_abs(error, cpl_vector_get(cpl_bivector_get_y(pol), i),
194 DBL_EPSILON);
195 }
196 // Followed by more NANs
197 for (; i < CR2RES_DETECTOR_SIZE; i++){
198 cpl_test(isnan(cpl_vector_get(cpl_bivector_get_x(pol), i)));
199 cpl_test(isnan(cpl_vector_get(cpl_bivector_get_y(pol), i)));
200 }
201
202 // Clean memory
203 for (j = 0; j < n; j++){
204 cpl_vector_delete(wl[j]);
205 }
206 cpl_bivector_delete(pol);
207 cpl_bivector_delete(spec);
208 cpl_free(intens);
209 cpl_free(wl);
210 cpl_free(errors);
211}
212
213static void test_cr2res_pol_demod_stokes_zeros(){
214 int n = 8; // as it has to be
215 cpl_vector ** wl = cpl_malloc(n * sizeof(cpl_vector*));
216 cpl_vector ** intens = cpl_malloc(n * sizeof(cpl_vector*));
217 cpl_vector ** errors = cpl_malloc(n * sizeof(cpl_vector*));
218 cpl_bivector * pol;
219
220 // spec = sigma = 1
221 // -> pol = 0
222 // -> sigma pol = 1 / sqrt(8)
223 cpl_bivector * spec = cpl_bivector_new(CR2RES_DETECTOR_SIZE);
224 for (cpl_size i = 0; i < CR2RES_DETECTOR_SIZE; i++)
225 {
226 cpl_vector_set(cpl_bivector_get_x(spec), i, 0);
227 cpl_vector_set(cpl_bivector_get_y(spec), i, 0);
228 }
229
230 // Set the wavelength
231 // for this test they all have the same wavelength scale
232 double wmin = 2000;
233 double wmax = 3000;
234 double step = (wmax - wmin) / CR2RES_DETECTOR_SIZE;
235 for (int i = 0; i < n; i++){
236 wl[i] = cpl_vector_new(CR2RES_DETECTOR_SIZE);
237 for (int j = 0; j < CR2RES_DETECTOR_SIZE; j++){
238 cpl_vector_set(wl[i], j, wmin + j * step);
239 }
240 }
241
242 // just use the same spectrum 8 times
243 for (int i = 0; i < n; i++)
244 {
245 intens[i] = cpl_bivector_get_x(spec);
246 errors[i] = cpl_bivector_get_y(spec);
247 }
248
249 cpl_test_nonnull(pol = cr2res_pol_demod_stokes(intens, wl, errors, n));
250
251 // Check that the results are as expected
252 for (cpl_size i = 0; i < CR2RES_DETECTOR_SIZE; i++)
253 {
254 cpl_test(isnan(cpl_vector_get(cpl_bivector_get_x(pol), i)));
255 cpl_test(isnan(cpl_vector_get(cpl_bivector_get_y(pol), i)));
256 }
257
258 // Clean memory
259 for (int i = 0; i < n; i++){
260 cpl_vector_delete(wl[i]);
261 }
262 cpl_bivector_delete(pol);
263 cpl_bivector_delete(spec);
264 cpl_free(intens);
265 cpl_free(wl);
266 cpl_free(errors);
267}
268
269/*----------------------------------------------------------------------------*/
273/*----------------------------------------------------------------------------*/
274int main(void)
275{
276 cpl_test_init(PACKAGE_BUGREPORT, CPL_MSG_DEBUG);
277
278 test_cr2res_pol_demod_stokes();
279 test_cr2res_pol_demod_stokes_different_wavelengths();
280 test_cr2res_pol_demod_stokes_zeros();
281
282 return cpl_test_end(0);
283}
284
int main(void)
Run the Unit tests.
cpl_bivector * cr2res_pol_demod_stokes(cpl_vector **intens, cpl_vector **wl, cpl_vector **errors, int n)
Demodulate extracted spectra into Stokes parameter.
Definition: cr2res_pol.c:106