fs_lib.c
上传用户:luckfish
上传日期:2021-12-16
资源大小:77k
文件大小:6k
源码类别:

语音压缩

开发平台:

Visual C++

  1. /*
  2. 2.4 kbps MELP Proposed Federal Standard speech coder
  3. version 1.2
  4. Copyright (c) 1996, Texas Instruments, Inc.  
  5. Texas Instruments has intellectual property rights on the MELP
  6. algorithm.  The Texas Instruments contact for licensing issues for
  7. commercial and non-government use is William Gordon, Director,
  8. Government Contracts, Texas Instruments Incorporated, Semiconductor
  9. Group (phone 972 480 7442).
  10. */
  11. /*
  12.   fs_lib.c: Fourier series subroutines 
  13. */
  14. /*  compiler include files  */
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <math.h>
  18. #include "spbstd.h"
  19. #include "mat.h"
  20. #include "fs.h"
  21. /*  compiler constants */
  22. #define PRINT 1
  23. /* */
  24. /* Subroutine FIND_HARM: find Fourier coefficients using */
  25. /* FFT of input signal divided into pitch dependent bins. */
  26. /* */
  27. #define FFTLENGTH 512
  28. /* Memory definition */
  29. static float find_hbuf[2*FFTLENGTH];
  30. static float mag[FFTLENGTH];
  31. void find_harm(float input[], float fsmag[], float pitch, int num_harm, 
  32.        int length)
  33. {
  34.     int i, j, k, iwidth, i2;
  35.     float temp, avg, fwidth;
  36.     for (i = 0; i < num_harm; i++)
  37.       fsmag[i] = 1.0;
  38.     avg = 0.0;
  39.     /* Perform peak-picking on FFT of input signal */
  40.     /* Calculate FFT of complex signal in scratch buffer */
  41.     v_zap(find_hbuf,2*FFTLENGTH);
  42.     for (i = 0; i < 2*length; i+=2)
  43. find_hbuf[i] = input[i/2];
  44.     fft(find_hbuf,FFTLENGTH,-1);
  45.     /* Calculate magnitude squared of coefficients */
  46.     for (i = 0; i < FFTLENGTH; i++ )
  47. mag[i] = find_hbuf[2*i]*find_hbuf[2*i] +
  48.     find_hbuf[(2*i)+1]*find_hbuf[(2*i)+1];
  49.     /* Implement pitch dependent staircase function */
  50.     fwidth = FFTLENGTH / pitch; /* Harmonic bin width */
  51.     iwidth = (int) fwidth;
  52.     if (iwidth < 2)
  53. iwidth = 2;
  54.     i2 = iwidth/2;
  55.     avg = 0.0;
  56.     if (num_harm > 0.25*pitch)
  57. num_harm = 0.25*pitch;
  58.     for (k = 0; k < num_harm; k++) {
  59. i = ((k+1)*fwidth) - i2 + 0.5; /* Start at peak-i2 */
  60. j = i + findmax(&mag[i],iwidth);
  61. fsmag[k] = mag[j];
  62. avg += mag[j];
  63.     }
  64.     /* Normalize Fourier series values to average magnitude */
  65.     temp = num_harm/(avg+ .0001);
  66.     for (i = 0; i < num_harm; i++) {
  67. fsmag[i] = sqrt(temp*fsmag[i]);
  68.     }
  69. }
  70. /* Subroutine FFT: Fast Fourier Transform  */
  71. /**************************************************************
  72. * Replaces data by its DFT, if isign is 1, or replaces data   *
  73. * by inverse DFT times nn if isign is -1.  data is a complex  *
  74. * array of length nn, input as a real array of length 2*nn.   *
  75. * nn MUST be an integer power of two.  This is not checked    *
  76. * The real part of the number should be in the zeroeth        *
  77. * of data , and the imaginary part should be in the next      *
  78. * element.  Hence all the real parts should have even indeces *
  79. * and the imaginary parts, odd indeces.       *
  80. * Data is passed in an array starting in position 0, but the  *
  81. * code is copied from Fortran so uses an internal pointer     *
  82. * which accesses position 0 as position 1, etc.       *
  83. * This code uses e+jwt sign convention, so isign should be    *
  84. * reversed for e-jwt.                                         *
  85. ***************************************************************/
  86. #define SWAP(a,b) tempr = (a);(a) = (b); (b) = tempr
  87. void fft(float *datam1,int nn,int isign)
  88. {
  89. int n,mmax,m,j,istep,i;
  90. double wtemp,wr,wpr,wpi,wi,theta;
  91. float register tempr,tempi;
  92. float *data;
  93. /*  Use pointer indexed from 1 instead of 0 */
  94. data = &datam1[-1];
  95. n = nn << 1;
  96. j = 1;
  97. for( i = 1; i < n; i+=2 ) {
  98.   if ( j > i) {
  99. SWAP(data[j],data[i]);
  100. SWAP(data[j+1],data[i+1]);
  101.   }
  102.   m = n >> 1;
  103.   while ( m >= 2 && j > m ) {
  104. j -= m;
  105. m >>= 1;
  106.   }
  107.   j += m;
  108. }
  109. mmax = 2;
  110. while ( n > mmax) {
  111.   istep = 2 * mmax;
  112.   theta = 6.28318530717959/(isign*mmax);
  113.   wtemp = sin(0.5*theta);
  114.   wpr   = -2.0*wtemp*wtemp;
  115.   wpi   = sin(theta);
  116.   wr = 1.0;
  117.   wi = 0.0;
  118.   for ( m = 1; m < mmax;m+=2) {
  119.     for ( i = m; i <= n; i += istep) {
  120.            j = i + mmax;
  121. tempr = wr * data[j] - wi * data[j+1];
  122.        tempi = wr * data[j+1] + wi * data[j];
  123.     data[j] = data[i] - tempr;
  124. data[j+1] = data[i+1] - tempi;
  125. data[i] += tempr;
  126. data[i+1] += tempi;
  127.     }
  128.     wr = (wtemp=wr)*wpr-wi*wpi+wr;
  129.     wi = wi*wpr+wtemp*wpi+wi;
  130.   }
  131.   mmax = istep;
  132. }
  133. }
  134. /* */
  135. /* Subroutine FINDMAX: find maximum value in an  */
  136. /* input array. */
  137. /* */
  138. int  findmax(float input[], int npts)
  139. {
  140. int register i, maxloc;
  141. float register  maxval, *p_in;
  142. p_in = &input[0];
  143. maxloc = 0;
  144. maxval = input[maxloc];
  145. for (i = 1; i < npts; i++ ) {
  146. if (*(++p_in) > maxval) {
  147. maxloc = i;
  148. maxval = *p_in;
  149. }
  150. }
  151. return(maxloc);
  152. }
  153. /* */
  154. /* Subroutine IDFT_REAL: take inverse discrete Fourier  */
  155. /* transform of real input coefficients. */
  156. /* Assume real time signal, so reduce computation */
  157. /* using symmetry between lower and upper DFT */
  158. /* coefficients. */
  159. /* */
  160. #define DFTMAX 160
  161. /* Memory definition */
  162. static float idftc[DFTMAX];
  163. void idft_real(float real[], float signal[], int length)
  164. {
  165.     int i, j, k, k_inc, length2;
  166.     float w;
  167. #if (PRINT)
  168.     if (length > DFTMAX) {
  169. printf("****ERROR: IDFT size too large **** n");
  170. exit(1);
  171.     }
  172. #endif
  173.     
  174.     length2 = (length/2)+1;
  175.     w = TWOPI / length;
  176.     for (i = 0; i < length; i++ ) {
  177. idftc[i] = cos(w*i);
  178.     }
  179.     real[0] *= (1.0/length);
  180.     for (i = 1; i < length2-1; i++ ) {
  181. real[i] *= (2.0/length);
  182.     }
  183.     if ((i*2) == length)
  184. real[i] *= (1.0/length);
  185.     else
  186. real[i] *= (2.0/length);
  187.     for (i = 0; i < length; i++ ) {
  188. signal[i] = real[0];
  189. k_inc = i;
  190. k = k_inc;
  191. for (j = 1; j < length2; j++ ) {
  192.     signal[i] += real[j] * idftc[k];
  193.     k += k_inc;
  194.     if (k >= length)
  195. k -= length;
  196. }
  197.     }
  198. }