GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: src/../LFR_basic-parameters/basic_parameters.h Lines: 283 354 79.9 %
Date: 2018-10-05 11:31:31 Branches: 53 90 58.9 %

Line Branch Exec Source
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
17
#ifndef BASIC_PARAMETERS_H_INCLUDED
18
#define BASIC_PARAMETERS_H_INCLUDED
19
20
#include <math.h>
21
#include <stdio.h>
22
#include <stdint.h>
23
24
#include "basic_parameters_params.h"
25
26
static inline void BP1_set(float * compressed_spec_mat, float * k_coeff_intercalib, unsigned char nb_bins_compressed_spec_mat, unsigned char * lfr_bp1);
27
static inline void BP2_set(float * compressed_spec_mat, unsigned char nb_bins_compressed_spec_mat, unsigned char * lfr_bp2);
28
29
void init_k_coefficients_f0( float *k_coeff_intercalib, unsigned char nb_binscompressed_matrix );
30
void init_k_coefficients_f1( float *k_coeff_intercalib, unsigned char nb_binscompressed_matrix );
31
void init_k_coefficients_f2( float *k_coeff_intercalib, unsigned char nb_binscompressed_matrix );
32
33
void init_k_coefficients( float *k_coeff_intercalib, unsigned char nb_binscompressed_matrix );
34
35
//***********************************
36
// STATIC INLINE FUNCTION DEFINITIONS
37
38
109
void BP1_set( float * compressed_spec_mat, float * k_coeff_intercalib, uint8_t nb_bins_compressed_spec_mat, uint8_t * lfr_bp1 ){
39
    float PSDB;                         // 32-bit floating point
40
    float PSDE;
41
    float tmp;
42
    float NVEC_V0;
43
    float NVEC_V1;
44
    float NVEC_V2;
45
    float aux;
46
    float tr_SB_SB;
47
    float e_cross_b_re;
48
    float e_cross_b_im;
49
    float n_cross_e_scal_b_re;
50
    float n_cross_e_scal_b_im;
51
    float ny;
52
    float nz;
53
    float bx_bx_star;
54
    float vphi;
55
    float significand;
56
    int exponent;                       // 32-bit signed integer
57
    float alpha_M;
58
59
    uint8_t nbitexp;                    // 8-bit unsigned integer
60
    uint8_t nbitsig;
61
    uint8_t tmp_uint8;
62
    uint8_t *pt_uint8;                  // pointer on unsigned 8-bit integer
63
    int8_t expmin;                      // 8-bit signed integer
64
    int8_t expmax;
65
    uint16_t rangesig;                  // 16-bit unsigned integer
66
    uint16_t psd;
67
    uint16_t exp;
68
    uint16_t tmp_uint16;
69
    uint16_t i;
70
71
109
    alpha_M = 45 * (3.1415927/180);
72
73
#ifdef DEBUG_TCH
74
    printf("BP1 : \n");
75
    printf("Number of bins: %d\n", nb_bins_compressed_spec_mat);
76
#endif
77
78
    // initialization for managing the exponents of the floating point data:
79
109
    nbitexp = 6;                           // number of bits for the exponent
80
109
    expmax = 32+5;                         // maximum value of the exponent
81
109
    expmin = expmax - (1 << nbitexp) + 1;  // accordingly the minimum exponent value
82
    // for floating point data to be recorded on 16-bit words:
83
109
    nbitsig = 16 - nbitexp;       // number of bits for the significand
84
109
    rangesig = (1 << nbitsig)-1;  // == 2^nbitsig - 1
85
86
#ifdef DEBUG_TCH
87
    printf("nbitexp : %d, expmax : %d, expmin : %d\n", nbitexp, expmax, expmin);
88
    printf("nbitsig : %d, rangesig : %d\n", nbitsig, rangesig);
89
#endif
90
91
2383
    for(i=0; i<nb_bins_compressed_spec_mat; i++){
92
        //==============================================
93
        // BP1 PSDB == PA_LFR_SC_BP1_PB_F0 == 16 bits = 6 bits (exponent) + 10 bits (significand)
94
4548
        PSDB =  compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX]          // S11
95
2274
              + compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+9]        // S22
96
2274
              + compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+16];      // S33
97
98
2274
        significand = frexpf(PSDB, &exponent);  // 0.5 <= significand < 1
99
                                                  // PSDB = significand * 2^exponent
100
101
2274
        if (exponent < expmin) { // value should be >= 0.5 * 2^expmin
102
          exponent = expmin;
103
          significand = 0.5;     // min value that can be recorded
104
        }
105
2274
        if (exponent > expmax) { // value should be <  0.5 * 2^(expmax+1)
106
          exponent = expmax;
107
          significand = 1.0;     // max value that can be recorded
108
        }
109
2274
        if (significand == 0) {  // in that case exponent == 0 too
110
          exponent = expmin;
111
          significand = 0.5;     // min value that can be recorded
112
        }
113
114
2274
        psd = (uint16_t) ((significand*2-1)*rangesig + 0.5); // Shift and cast into a 16-bit unsigned int with rounding
115
                                                             // where just the first nbitsig bits are used (0, ..., 2^nbitsig-1)
116
2274
        exp = (uint16_t) (exponent-expmin);      // Shift and cast into a 16-bit unsigned int where just
117
                                                 // the first nbitexp bits are used (0, ..., 2^nbitexp-1)
118
2274
        tmp_uint16 = psd | (exp << nbitsig);     // Put the exponent bits (nbitexp) next to the
119
                                                 // left place of the significand bits (nbitsig),
120
                                                 // making the 16-bit word to be recorded
121
2274
        pt_uint8 = (uint8_t*) &tmp_uint16;       // Affect an uint8_t pointer with the adress of tmp_uint16
122
#ifdef MSB_FIRST_TCH
123
2274
        lfr_bp1[i*NB_BYTES_BP1+2] = pt_uint8[0]; // Record MSB of tmp_uint16
124
2274
        lfr_bp1[i*NB_BYTES_BP1+3] = pt_uint8[1]; // Record LSB of tmp_uint16
125
#endif
126
#ifdef LSB_FIRST_TCH
127
        lfr_bp1[i*NB_BYTES_BP1+2] = pt_uint8[1]; // Record MSB of tmp_uint16
128
        lfr_bp1[i*NB_BYTES_BP1+3] = pt_uint8[0]; // Record LSB of tmp_uint16
129
#endif
130
#ifdef DEBUG_TCH
131
        printf("\nBin number: %d\n", i);
132
        printf("PSDB        : %16.8e\n",PSDB);
133
        printf("significand : %16.8e\n",significand);
134
        printf("exponent    : %d\n"    ,exponent);
135
        printf("psd for PSDB significand : %d\n",psd);
136
        printf("exp for PSDB exponent : %d\n",exp);
137
        printf("pt_uint8[1] for PSDB exponent + significand: %.3d or %.2x\n",pt_uint8[1], pt_uint8[1]);
138
        printf("pt_uint8[0] for PSDB            significand: %.3d or %.2x\n",pt_uint8[0], pt_uint8[0]);
139
        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]);
140
        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]);
141
#endif
142
        //==============================================
143
        // BP1 PSDE == PA_LFR_SC_BP1_PE_F0 == 16 bits = 6 bits (exponent) + 10 bits (significand)
144
4548
        PSDE =  compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+21] * k_coeff_intercalib[i*NB_K_COEFF_PER_BIN+K44_PE]        // S44
145
2274
              + compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+24] * k_coeff_intercalib[i*NB_K_COEFF_PER_BIN+K55_PE]        // S55
146
2274
              + compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+22] * k_coeff_intercalib[i*NB_K_COEFF_PER_BIN+K45_PE_RE]     // S45 Re
