Project

General

Profile

Bug #3123 » basic_parameters.h

bruno katra, 09/10/2018 01:36 PM

 
1
// In the frame of RPW LFR Sofware ICD Issue1 Rev8 (05/07/2013) => R2 FSW
2
// version 1.0: 31/07/2013
3
// version 1.1: 02/04/2014
4
// version 1.2: 30/04/2014
5
// version 1.3: 02/05/2014
6
// version 1.4: 16/05/2014
7
// version 1.5: 20/05/2014
8
// version 1.6: 19/12/2014
9
// version 1.7: 15/01/2015 (modifs de Paul + correction erreurs qui se compensaient (LSB <=> MSB + indices [0,2] <=> [1,3])
10
// version 1.8: 02/02/2015 (gestion des divisions par zéro)
11
// In the frame of RPW LFR Sofware ICD Issue3 Rev6 (27/01/2015) => R3 FSW
12
// version 2.0: 19/06/2015
13
// version 2.1: 22/06/2015 (modifs de Paul)
14
// 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)
15
// version 2.3: 01/07/2015 (affectation initiale des octets 7 et 9 dans les BP1 corrigée ...)
16
// version 2.4: 05/10/2018 (mise en conformité LOGISCOPE)
17

    
18
/*------------------------------------------------------------------------------
19
--  Solar Orbiter's Low Frequency Receiver Flight Software (LFR FSW),
20
--  This file is a part of the LFR FSW
21
--  Copyright (C) 2012-2018, Plasma Physics Laboratory - CNRS
22
--
23
--  This program is free software; you can redistribute it and/or modify
24
--  it under the terms of the GNU General Public License as published by
25
--  the Free Software Foundation; either version 2 of the License, or
26
--  (at your option) any later version.
27
--
28
--  This program is distributed in the hope that it will be useful,
29
--  but WITHOUT ANY WARRANTY; without even the implied warranty of
30
--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31
--  GNU General Public License for more details.
32
--
33
--  You should have received a copy of the GNU General Public License
34
--  along with this program; if not, write to the Free Software
35
--  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
36
-------------------------------------------------------------------------------*/
37
/*--                  Author : Thomas Chust
38
--                   Contact : Thomas Chust
39
--                      Mail : thomas.chust@lpp.polytechnique.fr
40
----------------------------------------------------------------------------*/
41

    
42
#ifndef BASIC_PARAMETERS_H_INCLUDED
43
#define BASIC_PARAMETERS_H_INCLUDED
44

    
45
#include <math.h>
46
#include <stdio.h>
47
#include <stdint.h>
48

    
49
#include "basic_parameters_params.h"
50

    
51
static inline void BP1_set(float * compressed_spec_mat, float * k_coeff_intercalib, unsigned char nb_bins_compressed_spec_mat, unsigned char * lfr_bp1);
52
static inline void BP2_set(float * compressed_spec_mat, unsigned char nb_bins_compressed_spec_mat, unsigned char * lfr_bp2);
53

    
54
void init_k_coefficients_f0( float *k_coeff_intercalib, unsigned char nb_binscompressed_matrix );
55
void init_k_coefficients_f1( float *k_coeff_intercalib, unsigned char nb_binscompressed_matrix );
56
void init_k_coefficients_f2( float *k_coeff_intercalib, unsigned char nb_binscompressed_matrix );
57

    
58
void init_k_coefficients( float *k_coeff_intercalib, unsigned char nb_binscompressed_matrix );
59

    
60
//***********************************
61
// STATIC INLINE FUNCTION DEFINITIONS
62

    
63
void BP1_set( float * compressed_spec_mat, float * k_coeff_intercalib, uint8_t nb_bins_compressed_spec_mat, uint8_t * lfr_bp1 ){
64
    float PSDB;                         // 32-bit floating point
65
    float PSDE;
66
    float tmp;
67
    float NVEC_V0;
68
    float NVEC_V1;
69
    float NVEC_V2;
70
    float aux;
71
    float tr_SB_SB;
72
    float e_cross_b_re;
73
    float e_cross_b_im;
74
    float n_cross_e_scal_b_re;
75
    float n_cross_e_scal_b_im;
76
    float ny;
77
    float nz;
78
    float bx_bx_star;
79
    float vphi;
80
    float significand;
81
    int exponent;                       // 32-bit signed integer
82
    float alpha_M;
83

    
84
    uint8_t nbitexp;                    // 8-bit unsigned integer
85
    uint8_t nbitsig;
86
    uint8_t tmp_uint8;
87
    uint8_t *pt_uint8;                  // pointer on unsigned 8-bit integer
88
    int8_t expmin;                      // 8-bit signed integer
89
    int8_t expmax;
90
    uint16_t rangesig;                  // 16-bit unsigned integer
91
    uint16_t psd;
92
    uint16_t exp;
93
    uint16_t tmp_uint16;
94
    uint16_t i;
95

    
96
    alpha_M = 45 * (3.1415927/180);
97

    
98
#ifdef DEBUG_TCH
99
    printf("BP1 : \n");
100
    printf("Number of bins: %d\n", nb_bins_compressed_spec_mat);
101
#endif
102

    
103
    // initialization for managing the exponents of the floating point data:
104
    nbitexp = 6;                           // number of bits for the exponent
105
    expmax = 32+5;                         // maximum value of the exponent
106
    expmin = (expmax - (1 << nbitexp)) + 1;  // accordingly the minimum exponent value
107
    // for floating point data to be recorded on 16-bit words:
108
    nbitsig = 16 - nbitexp;       // number of bits for the significand
109
    rangesig = (1 << nbitsig)-1;  // == 2^nbitsig - 1
110

    
111
#ifdef DEBUG_TCH
112
    printf("nbitexp : %d, expmax : %d, expmin : %d\n", nbitexp, expmax, expmin);
113
    printf("nbitsig : %d, rangesig : %d\n", nbitsig, rangesig);
114
#endif
115

    
116
    for(i=0; i<nb_bins_compressed_spec_mat; i++){
117
        //==============================================
118
        // BP1 PSDB == PA_LFR_SC_BP1_PB_F0 == 16 bits = 6 bits (exponent) + 10 bits (significand)
119
        PSDB =  compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX]          // S11
120
              + compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 9]        // S22
121
              + compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 16];      // S33
122

    
123
        significand = frexpf(PSDB, &exponent);  // 0.5 <= significand < 1
124
                                                  // PSDB = significand * 2^exponent
125

    
126
        if (exponent < expmin) { // value should be >= 0.5 * 2^expmin
127
          exponent = expmin;
128
          significand = 0.5;     // min value that can be recorded
129
        }
130
        if (exponent > expmax) { // value should be <  0.5 * 2^(expmax+1)
131
          exponent = expmax;
132
          significand = 1.0;     // max value that can be recorded
133
        }
134
        if (significand == 0) {  // in that case exponent == 0 too
135
          exponent = expmin;
136
          significand = 0.5;     // min value that can be recorded
137
        }
138

    
139
        psd = (uint16_t) ((((significand*2) - 1)*rangesig) + 0.5); // Shift and cast into a 16-bit unsigned int with rounding
140
                                                             // where just the first nbitsig bits are used (0, ..., 2^nbitsig-1)
141
        exp = (uint16_t) (exponent-expmin);      // Shift and cast into a 16-bit unsigned int where just
142
                                                 // the first nbitexp bits are used (0, ..., 2^nbitexp-1)
143
        tmp_uint16 = psd | (exp << nbitsig);     // Put the exponent bits (nbitexp) next to the
144
                                                 // left place of the significand bits (nbitsig),
145
                                                 // making the 16-bit word to be recorded
146
        pt_uint8 = (uint8_t*) &tmp_uint16;       // Affect an uint8_t pointer with the adress of tmp_uint16
147
#ifdef MSB_FIRST_TCH
148
        lfr_bp1[(i*NB_BYTES_BP1)+2] = pt_uint8[0]; // Record MSB of tmp_uint16
149
        lfr_bp1[(i*NB_BYTES_BP1)+3] = pt_uint8[1]; // Record LSB of tmp_uint16
150
#endif
151
#ifdef LSB_FIRST_TCH
152
        lfr_bp1[(i*NB_BYTES_BP1)+2] = pt_uint8[1]; // Record MSB of tmp_uint16
153
        lfr_bp1[(i*NB_BYTES_BP1)+3] = pt_uint8[0]; // Record LSB of tmp_uint16
154
#endif
155
#ifdef DEBUG_TCH
156
        printf("\nBin number: %d\n", i);
157
        printf("PSDB        : %16.8e\n",PSDB);
158
        printf("significand : %16.8e\n",significand);
159
        printf("exponent    : %d\n"    ,exponent);
160
        printf("psd for PSDB significand : %d\n",psd);
161
        printf("exp for PSDB exponent : %d\n",exp);
162
        printf("pt_uint8[1] for PSDB exponent + significand: %.3d or %.2x\n",pt_uint8[1], pt_uint8[1]);
163
        printf("pt_uint8[0] for PSDB            significand: %.3d or %.2x\n",pt_uint8[0], pt_uint8[0]);
164
        printf("lfr_bp1[i*NB_BYTES_BP1+2] : %.3d or %.2x\n",lfr_bp1[i*NB_BYTES_BP1+2], lfr_bp1[i*NB_BYTES_BP1+2]);
165
        printf("lfr_bp1[i*NB_BYTES_BP1+3] : %.3d or %.2x\n",lfr_bp1[i*NB_BYTES_BP1+3], lfr_bp1[i*NB_BYTES_BP1+3]);
