-
Notifications
You must be signed in to change notification settings - Fork 2
/
basic_parameters.c
417 lines (378 loc) · 16.1 KB
/
basic_parameters.c
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
// clang-format off
// In the frame of RPW LFR Sofware ICD Issue1 Rev8 (05/07/2013) => R2 FSW
// version 1.0: 31/07/2013
// version 1.1: 02/04/2014
// version 1.2: 30/04/2014
// version 1.3: 02/05/2014
// version 1.4: 16/05/2014
// version 1.5: 20/05/2014
// version 1.6: 19/12/2014
// version 1.7: 15/01/2015 (modifs de Paul + correction erreurs qui se compensaient (LSB <=> MSB + indices [0,2] <=> [1,3])
// version 1.8: 02/02/2015 (gestion des divisions par zéro)
// In the frame of RPW LFR Sofware ICD Issue3 Rev6 (27/01/2015) => R3 FSW
// version 2.0: 19/06/2015
// version 2.1: 22/06/2015 (modifs de Paul)
// version 2.2: 23/06/2015 (modifs de l'ordre de déclaration/définition de init_k_coefficients dans basic_parameters.c ... + maintien des declarations dans le .h)
// version 2.3: 01/07/2015 (affectation initiale des octets 7 et 9 dans les BP1 corrigée ...)
// version 2.4: 05/10/2018 (added GPL headers)
// version 2.5: 09/10/2018 (dans main.c #include "basic_parameters_utilities.h" est changé par les déclarations extern correspondantes ...!
// + delta mise en conformité LOGISCOPE)
// clang-format on
/*------------------------------------------------------------------------------
-- Solar Orbiter's Low Frequency Receiver Flight Software (LFR FSW),
-- This file is a part of the LFR FSW
-- Copyright (C) 2012-2018, Plasma Physics Laboratory - CNRS
--
-- 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-------------------------------------------------------------------------------*/
/*-- Author : Thomas Chust
-- Contact : Thomas Chust
-- Mail : [email protected]
----------------------------------------------------------------------------*/
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include "basic_parameters_params.h"
#include "custom_floats.h"
#include "fsw_debug.h"
#include "processing/ASM/spectralmatrices.h"
inline const float* next_matrix(const float* const spectral_matrix) __attribute__((always_inline));
const float* next_matrix(const float* const spectral_matrix)
{
DEBUG_CHECK_PTR(spectral_matrix);
return spectral_matrix + NB_FLOATS_PER_SM;
}
inline float elec_power_spectrum_density(const float* const spectral_matrix)
__attribute__((always_inline));
float elec_power_spectrum_density(const float* const spectral_matrix)
{
DEBUG_CHECK_PTR(spectral_matrix);
return spectral_matrix[ASM_COMP_E1E1] + spectral_matrix[ASM_COMP_E2E2];
}
inline float mag_power_spectrum_density(const float* const spectral_matrix)
__attribute__((always_inline));
inline float mag_power_spectrum_density(const float* const spectral_matrix)
{
DEBUG_CHECK_PTR(spectral_matrix);
return spectral_matrix[ASM_COMP_B1B1] + spectral_matrix[ASM_COMP_B2B2]
+ spectral_matrix[ASM_COMP_B3B3];
}
typedef struct
{
float x;
float y;
float z;
float ab; // Ellipse a.b product
} normal_wave_vector_t;
inline float square(const float value) __attribute__((always_inline));
float square(const float value)
{
return value * value;
}
inline normal_wave_vector_t normal_wave_vector(const float* const spectral_matrix)
__attribute__((always_inline));
normal_wave_vector_t normal_wave_vector(const float* const spectral_matrix)
{
DEBUG_CHECK_PTR(spectral_matrix);
const float ab = sqrtf(square(spectral_matrix[ASM_COMP_B1B2_imag])
+ square(spectral_matrix[ASM_COMP_B1B3_imag])
+ square(spectral_matrix[ASM_COMP_B2B3_imag]));
if (ab != 0.)
{
normal_wave_vector_t nvec = { .x = spectral_matrix[ASM_COMP_B2B3_imag] / ab,
.y = -spectral_matrix[ASM_COMP_B1B3_imag] / ab,
.z = spectral_matrix[ASM_COMP_B1B2_imag] / ab,
.ab = ab };
return nvec;
}
else
{
normal_wave_vector_t nvec = { .x = 0., .y = 0., .z = 0., .ab = 0. };
return nvec;
}
}
inline float wave_ellipticity_estimator(const float mag_PSD, const float nvec_denom)
__attribute__((always_inline));
float wave_ellipticity_estimator(const float mag_PSD, const float nvec_denom)
{
if (mag_PSD != 0.f)
{
return 2.f * nvec_denom / mag_PSD;
}
else
{
return 0.;
}
}
inline float degree_of_polarization(const float mag_PSD, const float* const spectral_matrix)
__attribute__((always_inline));
float degree_of_polarization(const float mag_PSD, const float* const spectral_matrix)
{
DEBUG_CHECK_PTR(spectral_matrix);
const float B_square_trace = square(spectral_matrix[ASM_COMP_B1B1])
+ square(spectral_matrix[ASM_COMP_B2B2]) + square(spectral_matrix[ASM_COMP_B3B3])
+ 2.f
* (square(spectral_matrix[ASM_COMP_B1B2]) + square(spectral_matrix[ASM_COMP_B1B2_imag])
+ square(spectral_matrix[ASM_COMP_B1B3])
+ square(spectral_matrix[ASM_COMP_B1B3_imag])
+ square(spectral_matrix[ASM_COMP_B2B3])
+ square(spectral_matrix[ASM_COMP_B2B3_imag]));
const float square_B_trace = square(mag_PSD);
if (square_B_trace != 0.)
{
return sqrtf((3.f * B_square_trace - square_B_trace) / (2.f * square_B_trace));
}
else
{
return 0.f;
}
}
inline compressed_complex X_poynting_vector(const float* const spectral_matrix)
__attribute__((always_inline));
compressed_complex X_poynting_vector(const float* const spectral_matrix)
{
DEBUG_CHECK_PTR(spectral_matrix);
// E1B3 - E2B2
compressed_complex X_PV;
X_PV.real = spectral_matrix[ASM_COMP_B3E1] - spectral_matrix[ASM_COMP_B2E2];
const float imag = spectral_matrix[ASM_COMP_B2E2_imag] - spectral_matrix[ASM_COMP_B3E1_imag];
X_PV.arg = fabs(imag) > fabs(X_PV.real);
return X_PV;
}
inline float modulus(const float a, const float b) __attribute__((always_inline));
float modulus(const float a, const float b)
{
return sqrtf(square(a) + square(b));
}
inline float cplx_modulus(const _Complex float value) __attribute__((always_inline));
float cplx_modulus(const _Complex float value)
{
return modulus(__real__ value, __imag__ value);
}
inline compressed_complex phase_velocity_estimator(const float* const spectral_matrix,
const normal_wave_vector_t nvec) __attribute__((always_inline));
compressed_complex phase_velocity_estimator(
const float* const spectral_matrix, const normal_wave_vector_t nvec)
{
DEBUG_CHECK_PTR(spectral_matrix);
/*
VPHI = abs(NEBX) * sign( Re[NEBX] ) / BXBX
with:
NEBX = nY<EZBX*>/rho_EZBX - nZ<EYBX*>/rho_EYBX = n2<E2B1*>/rho_E2B1 - n3<E1B1*>/rho_E1B1
rho_E2B1 = |<E2B1*>| / sqrt(<E2E2*><B1B1*>)
rho_E1B1 = |<E1B1*>| / sqrt(<E1E1*><B1B1*>)
BXBX = <BXBX*> = <B1B1*>
*/
compressed_complex vphi = { .real = 0, .arg = 0 };
const float sqrt_E2E2B1B1
= sqrtf(spectral_matrix[ASM_COMP_E2E2] * spectral_matrix[ASM_COMP_B1B1]);
const float sqrt_E1E1B1B1
= sqrtf(spectral_matrix[ASM_COMP_E1E1] * spectral_matrix[ASM_COMP_B1B1]);
const float mod_E2B1
= modulus(spectral_matrix[ASM_COMP_B1E2], spectral_matrix[ASM_COMP_B1E2_imag]);
const float mod_E1B1
= modulus(spectral_matrix[ASM_COMP_B1E1], spectral_matrix[ASM_COMP_B1E1_imag]);
_Complex float NEBX;
if (mod_E2B1 != 0. && mod_E1B1 != 0.)
{
__real__ NEBX = nvec.y * spectral_matrix[ASM_COMP_B1E2] * sqrt_E2E2B1B1 / mod_E2B1
- nvec.z * spectral_matrix[ASM_COMP_B1E1] * sqrt_E1E1B1B1 / mod_E1B1;
__imag__ NEBX = nvec.z * spectral_matrix[ASM_COMP_B1E1_imag] * sqrt_E1E1B1B1 / mod_E1B1
- nvec.y * spectral_matrix[ASM_COMP_B1E2_imag] * sqrt_E2E2B1B1 / mod_E2B1;
const float BXBX = spectral_matrix[ASM_COMP_B1B1];
if (BXBX != 0.f)
{
vphi.real = cplx_modulus(NEBX) / BXBX;
if (__real__ NEBX < 0.)
{
vphi.real = -vphi.real;
}
}
else
{
if (__real__ NEBX >= 0.)
{
vphi.real = 1.e+20f;
}
else
{
vphi.real = -1.e+20f;
}
}
vphi.arg = fabs(__imag__ NEBX) > fabs(__real__ NEBX);
}
return vphi;
}
inline uint8_t* encode_nvec_z_ellip_dop(const float nvec_z, const float ellipticity,
const float DOP, uint8_t* const bp_buffer_frame) __attribute__((always_inline));
uint8_t* encode_nvec_z_ellip_dop(
const float nvec_z, const float ellipticity, const float DOP, uint8_t* const bp_buffer_frame)
{
DEBUG_CHECK_PTR(bp_buffer_frame);
const str_float_t z = { .value = nvec_z };
#ifdef LFR_BIG_ENDIAN
union __attribute__((__packed__))
{
uint8_t value;
struct __attribute__((__packed__))
{
uint8_t nvec_z_sign : 1;
uint8_t ellipticity : 4;
uint8_t DOP : 3;
} str;
} result;
#endif
#ifdef LFR_LITTLE_ENDIAN
union __attribute__((__packed__))
{
uint8_t value;
struct __attribute__((__packed__))
{
uint8_t DOP : 3;
uint8_t ellipticity : 4;
uint8_t nvec_z_sign : 1;
} str;
} result;
#endif
result.str.nvec_z_sign = z.str.sign;
result.str.ellipticity = (uint8_t)(ellipticity * 15.f + 0.5f);
result.str.DOP = (uint8_t)(DOP * 7.f + 0.5f);
bp_buffer_frame[0] = result.value;
return bp_buffer_frame + 1;
}
inline uint8_t* encode_uint16_t(const uint16_t value, uint8_t* const bp_buffer_frame)
__attribute__((always_inline));
uint8_t* encode_uint16_t(const uint16_t value, uint8_t* const bp_buffer_frame)
{
DEBUG_CHECK_PTR(bp_buffer_frame);
const str_uint16_t value_split = { .value = value };
bp_buffer_frame[0] = value_split.str.MSB;
bp_buffer_frame[1] = value_split.str.LSB;
return bp_buffer_frame + 2;
}
inline uint8_t* encode_float_uint8_t(float value, uint8_t* const bp_buffer_frame)
__attribute__((always_inline));
uint8_t* encode_float_uint8_t(float value, uint8_t* const bp_buffer_frame)
{
DEBUG_CHECK_PTR(bp_buffer_frame);
bp_buffer_frame[0] = (uint8_t)(value * 127.5 + 128);
return bp_buffer_frame + 1;
}
inline uint8_t* encode_BP1(const float mag_PSD, const float elec_PSD,
const normal_wave_vector_t nvec, const float ellipticity, const float DOP,
const compressed_complex X_PV, const compressed_complex VPHI, uint8_t* bp1_buffer_frame)
__attribute__((always_inline));
uint8_t* encode_BP1(const float mag_PSD, const float elec_PSD, const normal_wave_vector_t nvec,
const float ellipticity, const float DOP, const compressed_complex X_PV,
const compressed_complex VPHI, uint8_t* bp1_buffer_frame)
{
DEBUG_CHECK_PTR(bp_buffer_frame);
{
bp1_buffer_frame = encode_uint16_t(to_custom_float_6_10(elec_PSD), bp1_buffer_frame);
}
{
bp1_buffer_frame = encode_uint16_t(to_custom_float_6_10(mag_PSD), bp1_buffer_frame);
}
{
bp1_buffer_frame = encode_float_uint8_t(nvec.x, bp1_buffer_frame);
bp1_buffer_frame = encode_float_uint8_t(nvec.y, bp1_buffer_frame);
bp1_buffer_frame = encode_nvec_z_ellip_dop(nvec.z, ellipticity, DOP, bp1_buffer_frame);
}
{
bp1_buffer_frame = encode_uint16_t(to_custom_float_1_1_6_8(X_PV), bp1_buffer_frame);
}
{
bp1_buffer_frame = encode_uint16_t(to_custom_float_1_1_6_8(VPHI), bp1_buffer_frame);
}
return bp1_buffer_frame;
}
void compute_BP1(const float* const spectral_matrices, const uint8_t spectral_matrices_count,
uint8_t* bp1_buffer)
{
DEBUG_CHECK_PTR(spectral_matrices);
DEBUG_CHECK_PTR(bp1_buffer);
const float* spectral_matrix_ptr = spectral_matrices;
uint8_t* bp1_buffer_frame = bp1_buffer;
for (int i = 0; i < spectral_matrices_count; i++)
{
const float mag_PSD = mag_power_spectrum_density(spectral_matrix_ptr);
const float elec_PSD = elec_power_spectrum_density(spectral_matrix_ptr);
const normal_wave_vector_t nvec = normal_wave_vector(spectral_matrix_ptr);
const float ellipticity = wave_ellipticity_estimator(mag_PSD, nvec.ab);
const float DOP = degree_of_polarization(mag_PSD, spectral_matrix_ptr);
const compressed_complex X_PV = X_poynting_vector(spectral_matrix_ptr);
const compressed_complex VPHI = phase_velocity_estimator(spectral_matrix_ptr, nvec);
bp1_buffer_frame
= encode_BP1(mag_PSD, elec_PSD, nvec, ellipticity, DOP, X_PV, VPHI, bp1_buffer_frame);
spectral_matrix_ptr = next_matrix(spectral_matrix_ptr);
}
}
inline uint8_t* _compute_BP2_cross_component(uint8_t* const bp2_frame, float auto1, float auto2,
float cross_re, float cross_imag) __attribute__((always_inline));
uint8_t* _compute_BP2_cross_component(
uint8_t* const bp2_frame, float auto1, float auto2, float cross_re, float cross_imag)
{
DEBUG_CHECK_PTR(bp2_frame);
const float aux = sqrtf(auto1 * auto2);
if (aux != 0.f)
{
encode_float_uint8_t(cross_re / aux, bp2_frame);
encode_float_uint8_t(cross_imag / aux, bp2_frame + 10);
}
else
{
encode_float_uint8_t(0.f, bp2_frame);
encode_float_uint8_t(0.f, bp2_frame + 10);
}
return bp2_frame + 1;
}
void compute_BP2(const float* const spectral_matrices, const uint8_t spectral_matrices_count,
uint8_t* bp2_buffer)
{
DEBUG_CHECK_PTR(spectral_matrices);
DEBUG_CHECK_PTR(bp2_buffer);
const float* sm_ptr = spectral_matrices;
uint8_t* bp2_frame = bp2_buffer;
for (int i = 0; i < spectral_matrices_count; i++)
{
bp2_frame = encode_uint16_t(to_custom_float_6_10(sm_ptr[ASM_COMP_B1B1]), bp2_frame);
bp2_frame = encode_uint16_t(to_custom_float_6_10(sm_ptr[ASM_COMP_B2B2]), bp2_frame);
bp2_frame = encode_uint16_t(to_custom_float_6_10(sm_ptr[ASM_COMP_B3B3]), bp2_frame);
bp2_frame = encode_uint16_t(to_custom_float_6_10(sm_ptr[ASM_COMP_E1E1]), bp2_frame);
bp2_frame = encode_uint16_t(to_custom_float_6_10(sm_ptr[ASM_COMP_E2E2]), bp2_frame);
bp2_frame = _compute_BP2_cross_component(bp2_frame, sm_ptr[ASM_COMP_B1B1],
sm_ptr[ASM_COMP_B2B2], sm_ptr[ASM_COMP_B1B2], sm_ptr[ASM_COMP_B1B2_imag]);
bp2_frame = _compute_BP2_cross_component(bp2_frame, sm_ptr[ASM_COMP_B1B1],
sm_ptr[ASM_COMP_B3B3], sm_ptr[ASM_COMP_B1B3], sm_ptr[ASM_COMP_B1B3_imag]);
bp2_frame = _compute_BP2_cross_component(bp2_frame, sm_ptr[ASM_COMP_B1B1],
sm_ptr[ASM_COMP_E1E1], sm_ptr[ASM_COMP_B1E1], sm_ptr[ASM_COMP_B1E1_imag]);
bp2_frame = _compute_BP2_cross_component(bp2_frame, sm_ptr[ASM_COMP_B1B1],
sm_ptr[ASM_COMP_E2E2], sm_ptr[ASM_COMP_B1E2], sm_ptr[ASM_COMP_B1E2_imag]);
bp2_frame = _compute_BP2_cross_component(bp2_frame, sm_ptr[ASM_COMP_B2B2],
sm_ptr[ASM_COMP_B3B3], sm_ptr[ASM_COMP_B2B3], sm_ptr[ASM_COMP_B2B3_imag]);
bp2_frame = _compute_BP2_cross_component(bp2_frame, sm_ptr[ASM_COMP_B2B2],
sm_ptr[ASM_COMP_E1E1], sm_ptr[ASM_COMP_B2E1], sm_ptr[ASM_COMP_B2E1_imag]);
bp2_frame = _compute_BP2_cross_component(bp2_frame, sm_ptr[ASM_COMP_B2B2],
sm_ptr[ASM_COMP_E2E2], sm_ptr[ASM_COMP_B2E2], sm_ptr[ASM_COMP_B2E2_imag]);
bp2_frame = _compute_BP2_cross_component(bp2_frame, sm_ptr[ASM_COMP_B3B3],
sm_ptr[ASM_COMP_E1E1], sm_ptr[ASM_COMP_B3E1], sm_ptr[ASM_COMP_B3E1_imag]);
bp2_frame = _compute_BP2_cross_component(bp2_frame, sm_ptr[ASM_COMP_B3B3],
sm_ptr[ASM_COMP_E2E2], sm_ptr[ASM_COMP_B3E2], sm_ptr[ASM_COMP_B3E2_imag]);
bp2_frame = _compute_BP2_cross_component(bp2_frame, sm_ptr[ASM_COMP_E1E1],
sm_ptr[ASM_COMP_E2E2], sm_ptr[ASM_COMP_E1E2], sm_ptr[ASM_COMP_E1E2_imag]);
bp2_frame += 10;
sm_ptr = next_matrix(sm_ptr);
}
}