147
2274
              - compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+23] * k_coeff_intercalib[i*NB_K_COEFF_PER_BIN+K45_PE_IM];    // S45 Im
148
149
2274
        significand = frexpf(PSDE, &exponent); // 0.5 <= significand < 1
150
                                               // PSDE = significand * 2^exponent
151
152
2274
        if (exponent < expmin) { // value should be >= 0.5 * 2^expmin
153
          exponent = expmin;
154
          significand = 0.5;     // min value that can be recorded
155
        }
156
2274
        if (exponent > expmax) { // value should be <  0.5 * 2^(expmax+1)
157
          exponent = expmax;
158
          significand = 1.0;     // max value that can be recorded
159
        }
160
2274
        if (significand == 0) {// in that case exponent == 0 too
161
          exponent = expmin;
162
          significand = 0.5;   // min value that can be recorded
163
        }
164
165
2274
        psd = (uint16_t) ((significand*2-1)*rangesig + 0.5); // Shift and cast into a 16-bit unsigned int with rounding
166
                                                             // where just the first nbitsig bits are used (0, ..., 2^nbitsig-1)
167
2274
        exp = (uint16_t) (exponent-expmin);      // Shift and cast into a 16-bit unsigned int where just
168
                                                 // the first nbitexp bits are used (0, ..., 2^nbitexp-1)
169
2274
        tmp_uint16 = psd | (exp << nbitsig);     // Put the exponent bits (nbitexp) next to the
170
                                                 // left place of the significand bits (nbitsig),
171
                                                 // making the 16-bit word to be recorded
172
2274
        pt_uint8 = (uint8_t*) &tmp_uint16;       // Affect an uint8_t pointer with the adress of tmp_uint16
173
#ifdef MSB_FIRST_TCH
174
2274
        lfr_bp1[i*NB_BYTES_BP1+0] = pt_uint8[0]; // Record MSB of tmp_uint16
175
2274
        lfr_bp1[i*NB_BYTES_BP1+1] = pt_uint8[1]; // Record LSB of tmp_uint16
176
#endif
177
#ifdef LSB_FIRST_TCH
178
        lfr_bp1[i*NB_BYTES_BP1+0] = pt_uint8[1]; // Record MSB of tmp_uint16
179
        lfr_bp1[i*NB_BYTES_BP1+1] = pt_uint8[0]; // Record LSB of tmp_uint16
180
#endif
181
#ifdef DEBUG_TCH
182
        printf("PSDE        : %16.8e\n",PSDE);
183
        printf("significand : %16.8e\n",significand);
184
        printf("exponent    : %d\n"    ,exponent);
185
        printf("psd for PSDE significand : %d\n",psd);
186
        printf("exp for PSDE exponent : %d\n",exp);
187
        printf("pt_uint8[1] for PSDE exponent + significand: %.3d or %.2x\n",pt_uint8[1], pt_uint8[1]);
188
        printf("pt_uint8[0] for PSDE            significand: %.3d or %.2x\n",pt_uint8[0], pt_uint8[0]);
189
        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]);
190
        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]);
191
#endif
192
        //==============================================================================
193
        // BP1 normal wave vector == PA_LFR_SC_BP1_NVEC_V0_F0 == 8 bits
194
                               // == PA_LFR_SC_BP1_NVEC_V1_F0 == 8 bits
195
                               // == PA_LFR_SC_BP1_NVEC_V2_F0 == 1 sign bit
196
4548
        tmp = sqrt( compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+2] *compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+2]   //Im S12
197
2274
                   +compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+4] *compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+4]   //Im S13
198
2274
                   +compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+11]*compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+11]  //Im S23
199
                   );
200
2274
        if (tmp != 0.) { // no division by 0.
201
2183
            NVEC_V0 =  compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+11]/ tmp;  // S23 Im  => n1
202
2183
            NVEC_V1 = -compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+4] / tmp;  // S13 Im  => n2
203
2183
            NVEC_V2 =  compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+2] / tmp;  // S12 Im  => n3
204
        }
205
        else
206
        {
207
91
            NVEC_V0 = 0.;
208
91
            NVEC_V1 = 0.;
209
91
            NVEC_V2 = 0.;
210
        }
211
2274
        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
212
2274
        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
213
2274
        pt_uint8 = (uint8_t*) &NVEC_V2;                              // Affect an uint8_t pointer with the adress of NVEC_V2
214
#ifdef LSB_FIRST_TCH
215
        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)
216
                                                         // Record it at the 8th bit position (from the right to the left) of lfr_bp1[i*NB_BYTES_BP1+6]
217
#endif
218
#ifdef MSB_FIRST_TCH
219
2274
        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)
220
                                                         // Record it at the 8th bit position (from the right to the left) of lfr_bp1[i*NB_BYTES_BP1+6]
221
#endif
222
#ifdef DEBUG_TCH
223
        printf("NVEC_V0  : %16.8e\n",NVEC_V0);
224
        printf("NVEC_V1  : %16.8e\n",NVEC_V1);
225
        printf("NVEC_V2  : %16.8e\n",NVEC_V2);
226
        printf("lfr_bp1[i*NB_BYTES_BP1+4] for NVEC_V0 : %u\n",lfr_bp1[i*NB_BYTES_BP1+4]);
227
        printf("lfr_bp1[i*NB_BYTES_BP1+5] for NVEC_V1 : %u\n",lfr_bp1[i*NB_BYTES_BP1+5]);
228
        printf("lfr_bp1[i*NB_BYTES_BP1+6] for NVEC_V2 : %u\n",lfr_bp1[i*NB_BYTES_BP1+6]);
229
#endif
230
        //=======================================================
231
        // BP1 ellipticity == PA_LFR_SC_BP1_ELLIP_F0 == 4 bits
232
2274
        if (PSDB != 0.) { // no division by 0.
233
2274
            aux = 2*tmp / PSDB;                   // Compute the ellipticity
234
        }
235
        else
236
        {
237
            aux = 0.;
238
        }
239
2274
        tmp_uint8 = (uint8_t) (aux*15 + 0.5); // Shift and cast into a 8-bit uint8_t with rounding
240
                                              // where just the first 4 bits are used (0, ..., 15)
241
2274
        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
242
                                                                                  // of the sign bit of NVEC_V2 (recorded
243
                                                                                  // previously in lfr_bp1[i*NB_BYTES_BP1+6])
244
#ifdef DEBUG_TCH
245
        printf("ellipticity  : %16.8e\n",aux);
246
        printf("tmp_uint8 for ellipticity : %u\n",tmp_uint8);
247
        printf("lfr_bp1[i*NB_BYTES_BP1+6] for NVEC_V2 + ellipticity : %u\n",lfr_bp1[i*NB_BYTES_BP1+6]);
248
#endif
249
        //==============================================================
250
        // BP1 degree of polarization == PA_LFR_SC_BP1_DOP_F0 == 3 bits
251
4548
        tr_SB_SB = compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX]     *compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX]
252
2274
                 + compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+9]   *compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+9]
253
2274
                 + compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+16]  *compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+16]
254
2274
                 + 2 * compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+1] *compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+1]
255
2274
                 + 2 * compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+2] *compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+2]
256
2274
                 + 2 * compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+3] *compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+3]
257
2274
                 + 2 * compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+4] *compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+4]
258
2274
                 + 2 * compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+10]*compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+10]
259
2274
                 + 2 * compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+11]*compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+11];
260
2274
        aux = PSDB*PSDB;
261
2274
        if (aux != 0.) { // no division by 0.
262
2274
            tmp = ( 3*tr_SB_SB - aux ) / ( 2 * aux );  // Compute the degree of polarisation
263
        }
264
        else
265
        {
266
            tmp = 0.;
267
        }
