summaryrefslogtreecommitdiff
path: root/cp/spoc/test/src/test_coeff_check.c
blob: 5a50d41d0bc032fdd3ce45d6ba05bb59bdf291ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/* Cesar project {{{
 *
 * Copyright (C) 2008 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    src/test_spoc.c
 * \brief   « brief description »
 * \ingroup « module »
 *
 * « long description »
 */
#include "define_spoc_c.h"
#include <stdio.h>
#include <stdlib.h>
#include "math.h"
#include "lib/test.h"

#define FAIL_LIMIT 1e-6
#define SPOC_TEST_DBG 0

double kaiser_d[10] = { 0.9905, 0.9622, 0.9164, 0.8546, 0.7792, 0.6929, 0.5989, 0.5005, 0.4011, 0.304 };

double
err (double a, double ref)
{
    double ret = 0;
    //printf ("DBG : a=%5.15g ref=%5.15g\n", a, ref);
    if (ref == 0)
    {
        ret = a;
    }
    else
    {
        if (a == 0) ret = ref;
        else ret = (a-ref)/ref;
    }
    if (ret<0) ret = -ret;
    return(ret);
}

double
data_Mcheck_dbg_print (char* filestring, char* haut_str,  char *bas_str,
                      spoc_prec_t *tx_coeff_haut, spoc_prec_t *tx_coeff_bas,
                      spoc_prec_t *rx_coeff_haut, spoc_prec_t *rx_coeff_bas )
{
    double tx_haut, tx_bas;
    double rx_haut, rx_bas;
    double tx_haut_err, tx_bas_err;
    double rx_haut_err, rx_bas_err;
    double ret=0;
    FILE *fref = fopen (filestring, "r");
    int i;
    for (i=0; i<41; i++)
    {
        fscanf (fref, "%lg %lg %lg %lg", &tx_haut, &tx_bas, &rx_haut, &rx_bas);
        tx_haut_err = err (tx_coeff_haut[i], tx_haut);
        tx_bas_err = err(tx_coeff_bas[i], tx_bas);
        rx_haut_err = err (rx_coeff_haut[i], rx_haut);
        rx_bas_err = err(rx_coeff_bas[i], rx_bas);
        if (ret < rx_haut_err) ret = rx_haut_err;
        if (ret < rx_bas_err) ret = rx_bas_err;
        if (ret < tx_haut_err) ret = tx_haut_err;
        if (ret < tx_bas_err) ret = tx_bas_err;
#if defined (SPOC_TEST_DBG) && (SPOC_TEST_DBG==2)
        printf ("TX_%s[%d] %5.15g<-->%5.15g\t %5.15g\n", haut_str, i-20, tx_coeff_haut[i], tx_haut, tx_haut_err);
        printf ("TX_%s[%d] %5.15g<-->%5.15g\t %5.15g ", bas_str, i-20, tx_coeff_bas[i], tx_bas, tx_bas_err);
        if ((tx_bas_err > FAIL_LIMIT) || (tx_haut_err > FAIL_LIMIT)) printf (" * \n");
        else printf ("\n");
        printf ("RX_%s[%d] %5.15g<-->%5.15g\t %5.15g\n", haut_str, i-20, rx_coeff_haut[i], rx_haut, rx_haut_err);
        printf ("RX_%s[%d] %5.15g<-->%5.15g\t %5.15g  ", bas_str, i-20, rx_coeff_bas[i], rx_bas, rx_bas_err);
        if ((rx_bas_err > FAIL_LIMIT) || (rx_haut_err > FAIL_LIMIT)) printf (" * \n");
        else printf ("\n");
#endif
    }
    fclose (fref);
    return ret;
}

void
test_Mcoeff_check (test_t test, double rho)
{
    char cmd_scilab[128];
    char test_string[64];
    sprintf (test_string, "rho=%lg\n", rho);
    test_begin (test, test_string)
    {
        spoc_MCoeff (rho);
        sprintf (cmd_scilab,"scilab -f ./coeff_generation.scilab -nogui -args %lg > ./obj/toto.log 2>./obj/titi.log </dev/null", rho);
        system (cmd_scilab);
#if defined (SPOC_TEST_DBG) && (SPOC_TEST_DBG==2)
        printf ("\nargs\n");
        data_Mcheck_dbg_print ("./obj/args_of_sinc.txt", "ARGS_HAUT", "ARGS_BAS ",
                               args_haut, args_bas, args_haut_rx, args_bas_rx);
        printf ("\nsinc\n");
        data_Mcheck_dbg_print ("./obj/sinc.txt", "SINC_HAUT", "SINC_BAS ",
                               sinc_haut, sinc_bas, sinc_haut_rx, sinc_bas_rx);
        printf ("\ncoeff\n");
#endif
        double err = data_Mcheck_dbg_print ("./obj/coeff.txt", "ORDON", "PENTE",
                                            reg_ordo_tx, reg_pente_tx, reg_ordo_rx, reg_pente_rx);
#if defined (SPOC_TEST_DBG) && (SPOC_TEST_DBG>=1)
        printf ("Matrix M  ... err = %5.15g\n", err);
#endif
        test_fail_if (err > FAIL_LIMIT);
    } test_end;
}

