File: tquant.c

package info (click to toggle)
codec2 0.9.2-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 113,072 kB
  • sloc: ansic: 412,877; python: 4,004; sh: 1,540; objc: 817; asm: 683; makefile: 588
file content (214 lines) | stat: -rw-r--r-- 5,572 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*---------------------------------------------------------------------------*\

  FILE........: tquant.c
  AUTHOR......: David Rowe
  DATE CREATED: 22/8/10

  Generates quantisation curves for plotting on Octave.

\*---------------------------------------------------------------------------*/

/*
  Copyright (C) 2010 David Rowe

  All rights reserved.

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU Lesser General Public License version 2.1, as
  published by the Free Software Foundation.  This program is
  distributed in the hope that it will be useful, but WITHOUT ANY
  WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
  License for more details.

  You should have received a copy of the GNU Lesser General Public License
  along with this program; if not, see <http://www.gnu.org/licenses/>.
*/

#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

#include "defines.h"
#include "dump.h"
#include "quantise.h"

int test_Wo_quant();
int test_lsp_quant();
int test_lsp(int lsp_number, int levels, float max_error_hz);
int test_energy_quant(int levels, float max_error_dB);

int main() {
    quantise_init();
    test_Wo_quant();
    test_lsp_quant();
    test_energy_quant(E_LEVELS, 0.5*(E_MAX_DB - E_MIN_DB)/E_LEVELS);

    return 0;
}

int test_lsp_quant() {
    test_lsp( 1, 16,  12.5);
    test_lsp( 2, 16,  12.5);
    test_lsp( 3, 16,  25);
    test_lsp( 4, 16,  50);
    test_lsp( 5, 16,  50);
    test_lsp( 6, 16,  50);
    test_lsp( 7, 16,  50);
    test_lsp( 8,  8,  50);
    test_lsp( 9,  8,  50);
    test_lsp(10,  4, 100);

    return 0;
}

int test_energy_quant(int levels, float max_error_dB) {
    FILE  *fe;
    float  e,e_dec, error, low_e, high_e;
    int    index, index_in, index_out, i;

    /* check 1:1 match between input and output levels */

    for(i=0; i<levels; i++) {
	index_in = i;
	e = decode_energy(index_in, E_BITS);
	index_out = encode_energy(e, E_BITS);
	if (index_in != index_out) {
	    printf("edB: %f index_in: %d index_out: %d\n",
		   10.0*log10(e), index_in, index_out);
	    exit(0);
	}
    }

    /* check error over range of quantiser */

    low_e = decode_energy(0, E_BITS);
    high_e = decode_energy(levels-1, E_BITS);
    fe = fopen("energy_err.txt", "wt");

    for(e=low_e; e<high_e; e +=(high_e-low_e)/1000.0) {
	index = encode_energy(e, E_BITS);
	e_dec = decode_energy(index, E_BITS);
	error = 10.0*log10(e) - 10.0*log10(e_dec);
	fprintf(fe, "%f\n", error);
	if (fabs(error) > max_error_dB) {
	    printf("error: %f %f\n", error, max_error_dB);
	    exit(0);
	}
    }

    fclose(fe);
    return 0;
}

int test_lsp(int lsp_number, int levels, float max_error_hz) {
    float lsp[LPC_ORD];
    int   indexes_in[LPC_ORD];
    int   indexes_out[LPC_ORD];
    int   indexes[LPC_ORD];
    int   i;
    float lowf, highf, f, error;
    char  s[MAX_STR];
    FILE *flsp;
    float max_error_rads;

    lsp_number--;
    max_error_rads = max_error_hz*TWO_PI/FS;

    for(i=0; i<LPC_ORD; i++)
	indexes_in[i] = 0;

    for(i=0; i<levels; i++) {
	indexes_in[lsp_number] = i;
	decode_lsps_scalar(lsp, indexes_in, LPC_ORD);
	encode_lsps_scalar(indexes_out, lsp,LPC_ORD);
	if (indexes_in[lsp_number] != indexes_out[lsp_number]) {
	    printf("freq: %f index_in: %d index_out: %d\n",
		   lsp[lsp_number]+1, indexes_in[lsp_number],
		   indexes_out[lsp_number]);
	    exit(0);
	}
    }

    for(i=0; i<LPC_ORD; i++)
	indexes[i] = 0;
    indexes[lsp_number] = 0;
    decode_lsps_scalar(lsp, indexes, LPC_ORD);
    lowf = lsp[lsp_number];
    indexes[lsp_number] = levels - 1;
    decode_lsps_scalar(lsp, indexes, LPC_ORD);
    highf = lsp[lsp_number];
    sprintf(s,"lsp%d_err.txt", lsp_number+1);
    flsp = fopen(s, "wt");

    for(f=lowf; f<highf; f +=(highf-lowf)/1000.0) {
	lsp[lsp_number] = f;
	encode_lsps_scalar(indexes, lsp, LPC_ORD);
	decode_lsps_scalar(lsp, indexes, LPC_ORD);
	error = f - lsp[lsp_number];
	fprintf(flsp, "%f\n", error);
	if (fabs(error) > max_error_rads) {
	    printf("%d error: %f %f\n", lsp_number+1, error, max_error_rads);
	    exit(0);
	}
    }

    fclose(flsp);

    printf("OK\n");

    return 0;
}

int test_Wo_quant() {
    int    c;
    FILE  *f;
    float  Wo,Wo_dec, error, step_size;
    int    index, index_in, index_out;

    /* output Wo quant curve for plotting */

    f = fopen("quant_pitch.txt","wt");

    for(Wo=0.9*(TWO_PI/P_MAX); Wo<=1.1*(TWO_PI/P_MIN); Wo += 0.001) {
	index = encode_Wo(Wo, WO_BITS);
	fprintf(f, "%f %d\n", Wo, index);
    }

    fclose(f);

    /* check for all Wo codes we get 1:1 match between encoder
       and decoder Wo levels */

    for(c=0; c<WO_LEVELS; c++) {
	index_in = c;
	Wo = decode_Wo(index_in, WO_BITS);
        index_out = encode_Wo(Wo, WO_BITS);
	if (index_in != index_out)
	    printf("  Wo %f index_in %d index_out %d\n", Wo,
		   index_in, index_out);
    }

    /* measure quantisation error stats and compare to expected.  Also
       plot histogram of error file to check. */

    f = fopen("quant_pitch_err.txt","wt");
    step_size = ((TWO_PI/P_MIN) - (TWO_PI/P_MAX))/WO_LEVELS;

    for(Wo=TWO_PI/P_MAX; Wo<0.99*TWO_PI/P_MIN; Wo += 0.0001) {
	index = encode_Wo(Wo, WO_BITS);
	Wo_dec = decode_Wo(index, WO_BITS);
	error = Wo - Wo_dec;
	if (fabs(error) > (step_size/2.0)) {
	    printf("error: %f  step_size/2: %f\n", error, step_size/2.0);
	    exit(0);
	}
	fprintf(f,"%f\n",error);
    }
    printf("OK\n");

    fclose(f);
    return 0;
}