268
2274
        tmp_uint8 = (uint8_t) (tmp*7 + 0.5);       // Shift and cast into a 8-bit uint8_t with rounding
269
                                                   // where just the first 3 bits are used (0, ..., 7)
270
2274
        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
271
                                                                           // (from the right to the left) of lfr_bp1[i*NB_BYTES_BP1+6]
272
#ifdef DEBUG_TCH
273
        printf("DOP  : %16.8e\n",tmp);
274
        printf("tmp_uint8 for DOP : %u\n",tmp_uint8);
275
        printf("lfr_bp1[i*NB_BYTES_BP1+6] for NVEC_V2 + ellipticity + DOP : %u\n",lfr_bp1[i*NB_BYTES_BP1+6]);
276
#endif
277
        //=======================================================================================
278
        // BP1 X_SO-component of the Poynting flux == PA_LFR_SC_BP1_SX_F0 == 16 bits
279
        //                                          = 1 sign bit + 1 argument bit (two sectors)
280
        //                                          + 6 bits (exponent) + 8 bits (significand)
281
4548
        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
282
2274
                      + compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+19]*k_coeff_intercalib[i*NB_K_COEFF_PER_BIN+K35_SX_RE]  //S35 Re
283
2274
                      + compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+5] *k_coeff_intercalib[i*NB_K_COEFF_PER_BIN+K14_SX_RE]  //S14 Re
284
2274
                      + compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+7] *k_coeff_intercalib[i*NB_K_COEFF_PER_BIN+K15_SX_RE]  //S15 Re
285
2274
                      + compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+12]*k_coeff_intercalib[i*NB_K_COEFF_PER_BIN+K24_SX_RE]  //S24 Re
286
2274
                      + compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+14]*k_coeff_intercalib[i*NB_K_COEFF_PER_BIN+K25_SX_RE]  //S25 Re
287
2274
                      + compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+18]*k_coeff_intercalib[i*NB_K_COEFF_PER_BIN+K34_SX_IM]  //S34 Im
288
2274
                      + compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+20]*k_coeff_intercalib[i*NB_K_COEFF_PER_BIN+K35_SX_IM]  //S35 Im
289
2274
                      + compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+6] *k_coeff_intercalib[i*NB_K_COEFF_PER_BIN+K14_SX_IM]  //S14 Im
290
2274
                      + compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+8] *k_coeff_intercalib[i*NB_K_COEFF_PER_BIN+K15_SX_IM]  //S15 Im
291
2274
                      + compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+13]*k_coeff_intercalib[i*NB_K_COEFF_PER_BIN+K24_SX_IM]  //S24 Im
292
2274
                      + compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+15]*k_coeff_intercalib[i*NB_K_COEFF_PER_BIN+K25_SX_IM]; //S25 Im
293
        // Im(S_ji) = -Im(S_ij)
294
        // k_ji = k_ij
295
4548
        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
296
2274
                      + compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+19]*k_coeff_intercalib[i*NB_K_COEFF_PER_BIN+K35_SX_IM]  //S35 Re
297
2274
                      + compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+5] *k_coeff_intercalib[i*NB_K_COEFF_PER_BIN+K14_SX_IM]  //S14 Re
298
2274
                      + compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+7] *k_coeff_intercalib[i*NB_K_COEFF_PER_BIN+K15_SX_IM]  //S15 Re
299
2274
                      + compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+12]*k_coeff_intercalib[i*NB_K_COEFF_PER_BIN+K24_SX_IM]  //S24 Re
300
2274
                      + compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+14]*k_coeff_intercalib[i*NB_K_COEFF_PER_BIN+K25_SX_IM]  //S25 Re
301
2274
                      - compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+18]*k_coeff_intercalib[i*NB_K_COEFF_PER_BIN+K34_SX_RE]  //S34 Im
302
2274
                      - compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+20]*k_coeff_intercalib[i*NB_K_COEFF_PER_BIN+K35_SX_RE]  //S35 Im
303
2274
                      - compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+6] *k_coeff_intercalib[i*NB_K_COEFF_PER_BIN+K14_SX_RE]  //S14 Im
304
2274
                      - compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+8] *k_coeff_intercalib[i*NB_K_COEFF_PER_BIN+K15_SX_RE]  //S15 Im
305
2274
                      - compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+13]*k_coeff_intercalib[i*NB_K_COEFF_PER_BIN+K24_SX_RE]  //S24 Im
306
2274
                      - compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+15]*k_coeff_intercalib[i*NB_K_COEFF_PER_BIN+K25_SX_RE]; //S25 Im
307
#ifdef DEBUG_TCH
308
        printf("ReaSX       : %16.8e\n",e_cross_b_re);
309
#endif
310
2274
        pt_uint8 = (uint8_t*) &e_cross_b_re;      // Affect an uint8_t pointer with the adress of e_cross_b_re
311
#ifdef LSB_FIRST_TCH
312
313
        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)
314
                                                                     // Record it at the 8th bit position (from the right to the left)
315
                                                                     // of lfr_bp1[i*NB_BYTES_BP1+7]
316
        pt_uint8[3] = (pt_uint8[3] & 0x7f);       // Make e_cross_b_re be positive in any case: |ReaSX|
317
#endif
318
#ifdef MSB_FIRST_TCH
319
2274
        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)
320
                                                                     // Record it at the 8th bit position (from the right to the left)
321
                                                                     // of lfr_bp1[i*NB_BYTES_BP1+7]
322
2274
        pt_uint8[0] = (pt_uint8[0] & 0x7f);       // Make e_cross_b_re be positive in any case: |ReaSX|
323
#endif
324
2274
        significand = frexpf(e_cross_b_re, &exponent); // 0.5 <= significand < 1
325
                                                       // ReaSX = significand * 2^exponent
326
2274
        if (exponent < expmin) { // value should be >= 0.5 * 2^expmin
327
          exponent = expmin;
328
          significand = 0.5;     // min value that can be recorded
329
        }
330
2274
        if (exponent > expmax) { // value should be <  0.5 * 2^(expmax+1)
331
          exponent = expmax;
332
          significand = 1.0;     // max value that can be recorded
333
        }
334
2274
        if (significand == 0) {  // in that case exponent == 0 too
335
2
          exponent = expmin;
336
2
          significand = 0.5;     // min value that can be recorded
337
        }
338
339
2274
        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
340
                                                                             // where all bits are used (0, ..., 255)
341
2274
        tmp_uint8 = (uint8_t) (exponent-expmin); // Shift and cast into a 8-bit uint8_t where
342
                                                 // just the first nbitexp bits are used (0, ..., 2^nbitexp-1)
343
#ifdef DEBUG_TCH
344
        printf("|ReaSX|     : %16.8e\n",e_cross_b_re);
345
        printf("significand : %16.8e\n",significand);
346
        printf("exponent    : %d\n"    ,exponent);
347
        printf("tmp_uint8        for ReaSX exponent    : %d\n",tmp_uint8);
348
#endif
349
2274
        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
350
                                                                           // (from the right to the left) of lfr_bp1[i*NB_BYTES_BP1+7]
351
#ifdef DEBUG_TCH
352
        printf("lfr_bp1[i*NB_BYTES_BP1+7] for ReaSX sign + RealSX exponent : %u\n",lfr_bp1[i*NB_BYTES_BP1+7]);
353
        printf("lfr_bp1[i*NB_BYTES_BP1+8] for ReaSX significand            : %u\n",lfr_bp1[i*NB_BYTES_BP1+8]);
354
        printf("ImaSX       : %16.8e\n",e_cross_b_im);
355
#endif
356
2274
        pt_uint8 = (uint8_t*) &e_cross_b_im; // Affect an uint8_t pointer with the adress of e_cross_b_im
357
#ifdef LSB_FIRST_TCH
358
        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)
