melp_ana.c
上传用户:csczyc
上传日期:2021-02-19
资源大小:1051k
文件大小:12k
源码类别:

语音压缩

开发平台:

C/C++

  1. /*
  2.     Name: melp_ana.c
  3.     Description: MELP analysis
  4.     Inputs:
  5.       speech[] - input speech signal
  6.     Outputs: 
  7.       *par - MELP parameter structure
  8.     Returns: void
  9. */
  10. /* compiler include files */
  11. #include <stdio.h>
  12. #include <math.h>
  13. #include "mathhalf.h"
  14. #include "melp.h"
  15. #include "spbstd.h"
  16. #include "lpc.h"
  17. #include "mat.h"
  18. #include "vq.h"
  19. #include "fs.h"
  20. #include "pit.h"
  21. #include "math_lib.h"
  22. #include "constant.h"
  23. #include "wmops.h"
  24. /* compiler constants */
  25. #define BEGIN 0
  26. #define END 1
  27. #define HF_CORR_Q15 4       /* 0.0001220703125 in Q15 */
  28. #define LAG_WIN 0.005  /* not used */
  29. #define PDECAY_Q15 31129            /* (0.95*(1<<15)) */
  30. #define PDECAY_PITCH_Q7 320         /* ((0.05*DEFAULT_PITCH)*(1<<7)) */
  31. #define PEAK_THRESH_Q12 5488        /* (1.34*(1<<12)) */
  32. #define PEAK_THR2_Q12 6553          /* (1.6*(1<<12)) */
  33. #define SILENCE_DB_Q8 7680          /* (30.0*(1<<8)) */
  34. #define MAX_ORD LPF_ORD
  35. #define FRAME_BEG (PITCHMAX-(FRAME/2))
  36. #define FRAME_END (FRAME_BEG+FRAME)
  37. #define PITCH_BEG (FRAME_END-PITCHMAX)
  38. #define PITCH_FR ((2*PITCHMAX)+1)
  39. #define IN_BEG (PITCH_BEG+PITCH_FR-FRAME)
  40. #define SIG_LENGTH (LPF_ORD+PITCH_FR)
  41. /* external memory references */
  42.  
  43. extern Shortword win_cof[];
  44. extern Shortword lpf_num[];
  45. extern Shortword lpf_den[];
  46. extern Shortword msvq_cb[];
  47. extern Shortword msvq_cb_mean[];
  48. extern Shortword fsvq_cb[];
  49. extern Shortword fsvq_weighted;
  50. extern FILE *fp_indat,*fp_outdat;
  51. /* memory definitions */
  52.  
  53. static Shortword sigbuf[SIG_LENGTH];
  54. static Shortword speech[IN_BEG+FRAME];
  55. static Shortword dcdelin[DC_ORD];
  56. static Shortword dcdelout_hi[DC_ORD];
  57. static Shortword dcdelout_lo[DC_ORD];
  58. static Shortword lpfsp_delin[LPF_ORD];
  59. static Shortword lpfsp_delout[LPF_ORD];
  60. static Shortword pitch_avg;
  61. static Shortword fpitch[2];
  62. static struct msvq_param vq_par;  /* MSVQ parameters */
  63. static struct msvq_param fs_vq_par;  /* Fourier series VQ parameters */
  64. static Shortword w_fs[NUM_HARM];
  65. Shortword frames=0;
  66. void melp_ana(Shortword sp_in[],struct melp_param *par)
  67. {
  68.     Shortword i;
  69.     Shortword begin;
  70.     Shortword sub_pitch;
  71.     Shortword temp,pcorr,bpthresh;
  72.     Shortword r[LPC_ORD+1],refc[LPC_ORD+1],lpc[LPC_ORD+1];
  73.     Shortword weights[LPC_ORD];
  74.     Shortword section;
  75.     Shortword temp_delin[LPF_ORD];
  76.     Shortword temp_delout[LPF_ORD];
  77.     /* char in_num[20];*/
  78.     /* Remove DC from input speech */
  79.     dc_rmv(sp_in,&speech[IN_BEG],dcdelin,dcdelout_hi,dcdelout_lo,FRAME);
  80. //#if (COMPLEXITY_COUNT)
  81. //complexity_count();
  82. //#endif
  83.     /* Copy input speech to pitch window and lowpass filter */
  84.     v_equ(&sigbuf[LPF_ORD],&speech[PITCH_BEG],PITCH_FR);
  85.     for (section=0; section<LPF_ORD/2; section++) {
  86.       iir_2nd_s(&sigbuf[LPF_ORD],&lpf_den[section*3],&lpf_num[section*3],
  87.                 &sigbuf[LPF_ORD],&lpfsp_delin[section*2],
  88.                 &lpfsp_delout[section*2],FRAME);
  89.       /* save delay buffers for the next overlapping frame */
  90.       for (i = section*2; i < section*2+2; i++) {
  91.         temp_delin[i] = lpfsp_delin[i];    //  data_move();mark del
  92.         temp_delout[i] = lpfsp_delout[i];    //  data_move();mark del
  93.       }
  94.       iir_2nd_s(&sigbuf[LPF_ORD+FRAME],&lpf_den[section*3],
  95.                 &lpf_num[section*3],&sigbuf[LPF_ORD+FRAME],
  96.                 &lpfsp_delin[section*2],&lpfsp_delout[section*2],
  97.                 PITCH_FR-FRAME);
  98.       /* restore delay buffers for the next overlapping frame */
  99.       for (i = section*2; i < section*2+2; i++) {
  100.         lpfsp_delin[i] = temp_delin[i];   //   data_move();mark del
  101.         lpfsp_delout[i] = temp_delout[i];    //  data_move();mark del
  102.       }
  103.     }
  104. //#if (COMPLEXITY_COUNT)
  105. //complexity_count();
  106. //#endif
  107.     /* Perform global pitch search at frame end on lowpass speech signal */
  108.     /* Note: avoid short pitches due to formant tracking */
  109.     f_pitch_scale(&sigbuf[LPF_ORD],&sigbuf[LPF_ORD],PITCH_FR);
  110.     fpitch[END] = find_pitch(&sigbuf[LPF_ORD+(PITCH_FR/2)],&temp,
  111.      (2*PITCHMIN),PITCHMAX,PITCHMAX);
  112.     fpitch[END] = shl_a(fpitch[END],7);     /* fpitch in Q7 */
  113. //#if (COMPLEXITY_COUNT)
  114. //complexity_count();
  115. //#endif
  116.     /* Perform bandpass voicing analysis for end of frame */
  117.     bpvc_ana(&speech[FRAME_END], fpitch, &par->bpvc[0], &sub_pitch);
  118. //#if (COMPLEXITY_COUNT)
  119. //complexity_count();
  120. //#endif
  121.     /* Force jitter if lowest band voicing strength is weak */    
  122.     if (par->bpvc[0] < VJIT_Q14)      /* par->bpvc in Q14 */
  123. par->jitter = (Shortword)MAX_JITTER_Q15;   /* par->jitter in Q15 */
  124.     else
  125. par->jitter = (Shortword)0;
  126.     /* Calculate LPC for end of frame */
  127.     lpc_autocorr(&speech[(FRAME_END-(LPC_FRAME/2))],win_cof,r,
  128.                  HF_CORR_Q15,(Shortword)LAG_WIN,LPC_ORD,LPC_FRAME);
  129.     lpc[0] = ONE_Q12;        /* 1 in Q12 */
  130.     lpc_schur(r,lpc,refc,LPC_ORD);     /* lpc in Q12, refc in Q15 */
  131. //#if (COMPLEXITY_COUNT)
  132. //complexity_count();
  133. //#endif
  134.     /* Calculate LPC residual */
  135.     zerflt(&speech[PITCH_BEG],lpc,&sigbuf[LPF_ORD],LPC_ORD,PITCH_FR);
  136. //#if (COMPLEXITY_COUNT)
  137. //complexity_count();
  138. //#endif
  139.     /* Check peakiness of residual signal */
  140.     begin = (LPF_ORD+(PITCHMAX/2));
  141.     temp = peakiness(&sigbuf[begin],PITCHMAX);    /* temp in Q12 */
  142. //#if (COMPLEXITY_COUNT)
  143. //complexity_count();
  144. //#endif
  145.     /* Peakiness: force lowest band to be voiced  */
  146.     if (temp > PEAK_THRESH_Q12) {
  147. par->bpvc[0] = 1<<14;
  148.     }
  149.     /* Extreme peakiness: force second and third bands to be voiced */
  150.     if (temp > PEAK_THR2_Q12) {
  151. par->bpvc[1] = 1<<14;
  152. par->bpvc[2] = 1<<14;
  153.     }
  154.     /* Calculate overall frame pitch using lowpass filtered residual */
  155.     par->pitch = pitch_ana(&speech[FRAME_END], &sigbuf[LPF_ORD+PITCHMAX], 
  156.    sub_pitch,pitch_avg,&pcorr);
  157. //#if (COMPLEXITY_COUNT)
  158. //complexity_count();
  159. //#endif
  160.     bpthresh = (Shortword)BPTHRESH_Q14;
  161.     /* Calculate gain of input speech for each gain subframe */
  162.     for (i = 0; i < NUM_GAINFR; i++) {
  163. if (par->bpvc[0] > bpthresh) {
  164.     /* voiced mode: pitch synchronous window length */
  165.     temp = sub_pitch;
  166.     par->gain[i] = gain_ana(&speech[FRAME_BEG+(i+1)*GAINFR],
  167.     temp,MIN_GAINFR,PITCHMAX_X2);
  168. }
  169. else {
  170.     temp = (Shortword)GAIN_PITCH_Q7;
  171.     par->gain[i] = gain_ana(&speech[FRAME_BEG+(i+1)*GAINFR],
  172.     temp,0,PITCHMAX_X2);
  173. }
  174.     }
  175. //#if (COMPLEXITY_COUNT)
  176. //complexity_count();
  177. //#endif
  178.     /* Update average pitch value */
  179.     if (par->gain[NUM_GAINFR-1] > SILENCE_DB_Q8)    /* par->gain in Q8 */
  180.       temp = pcorr;
  181.     else
  182.       temp = 0;
  183.     /* pcorr in Q14 */
  184.     pitch_avg = p_avg_update(par->pitch,temp,(Shortword)VMIN_Q14);
  185.     /* Calculate Line Spectral Frequencies */
  186.     lpc_pred2lsp(lpc,par->lsf,LPC_ORD);
  187. //#if (COMPLEXITY_COUNT)
  188. //complexity_count();
  189. //#endif
  190.     /* Force minimum LSF bandwidth (separation) */
  191.     lpc_clamp(par->lsf,BWMIN_Q15,LPC_ORD);
  192.     /* Quantize MELP parameters to 2400 bps and generate bitstream */
  193.     /* Quantize LSF's with MSVQ */
  194.     vq_lspw(weights, &par->lsf[1], lpc, LPC_ORD);
  195.     msvq_enc(&par->lsf[1], weights, &par->lsf[1], vq_par);
  196. //#if (COMPLEXITY_COUNT)
  197. //complexity_count();
  198. //#endif
  199. frames++;
  200.     par->msvq_index = vq_par.indices;
  201.     /* Force minimum LSF bandwidth (separation) */
  202.     lpc_clamp(par->lsf,BWMIN_Q15,LPC_ORD);
  203.     /* Quantize logarithmic pitch period */
  204.     /* Reserve all zero code for completely unvoiced */
  205.     par->pitch = log10_fxp(par->pitch,7);      /* par->pitch in Q12 */
  206.     quant_u(&par->pitch,&par->pitch_index,(Shortword)PIT_QLO_Q12,
  207.     (Shortword)PIT_QUP_Q12,PIT_QLEV_M1,PIT_QLEV_M1_Q8,1,7);
  208.     /* convert pitch back to linear in Q7 */
  209.     par->pitch = pow10_fxp(par->pitch,7);
  210.     /* Quantize gain terms with uniform log quantizer */
  211.     q_gain(par->gain, par->gain_index,(Shortword)GN_QLO_Q8,
  212.    (Shortword)GN_QUP_Q8,GN_QLEV_M1,GN_QLEV_M1_Q10,0,5);
  213.     
  214.     /* Quantize jitter */
  215.     /* quant_u(&par->jitter,&par->jit_index,0,MAX_JITTER_Q15,2); */
  216.     if (par->jitter < shr((Shortword)MAX_JITTER_Q15,1)) {
  217.       par->jitter = 0;
  218.       par->jit_index = 0;
  219.     }
  220.     else {
  221.       par->jitter = (Shortword)MAX_JITTER_Q15;
  222.       par->jit_index = 1;
  223.     }
  224.     /* Quantize bandpass voicing */
  225.     par->uv_flag = q_bpvc(&par->bpvc[0],&par->bpvc_index,bpthresh,
  226.   NUM_BANDS);
  227. //#if (COMPLEXITY_COUNT)
  228. //complexity_count();
  229. //#endif
  230.     /* Calculate Fourier coefficients of residual signal from quantized LPC */
  231.     fill(par->fs_mag,ONE_Q13,NUM_HARM);
  232.     if (par->bpvc[0] > bpthresh) {
  233. lpc_lsp2pred(par->lsf,lpc,LPC_ORD);
  234. zerflt(&speech[(FRAME_END-(LPC_FRAME/2))],lpc,sigbuf,
  235.        LPC_ORD,LPC_FRAME);
  236. window(sigbuf,win_cof,sigbuf,LPC_FRAME);
  237. find_harm(sigbuf, par->fs_mag, par->pitch, NUM_HARM, LPC_FRAME);
  238.     }
  239. //#if (COMPLEXITY_COUNT)
  240. //complexity_count();
  241. //#endif
  242.     
  243.     /* quantize Fourier coefficients */
  244.     /* pre-weight vector, then use Euclidean distance */
  245.     window_Q(&par->fs_mag[0],w_fs,&par->fs_mag[0],NUM_HARM,14);
  246.     fsvq_enc(&par->fs_mag[0], &par->fs_mag[0], fs_vq_par);
  247. //#if (COMPLEXITY_COUNT)
  248. //complexity_count();
  249. //#endif
  250.     /* Set MELP indeces to point to same array */
  251.     par->fsvq_index = fs_vq_par.indices;
  252.     /* Update MSVQ information */
  253.     par->msvq_stages = vq_par.num_stages;
  254.     par->msvq_bits = vq_par.num_bits;
  255.     /* Write channel bitstream */
  256.     melp_chn_write(par);
  257.     /* Update delay buffers for next frame */
  258.     v_equ(&speech[0],&speech[FRAME],IN_BEG);
  259.     fpitch[BEGIN] = fpitch[END];
  260. //#if (COMPLEXITY_COUNT)
  261. //complexity_count();
  262. //#endif
  263. } /* melp_ana */
  264. /* 
  265.  * melp_ana_init: perform initialization 
  266.  */
  267. void melp_ana_init()
  268. {
  269.     Shortword j;
  270.     bpvc_ana_init(FRAME,PITCHMIN,PITCHMAX,NUM_BANDS,2,MINLENGTH);
  271.     pitch_ana_init(PITCHMIN,PITCHMAX,FRAME,LPF_ORD,MINLENGTH);
  272.     p_avg_init((Shortword)PDECAY_Q15,DEFAULT_PITCH_Q7,
  273.        (Shortword)PDECAY_PITCH_Q7,3);
  274.     v_zap(speech,IN_BEG+FRAME);
  275.     pitch_avg=DEFAULT_PITCH_Q7;
  276.     fill(fpitch,DEFAULT_PITCH_Q7,2);
  277.     v_zap(lpfsp_delin,LPF_ORD);
  278.     v_zap(lpfsp_delout,LPF_ORD);
  279.     /* Initialize multi-stage vector quantization (read codebook) */
  280.     vq_par.num_best = MSVQ_M;
  281.     vq_par.num_stages = 4;
  282.     vq_par.dimension = 10;
  283.     /* 
  284.      * Allocate memory for number of levels per stage and indices
  285.      * and for number of bits per stage 
  286.      */
  287.  
  288.     MEM_ALLOC(MALLOC,vq_par.num_levels,vq_par.num_stages,Shortword);
  289.     MEM_ALLOC(MALLOC,vq_par.indices,vq_par.num_stages,Shortword);
  290.     MEM_ALLOC(MALLOC,vq_par.num_bits,vq_par.num_stages,Shortword);
  291.     vq_par.num_levels[0] = 128;
  292.     vq_par.num_levels[1] = 64;
  293.     vq_par.num_levels[2] = 64;
  294.     vq_par.num_levels[3] = 64;
  295.     vq_par.num_bits[0] = 7;
  296.     vq_par.num_bits[1] = 6;
  297.     vq_par.num_bits[2] = 6;
  298.     vq_par.num_bits[3] = 6;
  299.     vq_par.cb = msvq_cb;
  300.     vq_par.cb_mean = msvq_cb_mean;
  301.     /* Initialize Fourier magnitude vector quantization (read codebook) */
  302.     fs_vq_par.num_best = 1;
  303.     fs_vq_par.num_stages = 1;
  304.     fs_vq_par.dimension = NUM_HARM;
  305.     /* 
  306.      * Allocate memory for number of levels per stage and indices
  307.      * and for number of bits per stage 
  308.      */
  309.  
  310.     MEM_ALLOC(MALLOC,fs_vq_par.num_levels,fs_vq_par.num_stages,Shortword);
  311.     MEM_ALLOC(MALLOC,fs_vq_par.indices,fs_vq_par.num_stages,Shortword);
  312.     MEM_ALLOC(MALLOC,fs_vq_par.num_bits,fs_vq_par.num_stages,Shortword);
  313.     fs_vq_par.num_levels[0] = FS_LEVELS;
  314.     fs_vq_par.num_bits[0] = FS_BITS;
  315.     fs_vq_par.cb = fsvq_cb;
  316.     /* Initialize fixed MSE weighting and inverse of weighting */
  317.     vq_fsw(w_fs, NUM_HARM, X60_Q9);
  318.     /* Pre-weight codebook (assume single stage only) */
  319.     if (fsvq_weighted == 0)
  320.       {
  321.   fsvq_weighted = 1;
  322.   for (j = 0; j < fs_vq_par.num_levels[0]; j++)
  323.             window_Q(&fs_vq_par.cb[j*NUM_HARM],w_fs,
  324.      &fs_vq_par.cb[j*NUM_HARM],NUM_HARM,14);
  325.       }
  326.     /* Initialize wr_array and wi_array */
  327.     fs_init();
  328. } /* melp_ana_init */