melp_ana.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.     Name: melp_ana.c
  13.     Description: MELP analysis
  14.     Inputs:
  15.       speech[] - input speech signal
  16.     Outputs: 
  17.       *par - MELP parameter structure
  18.     Returns: void
  19. */
  20. /* compiler include files */
  21. #include <stdio.h>
  22. #include <math.h>
  23. #include "melp.h"
  24. #include "spbstd.h"
  25. #include "lpc.h"
  26. #include "mat.h"
  27. #include "vq.h"
  28. #include "fs.h"
  29. #include "pit.h"
  30. /* compiler constants */
  31. #define BEGIN 0
  32. #define END 1
  33. #define BWFACT 0.994
  34. #define PDECAY 0.95
  35. #define PEAK_THRESH 1.34
  36. #define PEAK_THR2 1.6
  37. #define SILENCE_DB 30.0
  38. #define MAX_ORD LPF_ORD
  39. #define FRAME_BEG (PITCHMAX-(FRAME/2))
  40. #define FRAME_END (FRAME_BEG+FRAME)
  41. #define PITCH_BEG (FRAME_END-PITCHMAX)
  42. #define PITCH_FR ((2*PITCHMAX)+1)
  43. #define IN_BEG (PITCH_BEG+PITCH_FR-FRAME)
  44. #define SIG_LENGTH (LPF_ORD+PITCH_FR)
  45. /* external memory references */
  46.  
  47. extern float win_cof[LPC_FRAME];
  48. extern float lpf_num[LPF_ORD+1];
  49. extern float lpf_den[LPF_ORD+1];
  50. extern float msvq_cb[];
  51. extern float fsvq_cb[];
  52. extern int fsvq_weighted;
  53. /* memory definitions */
  54.  
  55. static float sigbuf[SIG_LENGTH];
  56. static float speech[IN_BEG+FRAME];
  57. static float dcdel[DC_ORD];
  58. static float lpfsp_del[LPF_ORD];
  59. static float pitch_avg;
  60. static float fpitch[2];
  61. static struct msvq_param vq_par;  /* MSVQ parameters */
  62. static struct msvq_param fs_vq_par;  /* Fourier series VQ parameters */
  63. static float w_fs[NUM_HARM];
  64. void melp_ana(float sp_in[],struct melp_param *par)
  65. {
  66.     int i;
  67.     int begin;
  68.     float sub_pitch;
  69.     float temp,pcorr,bpthresh;
  70.     float r[LPC_ORD+1],refc[LPC_ORD+1],lpc[LPC_ORD+1];
  71.     float weights[LPC_ORD];
  72.         
  73.     /* Remove DC from input speech */
  74.     dc_rmv(sp_in,&speech[IN_BEG],dcdel,FRAME);
  75.     
  76.     /* Copy input speech to pitch window and lowpass filter */
  77.     v_equ(&sigbuf[LPF_ORD],&speech[PITCH_BEG],PITCH_FR);
  78.     v_equ(sigbuf,lpfsp_del,LPF_ORD);
  79.     polflt(&sigbuf[LPF_ORD],lpf_den,&sigbuf[LPF_ORD],LPF_ORD,PITCH_FR);
  80.     v_equ(lpfsp_del,&sigbuf[FRAME],LPF_ORD);
  81.     zerflt(&sigbuf[LPF_ORD],lpf_num,&sigbuf[LPF_ORD],LPF_ORD,PITCH_FR);
  82.     
  83.     /* Perform global pitch search at frame end on lowpass speech signal */
  84.     /* Note: avoid short pitches due to formant tracking */
  85.     fpitch[END] = find_pitch(&sigbuf[LPF_ORD+(PITCH_FR/2)],&temp,
  86.      (2*PITCHMIN),PITCHMAX,PITCHMAX);
  87.     
  88.     /* Perform bandpass voicing analysis for end of frame */
  89.     bpvc_ana(&speech[FRAME_END], fpitch, &par->bpvc[0], &sub_pitch);
  90.     
  91.     /* Force jitter if lowest band voicing strength is weak */    
  92.     if (par->bpvc[0] < VJIT)
  93. par->jitter = MAX_JITTER;
  94.     else
  95. par->jitter = 0.0;
  96.     
  97.     /* Calculate LPC for end of frame */
  98.     window(&speech[(FRAME_END-(LPC_FRAME/2))],win_cof,sigbuf,LPC_FRAME);
  99.     autocorr(sigbuf,r,LPC_ORD,LPC_FRAME);
  100.     lpc[0] = 1.0;
  101.     lpc_schur(r,lpc,refc,LPC_ORD);
  102.     lpc_bw_expand(lpc,lpc,BWFACT,LPC_ORD);
  103.     
  104.     /* Calculate LPC residual */
  105.     zerflt(&speech[PITCH_BEG],lpc,&sigbuf[LPF_ORD],LPC_ORD,PITCH_FR);
  106.         
  107.     /* Check peakiness of residual signal */
  108.     begin = (LPF_ORD+(PITCHMAX/2));
  109.     temp = peakiness(&sigbuf[begin],PITCHMAX);
  110.     
  111.     /* Peakiness: force lowest band to be voiced  */
  112.     if (temp > PEAK_THRESH) {
  113. par->bpvc[0] = 1.0;
  114.     }
  115.     
  116.     /* Extreme peakiness: force second and third bands to be voiced */
  117.     if (temp > PEAK_THR2) {
  118. par->bpvc[1] = 1.0;
  119. par->bpvc[2] = 1.0;
  120.     }
  121.     /* Calculate overall frame pitch using lowpass filtered residual */
  122.     par->pitch = pitch_ana(&speech[FRAME_END], &sigbuf[LPF_ORD+PITCHMAX], 
  123.    sub_pitch,pitch_avg,&pcorr);
  124.     bpthresh = BPTHRESH;
  125.     
  126.     /* Calculate gain of input speech for each gain subframe */
  127.     for (i = 0; i < NUM_GAINFR; i++) {
  128. if (par->bpvc[0] > bpthresh) {
  129.     /* voiced mode: pitch synchronous window length */
  130.     temp = sub_pitch;
  131.     par->gain[i] = gain_ana(&speech[FRAME_BEG+(i+1)*GAINFR],
  132.     temp,MIN_GAINFR,2*PITCHMAX);
  133. }
  134. else {
  135.     temp = 1.33*GAINFR - 0.5;
  136.     par->gain[i] = gain_ana(&speech[FRAME_BEG+(i+1)*GAINFR],
  137.     temp,0,2*PITCHMAX);
  138. }
  139.     }
  140.     
  141.     /* Update average pitch value */
  142.     if (par->gain[NUM_GAINFR-1] > SILENCE_DB)
  143.       temp = pcorr;
  144.     else
  145.       temp = 0.0;
  146.     pitch_avg = p_avg_update(par->pitch, temp, VMIN);
  147.     
  148.     /* Calculate Line Spectral Frequencies */
  149.     lpc_pred2lsp(lpc,par->lsf,LPC_ORD);
  150.     
  151.     /* Force minimum LSF bandwidth (separation) */
  152.     lpc_clamp(par->lsf,BWMIN,LPC_ORD);
  153.     
  154.     /* Quantize MELP parameters to 2400 bps and generate bitstream */
  155.     
  156.     /* Quantize LSF's with MSVQ */
  157.     vq_lspw(weights, &par->lsf[1], lpc, LPC_ORD);
  158.     msvq_enc(&par->lsf[1], weights, &par->lsf[1], vq_par);
  159.     par->msvq_index = vq_par.indices;
  160.     
  161.     /* Force minimum LSF bandwidth (separation) */
  162.     lpc_clamp(par->lsf,BWMIN,LPC_ORD);
  163.     
  164.     /* Quantize logarithmic pitch period */
  165.     /* Reserve all zero code for completely unvoiced */
  166.     par->pitch = log10(par->pitch);
  167.     quant_u(&par->pitch,&par->pitch_index,PIT_QLO,PIT_QUP,PIT_QLEV);
  168.     par->pitch = pow(10.0,par->pitch);
  169.     
  170.     /* Quantize gain terms with uniform log quantizer */
  171.     q_gain(par->gain, par->gain_index,GN_QLO,GN_QUP,GN_QLEV);
  172.     
  173.     /* Quantize jitter and bandpass voicing */
  174.     quant_u(&par->jitter,&par->jit_index,0.0,MAX_JITTER,2);
  175.     par->uv_flag = q_bpvc(&par->bpvc[0],&par->bpvc_index,bpthresh,
  176.   NUM_BANDS);
  177.     
  178.     /* Calculate Fourier coefficients of residual signal from quantized LPC */
  179.     fill(par->fs_mag,1.0,NUM_HARM);
  180.     if (par->bpvc[0] > bpthresh) {
  181. lpc_lsp2pred(par->lsf,lpc,LPC_ORD);
  182. zerflt(&speech[(FRAME_END-(LPC_FRAME/2))],lpc,sigbuf,
  183.        LPC_ORD,LPC_FRAME);
  184. window(sigbuf,win_cof,sigbuf,LPC_FRAME);
  185. find_harm(sigbuf, par->fs_mag, par->pitch, NUM_HARM, LPC_FRAME);
  186.     }
  187.     
  188.     /* quantize Fourier coefficients */
  189.     /* pre-weight vector, then use Euclidean distance */
  190.     window(&par->fs_mag[0],w_fs,&par->fs_mag[0],NUM_HARM);
  191.     fsvq_enc(&par->fs_mag[0], &par->fs_mag[0], fs_vq_par);
  192.     
  193.     /* Set MELP indeces to point to same array */
  194.     par->fsvq_index = fs_vq_par.indices;
  195.     /* Update MSVQ information */
  196.     par->msvq_stages = vq_par.num_stages;
  197.     par->msvq_bits = vq_par.num_bits;
  198.     /* Write channel bitstream */
  199.     melp_chn_write(par);
  200.     /* Update delay buffers for next frame */
  201.     v_equ(&speech[0],&speech[FRAME],IN_BEG);
  202.     fpitch[BEGIN] = fpitch[END];
  203. }
  204. /* 
  205.  * melp_ana_init: perform initialization 
  206.  */
  207. void melp_ana_init()
  208. {
  209.     int j;
  210.     bpvc_ana_init(FRAME,PITCHMIN,PITCHMAX,NUM_BANDS,2,MINLENGTH);
  211.     pitch_ana_init(PITCHMIN,PITCHMAX,FRAME,LPF_ORD,MINLENGTH);
  212.     p_avg_init(PDECAY,DEFAULT_PITCH,3);
  213.     v_zap(speech,IN_BEG+FRAME);
  214.     pitch_avg=DEFAULT_PITCH;
  215.     fill(fpitch,DEFAULT_PITCH,2);
  216.     v_zap(lpfsp_del,LPF_ORD);
  217.     /* Initialize multi-stage vector quantization (read codebook) */
  218.     vq_par.num_best = MSVQ_M;
  219.     vq_par.num_stages = 4;
  220.     vq_par.dimension = 10;
  221.     /* 
  222.      * Allocate memory for number of levels per stage and indices
  223.      * and for number of bits per stage 
  224.      */
  225.  
  226.     MEM_ALLOC(MALLOC,vq_par.num_levels,vq_par.num_stages,int);
  227.     MEM_ALLOC(MALLOC,vq_par.indices,vq_par.num_stages,int);
  228.     MEM_ALLOC(MALLOC,vq_par.num_bits,vq_par.num_stages,int);
  229.     vq_par.num_levels[0] = 128;
  230.     vq_par.num_levels[1] = 64;
  231.     vq_par.num_levels[2] = 64;
  232.     vq_par.num_levels[3] = 64;
  233.     vq_par.num_bits[0] = 7;
  234.     vq_par.num_bits[1] = 6;
  235.     vq_par.num_bits[2] = 6;
  236.     vq_par.num_bits[3] = 6;
  237.     vq_par.cb = msvq_cb;
  238.     /* Scale codebook to 0 to 1 */
  239.     v_scale(vq_par.cb,(2.0/FSAMP),3200);
  240.     /* Initialize Fourier magnitude vector quantization (read codebook) */
  241.     fs_vq_par.num_best = 1;
  242.     fs_vq_par.num_stages = 1;
  243.     fs_vq_par.dimension = NUM_HARM;
  244.     /* 
  245.      * Allocate memory for number of levels per stage and indices
  246.      * and for number of bits per stage 
  247.      */
  248.  
  249.     MEM_ALLOC(MALLOC,fs_vq_par.num_levels,fs_vq_par.num_stages,int);
  250.     MEM_ALLOC(MALLOC,fs_vq_par.indices,fs_vq_par.num_stages,int);
  251.     MEM_ALLOC(MALLOC,fs_vq_par.num_bits,fs_vq_par.num_stages,int);
  252.     fs_vq_par.num_levels[0] = FS_LEVELS;
  253.     fs_vq_par.num_bits[0] = FS_BITS;
  254.     fs_vq_par.cb = fsvq_cb;
  255.     /* Initialize fixed MSE weighting and inverse of weighting */
  256.     vq_fsw(w_fs, NUM_HARM, 60.0);
  257.     /* Pre-weight codebook (assume single stage only) */
  258.     if (fsvq_weighted == 0)
  259.       {
  260.   fsvq_weighted = 1;
  261.   for (j = 0; j < fs_vq_par.num_levels[0]; j++)
  262.     window(&fs_vq_par.cb[j*NUM_HARM],w_fs,&fs_vq_par.cb[j*NUM_HARM],
  263.    NUM_HARM);
  264.       }
  265. }