359
#endif
360
#ifdef MSB_FIRST_TCH
361
2274
        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)
362
#endif
363
2274
        tmp_uint8 = (e_cross_b_im > e_cross_b_re) ? 0x40 : 0x00; // Determine the sector argument of SX. If |Im| > |Re| affect
364
                                                                 // an unsigned 8-bit char with 01000000; otherwise with null.
365
2274
        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
366
                                                                            // to the left) of lfr_bp1[i*NB_BYTES_BP1+7], by simple logical addition.
367
#ifdef DEBUG_TCH
368
        printf("|ImaSX|     : %16.8e\n",e_cross_b_im);
369
        printf("ArgSX sign  : %u\n",tmp_uint8);
370
        printf("lfr_bp1[i*NB_BYTES_BP1+7] for ReaSX & ArgSX signs + ReaSX exponent  : %u\n",lfr_bp1[i*NB_BYTES_BP1+7]);
371
#endif
372
        //======================================================================
373
        // BP1 phase velocity estimator == PA_LFR_SC_BP1_VPHI_F0 == 16 bits
374
        //                                          = 1 sign bit + 1 argument bit (two sectors)
375
        //                                          + 6 bits (exponent) + 8 bits (significand)
376
2274
        ny = sin(alpha_M)*NVEC_V1 + cos(alpha_M)*NVEC_V2;
377
2274
        nz = NVEC_V0;
378
6822
        bx_bx_star = cos(alpha_M)*cos(alpha_M)*compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+9]   // S22 Re
379
2274
                   + sin(alpha_M)*sin(alpha_M)*compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+16]  // S33 Re
380
4548
                 - 2*sin(alpha_M)*cos(alpha_M)*compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+10]; // S23 Re
381
382
6822
        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
383
2274
                                   +compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+14]*k_coeff_intercalib[i*NB_K_COEFF_PER_BIN+K25_NY_RE]  //S25 Re
384
2274
                                   +compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+17]*k_coeff_intercalib[i*NB_K_COEFF_PER_BIN+K34_NY_RE]  //S34 Re
385
2274
                                   +compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+19]*k_coeff_intercalib[i*NB_K_COEFF_PER_BIN+K35_NY_RE]  //S35 Re
386
2274
                                   +compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+13]*k_coeff_intercalib[i*NB_K_COEFF_PER_BIN+K24_NY_IM]  //S24 Im
387
2274
                                   +compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+15]*k_coeff_intercalib[i*NB_K_COEFF_PER_BIN+K25_NY_IM]  //S25 Im
388
2274
                                   +compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+18]*k_coeff_intercalib[i*NB_K_COEFF_PER_BIN+K34_NY_IM]  //S34 Im
389
2274
                                   +compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+20]*k_coeff_intercalib[i*NB_K_COEFF_PER_BIN+K35_NY_IM]) //S35 Im
390
4548
                            + 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
391
2274
                                   +compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+14]*k_coeff_intercalib[i*NB_K_COEFF_PER_BIN+K25_NZ_RE]  //S25 Re
392
2274
                                   +compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+17]*k_coeff_intercalib[i*NB_K_COEFF_PER_BIN+K34_NZ_RE]  //S34 Re
393
2274
                                   +compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+19]*k_coeff_intercalib[i*NB_K_COEFF_PER_BIN+K35_NZ_RE]  //S35 Re
394
2274
                                   +compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+13]*k_coeff_intercalib[i*NB_K_COEFF_PER_BIN+K24_NZ_IM]  //S24 Im
395
2274
                                   +compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+15]*k_coeff_intercalib[i*NB_K_COEFF_PER_BIN+K25_NZ_IM]  //S25 Im
396
2274
                                   +compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+18]*k_coeff_intercalib[i*NB_K_COEFF_PER_BIN+K34_NZ_IM]  //S34 Im
397
2274
                                   +compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+20]*k_coeff_intercalib[i*NB_K_COEFF_PER_BIN+K35_NZ_IM]);//S35 Im
398
        // Im(S_ji) = -Im(S_ij)
399
        // k_ji = k_ij
400
6822
        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
401
2274
                                   +compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+14]*k_coeff_intercalib[i*NB_K_COEFF_PER_BIN+K25_NY_IM]  //S25 Re
402
2274
                                   +compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+17]*k_coeff_intercalib[i*NB_K_COEFF_PER_BIN+K34_NY_IM]  //S34 Re
403
2274
                                   +compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+19]*k_coeff_intercalib[i*NB_K_COEFF_PER_BIN+K35_NY_IM]  //S35 Re
404
2274
                                   -compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+13]*k_coeff_intercalib[i*NB_K_COEFF_PER_BIN+K24_NY_RE]  //S24 Im
405
2274
                                   -compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+15]*k_coeff_intercalib[i*NB_K_COEFF_PER_BIN+K25_NY_RE]  //S25 Im
406
2274
                                   -compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+18]*k_coeff_intercalib[i*NB_K_COEFF_PER_BIN+K34_NY_RE]  //S34 Im
407
2274
                                   -compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+20]*k_coeff_intercalib[i*NB_K_COEFF_PER_BIN+K35_NY_RE]) //S35 Im
408
4548
                            + 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
409
2274
                                   +compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+14]*k_coeff_intercalib[i*NB_K_COEFF_PER_BIN+K25_NZ_IM]  //S25 Re
410
2274
                                   +compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+17]*k_coeff_intercalib[i*NB_K_COEFF_PER_BIN+K34_NZ_IM]  //S34 Re
411
2274
                                   +compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+19]*k_coeff_intercalib[i*NB_K_COEFF_PER_BIN+K35_NZ_IM]  //S35 Re
412
2274
                                   -compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+13]*k_coeff_intercalib[i*NB_K_COEFF_PER_BIN+K24_NZ_RE]  //S24 Im
413
2274
                                   -compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+15]*k_coeff_intercalib[i*NB_K_COEFF_PER_BIN+K25_NZ_RE]  //S25 Im
414
2274
                                   -compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+18]*k_coeff_intercalib[i*NB_K_COEFF_PER_BIN+K34_NZ_RE]  //S34 Im
415
2274
                                   -compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+20]*k_coeff_intercalib[i*NB_K_COEFF_PER_BIN+K35_NZ_RE]);//S35 Im
416
#ifdef DEBUG_TCH
417
        printf("n_cross_e_scal_b_re   : %16.8e\n",n_cross_e_scal_b_re);
418
        printf("n_cross_e_scal_b_im   : %16.8e\n",n_cross_e_scal_b_im);
419
#endif
420
        // vphi = n_cross_e_scal_b_re / bx_bx_star => sign(VPHI) = sign(n_cross_e_scal_b_re)
421
2274
        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
422
#ifdef LSB_FIRST_TCH
423
        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)
424
                                                                     // Record it at the 8th bit position (from the right to the left)
425
                                                                     // of lfr_bp1[i*NB_BYTES_BP1+9]
426
        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|
427
#endif
428
#ifdef MSB_FIRST_TCH
429
2274
        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)
430
                                                                     // Record it at the 8th bit position (from the right to the left)
431
                                                                     // of lfr_bp1[i*NB_BYTES_BP1+9]
432
2274
        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|
433
#endif
434
2274
        if (bx_bx_star != 0.) { // no division by 0.
435
2274
            vphi = n_cross_e_scal_b_re / bx_bx_star;  // Compute |VPHI|
436
        }
437
        else
438
        {
439
            vphi = 1.e+20;                         // Put a huge value
440
        }
441
2274
        significand = frexpf(vphi, &exponent);  // 0.5 <= significand < 1
442
                                                // vphi = significand * 2^exponent
443
2274
        if (exponent < expmin) { // value should be >= 0.5 * 2^expmin
444
2
          exponent = expmin;
445
2
          significand = 0.5;     // min value that can be recorded
446
        }
