2
0

genlsp.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*--------------------------------------------------------------------------*\
  2. FILE........: genlsp.c
  3. AUTHOR......: David Rowe
  4. DATE CREATED: 23/2/95
  5. This program genrates a text file of LSP vectors from an input
  6. speech file.
  7. \*--------------------------------------------------------------------------*/
  8. /*
  9. Copyright (C) 2009 David Rowe
  10. All rights reserved.
  11. This program is free software; you can redistribute it and/or modify
  12. it under the terms of the GNU Lesser General Public License version 2, as
  13. published by the Free Software Foundation. This program is
  14. distributed in the hope that it will be useful, but WITHOUT ANY
  15. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  16. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  17. License for more details.
  18. You should have received a copy of the GNU Lesser General Public License
  19. along with this program; if not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #define P 12 /* LP order */
  22. #define LSP_DELTA1 0.01 /* grid spacing for LSP root searches */
  23. #define NW 279 /* frame size in samples */
  24. #define N 80 /* frame to frame shift */
  25. #define THRESH 40.0 /* threshold energy/sample for frame inclusion */
  26. #define PI 3.141592654 /* mathematical constant */
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <math.h>
  31. #include "lpc.h" /* LPC analysis functions */
  32. #include "lsp.h" /* LSP encode/decode functions */
  33. int switch_present(sw,argc,argv)
  34. char sw[]; /* switch in string form */
  35. int argc; /* number of command line arguments */
  36. char *argv[]; /* array of command line arguments in string form */
  37. {
  38. int i; /* loop variable */
  39. for(i=1; i<argc; i++)
  40. if (!strcmp(sw,argv[i]))
  41. return(i);
  42. return 0;
  43. }
  44. int main(int argc, char *argv[]) {
  45. FILE *fspc; /* input file ptr for test database */
  46. FILE *flsp; /* output text file of LSPs */
  47. short buf[N]; /* input frame of speech samples */
  48. float Sn[NW]; /* float input speech samples */
  49. float ak[P+1]; /* LPCs for current frame */
  50. float lsp[P]; /* LSPs for current frame */
  51. float lsp_prev[P]; /* LSPs for previous frame */
  52. float E; /* frame energy */
  53. long f; /* number of frames */
  54. long af; /* number frames with "active" speech */
  55. float Eres; /* LPC residual energy */
  56. int i;
  57. int roots;
  58. int unstables;
  59. int lspd, log, lspdt;
  60. float diff;
  61. /* Initialise ------------------------------------------------------*/
  62. if (argc < 3) {
  63. printf("usage: %s RawFile LSPTextFile [--lspd] [--log] [--lspdt] \n", argv[0]);
  64. exit(1);
  65. }
  66. /* Open files */
  67. fspc = fopen(argv[1],"rb");
  68. if (fspc == NULL) {
  69. printf("Error opening input SPC file: %s",argv[1]);
  70. exit(1);
  71. }
  72. flsp = fopen(argv[2],"wt");
  73. if (flsp == NULL) {
  74. printf("Error opening output LSP file: %s",argv[2]);
  75. exit(1);
  76. }
  77. lspd = switch_present("--lspd", argc, argv);
  78. log = switch_present("--log", argc, argv);
  79. lspdt = switch_present("--lspdt", argc, argv);
  80. for(i=0; i<NW; i++)
  81. Sn[i] = 0.0;
  82. /* Read SPC file, and determine aks[] for each frame ------------------*/
  83. f = af = 0;
  84. unstables = 0;
  85. while(fread(buf,sizeof(short),N,fspc) == N) {
  86. for(i=0; i<NW-N; i++)
  87. Sn[i] = Sn[i+N];
  88. E = 0.0;
  89. for(i=0; i<N; i++) {
  90. Sn[i+NW-N] = buf[i];
  91. E += Sn[i]*Sn[i];
  92. }
  93. E = 0.0;
  94. for(i=0; i<NW; i++) {
  95. E += Sn[i]*Sn[i];
  96. }
  97. E = 10.0*log10(E/NW);
  98. /* If energy high enough, include this frame */
  99. f++;
  100. if (E > THRESH) {
  101. af++;
  102. printf("Active Frame: %ld unstables: %d\n",af, unstables);
  103. find_aks(Sn, ak, NW, P, &Eres);
  104. roots = lpc_to_lsp(ak, P , lsp, 5, LSP_DELTA1);
  105. if (roots == P) {
  106. if (lspd) {
  107. if (log) {
  108. fprintf(flsp,"%f ",log10(lsp[0]));
  109. for(i=1; i<P; i++) {
  110. diff = lsp[i]-lsp[i-1];
  111. if (diff < (PI/4000.0)*25.0) diff = (PI/4000.0)*25.0;
  112. fprintf(flsp,"%f ",log10(diff));
  113. }
  114. }
  115. else {
  116. fprintf(flsp,"%f ",lsp[0]);
  117. for(i=1; i<P; i++)
  118. fprintf(flsp,"%f ",lsp[i]-lsp[i-1]);
  119. }
  120. fprintf(flsp,"\n");
  121. }
  122. else if (lspdt) {
  123. for(i=0; i<P; i++)
  124. fprintf(flsp,"%f ",lsp[i]-lsp_prev[i]);
  125. fprintf(flsp,"\n");
  126. }
  127. else {
  128. if (log) {
  129. for(i=0; i<P; i++)
  130. fprintf(flsp,"%f ",log10(lsp[i]));
  131. fprintf(flsp,"\n");
  132. }
  133. else {
  134. for(i=0; i<P; i++)
  135. fprintf(flsp,"%f ",lsp[i]);
  136. fprintf(flsp,"\n");
  137. }
  138. }
  139. memcpy(lsp_prev, lsp, sizeof(lsp));
  140. }
  141. else
  142. unstables++;
  143. }
  144. }
  145. printf("%3.2f %% active frames\n", 100.0*(float)af/f);
  146. fclose(fspc);
  147. fclose(flsp);
  148. return 0;
  149. }