tquant.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*---------------------------------------------------------------------------*\
  2. FILE........: tquant.c
  3. AUTHOR......: David Rowe
  4. DATE CREATED: 22/8/10
  5. Generates quantisation curves for plotting on Octave.
  6. \*---------------------------------------------------------------------------*/
  7. /*
  8. Copyright (C) 2010 David Rowe
  9. All rights reserved.
  10. This program is free software; you can redistribute it and/or modify
  11. it under the terms of the GNU Lesser General Public License version 2, as
  12. published by the Free Software Foundation. This program is
  13. distributed in the hope that it will be useful, but WITHOUT ANY
  14. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  16. License for more details.
  17. You should have received a copy of the GNU Lesser General Public License
  18. along with this program; if not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <assert.h>
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <math.h>
  25. #include "defines.h"
  26. #include "dump.h"
  27. #include "quantise.h"
  28. int test_Wo_quant();
  29. int test_lsp_quant();
  30. int test_lsp(int lsp_number, int levels, float max_error_hz);
  31. int test_energy_quant(int levels, float max_error_dB);
  32. int main() {
  33. quantise_init();
  34. test_Wo_quant();
  35. test_lsp_quant();
  36. test_energy_quant(E_LEVELS, 0.5*(E_MAX_DB - E_MIN_DB)/E_LEVELS);
  37. return 0;
  38. }
  39. int test_lsp_quant() {
  40. test_lsp( 1, 16, 12.5);
  41. test_lsp( 2, 16, 12.5);
  42. test_lsp( 3, 16, 25);
  43. test_lsp( 4, 16, 50);
  44. test_lsp( 5, 16, 50);
  45. test_lsp( 6, 16, 50);
  46. test_lsp( 7, 16, 50);
  47. test_lsp( 8, 8, 50);
  48. test_lsp( 9, 8, 50);
  49. test_lsp(10, 4, 100);
  50. return 0;
  51. }
  52. int test_energy_quant(int levels, float max_error_dB) {
  53. FILE *fe;
  54. float e,e_dec, error, low_e, high_e;
  55. int index, index_in, index_out, i;
  56. /* check 1:1 match between input and output levels */
  57. for(i=0; i<levels; i++) {
  58. index_in = i;
  59. e = decode_energy(index_in);
  60. index_out = encode_energy(e);
  61. if (index_in != index_out) {
  62. printf("edB: %f index_in: %d index_out: %d\n",
  63. 10.0*log10(e), index_in, index_out);
  64. exit(0);
  65. }
  66. }
  67. /* check error over range of quantiser */
  68. low_e = decode_energy(0);
  69. high_e = decode_energy(levels-1);
  70. fe = fopen("energy_err.txt", "wt");
  71. for(e=low_e; e<high_e; e +=(high_e-low_e)/1000.0) {
  72. index = encode_energy(e);
  73. e_dec = decode_energy(index);
  74. error = 10.0*log10(e) - 10.0*log10(e_dec);
  75. fprintf(fe, "%f\n", error);
  76. if (fabs(error) > max_error_dB) {
  77. printf("error: %f %f\n", error, max_error_dB);
  78. exit(0);
  79. }
  80. }
  81. fclose(fe);
  82. return 0;
  83. }
  84. int test_lsp(int lsp_number, int levels, float max_error_hz) {
  85. float lsp[LPC_ORD];
  86. int indexes_in[LPC_ORD];
  87. int indexes_out[LPC_ORD];
  88. int indexes[LPC_ORD];
  89. int i;
  90. float lowf, highf, f, error;
  91. char s[MAX_STR];
  92. FILE *flsp;
  93. float max_error_rads;
  94. lsp_number--;
  95. max_error_rads = max_error_hz*TWO_PI/FS;
  96. for(i=0; i<LPC_ORD; i++)
  97. indexes_in[i] = 0;
  98. for(i=0; i<levels; i++) {
  99. indexes_in[lsp_number] = i;
  100. decode_lsps_scalar(lsp, indexes_in, LPC_ORD);
  101. encode_lsps_scalar(indexes_out, lsp,LPC_ORD);
  102. if (indexes_in[lsp_number] != indexes_out[lsp_number]) {
  103. printf("freq: %f index_in: %d index_out: %d\n",
  104. lsp[lsp_number]+1, indexes_in[lsp_number],
  105. indexes_out[lsp_number]);
  106. exit(0);
  107. }
  108. }
  109. for(i=0; i<LPC_ORD; i++)
  110. indexes[i] = 0;
  111. indexes[lsp_number] = 0;
  112. decode_lsps_scalar(lsp, indexes, LPC_ORD);
  113. lowf = lsp[lsp_number];
  114. indexes[lsp_number] = levels - 1;
  115. decode_lsps_scalar(lsp, indexes, LPC_ORD);
  116. highf = lsp[lsp_number];
  117. sprintf(s,"lsp%d_err.txt", lsp_number+1);
  118. flsp = fopen(s, "wt");
  119. for(f=lowf; f<highf; f +=(highf-lowf)/1000.0) {
  120. lsp[lsp_number] = f;
  121. encode_lsps_scalar(indexes, lsp, LPC_ORD);
  122. decode_lsps_scalar(lsp, indexes, LPC_ORD);
  123. error = f - lsp[lsp_number];
  124. fprintf(flsp, "%f\n", error);
  125. if (fabs(error) > max_error_rads) {
  126. printf("%d error: %f %f\n", lsp_number+1, error, max_error_rads);
  127. exit(0);
  128. }
  129. }
  130. fclose(flsp);
  131. printf("OK\n");
  132. return 0;
  133. }
  134. int test_Wo_quant() {
  135. int c;
  136. FILE *f;
  137. float Wo,Wo_dec, error, step_size;
  138. int index, index_in, index_out;
  139. /* output Wo quant curve for plotting */
  140. f = fopen("quant_pitch.txt","wt");
  141. for(Wo=0.9*(TWO_PI/P_MAX); Wo<=1.1*(TWO_PI/P_MIN); Wo += 0.001) {
  142. index = encode_Wo(Wo);
  143. fprintf(f, "%f %d\n", Wo, index);
  144. }
  145. fclose(f);
  146. /* check for all Wo codes we get 1:1 match between encoder
  147. and decoder Wo levels */
  148. for(c=0; c<WO_LEVELS; c++) {
  149. index_in = c;
  150. Wo = decode_Wo(index_in);
  151. index_out = encode_Wo(Wo);
  152. if (index_in != index_out)
  153. printf(" Wo %f index_in %d index_out %d\n", Wo,
  154. index_in, index_out);
  155. }
  156. /* measure quantisation error stats and compare to expected. Also
  157. plot histogram of error file to check. */
  158. f = fopen("quant_pitch_err.txt","wt");
  159. step_size = ((TWO_PI/P_MIN) - (TWO_PI/P_MAX))/WO_LEVELS;
  160. for(Wo=TWO_PI/P_MAX; Wo<0.99*TWO_PI/P_MIN; Wo += 0.0001) {
  161. index = encode_Wo(Wo);
  162. Wo_dec = decode_Wo(index);
  163. error = Wo - Wo_dec;
  164. if (fabs(error) > (step_size/2.0)) {
  165. printf("error: %f step_size/2: %f\n", error, step_size/2.0);
  166. exit(0);
  167. }
  168. fprintf(f,"%f\n",error);
  169. }
  170. printf("OK\n");
  171. fclose(f);
  172. return 0;
  173. }