166
#endif
167
        //==============================================
168
        // BP1 PSDE == PA_LFR_SC_BP1_PE_F0 == 16 bits = 6 bits (exponent) + 10 bits (significand)
169
        PSDE =  (compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 21] * k_coeff_intercalib[(i*NB_K_COEFF_PER_BIN) + K44_PE])        // S44
170
              + (compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 24] * k_coeff_intercalib[(i*NB_K_COEFF_PER_BIN) + K55_PE])        // S55
171
              + (compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 22] * k_coeff_intercalib[(i*NB_K_COEFF_PER_BIN) + K45_PE_RE])     // S45 Re
172
              - (compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 23] * k_coeff_intercalib[(i*NB_K_COEFF_PER_BIN) + K45_PE_IM]);    // S45 Im
173

    
174
        significand = frexpf(PSDE, &exponent); // 0.5 <= significand < 1
175
                                               // PSDE = significand * 2^exponent
176

    
177
        if (exponent < expmin) { // value should be >= 0.5 * 2^expmin
178
          exponent = expmin;
179
          significand = 0.5;     // min value that can be recorded
180
        }
181
        if (exponent > expmax) { // value should be <  0.5 * 2^(expmax+1)
182
          exponent = expmax;
183
          significand = 1.0;     // max value that can be recorded
184
        }
185
        if (significand == 0) {// in that case exponent == 0 too
186
          exponent = expmin;
187
          significand = 0.5;   // min value that can be recorded
188
        }
189

    
190
        psd = (uint16_t) ((((significand*2)-1)*rangesig) + 0.5); // Shift and cast into a 16-bit unsigned int with rounding
191
                                                             // where just the first nbitsig bits are used (0, ..., 2^nbitsig-1)
192
        exp = (uint16_t) (exponent-expmin);      // Shift and cast into a 16-bit unsigned int where just
193
                                                 // the first nbitexp bits are used (0, ..., 2^nbitexp-1)
194
        tmp_uint16 = psd | (exp << nbitsig);     // Put the exponent bits (nbitexp) next to the
195
                                                 // left place of the significand bits (nbitsig),
196
                                                 // making the 16-bit word to be recorded
197
        pt_uint8 = (uint8_t*) &tmp_uint16;       // Affect an uint8_t pointer with the adress of tmp_uint16
198
#ifdef MSB_FIRST_TCH
199
        lfr_bp1[(i*NB_BYTES_BP1) + 0] = pt_uint8[0]; // Record MSB of tmp_uint16
200
        lfr_bp1[(i*NB_BYTES_BP1) + 1] = pt_uint8[1]; // Record LSB of tmp_uint16
201
#endif
202
#ifdef LSB_FIRST_TCH
203
        lfr_bp1[(i*NB_BYTES_BP1) + 0] = pt_uint8[1]; // Record MSB of tmp_uint16
204
        lfr_bp1[(i*NB_BYTES_BP1) + 1] = pt_uint8[0]; // Record LSB of tmp_uint16
205
#endif
206
#ifdef DEBUG_TCH
207
        printf("PSDE        : %16.8e\n",PSDE);
208
        printf("significand : %16.8e\n",significand);
209
        printf("exponent    : %d\n"    ,exponent);
210
        printf("psd for PSDE significand : %d\n",psd);
211
        printf("exp for PSDE exponent : %d\n",exp);
212
        printf("pt_uint8[1] for PSDE exponent + significand: %.3d or %.2x\n",pt_uint8[1], pt_uint8[1]);
213
        printf("pt_uint8[0] for PSDE            significand: %.3d or %.2x\n",pt_uint8[0], pt_uint8[0]);
214
        printf("lfr_bp1[i*NB_BYTES_BP1+0] : %.3d or %.2x\n",lfr_bp1[i*NB_BYTES_BP1+0], lfr_bp1[i*NB_BYTES_BP1+0]);
215
        printf("lfr_bp1[i*NB_BYTES_BP1+1] : %.3d or %.2x\n",lfr_bp1[i*NB_BYTES_BP1+1], lfr_bp1[i*NB_BYTES_BP1+1]);
216
#endif
217
        //==============================================================================
218
        // BP1 normal wave vector == PA_LFR_SC_BP1_NVEC_V0_F0 == 8 bits
219
                               // == PA_LFR_SC_BP1_NVEC_V1_F0 == 8 bits
220
                               // == PA_LFR_SC_BP1_NVEC_V2_F0 == 1 sign bit
221
        tmp = sqrt( (compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 2] *compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 2])   //Im S12
222
                   + (compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 4] *compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 4])   //Im S13
223
                   + (compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 11]*compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 11])  //Im S23
224
                   );
225
        if (tmp != 0.) { // no division by 0.
226
            NVEC_V0 =  compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 11] / tmp;  // S23 Im  => n1
227
            NVEC_V1 = (-compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 4]) / tmp;  // S13 Im  => n2
228
            NVEC_V2 =  compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 2] / tmp;  // S12 Im  => n3
229
        }
230
        else
231
        {
232
            NVEC_V0 = 0.;
233
            NVEC_V1 = 0.;
234
            NVEC_V2 = 0.;
235
        }
236
        lfr_bp1[(i*NB_BYTES_BP1) + 4] = (uint8_t) ((NVEC_V0*127.5) + 128); // Shift and cast into a 8-bit uint8_t (0, ..., 255) with rounding
237
        lfr_bp1[(i*NB_BYTES_BP1) + 5] = (uint8_t) ((NVEC_V1*127.5) + 128); // Shift and cast into a 8-bit uint8_t (0, ..., 255) with rounding
238
        pt_uint8 = (uint8_t*) &NVEC_V2;                              // Affect an uint8_t pointer with the adress of NVEC_V2
239
#ifdef LSB_FIRST_TCH
240
        lfr_bp1[(i*NB_BYTES_BP1) + 6] = pt_uint8[3] & 0x80;  // Extract the sign bit of NVEC_V2 (32-bit float, sign bit in the 4th octet:PC convention)
241
                                                         // Record it at the 8th bit position (from the right to the left) of lfr_bp1[i*NB_BYTES_BP1+6]
242
#endif
243
#ifdef MSB_FIRST_TCH
244
        lfr_bp1[(i*NB_BYTES_BP1) + 6] = pt_uint8[0] & 0x80;  // Extract the sign bit of NVEC_V2 (32-bit float, sign bit in the 1th octet:SPARC convention)
245
                                                         // Record it at the 8th bit position (from the right to the left) of lfr_bp1[i*NB_BYTES_BP1+6]
246
#endif
247
#ifdef DEBUG_TCH
248
        printf("NVEC_V0  : %16.8e\n",NVEC_V0);
249
        printf("NVEC_V1  : %16.8e\n",NVEC_V1);
250
        printf("NVEC_V2  : %16.8e\n",NVEC_V2);
251
        printf("lfr_bp1[i*NB_BYTES_BP1+4] for NVEC_V0 : %u\n",lfr_bp1[i*NB_BYTES_BP1+4]);
252
        printf("lfr_bp1[i*NB_BYTES_BP1+5] for NVEC_V1 : %u\n",lfr_bp1[i*NB_BYTES_BP1+5]);
253
        printf("lfr_bp1[i*NB_BYTES_BP1+6] for NVEC_V2 : %u\n",lfr_bp1[i*NB_BYTES_BP1+6]);
254
#endif
255
        //=======================================================
256
        // BP1 ellipticity == PA_LFR_SC_BP1_ELLIP_F0 == 4 bits
257
        if (PSDB != 0.) { // no division by 0.
258
            aux = 2*tmp / PSDB;                   // Compute the ellipticity
259
        }
260
        else
261
        {
262
            aux = 0.;
263
        }
264
        tmp_uint8 = (uint8_t) ((aux*15) + 0.5); // Shift and cast into a 8-bit uint8_t with rounding
265
                                              // where just the first 4 bits are used (0, ..., 15)
266
        lfr_bp1[(i*NB_BYTES_BP1) + 6] = lfr_bp1[(i*NB_BYTES_BP1) + 6] | (tmp_uint8 << 3); // Put these 4 bits next to the right place
267
                                                                                  // of the sign bit of NVEC_V2 (recorded
268
                                                                                  // previously in lfr_bp1[i*NB_BYTES_BP1+6])
269
#ifdef DEBUG_TCH
270
        printf("ellipticity  : %16.8e\n",aux);
271
        printf("tmp_uint8 for ellipticity : %u\n",tmp_uint8);
272
        printf("lfr_bp1[i*NB_BYTES_BP1+6] for NVEC_V2 + ellipticity : %u\n",lfr_bp1[i*NB_BYTES_BP1+6]);
273
#endif
274
        //==============================================================
275
        // BP1 degree of polarization == PA_LFR_SC_BP1_DOP_F0 == 3 bits
276
        tr_SB_SB = (compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX] * compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX])
277
                 + (compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 9] * compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 9])
278
                 + (compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 16] * compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 16])
279
                 + (2 * compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 1] * compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 1])
280
                 + (2 * compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 2] * compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 2])
281
                 + (2 * compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 3] * compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 3])
282
                 + (2 * compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 4] * compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 4])
283
                 + (2 * compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 10]* compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 10])
284
                 + (2 * compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 11]* compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 11]);
285
        aux = PSDB*PSDB;
286
        if (aux != 0.) { // no division by 0.
287
            tmp = ( 3*tr_SB_SB - aux ) / ( 2 * aux );  // Compute the degree of polarisation
288
        }