double
sinc (double arg)
{
    if (arg == 0) return 1;
    else return (sin(arg)/arg);
}

void
test_CG_check (test_t test, double rho)
{
    char test_string[64];
    sprintf (test_string, "rho=%lg\n", rho);
    test_begin (test, test_string)
    {
        double CG_N[21], CG_FC10[21], CG_PR1[21], CG_PR2[21];
        int k;
        for (k=-10; k<=10; k++)
        {
            double kk, arg;
            if (k==0) kk=1.0; else kk = kaiser_d[abs(k)-1];
            arg = M_PI*(k-3072*rho); CG_N[k+10] = kk * sinc(arg);
            arg = M_PI*(k+3072*rho); CG_FC10[k+10] = kk * sinc(arg);
            arg = M_PI*(k+372*rho); CG_PR1[k+10] = kk * sinc(arg);
            arg = M_PI*(k+1140*rho); CG_PR2[k+10] = kk * sinc(arg);
        }
        spoc_CG (rho, -3072, reg_CG_N);
        spoc_CG (rho, 3072, reg_CG_FC10);
        spoc_CG (rho, 372, reg_CG_PR1);
        spoc_CG (rho, 1140, reg_CG_PR2);
        double err_N, err_FC10, err_PR1, err_PR2, error_global;
        error_global = 0;
        for (k=-10; k<=10; k++)
        {
            double error = 0;
            int i = 10+k;
            err_N = err (CG_N[i], reg_CG_N[i]);
            if (error < err_N) error = err_N;
            err_FC10 = err (CG_FC10[i], reg_CG_FC10[i]);
            if (error < err_FC10) error = err_FC10;
            err_PR1 = err (CG_PR1[i], reg_CG_PR1[i]);
            if (error < err_PR1) error = err_PR1;
            err_PR2 = err (CG_PR2[i], reg_CG_PR2[i]);
            if (error < err_PR2) error = err_PR2;
            if (error_global< error) error_global=error;
#if defined (SPOC_TEST_DBG) && (SPOC_TEST_DBG==2)
            printf ("N[%d]    %5.15g<-->%5.15g   %5.15g\n", k, CG_N[i], reg_CG_N[i], err_N);
            printf ("FC10[%d] %5.15g<-->%5.15g   %5.15g\n", k, CG_FC10[i], reg_CG_FC10[i], err_FC10);
            printf ("PR1[%d]  %5.15g<-->%5.15g   %5.15g\n", k, CG_PR1[i], reg_CG_PR1[i], err_PR1);
            printf ("PR2[%d]  %5.15g<-->%5.15g   %5.15g", k, CG_PR2[i], reg_CG_PR2[i], err_PR2);
            if (error > FAIL_LIMIT) printf ("  *\n"); else printf ("\n");
#endif
        }
#if defined (SPOC_TEST_DBG) && (SPOC_TEST_DBG>=1)
            printf ("Vectors [N,FC10,PR1,PR2]  ...err = %5.15g\n", error_global);
#endif
        test_fail_if (error_global > FAIL_LIMIT);
    } test_end;
}

int
main (int argc, char **argv)
{
    test_t test;
    test_init (test, argc, argv);
    int step, rho_eps, max_eps;
    for (step = 1; step<=100000; step = step*10)
    {
        rho_eps = -10 * step;
        max_eps = 10 * step;
        for (; rho_eps<=max_eps; rho_eps+=step)
        {
            double rho = rho_eps * 1e-9;
#if defined (SPOC_TEST_DBG) && (SPOC_TEST_DBG>=1)
            printf ("\nSPOC rho=%g \n", rho);
#endif
            test_case_begin(test, "check Mcoeff");
            test_Mcoeff_check(test, rho);
            test_case_begin(test, "check CGcoeff");
            test_CG_check(test, rho);
        }
    }
    test_result (test);
    return (test_nb_failed (test) == 0 ? 0 : 1);
}