speexlsptest.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*---------------------------------------------------------------------------*\
  2. FILE........: speexlsptest.c
  3. AUTHOR......: David Rowe
  4. DATE CREATED: 24/8/09
  5. Test LPC to LSP conversion and quantisation using Speex LSP quantiser.
  6. \*---------------------------------------------------------------------------*/
  7. /*
  8. Copyright (C) 2009 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 <stdio.h>
  22. #include <stdlib.h>
  23. #include <lpc.h>
  24. #include <lsp.h>
  25. #include <sd.h>
  26. #define N 160
  27. #define P 10
  28. #define LPC_FLOOR 0.0002 /* autocorrelation floor */
  29. #define LSP_DELTA1 0.2 /* grid spacing for LSP root searches */
  30. #define NDFT 256 /* DFT size for SD calculation */
  31. /* Speex lag window */
  32. const float lag_window[11] = {
  33. 1.00000, 0.99716, 0.98869, 0.97474, 0.95554, 0.93140, 0.90273, 0.86998,
  34. 0.83367, 0.79434, 0.75258
  35. };
  36. /*---------------------------------------------------------------------------*\
  37. find_aks_for_lsp()
  38. This function takes a frame of samples, and determines the linear
  39. prediction coefficients for that frame of samples. Modified version of
  40. find_aks from lpc.c to include autocorrelation noise floor and lag window
  41. to match Speex processing steps prior to LSP conversion.
  42. \*---------------------------------------------------------------------------*/
  43. void find_aks_for_lsp(
  44. float Sn[], /* Nsam samples with order sample memory */
  45. float a[], /* order+1 LPCs with first coeff 1.0 */
  46. int Nsam, /* number of input speech samples */
  47. int order, /* order of the LPC analysis */
  48. float *E /* residual energy */
  49. )
  50. {
  51. float Wn[N]; /* windowed frame of Nsam speech samples */
  52. float R[P+1]; /* order+1 autocorrelation values of Sn[] */
  53. int i;
  54. hanning_window(Sn,Wn,Nsam);
  55. autocorrelate(Wn,R,Nsam,order);
  56. R[0] += LPC_FLOOR;
  57. assert(order == 10); /* lag window only defined for order == 10 */
  58. for(i=0; i<=order; i++)
  59. R[i] *= lag_window[i];
  60. levinson_durbin(R,a,order);
  61. *E = 0.0;
  62. for(i=0; i<=order; i++)
  63. *E += a[i]*R[i];
  64. if (*E < 0.0)
  65. *E = 1E-12;
  66. }
  67. /*---------------------------------------------------------------------------*\
  68. MAIN
  69. \*---------------------------------------------------------------------------*/
  70. int main(int argc, char *argv[])
  71. {
  72. FILE *fin; /* input speech files */
  73. short buf[N]; /* buffer of 16 bit speech samples */
  74. float Sn[P+N]; /* input speech samples */
  75. float E;
  76. float ak[P+1]; /* LP coeffs */
  77. float ak_[P+1]; /* quantised LP coeffs */
  78. float lsp[P];
  79. float lsp_[P]; /* quantised LSPs */
  80. int roots; /* number of LSP roots found */
  81. int frames; /* frames processed so far */
  82. int i; /* loop variables */
  83. SpeexBits bits;
  84. float sd; /* SD for this frame */
  85. float totsd; /* accumulated SD so far */
  86. int gt2,gt4; /* number of frames > 2 and 4 dB SD */
  87. int unstables; /* number of unstable LSP frames */
  88. if (argc < 2) {
  89. printf("usage: %s InputFile\n", argv[0]);
  90. exit(0);
  91. }
  92. /* Open files */
  93. if ((fin = fopen(argv[1],"rb")) == NULL) {
  94. printf("Error opening input file: %s\n",argv[1]);
  95. exit(0);
  96. }
  97. /* Initialise */
  98. frames = 0;
  99. for(i=0; i<P; i++) {
  100. Sn[i] = 0.0;
  101. }
  102. ak_[0] = 1.0;
  103. speex_bits_init(&bits);
  104. totsd = 0.0;
  105. unstables = 0;
  106. gt2 = 0; gt4 = 0;
  107. /* Main loop */
  108. while( (fread(buf,sizeof(short),N,fin)) == N) {
  109. frames++;
  110. for(i=0; i<N; i++)
  111. Sn[P+i] = (float)buf[i];
  112. /* convert to LSP domain and back */
  113. find_aks(&Sn[P], ak, N, P, &E);
  114. roots = lpc_to_lsp(&ak[1], P , lsp, 10, LSP_DELTA1, NULL);
  115. if (roots == P) {
  116. speex_bits_reset(&bits);
  117. lsp_quant_lbr(lsp, lsp_, P, &bits);
  118. lsp_to_lpc(lsp_, &ak_[1], P, NULL);
  119. /* measure spectral distortion */
  120. sd = spectral_dist(ak, ak_, P, NDFT);
  121. if (sd > 2.0) gt2++;
  122. if (sd > 4.0) gt4++;
  123. totsd += sd;
  124. }
  125. else
  126. unstables++;
  127. }
  128. fclose(fin);
  129. printf("frames = %d Av sd = %3.2f dB", frames, totsd/frames);
  130. printf(" >2 dB %3.2f%% >4 dB %3.2f%% unstables: %d\n",gt2*100.0/frames,
  131. gt4*100.0/frames, unstables);
  132. return 0;
  133. }