447
2274
        if (exponent > expmax) { // value should be <  0.5 * 2^(expmax+1)
448
          exponent = expmax;
449
          significand = 1.0;     // max value that can be recorded
450
        }
451
2274
        if (significand == 0) {// in that case exponent == 0 too
452
101
          exponent = expmin;
453
101
          significand = 0.5;   // min value that can be recorded
454
        }
455
456
2274
        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
457
                                                                           // where all the bits are used (0, ..., 255)
458
2274
        tmp_uint8 = (uint8_t) (exponent-expmin); // Shift and cast into a 8-bit uint8_t where
459
                                                 // just the first nbitexp bits are used (0, ..., 2^nbitexp-1)
460
#ifdef DEBUG_TCH
461
        printf("|VPHI|      : %16.8e\n",vphi);
462
        printf("significand : %16.8e\n",significand);
463
        printf("exponent    : %d\n"    ,exponent);
464
        printf("tmp_uint8        for VPHI exponent    : %d\n",tmp_uint8);
465
#endif
466
2274
        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
467
                                                                           // (from the right to the left) of lfr_bp1[i*NB_BYTES_BP1+9]
468
#ifdef DEBUG_TCH
469
        printf("lfr_bp1[i*NB_BYTES_BP1+9]  for VPHI sign + VPHI exponent : %u\n",lfr_bp1[i*NB_BYTES_BP1+9]);
470
        printf("lfr_bp1[i*NB_BYTES_BP1+10] for VPHI significand          : %u\n",lfr_bp1[i*NB_BYTES_BP1+10]);
471
#endif
472
2274
        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
473
#ifdef LSB_FIRST_TCH
474
        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)
475
#endif
476
#ifdef MSB_FIRST_TCH
477
2274
        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)
478
#endif
479
2274
        tmp_uint8 = (n_cross_e_scal_b_im > n_cross_e_scal_b_re) ? 0x40 : 0x00; // Determine the sector argument of NEBX. If |Im| > |Re| affect
480
                                                                               // an unsigned 8-bit char with 01000000; otherwise with null.
481
2274
        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
482
                                                                               // to the left) of lfr_bp1[i*NB_BYTES_BP1+9], by simple logical addition.
483
#ifdef DEBUG_TCH
484
        printf("|n_cross_e_scal_b_im|             : %16.8e\n",n_cross_e_scal_b_im);
485
        printf("|n_cross_e_scal_b_im|/bx_bx_star  : %16.8e\n",n_cross_e_scal_b_im/bx_bx_star);
486
        printf("ArgNEBX sign                      : %u\n",tmp_uint8);
487
        printf("lfr_bp1[i*NB_BYTES_BP1+9] for VPHI & ArgNEBX signs + VPHI exponent : %u\n",lfr_bp1[i*NB_BYTES_BP1+9]);
488
#endif
489
    }
490
109
}
491
492
19
void BP2_set( float * compressed_spec_mat, uint8_t nb_bins_compressed_spec_mat, uint8_t * lfr_bp2 )
493
{
494
    float cross_re;                     // 32-bit floating point
495
    float cross_im;
496
    float aux;
497
    float significand;
498
    int exponent;                       // 32-bit signed integer
499
    uint8_t nbitexp;                    // 8-bit unsigned integer
500
    uint8_t nbitsig;
501
    uint8_t *pt_uint8;                  // pointer on unsigned 8-bit integer
502
    int8_t expmin;                      // 8-bit signed integer
503
    int8_t expmax;
504
    uint16_t rangesig;                  // 16-bit unsigned integer
505
    uint16_t autocor;
506
    uint16_t exp;
507
    uint16_t tmp_uint16;
508
    uint16_t i;
509
510
#ifdef DEBUG_TCH
511
    printf("BP2 : \n");
512
    printf("Number of bins: %d\n", nb_bins_compressed_spec_mat);
513
#endif
514
515
    // For floating point data to be recorded on 16-bit words :
516
19
    nbitexp = 6;                  // number of bits for the exponent
517
19
    nbitsig = 16 - nbitexp;       // number of bits for the significand
518
19
    rangesig = (1 << nbitsig)-1;  // == 2^nbitsig - 1
519
19
    expmax = 32 + 5;
520
19
    expmin = expmax - (1 << nbitexp) + 1;
521
522
#ifdef DEBUG_TCH
523
524
    printf("nbitexp : %d, expmax : %d, expmin : %d\n", nbitexp, expmax, expmin);
525
    printf("nbitsig : %d, rangesig : %d\n", nbitsig, rangesig);
526
#endif
527
528
427
    for(i = 0; i<nb_bins_compressed_spec_mat; i++){
529
        //==============================================
530
        // BP2 normalized cross correlations == PA_LFR_SC_BP2_CROSS_F0 == 10 * (8+8) bits
531
                                          // == PA_LFR_SC_BP2_CROSS_RE_0_F0 == 8 bits
532
                                          // == PA_LFR_SC_BP2_CROSS_IM_0_F0 == 8 bits
533
                                          // == PA_LFR_SC_BP2_CROSS_RE_1_F0 == 8 bits
534
                                          // == PA_LFR_SC_BP2_CROSS_IM_1_F0 == 8 bits
535
                                          // == PA_LFR_SC_BP2_CROSS_RE_2_F0 == 8 bits
536
                                          // == PA_LFR_SC_BP2_CROSS_IM_2_F0 == 8 bits
537
                                          // == PA_LFR_SC_BP2_CROSS_RE_3_F0 == 8 bits
538
                                          // == PA_LFR_SC_BP2_CROSS_IM_3_F0 == 8 bits
539
                                          // == PA_LFR_SC_BP2_CROSS_RE_4_F0 == 8 bits
540
                                          // == PA_LFR_SC_BP2_CROSS_IM_4_F0 == 8 bits
541
                                          // == PA_LFR_SC_BP2_CROSS_RE_5_F0 == 8 bits
542
                                          // == PA_LFR_SC_BP2_CROSS_IM_5_F0 == 8 bits
543
                                          // == PA_LFR_SC_BP2_CROSS_RE_6_F0 == 8 bits
544
                                          // == PA_LFR_SC_BP2_CROSS_IM_6_F0 == 8 bits
545
                                          // == PA_LFR_SC_BP2_CROSS_RE_7_F0 == 8 bits
546
                                          // == PA_LFR_SC_BP2_CROSS_IM_7_F0 == 8 bits
547
                                          // == PA_LFR_SC_BP2_CROSS_RE_8_F0 == 8 bits
548
                                          // == PA_LFR_SC_BP2_CROSS_IM_8_F0 == 8 bits
549
                                          // == PA_LFR_SC_BP2_CROSS_RE_9_F0 == 8 bits
550
                                          // == PA_LFR_SC_BP2_CROSS_IM_9_F0 == 8 bits
551
        // S12
552
408
        aux = sqrt(compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX]*compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+9]);
553
408
        if (aux != 0.) { // no division by 0.
554
408
        cross_re = compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+1] / aux;
555
408
        cross_im = compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+2] / aux;
556
        }
557
        else
558
        {
559
        cross_re = 0.;
560
        cross_im = 0.;
561
        }
562
408
        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
563
408
        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
564
#ifdef DEBUG_TCH
565
        printf("\nBin number: %d\n", i);
566
        printf("lfr_bp2[i*NB_BYTES_BP2+10] for cross12_re (%16.8e) : %.3u\n",cross_re, lfr_bp2[i*NB_BYTES_BP2+10]);
567
        printf("lfr_bp2[i*NB_BYTES_BP2+20] for cross12_im (%16.8e) : %.3u\n",cross_im, lfr_bp2[i*NB_BYTES_BP2+20]);