289
        else
290
        {
291
            tmp = 0.;
292
        }
293
        tmp_uint8 = (uint8_t) ((tmp*7) + 0.5);       // Shift and cast into a 8-bit uint8_t with rounding
294
                                                   // where just the first 3 bits are used (0, ..., 7)
295
        lfr_bp1[(i*NB_BYTES_BP1) + 6] = lfr_bp1[(i*NB_BYTES_BP1) + 6] | tmp_uint8; // Record these 3 bits at the 3 first bit positions
296
                                                                           // (from the right to the left) of lfr_bp1[i*NB_BYTES_BP1+6]
297
#ifdef DEBUG_TCH
298
        printf("DOP  : %16.8e\n",tmp);
299
        printf("tmp_uint8 for DOP : %u\n",tmp_uint8);
300
        printf("lfr_bp1[i*NB_BYTES_BP1+6] for NVEC_V2 + ellipticity + DOP : %u\n",lfr_bp1[i*NB_BYTES_BP1+6]);
301
#endif
302
        //=======================================================================================
303
        // BP1 X_SO-component of the Poynting flux == PA_LFR_SC_BP1_SX_F0 == 16 bits
304
        //                                          = 1 sign bit + 1 argument bit (two sectors)
305
        //                                          + 6 bits (exponent) + 8 bits (significand)
306
        e_cross_b_re =  (compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 17] * k_coeff_intercalib[(i*NB_K_COEFF_PER_BIN) + K34_SX_RE])  //S34 Re
307
                      + (compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 19] * k_coeff_intercalib[(i*NB_K_COEFF_PER_BIN) + K35_SX_RE])  //S35 Re
308
                      + (compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 5] * k_coeff_intercalib[(i*NB_K_COEFF_PER_BIN) + K14_SX_RE])  //S14 Re
309
                      + (compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 7] * k_coeff_intercalib[(i*NB_K_COEFF_PER_BIN) + K15_SX_RE])  //S15 Re
310
                      + (compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 12] * k_coeff_intercalib[(i*NB_K_COEFF_PER_BIN) + K24_SX_RE])  //S24 Re
311
                      + (compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 14] * k_coeff_intercalib[(i*NB_K_COEFF_PER_BIN) + K25_SX_RE]) //S25 Re
312
                      + (compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 18] * k_coeff_intercalib[(i*NB_K_COEFF_PER_BIN) + K34_SX_IM])  //S34 Im
313
                      + (compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 20] * k_coeff_intercalib[(i*NB_K_COEFF_PER_BIN) + K35_SX_IM])  //S35 Im
314
                      + (compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 6] * k_coeff_intercalib[(i*NB_K_COEFF_PER_BIN) + K14_SX_IM])  //S14 Im
315
                      + (compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 8] * k_coeff_intercalib[(i*NB_K_COEFF_PER_BIN) + K15_SX_IM])  //S15 Im
316
                      + (compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 13] * k_coeff_intercalib[(i*NB_K_COEFF_PER_BIN) + K24_SX_IM])  //S24 Im
317
                      + (compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 15] * k_coeff_intercalib[(i*NB_K_COEFF_PER_BIN) + K25_SX_IM]); //S25 Im
318
        // Im(S_ji) = -Im(S_ij)
319
        // k_ji = k_ij
320
        e_cross_b_im =  (compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 17]*k_coeff_intercalib[(i*NB_K_COEFF_PER_BIN) + K34_SX_IM])  //S34 Re
321
                      + (compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 19]*k_coeff_intercalib[(i*NB_K_COEFF_PER_BIN) + K35_SX_IM])  //S35 Re
322
                      + (compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 5] *k_coeff_intercalib[(i*NB_K_COEFF_PER_BIN) + K14_SX_IM])  //S14 Re
323
                      + (compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 7] *k_coeff_intercalib[(i*NB_K_COEFF_PER_BIN) + K15_SX_IM])  //S15 Re
324
                      + (compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 12]*k_coeff_intercalib[(i*NB_K_COEFF_PER_BIN) + K24_SX_IM])  //S24 Re
325
                      + ((compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 14]*k_coeff_intercalib[(i*NB_K_COEFF_PER_BIN) + K25_SX_IM])  //S25 Re
326
                      - (compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 18]*k_coeff_intercalib[(i*NB_K_COEFF_PER_BIN) + K34_SX_RE])  //S34 Im
327
                      - (compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 20]*k_coeff_intercalib[(i*NB_K_COEFF_PER_BIN) + K35_SX_RE])  //S35 Im
328
                      - (compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 6] *k_coeff_intercalib[(i*NB_K_COEFF_PER_BIN) + K14_SX_RE])  //S14 Im
329
                      - (compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 8] *k_coeff_intercalib[(i*NB_K_COEFF_PER_BIN) + K15_SX_RE])  //S15 Im
330
                      - (compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 13]*k_coeff_intercalib[(i*NB_K_COEFF_PER_BIN) + K24_SX_RE])  //S24 Im
331
                      - (compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 15]*k_coeff_intercalib[(i*NB_K_COEFF_PER_BIN) + K25_SX_RE])); //S25 Im
332
#ifdef DEBUG_TCH
333
        printf("ReaSX       : %16.8e\n",e_cross_b_re);
334
#endif
335
        pt_uint8 = (uint8_t*) &e_cross_b_re;      // Affect an uint8_t pointer with the adress of e_cross_b_re
336
#ifdef LSB_FIRST_TCH
337

    
338
        lfr_bp1[(i*NB_BYTES_BP1) + 7] = (uint8_t) (pt_uint8[3] & 0x80);  // Extract its sign bit (32-bit float, sign bit in the 4th octet:PC convention)
339
                                                                     // Record it at the 8th bit position (from the right to the left)
340
                                                                     // of lfr_bp1[i*NB_BYTES_BP1+7]
341
        pt_uint8[3] = (pt_uint8[3] & 0x7f);       // Make e_cross_b_re be positive in any case: |ReaSX|
342
#endif
343
#ifdef MSB_FIRST_TCH
344
        lfr_bp1[(i*NB_BYTES_BP1) + 7] = (uint8_t) (pt_uint8[0] & 0x80);  // Extract its sign bit (32-bit float, sign bit in the 1th octet:SPARC convention)
345
                                                                     // Record it at the 8th bit position (from the right to the left)
346
                                                                     // of lfr_bp1[i*NB_BYTES_BP1+7]
347
        pt_uint8[0] = (pt_uint8[0] & 0x7f);       // Make e_cross_b_re be positive in any case: |ReaSX|
348
#endif
349
        significand = frexpf(e_cross_b_re, &exponent); // 0.5 <= significand < 1
350
                                                       // ReaSX = significand * 2^exponent
351
        if (exponent < expmin) { // value should be >= 0.5 * 2^expmin
352
          exponent = expmin;
353
          significand = 0.5;     // min value that can be recorded
354
        }
355
        if (exponent > expmax) { // value should be <  0.5 * 2^(expmax+1)
356
          exponent = expmax;
357
          significand = 1.0;     // max value that can be recorded
358
        }
359
        if (significand == 0) {  // in that case exponent == 0 too
360
          exponent = expmin;
361
          significand = 0.5;     // min value that can be recorded
362
        }
363

    
364
        lfr_bp1[(i*NB_BYTES_BP1) + 8] = (uint8_t) ((((significand*2)-1)*255) + 0.5); // Shift and cast into a 8-bit uint8_t with rounding
365
                                                                             // where all bits are used (0, ..., 255)
366
        tmp_uint8 = (uint8_t) (exponent-expmin); // Shift and cast into a 8-bit uint8_t where
367
                                                 // just the first nbitexp bits are used (0, ..., 2^nbitexp-1)
368
#ifdef DEBUG_TCH
369
        printf("|ReaSX|     : %16.8e\n",e_cross_b_re);
370
        printf("significand : %16.8e\n",significand);
371
        printf("exponent    : %d\n"    ,exponent);
372
        printf("tmp_uint8        for ReaSX exponent    : %d\n",tmp_uint8);
373
#endif
374
        lfr_bp1[(i*NB_BYTES_BP1) + 7] = lfr_bp1[(i*NB_BYTES_BP1) + 7] | tmp_uint8; // Record these nbitexp bits in the nbitexp first bits
375
                                                                           // (from the right to the left) of lfr_bp1[i*NB_BYTES_BP1+7]
376
#ifdef DEBUG_TCH
377
        printf("lfr_bp1[i*NB_BYTES_BP1+7] for ReaSX sign + RealSX exponent : %u\n",lfr_bp1[i*NB_BYTES_BP1+7]);
378
        printf("lfr_bp1[i*NB_BYTES_BP1+8] for ReaSX significand            : %u\n",lfr_bp1[i*NB_BYTES_BP1+8]);
379
        printf("ImaSX       : %16.8e\n",e_cross_b_im);
380
#endif
381
        pt_uint8 = (uint8_t*) &e_cross_b_im; // Affect an uint8_t pointer with the adress of e_cross_b_im
382
#ifdef LSB_FIRST_TCH
383
        pt_uint8[3] = pt_uint8[3] & 0x7f;    // Make e_cross_b_im be positive in any case: |ImaSX| (32-bit float, sign bit in the 4th octet:PC convention)
384
#endif
385
#ifdef MSB_FIRST_TCH
386
        pt_uint8[0] = pt_uint8[0] & 0x7f;    // Make e_cross_b_im be positive in any case: |ImaSX| (32-bit float, sign bit in the 1th octet:SPARC convention)
