vparms.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. $Log$
  3. Revision 1.15 2004/06/26 03:50:14 markster
  4. Merge source cleanups (bug #1911)
  5. Revision 1.14 2003/02/12 13:59:15 matteo
  6. mer feb 12 14:56:57 CET 2003
  7. Revision 1.1.1.1 2003/02/12 13:59:15 matteo
  8. mer feb 12 14:56:57 CET 2003
  9. Revision 1.2 2000/01/05 08:20:40 markster
  10. Some OSS fixes and a few lpc changes to make it actually work
  11. * Revision 1.1 1996/08/19 22:30:04 jaf
  12. * Initial revision
  13. *
  14. */
  15. /* -- translated by f2c (version 19951025).
  16. You must link the resulting object file with the libraries:
  17. -lf2c -lm (in that order)
  18. */
  19. #include "f2c.h"
  20. #ifdef P_R_O_T_O_T_Y_P_E_S
  21. extern int vparms_(integer *vwin, real *inbuf, real *lpbuf, integer *buflim, integer *half, real *dither, integer *mintau, integer *zc, integer *lbe, integer *fbe, real *qs, real *rc1, real *ar_b__, real *ar_f__);
  22. #endif
  23. /* Table of constant values */
  24. static real c_b2 = 1.f;
  25. /* ********************************************************************* */
  26. /* VPARMS Version 50 */
  27. /* $Log$
  28. * Revision 1.15 2004/06/26 03:50:14 markster
  29. * Merge source cleanups (bug #1911)
  30. *
  31. * Revision 1.14 2003/02/12 13:59:15 matteo
  32. * mer feb 12 14:56:57 CET 2003
  33. *
  34. * Revision 1.1.1.1 2003/02/12 13:59:15 matteo
  35. * mer feb 12 14:56:57 CET 2003
  36. *
  37. * Revision 1.2 2000/01/05 08:20:40 markster
  38. * Some OSS fixes and a few lpc changes to make it actually work
  39. *
  40. * Revision 1.1 1996/08/19 22:30:04 jaf
  41. * Initial revision
  42. * */
  43. /* Revision 1.6 1996/03/29 18:01:16 jaf */
  44. /* Added some more comments about the range of INBUF and LPBUF that can */
  45. /* be read. Note that it is possible for index VWIN(2)+1 to be read from */
  46. /* INBUF, which might be outside of its defined range, although that will */
  47. /* require more careful checking. */
  48. /* Revision 1.5 1996/03/19 00:02:02 jaf */
  49. /* I just noticed that the argument DITHER is modified inside of this */
  50. /* subroutine. Comments were added explaining the possible final values. */
  51. /* Revision 1.4 1996/03/18 22:22:59 jaf */
  52. /* Finishing the job I said I did with the last check-in comments. */
  53. /* Revision 1.3 1996/03/18 22:22:17 jaf */
  54. /* Just added a few comments about which array indices of the arguments */
  55. /* are used, and mentioning that this subroutine has no local state. */
  56. /* Revision 1.2 1996/03/13 15:02:58 jaf */
  57. /* Comments added explaining that none of the local variables of this */
  58. /* subroutine need to be saved from one invocation to the next. */
  59. /* Revision 1.1 1996/02/07 14:50:42 jaf */
  60. /* Initial revision */
  61. /* ********************************************************************* */
  62. /* Calculate voicing parameters: */
  63. /* Input: */
  64. /* VWIN - Voicing window limits */
  65. /* Indices 1 through 2 read. */
  66. /* INBUF - Input speech buffer */
  67. /* Indices START-1 through STOP read, */
  68. /* where START and STOP are defined in the code (only written once).
  69. */
  70. /* Note that STOP can be as large as VWIN(2)+1 ! */
  71. /* LPBUF - Low pass filtered speech */
  72. /* Indices START-MINTAU through STOP+MINTAU read, */
  73. /* where START and STOP are defined in the code (only written once).
  74. */
  75. /* BUFLIM - Array bounds for INBUF and LPBUF */
  76. /* Indices 1 through 4 read. */
  77. /* HALF - Half frame (1 or 2) */
  78. /* MINTAU - Lag corresponding to minimum AMDF value (pitch estimate) */
  79. /* Input/Output: */
  80. /* DITHER - Zero crossing threshold */
  81. /* The resulting value might be the negation of the input */
  82. /* value. It might always be the same as the input value, */
  83. /* if the DO loop below always executes an even number of times. */
  84. /* Output: (all of them are written on every call) */
  85. /* ZC - Zero crossing rate */
  86. /* LBE - Low band energy (sum of magnitudes - SM) */
  87. /* FBE - Full band energy (SM) */
  88. /* QS - Ratio of 6 dB/oct preemphasized energy to full band energy */
  89. /* RC1 - First reflection coefficient */
  90. /* AR_B - Product of the causal forward and reverse pitch */
  91. /* prediction gains */
  92. /* AR_F - Product of the noncausal forward and reverse pitch */
  93. /* prediction gains */
  94. /* Internal: */
  95. /* OLDSGN - Previous sign of dithered signal */
  96. /* VLEN - Length of voicing window */
  97. /* START - Lower address of current half of voicing window */
  98. /* STOP - Upper address of current half of voicing window */
  99. /* E_0 - Energy of LPF speech (sum of squares - SS) */
  100. /* E_B - Energy of LPF speech backward one pitch period (SS) */
  101. /* E_F - Energy of LPF speech forward one pitch period (SS) */
  102. /* R_B - Autocovariance of LPF speech backward one pitch period */
  103. /* R_F - Autocovariance of LPF speech forward one pitch period */
  104. /* LP_RMS - Energy of LPF speech (sum of magnitudes - SM) */
  105. /* AP_RMS - Energy of all-pass speech (SM) */
  106. /* E_PRE - Energy of 6dB preemphasized speech (SM) */
  107. /* E0AP - Energy of all-pass speech (SS) */
  108. /* This subroutine has no local state. */
  109. /* Subroutine */ int vparms_(integer *vwin, real *inbuf, real *lpbuf, integer
  110. *buflim, integer *half, real *dither, integer *mintau, integer *zc,
  111. integer *lbe, integer *fbe, real *qs, real *rc1, real *ar_b__, real *
  112. ar_f__)
  113. {
  114. /* System generated locals */
  115. integer inbuf_offset, lpbuf_offset, i__1;
  116. real r__1, r__2;
  117. /* Builtin functions */
  118. double r_sign(real *, real *);
  119. integer i_nint(real *);
  120. /* Local variables */
  121. integer vlen, stop, i__;
  122. real e_pre__;
  123. integer start;
  124. real ap_rms__, e_0__, oldsgn, lp_rms__, e_b__, e_f__, r_b__, r_f__, e0ap;
  125. /* Arguments */
  126. /* Local variables that need not be saved */
  127. /* Calculate zero crossings (ZC) and several energy and correlation */
  128. /* measures on low band and full band speech. Each measure is taken */
  129. /* over either the first or the second half of the voicing window, */
  130. /* depending on the variable HALF. */
  131. /* Parameter adjustments */
  132. --vwin;
  133. --buflim;
  134. lpbuf_offset = buflim[3];
  135. lpbuf -= lpbuf_offset;
  136. inbuf_offset = buflim[1];
  137. inbuf -= inbuf_offset;
  138. /* Function Body */
  139. lp_rms__ = 0.f;
  140. ap_rms__ = 0.f;
  141. e_pre__ = 0.f;
  142. e0ap = 0.f;
  143. *rc1 = 0.f;
  144. e_0__ = 0.f;
  145. e_b__ = 0.f;
  146. e_f__ = 0.f;
  147. r_f__ = 0.f;
  148. r_b__ = 0.f;
  149. *zc = 0;
  150. vlen = vwin[2] - vwin[1] + 1;
  151. start = vwin[1] + (*half - 1) * vlen / 2 + 1;
  152. stop = start + vlen / 2 - 1;
  153. /* I'll use the symbol HVL in the table below to represent the value */
  154. /* VLEN/2. Note that if VLEN is odd, then HVL should be rounded down, */
  155. /* i.e., HVL = (VLEN-1)/2. */
  156. /* HALF START STOP */
  157. /* 1 VWIN(1)+1 VWIN(1)+HVL */
  158. /* 2 VWIN(1)+HVL+1 VWIN(1)+2*HVL */
  159. /* Note that if VLEN is even and HALF is 2, then STOP will be */
  160. /* VWIN(1)+VLEN = VWIN(2)+1. That could be bad, if that index of INBUF */
  161. /* is undefined. */
  162. r__1 = inbuf[start - 1] - *dither;
  163. oldsgn = (real)r_sign(&c_b2, &r__1);
  164. i__1 = stop;
  165. for (i__ = start; i__ <= i__1; ++i__) {
  166. lp_rms__ += (r__1 = lpbuf[i__], abs(r__1));
  167. ap_rms__ += (r__1 = inbuf[i__], abs(r__1));
  168. e_pre__ += (r__1 = inbuf[i__] - inbuf[i__ - 1], abs(r__1));
  169. /* Computing 2nd power */
  170. r__1 = inbuf[i__];
  171. e0ap += r__1 * r__1;
  172. *rc1 += inbuf[i__] * inbuf[i__ - 1];
  173. /* Computing 2nd power */
  174. r__1 = lpbuf[i__];
  175. e_0__ += r__1 * r__1;
  176. /* Computing 2nd power */
  177. r__1 = lpbuf[i__ - *mintau];
  178. e_b__ += r__1 * r__1;
  179. /* Computing 2nd power */
  180. r__1 = lpbuf[i__ + *mintau];
  181. e_f__ += r__1 * r__1;
  182. r_f__ += lpbuf[i__] * lpbuf[i__ + *mintau];
  183. r_b__ += lpbuf[i__] * lpbuf[i__ - *mintau];
  184. r__1 = inbuf[i__] + *dither;
  185. if (r_sign(&c_b2, &r__1) != oldsgn) {
  186. ++(*zc);
  187. oldsgn = -oldsgn;
  188. }
  189. *dither = -(*dither);
  190. }
  191. /* Normalized short-term autocovariance coefficient at unit sample delay
  192. */
  193. *rc1 /= max(e0ap,1.f);
  194. /* Ratio of the energy of the first difference signal (6 dB/oct preemphas
  195. is)*/
  196. /* to the energy of the full band signal */
  197. /* Computing MAX */
  198. r__1 = ap_rms__ * 2.f;
  199. *qs = e_pre__ / max(r__1,1.f);
  200. /* aR_b is the product of the forward and reverse prediction gains, */
  201. /* looking backward in time (the causal case). */
  202. *ar_b__ = r_b__ / max(e_b__,1.f) * (r_b__ / max(e_0__,1.f));
  203. /* aR_f is the same as aR_b, but looking forward in time (non causal case
  204. ).*/
  205. *ar_f__ = r_f__ / max(e_f__,1.f) * (r_f__ / max(e_0__,1.f));
  206. /* Normalize ZC, LBE, and FBE to old fixed window length of 180. */
  207. /* (The fraction 90/VLEN has a range of .58 to 1) */
  208. r__2 = (real) (*zc << 1);
  209. r__1 = r__2 * (90.f / vlen);
  210. *zc = i_nint(&r__1);
  211. /* Computing MIN */
  212. r__1 = lp_rms__ / 4 * (90.f / vlen);
  213. i__1 = i_nint(&r__1);
  214. *lbe = min(i__1,32767);
  215. /* Computing MIN */
  216. r__1 = ap_rms__ / 4 * (90.f / vlen);
  217. i__1 = i_nint(&r__1);
  218. *fbe = min(i__1,32767);
  219. return 0;
  220. } /* vparms_ */