568
#endif
569
        // S13
570
408
        aux = sqrt(compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX]*compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+16]);
571
408
        if (aux != 0.) { // no division by 0.
572
408
        cross_re = compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+3] / aux;
573
408
        cross_im = compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+4] / aux;
574
        }
575
        else
576
        {
577
        cross_re = 0.;
578
        cross_im = 0.;
579
        }
580
408
        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
581
408
        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
582
#ifdef DEBUG_TCH
583
        printf("lfr_bp2[i*NB_BYTES_BP2+11] for cross13_re (%16.8e) : %.3u\n",cross_re, lfr_bp2[i*NB_BYTES_BP2+11]);
584
        printf("lfr_bp2[i*NB_BYTES_BP2+21] for cross13_im (%16.8e) : %.3u\n",cross_im, lfr_bp2[i*NB_BYTES_BP2+21]);
585
#endif
586
        // S14
587
408
        aux = sqrt(compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX]*compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+21]);
588
408
        if (aux != 0.) { // no division by 0.
589
408
        cross_re = compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+5] / aux;
590
408
        cross_im = compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+6] / aux;
591
        }
592
        else
593
        {
594
        cross_re = 0.;
595
        cross_im = 0.;
596
        }
597
408
        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
598
408
        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
599
#ifdef DEBUG_TCH
600
        printf("lfr_bp2[i*NB_BYTES_BP2+12] for cross14_re (%16.8e) : %.3u\n",cross_re, lfr_bp2[i*NB_BYTES_BP2+12]);
601
        printf("lfr_bp2[i*NB_BYTES_BP2+22] for cross14_im (%16.8e) : %.3u\n",cross_im, lfr_bp2[i*NB_BYTES_BP2+22]);
602
#endif
603
        // S15
604
408
        aux = sqrt(compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX]*compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+24]);
605
408
        if (aux != 0.) { // no division by 0.
606
408
        cross_re = compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+7] / aux;
607
408
        cross_im = compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+8] / aux;
608
        }
609
        else
610
        {
611
        cross_re = 0.;
612
        cross_im = 0.;
613
        }
614
408
        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
615
408
        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
616
#ifdef DEBUG_TCH
617
        printf("lfr_bp2[i*NB_BYTES_BP2+13] for cross15_re (%16.8e) : %.3u\n",cross_re, lfr_bp2[i*NB_BYTES_BP2+13]);
618
        printf("lfr_bp2[i*NB_BYTES_BP2+23] for cross15_im (%16.8e) : %.3u\n",cross_im, lfr_bp2[i*NB_BYTES_BP2+23]);
619
#endif
620
        // S23
621
408
        aux = sqrt(compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+9]*compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+16]);
622
408
        if (aux != 0.) { // no division by 0.
623
408
        cross_re = compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+10] / aux;
624
408
        cross_im = compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+11] / aux;
625
        }
626
        else
627
        {
628
        cross_re = 0.;
629
        cross_im = 0.;
630
        }
631
408
        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
632
408
        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
633
#ifdef DEBUG_TCH
634
        printf("lfr_bp2[i*NB_BYTES_BP2+14] for cross23_re (%16.8e) : %.3u\n",cross_re, lfr_bp2[i*NB_BYTES_BP2+14]);
635
        printf("lfr_bp2[i*NB_BYTES_BP2+24] for cross23_im (%16.8e) : %.3u\n",cross_im, lfr_bp2[i*NB_BYTES_BP2+24]);
636
#endif
637
        // S24
638
408
        aux = sqrt(compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+9]*compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+21]);
639
408
        if (aux != 0.) { // no division by 0.
640
408
        cross_re = compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+12] / aux;
641
408
        cross_im = compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+13] / aux;
642
        }
643
        else
644
        {
645
        cross_re = 0.;
646
        cross_im = 0.;
647
        }
648
408
        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
649
408
        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
650
#ifdef DEBUG_TCH
651
        printf("lfr_bp2[i*NB_BYTES_BP2+15] for cross24_re (%16.8e) : %.3u\n",cross_re, lfr_bp2[i*NB_BYTES_BP2+15]);
652
        printf("lfr_bp2[i*NB_BYTES_BP2+25] for cross24_im (%16.8e) : %.3u\n",cross_im, lfr_bp2[i*NB_BYTES_BP2+25]);
653
#endif
654
        // S25
655
408
        aux = sqrt(compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+9]*compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+24]);
656
408
        if (aux != 0.) { // no division by 0.
657
408
        cross_re = compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+14] / aux;
658
408
        cross_im = compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+15] / aux;
659
        }
660
        else
661
        {
662
        cross_re = 0.;
663
        cross_im = 0.;
664
        }
665
408
        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
666
408
        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
667
#ifdef DEBUG_TCH
668
        printf("lfr_bp2[i*NB_BYTES_BP2+16] for cross25_re (%16.8e) : %.3u\n",cross_re, lfr_bp2[i*NB_BYTES_BP2+16]);
669
        printf("lfr_bp2[i*NB_BYTES_BP2+26] for cross25_im (%16.8e) : %.3u\n",cross_im, lfr_bp2[i*NB_BYTES_BP2+26]);
670
#endif
671
        // S34
672
408
        aux = sqrt(compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+16]*compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+21]);
673
408
        if (aux != 0.) { // no division by 0.
674
408
        cross_re = compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+17] / aux;
675
408
        cross_im = compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+18] / aux;
676
        }
677
        else
678
        {
679
        cross_re = 0.;
680
        cross_im = 0.;
681
        }
682
408
        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
683
408
        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
684
#ifdef DEBUG_TCH
685
        printf("lfr_bp2[i*NB_BYTES_BP2+17] for cross34_re (%16.8e) : %.3u\n",cross_re, lfr_bp2[i*NB_BYTES_BP2+17]);
686
        printf("lfr_bp2[i*NB_BYTES_BP2+27] for cross34_im (%16.8e) : %.3u\n",cross_im, lfr_bp2[i*NB_BYTES_BP2+27]);
687
#endif
688
        // S35
689
408
        aux = sqrt(compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+16]*compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+24]);
690
408
        if (aux != 0.) { // no division by 0.
691
408
        cross_re = compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+19] / aux;
692
408
        cross_im = compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+20] / aux;
693
        }
694
        else
695
        {
696
        cross_re = 0.;
697
        cross_im = 0.;
698
        }
699
408
        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
700
408
        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
701
#ifdef DEBUG_TCH
702
        printf("lfr_bp2[i*NB_BYTES_BP2+18] for cross35_re (%16.8e) : %.3u\n",cross_re, lfr_bp2[i*NB_BYTES_BP2+18]);
703
        printf("lfr_bp2[i*NB_BYTES_BP2+28] for cross35_im (%16.8e) : %.3u\n",cross_im, lfr_bp2[i*NB_BYTES_BP2+28]);
704
#endif
705
        // S45
706
408
        aux = sqrt(compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+21]*compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+24]);
707
408
        if (aux != 0.) { // no division by 0.
708
408
        cross_re = compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+22] / aux;
709
408
        cross_im = compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+23] / aux;
710
        }
711
        else
712
        {
713
        cross_re = 0.;
714
        cross_im = 0.;
715
        }
716
408
        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
717
408
        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
718
#ifdef DEBUG_TCH
719
        printf("lfr_bp2[i*NB_BYTES_BP2+19] for cross45_re (%16.8e) : %.3u\n",cross_re, lfr_bp2[i*NB_BYTES_BP2+19]);
720
        printf("lfr_bp2[i*NB_BYTES_BP2+29] for cross45_im (%16.8e) : %.3u\n",cross_im, lfr_bp2[i*NB_BYTES_BP2+29]);
721
#endif
722
        //==============================================