387
#endif
388
        // Determine the sector argument of SX. If |Im| > |Re| affect
389
        // an unsigned 8-bit char with 01000000; otherwise with null.
390
        if (e_cross_b_im > e_cross_b_re) {
391
            tmp_uint8 = 0x40;
392
        }
393
        else {
394
            tmp_uint8 = 0x00;
395
        }
396

    
397
        lfr_bp1[(i*NB_BYTES_BP1) + 7] = lfr_bp1[(i*NB_BYTES_BP1) + 7] |  tmp_uint8; // Record it as a sign bit at the 7th bit position (from the right
398
                                                                            // to the left) of lfr_bp1[i*NB_BYTES_BP1+7], by simple logical addition.
399
#ifdef DEBUG_TCH
400
        printf("|ImaSX|     : %16.8e\n",e_cross_b_im);
401
        printf("ArgSX sign  : %u\n",tmp_uint8);
402
        printf("lfr_bp1[i*NB_BYTES_BP1+7] for ReaSX & ArgSX signs + ReaSX exponent  : %u\n",lfr_bp1[i*NB_BYTES_BP1+7]);
403
#endif
404
        //======================================================================
405
        // BP1 phase velocity estimator == PA_LFR_SC_BP1_VPHI_F0 == 16 bits
406
        //                                          = 1 sign bit + 1 argument bit (two sectors)
407
        //                                          + 6 bits (exponent) + 8 bits (significand)
408
        ny = (sin(alpha_M)*NVEC_V1) + (cos(alpha_M)*NVEC_V2);
409
        nz = NVEC_V0;
410
        bx_bx_star = (cos(alpha_M)*cos(alpha_M)*compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 9])   // S22 Re
411
                   + ((sin(alpha_M)*sin(alpha_M)*compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 16])  // S33 Re
412
                   - (2*sin(alpha_M)*cos(alpha_M)*compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 10])); // S23 Re
413

    
414
        n_cross_e_scal_b_re = (ny * ((compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 12] * k_coeff_intercalib[(i*NB_K_COEFF_PER_BIN) + K24_NY_RE])  //S24 Re
415
                                   +(compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 14] * k_coeff_intercalib[(i*NB_K_COEFF_PER_BIN) + K25_NY_RE])  //S25 Re
416
                                   +(compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 17] * k_coeff_intercalib[(i*NB_K_COEFF_PER_BIN) + K34_NY_RE])  //S34 Re
417
                                   +(compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 19] * k_coeff_intercalib[(i*NB_K_COEFF_PER_BIN) + K35_NY_RE])  //S35 Re
418
                                   +(compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 13] * k_coeff_intercalib[(i*NB_K_COEFF_PER_BIN) + K24_NY_IM])  //S24 Im
419
                                   +(compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 15] * k_coeff_intercalib[(i*NB_K_COEFF_PER_BIN) + K25_NY_IM])  //S25 Im
420
                                   +(compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 18] * k_coeff_intercalib[(i*NB_K_COEFF_PER_BIN) + K34_NY_IM])  //S34 Im
421
                                   +(compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 20] * k_coeff_intercalib[(i*NB_K_COEFF_PER_BIN) + K35_NY_IM]))) //S35 Im
422
                            + (nz * ((compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 12] * k_coeff_intercalib[(i*NB_K_COEFF_PER_BIN) + K24_NZ_RE])  //S24 Re
423
                                   +(compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 14] * k_coeff_intercalib[(i*NB_K_COEFF_PER_BIN) + K25_NZ_RE])  //S25 Re
424
                                   +(compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 17] * k_coeff_intercalib[(i*NB_K_COEFF_PER_BIN) + K34_NZ_RE])  //S34 Re
425
                                   +(compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 19] * k_coeff_intercalib[(i*NB_K_COEFF_PER_BIN) + K35_NZ_RE])  //S35 Re
426
                                   +(compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 13] * k_coeff_intercalib[(i*NB_K_COEFF_PER_BIN) + K24_NZ_IM])  //S24 Im
427
                                   +(compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 15] * k_coeff_intercalib[(i*NB_K_COEFF_PER_BIN) + K25_NZ_IM])  //S25 Im
428
                                   +(compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 18] * k_coeff_intercalib[(i*NB_K_COEFF_PER_BIN) + K34_NZ_IM])  //S34 Im
429
                                   +(compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 20] * k_coeff_intercalib[(i*NB_K_COEFF_PER_BIN) + K35_NZ_IM])));//S35 Im
430
        // Im(S_ji) = -Im(S_ij)
431
        // k_ji = k_ij
432
        n_cross_e_scal_b_im = (ny * ((compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 12] * k_coeff_intercalib[(i*NB_K_COEFF_PER_BIN) + K24_NY_IM])  //S24 Re
433
                                   +(compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 14] * k_coeff_intercalib[(i*NB_K_COEFF_PER_BIN) + K25_NY_IM])  //S25 Re
434
                                   +(compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 17] * k_coeff_intercalib[(i*NB_K_COEFF_PER_BIN) + K34_NY_IM])  //S34 Re
435
                                   +((compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 19] * k_coeff_intercalib[(i*NB_K_COEFF_PER_BIN) + K35_NY_IM])  //S35 Re
436
                                   -(compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 13] * k_coeff_intercalib[(i*NB_K_COEFF_PER_BIN) + K24_NY_RE])  //S24 Im
437
                                   -(compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 15] * k_coeff_intercalib[(i*NB_K_COEFF_PER_BIN) + K25_NY_RE])  //S25 Im
438
                                   -(compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 18] * k_coeff_intercalib[(i*NB_K_COEFF_PER_BIN) + K34_NY_RE])  //S34 Im
439
                                   -(compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 20] * k_coeff_intercalib[(i*NB_K_COEFF_PER_BIN) + K35_NY_RE])))) //S35 Im
440
                            + (nz * ((compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 12] * k_coeff_intercalib[(i*NB_K_COEFF_PER_BIN) + K24_NZ_IM])  //S24 Re
441
                                   +(compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 14] * k_coeff_intercalib[(i*NB_K_COEFF_PER_BIN) + K25_NZ_IM])  //S25 Re
442
                                   +(compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 17] * k_coeff_intercalib[(i*NB_K_COEFF_PER_BIN) + K34_NZ_IM] ) //S34 Re
443
                                   +((compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 19] * k_coeff_intercalib[(i*NB_K_COEFF_PER_BIN) + K35_NZ_IM])  //S35 Re
444
                                   -(compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 13] * k_coeff_intercalib[(i*NB_K_COEFF_PER_BIN) + K24_NZ_RE])  //S24 Im
445
                                   -(compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 15] * k_coeff_intercalib[(i*NB_K_COEFF_PER_BIN) + K25_NZ_RE])  //S25 Im
446
                                   -(compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 18] * k_coeff_intercalib[(i*NB_K_COEFF_PER_BIN) + K34_NZ_RE])  //S34 Im
447
                                   -(compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 20] * k_coeff_intercalib[(i*NB_K_COEFF_PER_BIN) + K35_NZ_RE]))));//S35 Im
448
#ifdef DEBUG_TCH
449
        printf("n_cross_e_scal_b_re   : %16.8e\n",n_cross_e_scal_b_re);
450
        printf("n_cross_e_scal_b_im   : %16.8e\n",n_cross_e_scal_b_im);
451
#endif
452
        // vphi = n_cross_e_scal_b_re / bx_bx_star => sign(VPHI) = sign(n_cross_e_scal_b_re)
453
        pt_uint8 = (uint8_t*) &n_cross_e_scal_b_re; // Affect an uint8_t pointer with the adress of n_cross_e_scal_b_re
454
#ifdef LSB_FIRST_TCH
455
        lfr_bp1[(i*NB_BYTES_BP1) + 9] = (uint8_t) (pt_uint8[3] & 0x80);  // Extract its sign bit (32-bit float, sign bit in the 4th octet:PC convention)
456
                                                                     // Record it at the 8th bit position (from the right to the left)
457
                                                                     // of lfr_bp1[i*NB_BYTES_BP1+9]
458
        pt_uint8[3] = (pt_uint8[3] & 0x7f);     // Make n_cross_e_scal_b_re be positive in any case: |n_cross_e_scal_b_re|
459
#endif
460
#ifdef MSB_FIRST_TCH
461
        lfr_bp1[(i*NB_BYTES_BP1) + 9] = (uint8_t) (pt_uint8[0] & 0x80);  // Extract its sign bit (32-bit float, sign bit in the 1th octet:SPARC convention)
462
                                                                     // Record it at the 8th bit position (from the right to the left)
463
                                                                     // of lfr_bp1[i*NB_BYTES_BP1+9]
464
        pt_uint8[0] = (pt_uint8[0] & 0x7f);     // Make n_cross_e_scal_b_re be positive in any case: |n_cross_e_scal_b_re|
465
#endif
466
        if (bx_bx_star != 0.) { // no division by 0.
467
            vphi = n_cross_e_scal_b_re / bx_bx_star;  // Compute |VPHI|
468
        }
469
        else
470
        {
471
            vphi = 1.e+20;                         // Put a huge value
472
        }
473
        significand = frexpf(vphi, &exponent);  // 0.5 <= significand < 1
474
                                                // vphi = significand * 2^exponent
