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

语音压缩

开发平台:

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.   dsp_sub.c: general subroutines.
  13. */
  14. /*  compiler include files  */
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <math.h>
  18. #include "dsp_sub.h"
  19. #include "spbstd.h"
  20. #include "mat.h"
  21. typedef short SPEECH;
  22. #define PRINT 1 
  23. /* */
  24. /* Subroutine autocorr: calculate autocorrelations         */
  25. /* */
  26. void autocorr(float input[], float r[], int order, int npts)
  27. {
  28.     int i;
  29.     for (i = 0; i <= order; i++ )
  30.       r[i] = v_inner(&input[0],&input[i],(npts-i));
  31.     if (r[0] < 1.0)
  32.       r[0] = 1.0;
  33. }
  34. /* */
  35. /* Subroutine envelope: calculate time envelope of signal. */
  36. /*      Note: the delay history requires one previous sample    */
  37. /*      of the input signal and two previous output samples.    */
  38. /* */
  39. #define C2 (-0.9409)
  40. #define C1 1.9266
  41. void envelope(float input[], float prev_in, float output[], int npts)
  42. {
  43.     int i;
  44.     float curr_abs, prev_abs;
  45.     prev_abs = fabs(prev_in);
  46.     for (i = 0; i < npts; i++) {
  47. curr_abs = fabs(input[i]);
  48. output[i] = curr_abs - prev_abs + C2*output[i-2] + C1*output[i-1];
  49. prev_abs = curr_abs;
  50.     }
  51. }
  52. /* */
  53. /*  Subroutine fill: fill an input array with a value. */
  54. /* */
  55. void fill(float output[], float fillval, int npts)
  56. {
  57.   int i;
  58.   for (i = 0; i < npts; i++ )
  59.     output[i] = fillval;
  60. }
  61. /* */
  62. /* Subroutine interp_array: interpolate array              */
  63. /*                                                              */
  64. void interp_array(float prev[],float curr[],float out[],float ifact,int size)
  65. {
  66.     int i;
  67.     float ifact2;
  68.     ifact2 = 1.0 - ifact;
  69.     for (i = 0; i < size; i++)
  70.       out[i] = ifact*curr[i] + ifact2*prev[i];
  71.       
  72. }
  73. /* */
  74. /* Subroutine median: calculate median value               */
  75. /* */
  76. #define MAXSORT 5
  77. float median(float input[], int npts)
  78. {
  79.     int i,j,loc;
  80.     float insert_val;
  81.     float sorted[MAXSORT];
  82.     /* sort data in temporary array */
  83. #ifdef PRINT
  84.     if (npts > MAXSORT) {
  85. printf("ERROR: median size too large.n");
  86. exit(1);
  87.     }
  88. #endif
  89.     v_equ(sorted,input,npts);
  90.     for (i = 1; i < npts; i++) {
  91. /* for each data point */
  92. for (j = 0; j < i; j++) {
  93.     /* find location in current sorted list */
  94.     if (sorted[i] < sorted[j])
  95.       break;
  96. }
  97. /* insert new value */
  98. loc = j;
  99. insert_val = sorted[i];
  100. for (j = i; j > loc; j--)
  101.   sorted[j] = sorted[j-1];
  102. sorted[loc] = insert_val;
  103.     }
  104.     return(sorted[npts/2]);
  105. }
  106. #undef MAXSORT
  107. /* */
  108. /* Subroutine PACK_CODE: Pack bit code into channel. */
  109. /* */
  110. void pack_code(int code,unsigned int **p_ch_beg,int *p_ch_bit, int numbits, int wsize)
  111. {
  112.     int i,ch_bit;
  113.     unsigned int *ch_word;
  114. ch_bit = *p_ch_bit;
  115. ch_word = *p_ch_beg;
  116. for (i = 0; i < numbits; i++) {
  117. /* Mask in bit from code to channel word */
  118. if (ch_bit == 0)
  119.   *ch_word = ((code & (1<<i)) >> i);
  120. else
  121.   *ch_word |= (((code & (1<<i)) >> i) << ch_bit);
  122. /* Check for full channel word */
  123. if (++ch_bit >= wsize) {
  124. ch_bit = 0;
  125. (*p_ch_beg)++ ;
  126. ch_word++ ;
  127. }
  128. }
  129. /* Save updated bit counter */
  130. *p_ch_bit = ch_bit;
  131. }
  132. /* */
  133. /* Subroutine peakiness: estimate peakiness of input       */
  134. /*      signal using ratio of L2 to L1 norms.                   */
  135. /* */
  136. float peakiness(float input[], int npts)
  137. {
  138.     int i;
  139.     float sum_abs, peak_fact;
  140.     sum_abs = 0.0;
  141.     for (i = 0; i < npts; i++)
  142.       sum_abs += fabs(input[i]);
  143.     if (sum_abs > 0.01)
  144.       peak_fact = sqrt(npts*v_magsq(input,npts)) / sum_abs;
  145.     else
  146.       peak_fact = 0.0;
  147.     return(peak_fact);
  148. }
  149. /* */
  150. /* Subroutine polflt: all pole (IIR) filter. */
  151. /* Note: The filter coefficients represent the */
  152. /* denominator only, and the leading coefficient */
  153. /* is assumed to be 1. */
  154. /*      The output array can overlay the input.                 */
  155. /* */
  156. void polflt(float input[], float coeff[], float output[], int order,int npts)
  157. {
  158.     int i,j;
  159.     float accum;
  160.     
  161.     for (i = 0; i < npts; i++ ) {
  162. accum = input[i];
  163. for (j = 1; j <= order; j++ )
  164.     accum -= output[i-j] * coeff[j];
  165. output[i] = accum;
  166.     }
  167. }
  168. /* */
  169. /* Subroutine QUANT_U: quantize positive input value with  */
  170. /* symmetrical uniform quantizer over given positive */
  171. /* input range. */
  172. /* */
  173. void quant_u(float *p_data, int *p_index, float qmin, float qmax, int nlev)
  174. {
  175. register int i, j;
  176. register float step, qbnd, *p_in;
  177. p_in = p_data;
  178. /*  Define symmetrical quantizer stepsize */
  179. step = (qmax - qmin) / (nlev - 1);
  180. /*  Search quantizer boundaries */
  181. qbnd = qmin + (0.5 * step);
  182. j = nlev - 1;
  183. for (i = 0; i < j; i ++ ) {
  184. if (*p_in < qbnd)
  185. break;
  186. else
  187. qbnd += step;
  188. }
  189. /*  Quantize input to correct level */
  190. *p_in = qmin + (i * step);
  191. *p_index = i;
  192. }
  193. /* */
  194. /* Subroutine QUANT_U_DEC: decode uniformly quantized */
  195. /* value. */
  196. /* */
  197. void quant_u_dec(int index, float *p_data,float qmin, float qmax, int nlev)
  198. {
  199. register float step;
  200. /*  Define symmetrical quantizer stepsize */
  201. step = (qmax - qmin) / (nlev - 1);
  202. /*  Decode quantized level */
  203. *p_data = qmin + (index * step);
  204. }
  205. /* */
  206. /* Subroutine rand_num: generate random numbers to fill    */
  207. /*      array using system random number generator.             */
  208. /*                                                              */
  209. void rand_num(float output[], float amplitude, int npts)
  210. {
  211.     int i;
  212.     for (i = 0; i < npts; i++ ) {
  213. /* use system random number generator from -1 to +1 */
  214. #ifdef RAND_MAX
  215. /* ANSI C environment */
  216. output[i] = (amplitude*2.0) * ((float) rand()*(1.0/RAND_MAX) - 0.5);
  217. #else
  218. /* assume Sun OS4 */
  219. output[i] = amplitude * (float) (((random() >> 16)/32767. - .5)*2);
  220. #endif
  221.     }
  222. }
  223. /* */
  224. /* Subroutine READBL: read block of input data */
  225. /* */
  226. #define MAXSIZE 1024
  227. int readbl(float input[], FILE *fp_in, int size)
  228. {
  229.     int i, length;
  230.     SPEECH int_sp[MAXSIZE]; /*  integer input array */
  231. #ifdef PRINT
  232.     if (size > MAXSIZE) {
  233. printf("****ERROR: read block size too large **** n");
  234. exit(1);
  235.     }
  236. #endif
  237.     
  238.     length = fread(int_sp,sizeof(SPEECH),size,fp_in);
  239.     for (i = 0; i < length; i++ )
  240.       input[i] = int_sp[i];
  241.     for (i = length; i < size; i++ )
  242.       input[i] = 0.0;
  243.     return length;
  244. }
  245. #undef MAXSIZE
  246. /* */
  247. /* Subroutine UNPACK_CODE: Unpack bit code from channel. */
  248. /*      Return 1 if erasure, otherwise 0.                       */
  249. /* */
  250. int unpack_code(unsigned int **p_ch_beg, int *p_ch_bit, int *p_code, int numbits, int wsize, unsigned int ERASE_MASK)
  251. {
  252.     int ret_code;
  253.     int i,ch_bit;
  254.     unsigned int *ch_word;
  255. ch_bit = *p_ch_bit;
  256. ch_word = *p_ch_beg;
  257. *p_code = 0;
  258.         ret_code = *ch_word & ERASE_MASK;    
  259. for (i = 0; i < numbits; i++) {
  260. /* Mask in bit from channel word to code */
  261. *p_code |= (((*ch_word & (1<<ch_bit)) >> ch_bit) << i);
  262. /* Check for end of channel word */
  263. if (++ch_bit >= wsize) {
  264. ch_bit = 0;
  265. (*p_ch_beg)++ ;
  266. ch_word++ ;
  267. }
  268. }
  269. /*  Save updated bit counter */
  270. *p_ch_bit = ch_bit;
  271.     /* Catch erasure in new word if read */
  272.     if (ch_bit != 0)
  273.       ret_code |= *ch_word & ERASE_MASK;    
  274.     return(ret_code);
  275. }
  276. /* */
  277. /* Subroutine window: multiply signal by window            */
  278. /* */
  279. void window(float input[], float win_cof[], float output[], int npts)
  280. {
  281.     int i;
  282.     for (i = 0; i < npts; i++ )
  283.       output[i] = win_cof[i]*input[i];
  284. }
  285. /* */
  286. /* Subroutine WRITEBL: write block of output data */
  287. /* */
  288. #define MAXSIZE 1024
  289. #define SIGMAX 32767
  290. void writebl(float output[], FILE *fp_out, int size)
  291. {
  292.     int i;
  293.     SPEECH int_sp[MAXSIZE]; /*  integer input array */
  294.     float temp;
  295. #ifdef PRINT
  296.     if (size > MAXSIZE) {
  297. printf("****ERROR: write block size too large **** n");
  298. exit(1);
  299.     }
  300. #endif
  301.     
  302.     for (i = 0; i < size; i++ ) {
  303. temp = output[i];
  304. /* clamp to +- SIGMAX */
  305. if (temp > SIGMAX)
  306.   temp = SIGMAX;
  307. if (temp < -SIGMAX)
  308.   temp = -SIGMAX;
  309. int_sp[i] = temp;
  310.     }
  311.     fwrite(int_sp,sizeof(SPEECH),size,fp_out);
  312. }
  313. #undef MAXSIZE
  314. /* */
  315. /* Subroutine zerflt: all zero (FIR) filter. */
  316. /*      Note: the output array can overlay the input.           */
  317. /* */
  318. void zerflt(float input[], float coeff[], float output[], int order,int npts)
  319. {
  320.     int i,j;
  321.     float accum;
  322.     for (i = npts-1; i >= 0; i-- ) {
  323. accum = 0.0;
  324. for (j = 0; j <= order; j++ )
  325.     accum += input[i-j] * coeff[j];
  326. output[i] = accum;
  327.     }
  328. }