723
        // BP2  auto correlations == PA_LFR_SC_BP2_AUTO_F0 == 5*16 bits = 5*[6 bits (exponent) + 10 bits (significand)]
724
                               // == PA_LFR_SC_BP2_AUTO_A0_F0 == 16 bits
725
                               // == PA_LFR_SC_BP2_AUTO_A1_F0 == 16 bits
726
                               // == PA_LFR_SC_BP2_AUTO_A2_F0 == 16 bits
727
                               // == PA_LFR_SC_BP2_AUTO_A3_F0 == 16 bits
728
                               // == PA_LFR_SC_BP2_AUTO_A4_F0 == 16 bits
729
        // S11
730
408
        significand = frexpf(compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX], &exponent);  // 0.5 <= significand < 1
731
                                                                                                // S11 = significand * 2^exponent
732
#ifdef DEBUG_TCH
733
        printf("S11         : %16.8e\n",compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX]);
734
        printf("significand : %16.8e\n",significand);
735
        printf("exponent    : %d\n"    ,exponent);
736
#endif
737
408
        if (exponent < expmin) { // value should be >= 0.5 * 2^expmin
738
          exponent = expmin;
739
          significand = 0.5;     // min value that can be recorded
740
        }
741
408
        if (exponent > expmax) { // value should be <  0.5 * 2^(expmax+1)
742
          exponent = expmax;
743
          significand = 1.0;     // max value that can be recorded
744
        }
745
408
        if (significand == 0) {  // in that case exponent == 0 too
746
          exponent = expmin;
747
          significand = 0.5;     // min value that can be recorded
748
        }
749
750
408
        autocor = (uint16_t) ((significand*2-1)*rangesig + 0.5); // Shift and cast into a 16-bit unsigned int with rounding
751
                                                                 // where just the first nbitsig bits are used (0, ..., 2^nbitsig-1)
752
408
        exp = (uint16_t) (exponent-expmin);      // Shift and cast into a 16-bit unsigned int where just
753
                                                 // the first nbitexp bits are used (0, ..., 2^nbitexp-1)
754
408
        tmp_uint16 = autocor | (exp << nbitsig); // Put the exponent bits (nbitexp) next to the
755
                                                 // left place of the significand bits (nbitsig),
756
                                                 // making the 16-bit word to be recorded
757
408
        pt_uint8 = (uint8_t*) &tmp_uint16;       // Affect an uint8_t pointer with the adress of tmp_uint16
758
#ifdef MSB_FIRST_TCH
759
408
        lfr_bp2[i*NB_BYTES_BP2+0] = pt_uint8[0]; // Record MSB of tmp_uint16
760
408
        lfr_bp2[i*NB_BYTES_BP2+1] = pt_uint8[1]; // Record LSB of tmp_uint16
761
#endif
762
#ifdef LSB_FIRST_TCH
763
        lfr_bp2[i*NB_BYTES_BP2+0] = pt_uint8[1]; // Record MSB of tmp_uint16
764
        lfr_bp2[i*NB_BYTES_BP2+1] = pt_uint8[0]; // Record LSB of tmp_uint16
765
#endif
766
#ifdef DEBUG_TCH
767
        printf("autocor for S11 significand : %u\n",autocor);
768
        printf("exp for S11 exponent : %u\n",exp);
769
        printf("pt_uint8[1] for S11 exponent + significand : %.3d or %2x\n",pt_uint8[1], pt_uint8[1]);
770
        printf("pt_uint8[0] for S11            significand : %.3d or %2x\n",pt_uint8[0], pt_uint8[0]);
771
        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]);
772
        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]);
773
#endif
774
        // S22
775
408
        significand = frexpf(compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+9], &exponent);  // 0.5 <= significand < 1
776
                                                                                                  // S22 = significand * 2^exponent
777
#ifdef DEBUG_TCH
778
        printf("S22         : %16.8e\n",compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+9]);
779
        printf("significand : %16.8e\n",significand);
780
        printf("exponent    : %d\n"    ,exponent);
781
#endif
782
408
        if (exponent < expmin) { // value should be >= 0.5 * 2^expmin
783
          exponent = expmin;
784
          significand = 0.5;     // min value that can be recorded
785
        }
786
408
        if (exponent > expmax) { // value should be <  0.5 * 2^(expmax+1)
787
          exponent = expmax;
788
          significand = 1.0;     // max value that can be recorded
789
        }
790
408
        if (significand == 0) {  // in that case exponent == 0 too
791
          exponent = expmin;
792
          significand = 0.5;     // min value that can be recorded
793
        }
794
795
408
        autocor = (uint16_t) ((significand*2-1)*rangesig + 0.5); // Shift and cast into a 16-bit unsigned int with rounding
796
                                                                 // where just the first nbitsig bits are used (0, ..., 2^nbitsig-1)
797
408
        exp = (uint16_t) (exponent-expmin);      // Shift and cast into a 16-bit unsigned int where just
798
                                                 // the first nbitexp bits are used (0, ..., 2^nbitexp-1)
799
408
        tmp_uint16 = autocor | (exp << nbitsig); // Put the exponent bits (nbitexp) next to the
800
                                                 // left place of the significand bits (nbitsig),
801
                                                 // making the 16-bit word to be recorded
802
408
        pt_uint8 = (uint8_t*) &tmp_uint16;       // Affect an uint8_t pointer with the adress of tmp_uint16
803
#ifdef MSB_FIRST_TCH
804
408
        lfr_bp2[i*NB_BYTES_BP2+2] = pt_uint8[0]; // Record MSB of tmp_uint16
805
408
        lfr_bp2[i*NB_BYTES_BP2+3] = pt_uint8[1]; // Record LSB of tmp_uint16
806
#endif
807
#ifdef LSB_FIRST_TCH
808
        lfr_bp2[i*NB_BYTES_BP2+2] = pt_uint8[1]; // Record MSB of tmp_uint16
809
        lfr_bp2[i*NB_BYTES_BP2+3] = pt_uint8[0]; // Record LSB of tmp_uint16
810
#endif
811
#ifdef DEBUG_TCH
812
        printf("autocor for S22 significand : %u\n",autocor);
813
        printf("exp for S11 exponent : %u\n",exp);
814
        printf("pt_uint8[1] for S22 exponent + significand : %.3d or %2x\n",pt_uint8[1], pt_uint8[1]);
815
        printf("pt_uint8[0] for S22            significand : %.3d or %2x\n",pt_uint8[0], pt_uint8[0]);
816
        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]);
817
        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]);
818
#endif
819
        // S33
820
408
        significand = frexpf(compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+16], &exponent);  // 0.5 <= significand < 1
821
                                                                                                   // S33 = significand * 2^exponent
822
#ifdef DEBUG_TCH
823
        printf("S33         : %16.8e\n",compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+16]);
824
        printf("significand : %16.8e\n",significand);
825
        printf("exponent    : %d\n"    ,exponent);
826
#endif
827
408
        if (exponent < expmin) { // value should be >= 0.5 * 2^expmin
828
          exponent = expmin;
829
          significand = 0.5;     // min value that can be recorded
830
        }
831
408
        if (exponent > expmax) { // value should be <  0.5 * 2^(expmax+1)
832
          exponent = expmax;
833
          significand = 1.0;     // max value that can be recorded
834
        }
835
408
        if (significand == 0) {  // in that case exponent == 0 too
836
          exponent = expmin;
837
          significand = 0.5;     // min value that can be recorded
838
        }
839
840
408
        autocor = (uint16_t) ((significand*2-1)*rangesig + 0.5); // Shift and cast into a 16-bit unsigned int with rounding
841
                                                                 // where just the first nbitsig bits are used (0, ..., 2^nbitsig-1)
842
408
        exp = (uint16_t) (exponent-expmin);      // Shift and cast into a 16-bit unsigned int where just
