lsptest.c 5.4 KB

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