475
        if (exponent < expmin) { // value should be >= 0.5 * 2^expmin
476
          exponent = expmin;
477
          significand = 0.5;     // min value that can be recorded
478
        }
479
        if (exponent > expmax) { // value should be <  0.5 * 2^(expmax+1)
480
          exponent = expmax;
481
          significand = 1.0;     // max value that can be recorded
482
        }
483
        if (significand == 0) {// in that case exponent == 0 too
484
          exponent = expmin;
485
          significand = 0.5;   // min value that can be recorded
486
        }
487

    
488
        lfr_bp1[(i*NB_BYTES_BP1) + 10] = (uint8_t) ((((significand*2)-1)*255) + 0.5); // Shift and cast into a 8-bit uint8_t with rounding
489
                                                                           // where all the bits are used (0, ..., 255)
490
        tmp_uint8 = (uint8_t) (exponent-expmin); // Shift and cast into a 8-bit uint8_t where
491
                                                 // just the first nbitexp bits are used (0, ..., 2^nbitexp-1)
492
#ifdef DEBUG_TCH
493
        printf("|VPHI|      : %16.8e\n",vphi);
494
        printf("significand : %16.8e\n",significand);
495
        printf("exponent    : %d\n"    ,exponent);
496
        printf("tmp_uint8        for VPHI exponent    : %d\n",tmp_uint8);
497
#endif
498
        lfr_bp1[(i*NB_BYTES_BP1) + 9] = lfr_bp1[(i*NB_BYTES_BP1) + 9] | tmp_uint8; // Record these nbitexp bits in the nbitexp first bits
499
                                                                           // (from the right to the left) of lfr_bp1[i*NB_BYTES_BP1+9]
500
#ifdef DEBUG_TCH
501
        printf("lfr_bp1[i*NB_BYTES_BP1+9]  for VPHI sign + VPHI exponent : %u\n",lfr_bp1[i*NB_BYTES_BP1+9]);
502
        printf("lfr_bp1[i*NB_BYTES_BP1+10] for VPHI significand          : %u\n",lfr_bp1[i*NB_BYTES_BP1+10]);
503
#endif
504
        pt_uint8 = (uint8_t*) &n_cross_e_scal_b_im; // Affect an uint8_t pointer with the adress of n_cross_e_scal_b_im
505
#ifdef LSB_FIRST_TCH
506
        pt_uint8[3] = pt_uint8[3] & 0x7f;           // Make n_cross_e_scal_b_im be positive in any case: |ImaNEBX| (32-bit float, sign bit in the 4th octet:PC convention)
507
#endif
508
#ifdef MSB_FIRST_TCH
509
        pt_uint8[0] = pt_uint8[0] & 0x7f;           // Make n_cross_e_scal_b_im be positive in any case: |ImaNEBX| (32-bit float, sign bit in the 1th octet:SPARC convention)
510
#endif
511

    
512
        // Determine the sector argument of NEBX. If |Im| > |Re| affect
513
        // an unsigned 8-bit char with 01000000; otherwise with null.
514
        if (n_cross_e_scal_b_im > n_cross_e_scal_b_re) {
515
            tmp_uint8 = 0x40;
516
        }
517
        else {
518
            tmp_uint8 = 0x00;
519
        }
520

    
521
        lfr_bp1[(i*NB_BYTES_BP1) + 9] = lfr_bp1[(i*NB_BYTES_BP1) + 9] |  tmp_uint8;    // Record it as a sign bit at the 7th bit position (from the right
522
                                                                               // to the left) of lfr_bp1[i*NB_BYTES_BP1+9], by simple logical addition.
523
#ifdef DEBUG_TCH
524
        printf("|n_cross_e_scal_b_im|             : %16.8e\n",n_cross_e_scal_b_im);
525
        printf("|n_cross_e_scal_b_im|/bx_bx_star  : %16.8e\n",n_cross_e_scal_b_im/bx_bx_star);
526
        printf("ArgNEBX sign                      : %u\n",tmp_uint8);
527
        printf("lfr_bp1[i*NB_BYTES_BP1+9] for VPHI & ArgNEBX signs + VPHI exponent : %u\n",lfr_bp1[i*NB_BYTES_BP1+9]);
528
#endif
529
    }
530
}
531

    
532
void BP2_set( float * compressed_spec_mat, uint8_t nb_bins_compressed_spec_mat, uint8_t * lfr_bp2 )
533
{
534
    float cross_re;                     // 32-bit floating point
535
    float cross_im;
536
    float aux;
537
    float significand;
538
    int exponent;                       // 32-bit signed integer
539
    uint8_t nbitexp;                    // 8-bit unsigned integer
540
    uint8_t nbitsig;
541
    uint8_t *pt_uint8;                  // pointer on unsigned 8-bit integer
542
    int8_t expmin;                      // 8-bit signed integer
543
    int8_t expmax;
544
    uint16_t rangesig;                  // 16-bit unsigned integer
545
    uint16_t autocor;
546
    uint16_t exp;
547
    uint16_t tmp_uint16;
548
    uint16_t i;
549

    
550
#ifdef DEBUG_TCH
551
    printf("BP2 : \n");
552
    printf("Number of bins: %d\n", nb_bins_compressed_spec_mat);
553
#endif
554

    
555
    // For floating point data to be recorded on 16-bit words :
556
    nbitexp = 6;                  // number of bits for the exponent
557
    nbitsig = 16 - nbitexp;       // number of bits for the significand
558
    rangesig = (1 << nbitsig)-1;  // == 2^nbitsig - 1
559
    expmax = 32 + 5;
560
    expmin = (expmax - (1 << nbitexp)) + 1;
561

    
562
#ifdef DEBUG_TCH
563

    
564
    printf("nbitexp : %d, expmax : %d, expmin : %d\n", nbitexp, expmax, expmin);
565
    printf("nbitsig : %d, rangesig : %d\n", nbitsig, rangesig);
566
#endif
567

    
568
    for(i = 0; i<nb_bins_compressed_spec_mat; i++){
569
        //==============================================
570
        // BP2 normalized cross correlations == PA_LFR_SC_BP2_CROSS_F0 == 10 * (8+8) bits
571
                                          // == PA_LFR_SC_BP2_CROSS_RE_0_F0 == 8 bits
572
                                          // == PA_LFR_SC_BP2_CROSS_IM_0_F0 == 8 bits
573
                                          // == PA_LFR_SC_BP2_CROSS_RE_1_F0 == 8 bits
574
                                          // == PA_LFR_SC_BP2_CROSS_IM_1_F0 == 8 bits
575
                                          // == PA_LFR_SC_BP2_CROSS_RE_2_F0 == 8 bits
576
                                          // == PA_LFR_SC_BP2_CROSS_IM_2_F0 == 8 bits
577
                                          // == PA_LFR_SC_BP2_CROSS_RE_3_F0 == 8 bits
578
                                          // == PA_LFR_SC_BP2_CROSS_IM_3_F0 == 8 bits
579
                                          // == PA_LFR_SC_BP2_CROSS_RE_4_F0 == 8 bits
580
                                          // == PA_LFR_SC_BP2_CROSS_IM_4_F0 == 8 bits
581
                                          // == PA_LFR_SC_BP2_CROSS_RE_5_F0 == 8 bits
582
                                          // == PA_LFR_SC_BP2_CROSS_IM_5_F0 == 8 bits
583
                                          // == PA_LFR_SC_BP2_CROSS_RE_6_F0 == 8 bits
584
                                          // == PA_LFR_SC_BP2_CROSS_IM_6_F0 == 8 bits
585
                                          // == PA_LFR_SC_BP2_CROSS_RE_7_F0 == 8 bits
586
                                          // == PA_LFR_SC_BP2_CROSS_IM_7_F0 == 8 bits
587
                                          // == PA_LFR_SC_BP2_CROSS_RE_8_F0 == 8 bits
588
                                          // == PA_LFR_SC_BP2_CROSS_IM_8_F0 == 8 bits
589
                                          // == PA_LFR_SC_BP2_CROSS_RE_9_F0 == 8 bits
590
                                          // == PA_LFR_SC_BP2_CROSS_IM_9_F0 == 8 bits
591
        // S12
592
        aux = sqrt(compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX] * compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 9]);
593
        if (aux != 0.) { // no division by 0.
594
        cross_re = compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 1] / aux;
595
        cross_im = compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 2] / aux;
596
        }
597
        else
598
        {
599
        cross_re = 0.;
600
        cross_im = 0.;
601
        }
602
        lfr_bp2[(i*NB_BYTES_BP2) + 10] = (uint8_t) ((cross_re*127.5) + 128); // Shift and cast into a 8-bit uint8_t (0, ..., 255) with rounding
603
        lfr_bp2[(i*NB_BYTES_BP2) + 20] = (uint8_t) ((cross_im*127.5) + 128); // Shift and cast into a 8-bit uint8_t (0, ..., 255) with rounding
604
#ifdef DEBUG_TCH
605
        printf("\nBin number: %d\n", i);
606
        printf("lfr_bp2[i*NB_BYTES_BP2+10] for cross12_re (%16.8e) : %.3u\n",cross_re, lfr_bp2[i*NB_BYTES_BP2+10]);
607
        printf("lfr_bp2[i*NB_BYTES_BP2+20] for cross12_im (%16.8e) : %.3u\n",cross_im, lfr_bp2[i*NB_BYTES_BP2+20]);
608
#endif
609
        // S13
610
        aux = sqrt(compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX] * compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 16]);
611
        if (aux != 0.) { // no division by 0.
612
        cross_re = compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 3] / aux;