843
                                                 // the first nbitexp bits are used (0, ..., 2^nbitexp-1)
844
408
        tmp_uint16 = autocor | (exp << nbitsig); // Put the exponent bits (nbitexp) next to the
845
                                                 // left place of the significand bits (nbitsig),
846
                                                 // making the 16-bit word to be recorded
847
408
        pt_uint8 = (uint8_t*) &tmp_uint16;       // Affect an uint8_t pointer with the adress of tmp_uint16
848
#ifdef MSB_FIRST_TCH
849
408
        lfr_bp2[i*NB_BYTES_BP2+4] = pt_uint8[0]; // Record MSB of tmp_uint16
850
408
        lfr_bp2[i*NB_BYTES_BP2+5] = pt_uint8[1]; // Record LSB of tmp_uint16
851
#endif
852
#ifdef LSB_FIRST_TCH
853
        lfr_bp2[i*NB_BYTES_BP2+4] = pt_uint8[1]; // Record MSB of tmp_uint16
854
        lfr_bp2[i*NB_BYTES_BP2+5] = pt_uint8[0]; // Record LSB of tmp_uint16
855
#endif
856
#ifdef DEBUG_TCH
857
        printf("autocor for S33 significand : %u\n",autocor);
858
        printf("exp for S33 exponent : %u\n",exp);
859
        printf("pt_uint8[1] for S33 exponent + significand : %.3d or %2x\n",pt_uint8[1], pt_uint8[1]);
860
        printf("pt_uint8[0] for S33            significand : %.3d or %2x\n",pt_uint8[0], pt_uint8[0]);
861
        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]);
862
        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]);
863
#endif
864
        // S44
865
408
        significand = frexpf(compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+21], &exponent);  // 0.5 <= significand < 1
866
                                                                                                   // S44 = significand * 2^exponent
867
#ifdef DEBUG_TCH
868
        printf("S44         : %16.8e\n",compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+21]);
869
        printf("significand : %16.8e\n",significand);
870
        printf("exponent    : %d\n"    ,exponent);
871
#endif
872
873
408
        if (exponent < expmin) { // value should be >= 0.5 * 2^expmin
874
          exponent = expmin;
875
          significand = 0.5;     // min value that can be recorded
876
        }
877
408
        if (exponent > expmax) { // value should be <  0.5 * 2^(expmax+1)
878
          exponent = expmax;
879
          significand = 1.0;     // max value that can be recorded
880
        }
881
408
        if (significand == 0) {  // in that case exponent == 0 too
882
          exponent = expmin;
883
          significand = 0.5;     // min value that can be recorded
884
        }
885
886
408
        autocor = (uint16_t) ((significand*2-1)*rangesig + 0.5); // Shift and cast into a 16-bit unsigned int with rounding
887
                                                                 // where just the first nbitsig bits are used (0, ..., 2^nbitsig-1)
888
408
        exp = (uint16_t) (exponent-expmin);      // Shift and cast into a 16-bit unsigned int where just
889
                                                 // the first nbitexp bits are used (0, ..., 2^nbitexp-1)
890
408
        tmp_uint16 = autocor | (exp << nbitsig); // Put the exponent bits (nbitexp) next to the
891
                                                 // left place of the significand bits (nbitsig),
892
                                                 // making the 16-bit word to be recorded
893
408
        pt_uint8 = (uint8_t*) &tmp_uint16;       // Affect an uint8_t pointer with the adress of tmp_uint16
894
#ifdef MSB_FIRST_TCH
895
408
        lfr_bp2[i*NB_BYTES_BP2+6] = pt_uint8[0]; // Record MSB of tmp_uint16
896
408
        lfr_bp2[i*NB_BYTES_BP2+7] = pt_uint8[1]; // Record LSB of tmp_uint16
897
#endif
898
#ifdef LSB_FIRST_TCH
899
        lfr_bp2[i*NB_BYTES_BP2+6] = pt_uint8[1]; // Record MSB of tmp_uint16
900
        lfr_bp2[i*NB_BYTES_BP2+7] = pt_uint8[0]; // Record LSB of tmp_uint16
901
#endif
902
#ifdef DEBUG_TCH
903
        printf("autocor for S44 significand : %u\n",autocor);
904
        printf("exp for S44 exponent : %u\n",exp);
905
        printf("pt_uint8[1] for S44 exponent + significand : %.3d or %2x\n",pt_uint8[1], pt_uint8[1]);
906
        printf("pt_uint8[0] for S44            significand : %.3d or %2x\n",pt_uint8[0], pt_uint8[0]);
907
        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]);
908
        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]);
909
#endif
910
        // S55
911
408
        significand = frexpf(compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+24], &exponent);  // 0.5 <= significand < 1
912
                                                                                                   // S55 = significand * 2^exponent
913
#ifdef DEBUG_TCH
914
        printf("S55         : %16.8e\n",compressed_spec_mat[i*NB_VALUES_PER_SPECTRAL_MATRIX+24]);
915
        printf("significand : %16.8e\n",significand);
916
        printf("exponent    : %d\n"    ,exponent);
917
#endif
918
408
        if (exponent < expmin) { // value should be >= 0.5 * 2^expmin
919
          exponent = expmin;
920
          significand = 0.5;     // min value that can be recorded
921
        }
922
408
        if (exponent > expmax) { // value should be <  0.5 * 2^(expmax+1)
923
          exponent = expmax;
924
          significand = 1.0;     // max value that can be recorded
925
        }
926
408
        if (significand == 0) {  // in that case exponent == 0 too
927
          exponent = expmin;
928
          significand = 0.5;     // min value that can be recorded
929
        }
930
931
408
        autocor = (uint16_t) ((significand*2-1)*rangesig + 0.5); // Shift and cast into a 16-bit unsigned int with rounding
932
                                                                 // where just the first nbitsig bits are used (0, ..., 2^nbitsig-1)
933
408
        exp = (uint16_t) (exponent-expmin);      // Shift and cast into a 16-bit unsigned int where just
934
                                                 // the first nbitexp bits are used (0, ..., 2^nbitexp-1)
935
408
        tmp_uint16 = autocor | (exp << nbitsig); // Put the exponent bits (nbitexp) next to the
936
                                                 // left place of the significand bits (nbitsig),
937
                                                 // making the 16-bit word to be recorded
938
408
        pt_uint8 = (uint8_t*) &tmp_uint16;       // Affect an uint8_t pointer with the adress of tmp_uint16
939
#ifdef MSB_FIRST_TCH
940
408
        lfr_bp2[i*NB_BYTES_BP2+8] = pt_uint8[0]; // Record MSB of tmp_uint16
941
408
        lfr_bp2[i*NB_BYTES_BP2+9] = pt_uint8[1]; // Record LSB of tmp_uint16
942
        //printf("MSB:\n");
943
#endif
944
#ifdef LSB_FIRST_TCH
945
        lfr_bp2[i*NB_BYTES_BP2+8] = pt_uint8[1]; // Record MSB of tmp_uint16
946
        lfr_bp2[i*NB_BYTES_BP2+9] = pt_uint8[0]; // Record LSB of tmp_uint16
947
        //printf("LSB:\n");
948
#endif
949
#ifdef DEBUG_TCH
950
        printf("autocor for S55 significand : %u\n",autocor);
951
        printf("exp for S55 exponent : %u\n",exp);
952
        printf("pt_uint8[1] for S55 exponent + significand : %.3d or %2x\n",pt_uint8[1], pt_uint8[1]);
953
        printf("pt_uint8[0] for S55            significand : %.3d or %2x\n",pt_uint8[0], pt_uint8[0]);
954
        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]);
955
        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]);
956
#endif
957
    }
958
19
}
959
960
961
#endif // BASIC_PARAMETERS_H_INCLUDED