aboutsummaryrefslogtreecommitdiffstats
path: root/test/ut.c
blob: f099b70e5cf7901fc4eeb7ab1ba643f1fb22f2af (plain) (blame)
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
#include <3rd/fctx/fct.h>

#include "ut_dca_pr.h"
#include "ut_dca_pr_long.h"

#include "libpqf.h"

#include <stdio.h>
#include <tgmath.h>

////////////////////////////////////////////////////////////////////////////////

static float* create_chirp(int sz)
{
    int i = 0;
    float* buf = malloc(sizeof(float) * sz);
    static int done = 0;
    for(i = 0; i < sz; i++) {
        float t = i;
	buf[i] = sinf((t + t * t * 0.5 / 2.0) * 2.0 * M_PI/(float)sz);
    }
    return buf;
}

#define check_reconstruction(a, b, sz) \
    do { \
        uint16_t i = 0; \
        int scale = 1 << 18; \
        for (i = 0; i < sz; i ++) { \
            fct_chk((abs((int)round((a)[i] * scale) - (int)round((b)[i] * scale))) < 2); \
        } \
    } while (0) \

#define PQF_TEST_COVER(module) \
    FCT_SUITE_BGN(module) \
    { \
	uint16_t sb_sz = module.get_subband_sz(); \
	uint16_t sb_num = module.get_subbands_num(); \
        uint16_t proto_sz = module.get_proto_sz(); \
        const float* proto = module.get_proto(); \
	const float* dummy = calloc(sb_sz * sb_num, sizeof(float)); \
        FCT_TEST_BGN(module-IMP) \
        { \
		int k;\
            pqf_a_ctx_t ctx; \
            int status = pqf_create_a_ctx(sb_sz, sb_num, proto_sz, proto, &ctx); \
            fct_chk_eq_int(status, PQF_SUCCESS); \
	    float* in_buf1 = calloc(sb_sz * sb_num, sizeof(float)); \
            in_buf1[500] = 1.0; \
            float* out_buf = malloc(sizeof(float) * sb_sz * sb_num); \
            float* synth_buf = malloc(sizeof(float) * sb_sz * sb_num * 2); \
	    void* dec_ctx = module.create_ctx(); \
            pqf_do_analyse(ctx, in_buf1, out_buf); \
	    module.synth_filter(out_buf, synth_buf, dec_ctx); \
            pqf_do_analyse(ctx, dummy, out_buf); \
	    module.synth_filter(out_buf, synth_buf + sb_sz * sb_num, dec_ctx); \
\
            check_reconstruction(in_buf1, synth_buf + proto_sz, sb_sz * sb_num); \
\
            module.free_ctx(dec_ctx); \
	    free(synth_buf); \
            free(out_buf); \
            free(in_buf1); \
        } \
        FCT_TEST_END(); \
        FCT_TEST_BGN(module-DC) \
        { \
		int k; \
            pqf_a_ctx_t ctx; \
            uint16_t i = 0; \
            int status = pqf_create_a_ctx(sb_sz, sb_num, proto_sz, proto, &ctx); \
            fct_chk_eq_int(status, PQF_SUCCESS); \
	    float* in_buf1 = calloc(sb_sz * sb_num, sizeof(float)); \
	    for (i = 0; i < sb_sz * sb_num; i++) \
                in_buf1[i] = 1.0; \
            float* out_buf = malloc(sizeof(float) * sb_sz * sb_num); \
            float* synth_buf = malloc(sizeof(float) * sb_sz * sb_num * 2); \
	    void* dec_ctx = module.create_ctx(); \
            pqf_do_analyse(ctx, in_buf1, out_buf); \
	    module.synth_filter(out_buf, synth_buf, dec_ctx); \
            pqf_do_analyse(ctx, in_buf1, out_buf); \
	    module.synth_filter(out_buf, synth_buf + sb_sz * sb_num, dec_ctx); \
\
            check_reconstruction(in_buf1, synth_buf + proto_sz, sb_sz * sb_num); \
\
            module.free_ctx(dec_ctx); \
	    free(synth_buf); \
            free(out_buf); \
            free(in_buf1); \
        } \
        FCT_TEST_END(); \
        FCT_TEST_BGN(module-CHIRP-SHORT) \
        { \
            pqf_a_ctx_t ctx; \
            int status = pqf_create_a_ctx(sb_sz, sb_num, proto_sz, proto, &ctx); \
            uint16_t i = 0; \
            float* chirp = create_chirp(sb_sz * sb_num); \
            float* out_buf = malloc(sizeof(float) * sb_sz * sb_num); \
            float* synth_buf = malloc(sizeof(float) * sb_sz * sb_num * 2); \
            fct_chk_eq_int(status, PQF_SUCCESS); \
	    void* dec_ctx = module.create_ctx(); \
            pqf_do_analyse(ctx, chirp, out_buf); \
	    module.synth_filter(out_buf, synth_buf, dec_ctx); \
            pqf_do_analyse(ctx, dummy, out_buf); \
	    module.synth_filter(out_buf, synth_buf + sb_sz * sb_num, dec_ctx); \
\
            check_reconstruction(chirp, synth_buf + proto_sz, sb_sz * sb_num); \
\
            module.free_ctx(dec_ctx); \
	    free(synth_buf); \
            free(out_buf); \
            free(chirp); \
        } \
        FCT_TEST_END(); \
        FCT_TEST_BGN(module-CHIRP-LONG) \
        { \
            pqf_a_ctx_t ctx; \
            int status = pqf_create_a_ctx(sb_sz, sb_num, proto_sz, proto, &ctx); \
            uint16_t i = 0; \
            float* chirp = create_chirp(16384); \
            float* out_buf = malloc(sizeof(float) * sb_sz * sb_num); \
            float* synth_buf = malloc(sizeof(float) * 16384 + sb_sz * sb_num); \
            fct_chk_eq_int(status, PQF_SUCCESS); \
	    void* dec_ctx = module.create_ctx(); \
            pqf_do_analyse(ctx, chirp, out_buf); \
	    module.synth_filter(out_buf, synth_buf, dec_ctx); \
            for (i = sb_sz * sb_num; i < 16384; i += sb_sz * sb_num) { \
                pqf_do_analyse(ctx, chirp + i, out_buf); \
                module.synth_filter(out_buf, synth_buf + i, dec_ctx); \
            } \
            pqf_do_analyse(ctx, dummy, out_buf); \
	    module.synth_filter(out_buf, synth_buf + 16384, dec_ctx); \
\
            check_reconstruction(chirp, synth_buf + proto_sz, 16384); \
\
            module.free_ctx(dec_ctx); \
	    free(synth_buf); \
            free(out_buf); \
            free(chirp); \
        } \
        FCT_TEST_END(); \
        free((void*)dummy); \
    } \
    FCT_SUITE_END(); \


////////////////////////////////////////////////////////////////////////////////

FCT_BGN()
{
    PQF_TEST_COVER(ut_dca_pr)
    PQF_TEST_COVER(ut_dca_pr_long)
}
FCT_END();