613
        cross_im = compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 4] / aux;
614
        }
615
        else
616
        {
617
        cross_re = 0.;
618
        cross_im = 0.;
619
        }
620
        lfr_bp2[(i*NB_BYTES_BP2) + 11] = (uint8_t) ((cross_re*127.5) + 128); // Shift and cast into a 8-bit uint8_t (0, ..., 255) with rounding
621
        lfr_bp2[(i*NB_BYTES_BP2) + 21] = (uint8_t) ((cross_im*127.5) + 128); // Shift and cast into a 8-bit uint8_t (0, ..., 255) with rounding
622
#ifdef DEBUG_TCH
623
        printf("lfr_bp2[i*NB_BYTES_BP2+11] for cross13_re (%16.8e) : %.3u\n",cross_re, lfr_bp2[i*NB_BYTES_BP2+11]);
624
        printf("lfr_bp2[i*NB_BYTES_BP2+21] for cross13_im (%16.8e) : %.3u\n",cross_im, lfr_bp2[i*NB_BYTES_BP2+21]);
625
#endif
626
        // S14
627
        aux = sqrt(compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX] * compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 21]);
628
        if (aux != 0.) { // no division by 0.
629
        cross_re = compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 5] / aux;
630
        cross_im = compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 6] / aux;
631
        }
632
        else
633
        {
634
        cross_re = 0.;
635
        cross_im = 0.;
636
        }
637
        lfr_bp2[(i*NB_BYTES_BP2) + 12] = (uint8_t) ((cross_re*127.5) + 128); // Shift and cast into a 8-bit uint8_t (0, ..., 255) with rounding
638
        lfr_bp2[(i*NB_BYTES_BP2) + 22] = (uint8_t) ((cross_im*127.5) + 128); // Shift and cast into a 8-bit uint8_t (0, ..., 255) with rounding
639
#ifdef DEBUG_TCH
640
        printf("lfr_bp2[i*NB_BYTES_BP2+12] for cross14_re (%16.8e) : %.3u\n",cross_re, lfr_bp2[i*NB_BYTES_BP2+12]);
641
        printf("lfr_bp2[i*NB_BYTES_BP2+22] for cross14_im (%16.8e) : %.3u\n",cross_im, lfr_bp2[i*NB_BYTES_BP2+22]);
642
#endif
643
        // S15
644
        aux = sqrt(compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX] * compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 24]);
645
        if (aux != 0.) { // no division by 0.
646
        cross_re = compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 7] / aux;
647
        cross_im = compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 8] / aux;
648
        }
649
        else
650
        {
651
        cross_re = 0.;
652
        cross_im = 0.;
653
        }
654
        lfr_bp2[(i*NB_BYTES_BP2) + 13] = (uint8_t) ((cross_re*127.5) + 128); // Shift and cast into a 8-bit uint8_t (0, ..., 255) with rounding
655
        lfr_bp2[(i*NB_BYTES_BP2) + 23] = (uint8_t) ((cross_im*127.5) + 128); // Shift and cast into a 8-bit uint8_t (0, ..., 255) with rounding
656
#ifdef DEBUG_TCH
657
        printf("lfr_bp2[i*NB_BYTES_BP2+13] for cross15_re (%16.8e) : %.3u\n",cross_re, lfr_bp2[i*NB_BYTES_BP2+13]);
658
        printf("lfr_bp2[i*NB_BYTES_BP2+23] for cross15_im (%16.8e) : %.3u\n",cross_im, lfr_bp2[i*NB_BYTES_BP2+23]);
659
#endif
660
        // S23
661
        aux = sqrt(compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 9] * compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 16]);
662
        if (aux != 0.) { // no division by 0.
663
        cross_re = compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 10] / aux;
664
        cross_im = compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 11] / aux;
665
        }
666
        else
667
        {
668
        cross_re = 0.;
669
        cross_im = 0.;
670
        }
671
        lfr_bp2[(i*NB_BYTES_BP2) + 14] = (uint8_t) ((cross_re*127.5) + 128); // Shift and cast into a 8-bit uint8_t (0, ..., 255) with rounding
672
        lfr_bp2[(i*NB_BYTES_BP2) + 24] = (uint8_t) ((cross_im*127.5) + 128); // Shift and cast into a 8-bit uint8_t (0, ..., 255) with rounding
673
#ifdef DEBUG_TCH
674
        printf("lfr_bp2[i*NB_BYTES_BP2+14] for cross23_re (%16.8e) : %.3u\n",cross_re, lfr_bp2[i*NB_BYTES_BP2+14]);
675
        printf("lfr_bp2[i*NB_BYTES_BP2+24] for cross23_im (%16.8e) : %.3u\n",cross_im, lfr_bp2[i*NB_BYTES_BP2+24]);
676
#endif
677
        // S24
678
        aux = sqrt(compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 9] * compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 21]);
679
        if (aux != 0.) { // no division by 0.
680
        cross_re = compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 12] / aux;
681
        cross_im = compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 13] / aux;
682
        }
683
        else
684
        {
685
        cross_re = 0.;
686
        cross_im = 0.;
687
        }
688
        lfr_bp2[(i*NB_BYTES_BP2) + 15] = (uint8_t) ((cross_re*127.5) + 128); // Shift and cast into a 8-bit uint8_t (0, ..., 255) with rounding
689
        lfr_bp2[(i*NB_BYTES_BP2) + 25] = (uint8_t) ((cross_im*127.5) + 128); // Shift and cast into a 8-bit uint8_t (0, ..., 255) with rounding
690
#ifdef DEBUG_TCH
691
        printf("lfr_bp2[i*NB_BYTES_BP2+15] for cross24_re (%16.8e) : %.3u\n",cross_re, lfr_bp2[i*NB_BYTES_BP2+15]);
692
        printf("lfr_bp2[i*NB_BYTES_BP2+25] for cross24_im (%16.8e) : %.3u\n",cross_im, lfr_bp2[i*NB_BYTES_BP2+25]);
693
#endif
694
        // S25
695
        aux = sqrt(compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 9] * compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 24]);
696
        if (aux != 0.) { // no division by 0.
697
        cross_re = compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 14] / aux;
698
        cross_im = compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 15] / aux;
699
        }
700
        else
701
        {
702
        cross_re = 0.;
703
        cross_im = 0.;
704
        }
705
        lfr_bp2[(i*NB_BYTES_BP2) + 16] = (uint8_t) ((cross_re*127.5) + 128); // Shift and cast into a 8-bit uint8_t (0, ..., 255) with rounding
706
        lfr_bp2[(i*NB_BYTES_BP2) + 26] = (uint8_t) ((cross_im*127.5) + 128); // Shift and cast into a 8-bit uint8_t (0, ..., 255) with rounding
707
#ifdef DEBUG_TCH
708
        printf("lfr_bp2[i*NB_BYTES_BP2+16] for cross25_re (%16.8e) : %.3u\n",cross_re, lfr_bp2[i*NB_BYTES_BP2+16]);
709
        printf("lfr_bp2[i*NB_BYTES_BP2+26] for cross25_im (%16.8e) : %.3u\n",cross_im, lfr_bp2[i*NB_BYTES_BP2+26]);
710
#endif
711
        // S34
712
        aux = sqrt(compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 16] * compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 21]);
713
        if (aux != 0.) { // no division by 0.
714
        cross_re = compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 17] / aux;
715
        cross_im = compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 18] / aux;
716
        }
717
        else
718
        {
719
        cross_re = 0.;
720
        cross_im = 0.;
721
        }
722
        lfr_bp2[(i*NB_BYTES_BP2) + 17] = (uint8_t) ((cross_re*127.5) + 128); // Shift and cast into a 8-bit uint8_t (0, ..., 255) with rounding
723
        lfr_bp2[(i*NB_BYTES_BP2) + 27] = (uint8_t) ((cross_im*127.5) + 128); // Shift and cast into a 8-bit uint8_t (0, ..., 255) with rounding
724
#ifdef DEBUG_TCH
725
        printf("lfr_bp2[i*NB_BYTES_BP2+17] for cross34_re (%16.8e) : %.3u\n",cross_re, lfr_bp2[i*NB_BYTES_BP2+17]);
726
        printf("lfr_bp2[i*NB_BYTES_BP2+27] for cross34_im (%16.8e) : %.3u\n",cross_im, lfr_bp2[i*NB_BYTES_BP2+27]);
727
#endif
728
        // S35
729
        aux = sqrt(compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 16] * compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 24]);
730
        if (aux != 0.) { // no division by 0.
731
        cross_re = compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 19] / aux;
732
        cross_im = compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 20] / aux;
733
        }
734
        else
735
        {
736
        cross_re = 0.;
737
        cross_im = 0.;
738
        }
739
        lfr_bp2[(i*NB_BYTES_BP2) + 18] = (uint8_t) ((cross_re*127.5) + 128); // Shift and cast into a 8-bit uint8_t (0, ..., 255) with rounding
740
        lfr_bp2[(i*NB_BYTES_BP2) + 28] = (uint8_t) ((cross_im*127.5) + 128); // Shift and cast into a 8-bit uint8_t (0, ..., 255) with rounding
741
#ifdef DEBUG_TCH
742
        printf("lfr_bp2[i*NB_BYTES_BP2+18] for cross35_re (%16.8e) : %.3u\n",cross_re, lfr_bp2[i*NB_BYTES_BP2+18]);
743
        printf("lfr_bp2[i*NB_BYTES_BP2+28] for cross35_im (%16.8e) : %.3u\n",cross_im, lfr_bp2[i*NB_BYTES_BP2+28]);
744
#endif
745
        // S45
746
        aux = sqrt(compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 21]*compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 24]);
747
        if (aux != 0.) { // no division by 0.
748
        cross_re = compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 22] / aux;
749
        cross_im = compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 23] / aux;
750
        }
751
        else
752
        {
753
        cross_re = 0.;
754
        cross_im = 0.;
755
        }
756
        lfr_bp2[(i*NB_BYTES_BP2) + 19] = (uint8_t) ((cross_re*127.5) + 128); // Shift and cast into a 8-bit uint8_t (0, ..., 255) with rounding
757
        lfr_bp2[(i*NB_BYTES_BP2) + 29] = (uint8_t) ((cross_im*127.5) + 128); // Shift and cast into a 8-bit uint8_t (0, ..., 255) with rounding
758
#ifdef DEBUG_TCH
759
        printf("lfr_bp2[i*NB_BYTES_BP2+19] for cross45_re (%16.8e) : %.3u\n",cross_re, lfr_bp2[i*NB_BYTES_BP2+19]);
760
        printf("lfr_bp2[i*NB_BYTES_BP2+29] for cross45_im (%16.8e) : %.3u\n",cross_im, lfr_bp2[i*NB_BYTES_BP2+29]);
761
#endif
762
        //==============================================
763
        // BP2  auto correlations == PA_LFR_SC_BP2_AUTO_F0 == 5*16 bits = 5*[6 bits (exponent) + 10 bits (significand)]
764
                               // == PA_LFR_SC_BP2_AUTO_A0_F0 == 16 bits
765
                               // == PA_LFR_SC_BP2_AUTO_A1_F0 == 16 bits
766
                               // == PA_LFR_SC_BP2_AUTO_A2_F0 == 16 bits
767
                               // == PA_LFR_SC_BP2_AUTO_A3_F0 == 16 bits
768
                               // == PA_LFR_SC_BP2_AUTO_A4_F0 == 16 bits
769
        // S11
770
        significand = frexpf(compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX], &exponent);  // 0.5 <= significand < 1
771
                                                                                                // S11 = significand * 2^exponent
772
#ifdef DEBUG_TCH
773
        printf("S11         : %16.8e\n",compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX]);
774
        printf("significand : %16.8e\n",significand);
775
        printf("exponent    : %d\n"    ,exponent);
776
#endif
777
        if (exponent < expmin) { // value should be >= 0.5 * 2^expmin
778
          exponent = expmin;
779
          significand = 0.5;     // min value that can be recorded
780
        }
781
        if (exponent > expmax) { // value should be <  0.5 * 2^(expmax+1)
782
          exponent = expmax;
783
          significand = 1.0;     // max value that can be recorded
784
        }
785
        if (significand == 0) {  // in that case exponent == 0 too
786
          exponent = expmin;
787
          significand = 0.5;     // min value that can be recorded
788
        }
789

    
790
        autocor = (uint16_t) ((((significand*2)-1)*rangesig) + 0.5); // Shift and cast into a 16-bit unsigned int with rounding
791
                                                                 // where just the first nbitsig bits are used (0, ..., 2^nbitsig-1)
792
        exp = (uint16_t) (exponent-expmin);      // Shift and cast into a 16-bit unsigned int where just
793
                                                 // the first nbitexp bits are used (0, ..., 2^nbitexp-1)
794
        tmp_uint16 = autocor | (exp << nbitsig); // Put the exponent bits (nbitexp) next to the
795
                                                 // left place of the significand bits (nbitsig),
796
                                                 // making the 16-bit word to be recorded
797
        pt_uint8 = (uint8_t*) &tmp_uint16;       // Affect an uint8_t pointer with the adress of tmp_uint16
798
#ifdef MSB_FIRST_TCH
799
        lfr_bp2[(i*NB_BYTES_BP2) + 0] = pt_uint8[0]; // Record MSB of tmp_uint16
800
        lfr_bp2[(i*NB_BYTES_BP2) + 1] = pt_uint8[1]; // Record LSB of tmp_uint16
801
#endif
802
#ifdef LSB_FIRST_TCH
803
        lfr_bp2[(i*NB_BYTES_BP2) + 0] = pt_uint8[1]; // Record MSB of tmp_uint16
804
        lfr_bp2[(i*NB_BYTES_BP2) + 1] = pt_uint8[0]; // Record LSB of tmp_uint16
805
#endif
806
#ifdef DEBUG_TCH
807
        printf("autocor for S11 significand : %u\n",autocor);
808
        printf("exp for S11 exponent : %u\n",exp);
809
        printf("pt_uint8[1] for S11 exponent + significand : %.3d or %2x\n",pt_uint8[1], pt_uint8[1]);
810
        printf("pt_uint8[0] for S11            significand : %.3d or %2x\n",pt_uint8[0], pt_uint8[0]);
811
        printf("lfr_bp2[i*NB_BYTES_BP2+0] : %3u or %2x\n",lfr_bp2[i*NB_BYTES_BP2+0], lfr_bp2[i*NB_BYTES_BP2+0]);
812
        printf("lfr_bp2[i*NB_BYTES_BP2+1] : %3u or %2x\n",lfr_bp2[i*NB_BYTES_BP2+1], lfr_bp2[i*NB_BYTES_BP2+1]);
813
#endif
814
        // S22
815
        significand = frexpf(compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 9], &exponent);  // 0.5 <= significand < 1
816
                                                                                                  // S22 = significand * 2^exponent
817
#ifdef DEBUG_TCH
818
        printf("S22         : %16.8e\n",compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+9]);
819
        printf("significand : %16.8e\n",significand);
820
        printf("exponent    : %d\n"    ,exponent);
821
#endif
822
        if (exponent < expmin) { // value should be >= 0.5 * 2^expmin
823
          exponent = expmin;
824
          significand = 0.5;     // min value that can be recorded
825
        }
826
        if (exponent > expmax) { // value should be <  0.5 * 2^(expmax+1)
827
          exponent = expmax;
828
          significand = 1.0;     // max value that can be recorded
829
        }
830
        if (significand == 0) {  // in that case exponent == 0 too
831
          exponent = expmin;
832
          significand = 0.5;     // min value that can be recorded
833
        }
834

    
835
        autocor = (uint16_t) ((((significand*2)-1)*rangesig) + 0.5); // Shift and cast into a 16-bit unsigned int with rounding
836
                                                                 // where just the first nbitsig bits are used (0, ..., 2^nbitsig-1)
837
        exp = (uint16_t) (exponent-expmin);      // Shift and cast into a 16-bit unsigned int where just
838
                                                 // the first nbitexp bits are used (0, ..., 2^nbitexp-1)
839
        tmp_uint16 = autocor | (exp << nbitsig); // Put the exponent bits (nbitexp) next to the
840
                                                 // left place of the significand bits (nbitsig),
841
                                                 // making the 16-bit word to be recorded
842
        pt_uint8 = (uint8_t*) &tmp_uint16;       // Affect an uint8_t pointer with the adress of tmp_uint16
843
#ifdef MSB_FIRST_TCH
844
        lfr_bp2[(i*NB_BYTES_BP2) + 2] = pt_uint8[0]; // Record MSB of tmp_uint16
845
        lfr_bp2[(i*NB_BYTES_BP2) + 3] = pt_uint8[1]; // Record LSB of tmp_uint16
846
#endif
847
#ifdef LSB_FIRST_TCH
848
        lfr_bp2[(i*NB_BYTES_BP2) + 2] = pt_uint8[1]; // Record MSB of tmp_uint16
849
        lfr_bp2[(i*NB_BYTES_BP2) + 3] = pt_uint8[0]; // Record LSB of tmp_uint16
850
#endif
851
#ifdef DEBUG_TCH
852
        printf("autocor for S22 significand : %u\n",autocor);
853
        printf("exp for S11 exponent : %u\n",exp);
854
        printf("pt_uint8[1] for S22 exponent + significand : %.3d or %2x\n",pt_uint8[1], pt_uint8[1]);
855
        printf("pt_uint8[0] for S22            significand : %.3d or %2x\n",pt_uint8[0], pt_uint8[0]);
856
        printf("lfr_bp2[i*NB_BYTES_BP2+2] : %3u or %2x\n",lfr_bp2[i*NB_BYTES_BP2+2], lfr_bp2[i*NB_BYTES_BP2+2]);
857
        printf("lfr_bp2[i*NB_BYTES_BP2+3] : %3u or %2x\n",lfr_bp2[i*NB_BYTES_BP2+3], lfr_bp2[i*NB_BYTES_BP2+3]);
858
#endif
859
        // S33
860
        significand = frexpf(compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 16], &exponent);  // 0.5 <= significand < 1
861
                                                                                                   // S33 = significand * 2^exponent
862
#ifdef DEBUG_TCH
863
        printf("S33         : %16.8e\n",compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+16]);
864
        printf("significand : %16.8e\n",significand);
865
        printf("exponent    : %d\n"    ,exponent);
866
#endif
867
        if (exponent < expmin) { // value should be >= 0.5 * 2^expmin
868
          exponent = expmin;
869
          significand = 0.5;     // min value that can be recorded
870
        }
871
        if (exponent > expmax) { // value should be <  0.5 * 2^(expmax+1)
872
          exponent = expmax;
873
          significand = 1.0;     // max value that can be recorded
874
        }
875
        if (significand == 0) {  // in that case exponent == 0 too
876
          exponent = expmin;
877
          significand = 0.5;     // min value that can be recorded
878
        }
879

    
880
        autocor = (uint16_t) ((((significand*2)-1)*rangesig) + 0.5); // Shift and cast into a 16-bit unsigned int with rounding
881
                                                                 // where just the first nbitsig bits are used (0, ..., 2^nbitsig-1)
882
        exp = (uint16_t) (exponent-expmin);      // Shift and cast into a 16-bit unsigned int where just
883
                                                 // the first nbitexp bits are used (0, ..., 2^nbitexp-1)
884
        tmp_uint16 = autocor | (exp << nbitsig); // Put the exponent bits (nbitexp) next to the
885
                                                 // left place of the significand bits (nbitsig),
886
                                                 // making the 16-bit word to be recorded
887
        pt_uint8 = (uint8_t*) &tmp_uint16;       // Affect an uint8_t pointer with the adress of tmp_uint16
888
#ifdef MSB_FIRST_TCH
889
        lfr_bp2[(i*NB_BYTES_BP2) + 4] = pt_uint8[0]; // Record MSB of tmp_uint16
890
        lfr_bp2[(i*NB_BYTES_BP2) + 5] = pt_uint8[1]; // Record LSB of tmp_uint16
891
#endif
892
#ifdef LSB_FIRST_TCH
893
        lfr_bp2[(i*NB_BYTES_BP2) + 4] = pt_uint8[1]; // Record MSB of tmp_uint16
894
        lfr_bp2[(i*NB_BYTES_BP2) + 5] = pt_uint8[0]; // Record LSB of tmp_uint16
895
#endif
896
#ifdef DEBUG_TCH
897
        printf("autocor for S33 significand : %u\n",autocor);
898
        printf("exp for S33 exponent : %u\n",exp);
899
        printf("pt_uint8[1] for S33 exponent + significand : %.3d or %2x\n",pt_uint8[1], pt_uint8[1]);
900
        printf("pt_uint8[0] for S33            significand : %.3d or %2x\n",pt_uint8[0], pt_uint8[0]);
901
        printf("lfr_bp2[i*NB_BYTES_BP2+4] : %3u or %2x\n",lfr_bp2[i*NB_BYTES_BP2+4], lfr_bp2[i*NB_BYTES_BP2+4]);
902
        printf("lfr_bp2[i*NB_BYTES_BP2+5] : %3u or %2x\n",lfr_bp2[i*NB_BYTES_BP2+5], lfr_bp2[i*NB_BYTES_BP2+5]);
903
#endif
904
        // S44
905
        significand = frexpf(compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 21], &exponent);  // 0.5 <= significand < 1
906
                                                                                                   // S44 = significand * 2^exponent
907
#ifdef DEBUG_TCH
908
        printf("S44         : %16.8e\n",compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+21]);
909
        printf("significand : %16.8e\n",significand);
910
        printf("exponent    : %d\n"    ,exponent);
911
#endif
912

    
913
        if (exponent < expmin) { // value should be >= 0.5 * 2^expmin
914
          exponent = expmin;
915
          significand = 0.5;     // min value that can be recorded
916
        }
917
        if (exponent > expmax) { // value should be <  0.5 * 2^(expmax+1)
918
          exponent = expmax;
919
          significand = 1.0;     // max value that can be recorded
920
        }
921
        if (significand == 0) {  // in that case exponent == 0 too
922
          exponent = expmin;
923
          significand = 0.5;     // min value that can be recorded
924
        }
925

    
926
        autocor = (uint16_t) ((((significand*2)-1)*rangesig )+ 0.5); // Shift and cast into a 16-bit unsigned int with rounding
927
                                                                 // where just the first nbitsig bits are used (0, ..., 2^nbitsig-1)
928
        exp = (uint16_t) (exponent-expmin);      // Shift and cast into a 16-bit unsigned int where just
929
                                                 // the first nbitexp bits are used (0, ..., 2^nbitexp-1)
930
        tmp_uint16 = autocor | (exp << nbitsig); // Put the exponent bits (nbitexp) next to the
931
                                                 // left place of the significand bits (nbitsig),
932
                                                 // making the 16-bit word to be recorded
933
        pt_uint8 = (uint8_t*) &tmp_uint16;       // Affect an uint8_t pointer with the adress of tmp_uint16
934
#ifdef MSB_FIRST_TCH
935
        lfr_bp2[(i*NB_BYTES_BP2) + 6] = pt_uint8[0]; // Record MSB of tmp_uint16
936
        lfr_bp2[(i*NB_BYTES_BP2) + 7] = pt_uint8[1]; // Record LSB of tmp_uint16
937
#endif
938
#ifdef LSB_FIRST_TCH
939
        lfr_bp2[(i*NB_BYTES_BP2) + 6] = pt_uint8[1]; // Record MSB of tmp_uint16
940
        lfr_bp2[(i*NB_BYTES_BP2) + 7] = pt_uint8[0]; // Record LSB of tmp_uint16
941
#endif
942
#ifdef DEBUG_TCH
943
        printf("autocor for S44 significand : %u\n",autocor);
944
        printf("exp for S44 exponent : %u\n",exp);
945
        printf("pt_uint8[1] for S44 exponent + significand : %.3d or %2x\n",pt_uint8[1], pt_uint8[1]);
946
        printf("pt_uint8[0] for S44            significand : %.3d or %2x\n",pt_uint8[0], pt_uint8[0]);
947
        printf("lfr_bp2[i*NB_BYTES_BP2+6] : %3u or %2x\n",lfr_bp2[i*NB_BYTES_BP2+6], lfr_bp2[i*NB_BYTES_BP2+6]);
948
        printf("lfr_bp2[i*NB_BYTES_BP2+7] : %3u or %2x\n",lfr_bp2[i*NB_BYTES_BP2+7], lfr_bp2[i*NB_BYTES_BP2+7]);
949
#endif
950
        // S55
951
        significand = frexpf(compressed_spec_mat[(i*NB_VALUES_PER_SPECTRAL_MATRIX) + 24], &exponent);  // 0.5 <= significand < 1
952
                                                                                                   // S55 = significand * 2^exponent
953
#ifdef DEBUG_TCH
954
        printf("S55         : %16.8e\n",compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+24]);
955
        printf("significand : %16.8e\n",significand);
956
        printf("exponent    : %d\n"    ,exponent);
957
#endif
958
        if (exponent < expmin) { // value should be >= 0.5 * 2^expmin
959
          exponent = expmin;
960
          significand = 0.5;     // min value that can be recorded
961
        }
962
        if (exponent > expmax) { // value should be <  0.5 * 2^(expmax+1)
963
          exponent = expmax;
964
          significand = 1.0;     // max value that can be recorded
965
        }
966
        if (significand == 0) {  // in that case exponent == 0 too
967
          exponent = expmin;
968
          significand = 0.5;     // min value that can be recorded
969
        }
970

    
971
        autocor = (uint16_t) ((((significand*2)-1)*rangesig) + 0.5); // Shift and cast into a 16-bit unsigned int with rounding
972
                                                                 // where just the first nbitsig bits are used (0, ..., 2^nbitsig-1)
973
        exp = (uint16_t) (exponent-expmin);      // Shift and cast into a 16-bit unsigned int where just
974
                                                 // the first nbitexp bits are used (0, ..., 2^nbitexp-1)
975
        tmp_uint16 = autocor | (exp << nbitsig); // Put the exponent bits (nbitexp) next to the
976
                                                 // left place of the significand bits (nbitsig),
977
                                                 // making the 16-bit word to be recorded
978
        pt_uint8 = (uint8_t*) &tmp_uint16;       // Affect an uint8_t pointer with the adress of tmp_uint16
979
#ifdef MSB_FIRST_TCH
980
        lfr_bp2[(i*NB_BYTES_BP2) + 8] = pt_uint8[0]; // Record MSB of tmp_uint16
981
        lfr_bp2[(i*NB_BYTES_BP2) + 9] = pt_uint8[1]; // Record LSB of tmp_uint16
982
        //printf("MSB:\n");
983
#endif
984
#ifdef LSB_FIRST_TCH
985
        lfr_bp2[(i*NB_BYTES_BP2) + 8] = pt_uint8[1]; // Record MSB of tmp_uint16
986
        lfr_bp2[(i*NB_BYTES_BP2) + 9] = pt_uint8[0]; // Record LSB of tmp_uint16
987
        //printf("LSB:\n");
988
#endif
989
#ifdef DEBUG_TCH
990
        printf("autocor for S55 significand : %u\n",autocor);
991
        printf("exp for S55 exponent : %u\n",exp);
992
        printf("pt_uint8[1] for S55 exponent + significand : %.3d or %2x\n",pt_uint8[1], pt_uint8[1]);
993
        printf("pt_uint8[0] for S55            significand : %.3d or %2x\n",pt_uint8[0], pt_uint8[0]);
994
        printf("lfr_bp2[i*NB_BYTES_BP2+8] : %3u or %2x\n",lfr_bp2[i*NB_BYTES_BP2+8], lfr_bp2[i*NB_BYTES_BP2+8]);
995
        printf("lfr_bp2[i*NB_BYTES_BP2+9] : %3u or %2x\n",lfr_bp2[i*NB_BYTES_BP2+9], lfr_bp2[i*NB_BYTES_BP2+9]);
996
#endif
997
    }
998
}
999

    
1000

    
1001
#endif // BASIC_PARAMETERS_H_INCLUDED
    (1-1/1)