melp_syn.c
上传用户:cxx_68
上传日期:2021-02-21
资源大小:161k
文件大小:18k
源码类别:

语音压缩

开发平台:

Visual C++

  1. /*
  2. 2.4 kbps MELP Proposed Federal Standard speech coder
  3. Fixed-point C code, version 1.0
  4. Copyright (c) 1998, 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. The fixed-point version of the voice codec Mixed Excitation Linear
  11. Prediction (MELP) is based on specifications on the C-language software
  12. simulation contained in GSM 06.06 which is protected by copyright and
  13. is the property of the European Telecommunications Standards Institute
  14. (ETSI). This standard is available from the ETSI publication office
  15. tel. +33 (0)4 92 94 42 58. ETSI has granted a license to United States
  16. Department of Defense to use the C-language software simulation contained
  17. in GSM 06.06 for the purposes of the development of a fixed-point
  18. version of the voice codec Mixed Excitation Linear Prediction (MELP).
  19. Requests for authorization to make other use of the GSM 06.06 or
  20. otherwise distribute or modify them need to be addressed to the ETSI
  21. Secretariat fax: +33 493 65 47 16.
  22. */
  23. /*
  24.     Name: melp_syn.c
  25.     Description: MELP synthesis
  26.       This program takes the new parameters for a speech
  27.       frame and synthesizes the output speech.  It keeps
  28.       an internal record of the previous frame parameters
  29.       to use for interpolation.
  30.     Inputs:
  31.       *par - MELP parameter structure
  32.     Outputs: 
  33.       speech[] - output speech signal
  34.     Returns: void
  35. */
  36. /* compiler include files */
  37.  
  38. #include <stdio.h>
  39. #include <math.h>
  40. #include "mathhalf.h"
  41. #include "melp.h"
  42. #include "spbstd.h"
  43. #include "lpc.h"
  44. #include "mat.h"
  45. #include "vq.h"
  46. #include "fs.h"
  47. #include "math_lib.h"
  48. #include "constant.h"
  49. #include "wmops.h"
  50. /* compiler constants */
  51. #define ORIGINAL_SYNTH_GAIN 0
  52.  
  53. #if (MIX_ORD > DISP_ORD)
  54. #define BEGIN MIX_ORD
  55. #else
  56. #define BEGIN DISP_ORD
  57. #endif
  58. #define TILT_ORD 1
  59. #if (ORIGINAL_SYNTH_GAIN)
  60. #define SYN_GAIN_Q4 16000     /* (1000.0*(1<<4)) */
  61. #else
  62. #define SYN_GAIN_Q4 32000     /* (2000.0*(1<<4)) */
  63. #endif
  64. #define SCALEOVER    10
  65. #define INV_SCALEOVER_Q18  26214    /* ((1.0/SCALEOVER)*(1<<18)) */
  66. #define PDEL SCALEOVER
  67. /* external memory references */
  68.  
  69. extern Shortword bp_cof[NUM_BANDS][MIX_ORD+1];
  70. extern Shortword disp_cof[DISP_ORD+1];
  71. extern Shortword msvq_cb[];
  72. extern Shortword msvq_cb_mean[];
  73. extern Shortword fsvq_cb[];
  74. extern Shortword fsvq_weighted;
  75. extern FILE *fp_indat, *fp_outdat;
  76. /* temporary memory */
  77. static Shortword sigbuf[BEGIN+PITCHMAX];
  78. static Shortword sig2[BEGIN+PITCHMAX];
  79. static Shortword fs_real[PITCHMAX];
  80. static Shortword sp_out[BEGIN+FRAME];
  81. /* permanent memory */
  82.  
  83. static Shortword firstcall = 1; /* Just used for noise gain init */
  84. static Shortword prev_gain_err;
  85. static Shortword sigsave[PITCHMAX];
  86. static struct melp_param prev_par;
  87. static Shortword syn_begin;
  88. static Shortword prev_scale;
  89. static Shortword noise_gain = (Shortword)MIN_NOISE_Q8;
  90. static Shortword pulse_del[MIX_ORD],noise_del[MIX_ORD];
  91. static Shortword lpc_del[LPC_ORD],ase_del[LPC_ORD],tilt_del[TILT_ORD];
  92. static Shortword disp_del[DISP_ORD];
  93. static Shortword prev_lpc_gain;
  94. static struct msvq_param vq_par;  /* MSVQ parameters */
  95. static struct msvq_param fs_vq_par;  /* Fourier series VQ parameters */
  96. static Shortword w_fs_inv[NUM_HARM];
  97. /* these can be saved or recomputed */
  98. static Shortword prev_pcof[MIX_ORD+1],prev_ncof[MIX_ORD+1];
  99. static Shortword prev_tilt;
  100. static Shortword prev_gain;
  101. /*static Shortword frame=0;*/
  102. void melp_syn(struct melp_param *par,Shortword speech_out[])
  103. {
  104.     Shortword i, gaincnt;
  105.     Shortword erase;
  106.     Shortword length;
  107.     Shortword intfact, intfact1, ifact, ifact_gain;
  108.     Shortword gain,pulse_gain,pitch,jitter;
  109.     Shortword curr_tilt,tilt_cof[TILT_ORD+1];
  110.     Shortword sig_prob,syn_gain,lpc_gain;
  111.     Shortword lsf[LPC_ORD+1];
  112.     Shortword lpc[LPC_ORD+1];
  113.     Shortword ase_num[LPC_ORD+1],ase_den[LPC_ORD+1];
  114.     Shortword curr_pcof[MIX_ORD+1],curr_ncof[MIX_ORD+1];
  115.     Shortword pulse_cof[MIX_ORD+1],noise_cof[MIX_ORD+1];
  116.     Shortword temp1,temp2;
  117.     Longword L_temp1,L_temp2;
  118.     
  119.     /* Copy previous period of processed speech to output array */
  120.     if (syn_begin > 0) {
  121. if (syn_begin > FRAME) {
  122.     v_equ(&sp_out[BEGIN],&sigsave[0],FRAME);
  123.     /* past end: save remainder in sigsave[0] */
  124.     v_equ(&sigsave[0],&sigsave[FRAME],(Shortword)(syn_begin-FRAME));
  125. }
  126. else 
  127.   v_equ(&sp_out[BEGIN],&sigsave[0],syn_begin);
  128.     }
  129.     
  130.     erase = 0; /* no erasures yet */
  131.     
  132.     /* Update MSVQ information */
  133.     par->msvq_stages = vq_par.num_stages;
  134.     par->msvq_bits = vq_par.num_bits;
  135.     par->msvq_levels = vq_par.num_levels;
  136.     par->msvq_index = vq_par.indices;
  137.     par->fsvq_index = fs_vq_par.indices;
  138.     
  139.     /* Read and decode channel input buffer */
  140.     erase = melp_chn_read(par,&prev_par);
  141.     if (par->uv_flag != 1 && !erase) { 
  142. /* Un-weight Fourier magnitudes */
  143. window_Q(&par->fs_mag[0],w_fs_inv,&par->fs_mag[0],NUM_HARM,14);
  144.     }
  145.     /* Update adaptive noise level estimate based on last gain */
  146.     if (firstcall) {
  147. firstcall = 0;
  148. noise_gain = par->gain[NUM_GAINFR-1];   /* noise_gain in Q8 */
  149.     }
  150.     
  151.     else if (!erase) {
  152. for (i = 0; i < NUM_GAINFR; i++) {
  153.     noise_est(par->gain[i],&noise_gain,(Shortword)UPCONST_Q19,
  154.       (Shortword)DOWNCONST_Q17,(Shortword)MIN_NOISE_Q8,
  155.       (Shortword)MAX_NOISE_Q8);
  156.     /* Adjust gain based on noise level (noise suppression) */
  157.     noise_sup(&par->gain[i],noise_gain,(Shortword)MAX_NS_SUP_Q8,
  158.       (Shortword)MAX_NS_ATT_Q8,(Shortword)NFACT_Q8);
  159. }
  160.     
  161.     }
  162.     
  163.     /* Clamp LSP bandwidths to avoid sharp LPC filters */
  164.     lpc_clamp(par->lsf,BWMIN_Q15,LPC_ORD);
  165.     
  166.     /* Calculate spectral tilt for current frame for spectral enhancement */
  167.     tilt_cof[0] = ONE_Q15;        /* tilt_cof in Q15 */
  168.     lpc_lsp2pred(par->lsf,lpc,LPC_ORD);
  169. #if (!ORIGINAL_SYNTH_GAIN)
  170.     /* Use LPC prediction gain for adaptive scaling */
  171.     /* lpc_gain = sqrt(lpc_pred2refl(lpc,sig2,LPC_ORD));*/
  172.     lpc_gain = lpc_pred2refl(lpc,sig2,LPC_ORD);
  173.     lpc_gain = sqrt_fxp(lpc_gain,15);     /* lpc_gain in Q15 */
  174. #else
  175.     /* Original synthesis scaling */
  176.     lpc_pred2refl(lpc,sig2,LPC_ORD);
  177.     lpc_gain = ONE_Q15;
  178. #endif
  179.     if (sig2[1] < 0)
  180.       curr_tilt = shr(sig2[1],1);    /* curr_tilt in Q15 */
  181.     else
  182.       curr_tilt = 0;
  183.    
  184.     /* Disable pitch interpolation for high-pitched onsets */
  185.     /* if (par->pitch < 0.5*prev_par.pitch && 
  186.            par->gain[0] > 6.0 + prev_par.gain[NUM_GAINFR-1]) { */
  187.     temp1 = shr(prev_par.pitch,1);
  188.     temp2 = (Shortword)SIX_Q8 + prev_par.gain[NUM_GAINFR-1];
  189.     compare_nonzero();    compare_nonzero();
  190.     if ((par->pitch < temp1) && (par->gain[0] > temp2)) {
  191. /* copy current pitch into previous */
  192. prev_par.pitch = par->pitch;
  193.     }
  194.     
  195.     /* Set pulse and noise coefficients based on voicing strengths */
  196.     v_zap(curr_pcof,MIX_ORD+1);    /* curr_pcof in Q14 */
  197.     v_zap(curr_ncof,MIX_ORD+1);    /* curr_ncof in Q14 */
  198.     for (i = 0; i < NUM_BANDS; i++) {
  199. if (par->bpvc[i] > X05_Q14)
  200.     v_add(curr_pcof,&bp_cof[i][0],MIX_ORD+1);    /* bp_cof in Q14 */
  201. else
  202.     v_add(curr_ncof,&bp_cof[i][0],MIX_ORD+1);
  203.     }
  204.     /* Process each pitch period */
  205.     
  206.     while (syn_begin < FRAME) {
  207. /* interpolate previous and current parameters */
  208. ifact = divide_s(syn_begin,FRAME);    /* ifact in Q15 */
  209. if (syn_begin >= GAINFR) {
  210.     gaincnt = 2;
  211.     temp1 = sub(syn_begin,GAINFR);
  212.     ifact_gain = divide_s(temp1,GAINFR);
  213. }
  214. else {
  215.     gaincnt = 1;
  216.     ifact_gain = divide_s(syn_begin,GAINFR);
  217. }
  218. /* interpolate gain */
  219. if (gaincnt > 1) {
  220.   /* gain = ifact_gain*par->gain[gaincnt-1] + 
  221.             (1.0-ifact_gain)*par->gain[gaincnt-2]; */
  222.   L_temp1 = L_mult(par->gain[gaincnt-1],ifact_gain);
  223.   temp1 = sub(ONE_Q15,ifact_gain);
  224.   L_temp2 = L_mult(par->gain[gaincnt-2],temp1);
  225.   gain = extract_h(L_add(L_temp1,L_temp2));     /* gain in Q8 */
  226. }
  227. else {
  228.   /* gain = ifact_gain*par->gain[gaincnt-1] + 
  229.     (1.0-ifact_gain)*prev_par.gain[NUM_GAINFR-1]; */
  230.   L_temp1 = L_mult(par->gain[gaincnt-1],ifact_gain);
  231.   temp1 = sub(ONE_Q15,ifact_gain);
  232.   L_temp2 = L_mult(prev_par.gain[NUM_GAINFR-1],temp1);
  233.   gain = extract_h(L_add(L_temp1,L_temp2));     /* gain in Q8 */
  234. }
  235. /* Set overall interpolation path based on gain change */
  236. temp1 = sub(par->gain[NUM_GAINFR-1],prev_par.gain[NUM_GAINFR-1]);
  237. compare_nonzero();
  238. if (abs_s(temp1) > SIX_Q8) {
  239.   /* Power surge: use gain adjusted interpolation */
  240.   /* intfact = (gain - prev_par.gain[NUM_GAINFR-1]) / temp; */
  241.   temp2 = sub(gain,prev_par.gain[NUM_GAINFR-1]);
  242.   if (((temp2 > 0) && (temp1 < 0)) || ((temp2 < 0) && (temp1 > 0)))
  243.     intfact = 0;
  244.   else {
  245.     temp1 = abs_s(temp1);
  246.     temp2 = abs_s(temp2);
  247.     if (temp2 >= temp1)
  248.       intfact = ONE_Q15;
  249.     else
  250.       intfact = divide_s(temp2,temp1);    /* intfact in Q15 */
  251.   }
  252. }
  253. else
  254.   /* Otherwise, linear interpolation */
  255.   intfact = ifact;
  256. /* interpolate LSF's and convert to LPC filter */
  257. interp_array(prev_par.lsf,par->lsf,lsf,intfact,LPC_ORD+1);
  258. lpc_lsp2pred(lsf,lpc,LPC_ORD);
  259. /* Check signal probability for adaptive spectral enhancement filter */
  260. temp1 = add(noise_gain,(Shortword)X12_Q8);
  261. temp2 = add(noise_gain,(Shortword)X30_Q8);
  262. /* sig_prob in Q15 */
  263. sig_prob = lin_int_bnd(gain,temp1,temp2,0,ONE_Q15);
  264. /* Calculate adaptive spectral enhancement filter coefficients */
  265. ase_num[0] = ONE_Q12;    /* ase_num and ase_den in Q12 */
  266. temp1 = mult(sig_prob,(Shortword)ASE_NUM_BW_Q15);
  267. lpc_bw_expand(lpc,ase_num,temp1,LPC_ORD);
  268. temp1 = mult(sig_prob,(Shortword)ASE_DEN_BW_Q15);
  269. lpc_bw_expand(lpc,ase_den,temp1,LPC_ORD);
  270. /* tilt_cof[1] = sig_prob*(intfact*curr_tilt + 
  271. (1.0-intfact)*prev_tilt); */
  272. temp1 = mult(curr_tilt,intfact);
  273. intfact1 = sub(ONE_Q15,intfact);
  274. temp2 = mult(prev_tilt,intfact1);
  275. temp1 = add(temp1,temp2);
  276. tilt_cof[1] = mult(sig_prob,temp1);    /* tilt_cof in Q15 */
  277. /* interpolate pitch and pulse gain */
  278. /* syn_gain = SYN_GAIN*(intfact*lpc_gain + 
  279.               (1.0-intfact)*prev_lpc_gain); */
  280. temp1 = mult(lpc_gain,intfact);      /* lpc_gain in Q15 */
  281. temp2 = mult(prev_lpc_gain,intfact1);
  282. temp1 = add(temp1,temp2);      /* temp1 in Q15 */
  283. syn_gain = mult((Shortword)SYN_GAIN_Q4,temp1);    /* syn_gain in Q4 */
  284. /* pitch = intfact*par->pitch + (1.0-intfact)*prev_par.pitch; */
  285. temp1 = mult(par->pitch,intfact);
  286. temp2 = mult(prev_par.pitch,intfact1);
  287. pitch = add(temp1,temp2);    /* pitch in Q7 */
  288. /* pulse_gain = syn_gain*sqrt(pitch); */
  289. temp1 = sqrt_fxp(pitch,7);
  290. L_temp1 = L_mult(syn_gain,temp1);
  291. L_temp1 = L_shl(L_temp1,4);
  292. pulse_gain = extract_h(L_temp1);   /* pulse_gain in Q0 */
  293. /* interpolate pulse and noise coefficients */
  294. temp1 = sqrt_fxp(ifact,15);
  295. interp_array(prev_pcof,curr_pcof,pulse_cof,temp1,MIX_ORD+1);
  296. interp_array(prev_ncof,curr_ncof,noise_cof,temp1,MIX_ORD+1);
  297. /* interpolate jitter */
  298. /* jitter = ifact*par->jitter + (1.0-ifact)*prev_par.jitter; */
  299. temp1 = mult(par->jitter,ifact);
  300. temp2 = sub(ONE_Q15,ifact);
  301. temp2 = mult(prev_par.jitter,temp2);
  302. jitter = add(temp1,temp2);
  303. /* scale gain by 0.05 but keep gain in log */
  304. /* gain = pow(10.0,0.05*gain); */
  305. gain = mult((Shortword)X005_Q19,gain);      /* gain in Q12 */
  306. /* Set period length based on pitch and jitter */
  307. rand_num(&temp1,ONE_Q15,1);
  308. /* length = pitch * (1.0-jitter*temp) + 0.5; */
  309. temp1 = mult(jitter,temp1);
  310. temp1 = shr(temp1,1);           /* temp1 in Q14 */
  311. temp1 = sub(ONE_Q14,temp1);
  312. temp1 = mult(pitch,temp1);     /* length in Q6 */
  313. length = shift_r(temp1,-6);    /* length in Q0 with rounding */
  314. compare_nonzero();
  315. if (length < PITCHMIN)
  316.     length = PITCHMIN;
  317. compare_nonzero();
  318. if (length > PITCHMAX)
  319.     length = PITCHMAX;
  320. /* Use inverse DFT for pulse excitation */
  321. fill(fs_real,ONE_Q13,length);
  322. fs_real[0] = 0;
  323. interp_array(prev_par.fs_mag,par->fs_mag,&fs_real[1],intfact,
  324.      NUM_HARM);
  325. idft_real(fs_real,&sigbuf[BEGIN],length);   /* sigbuf in Q15 */
  326. /* Delay overall signal by PDEL samples (circular shift) */
  327. /* use fs_real as a scratch buffer */
  328. v_equ(fs_real,&sigbuf[BEGIN],length);
  329. v_equ(&sigbuf[BEGIN+PDEL],fs_real,(Shortword)(length-PDEL));
  330. v_equ(&sigbuf[BEGIN],&fs_real[length-PDEL],PDEL);
  331. /* Scale by pulse gain */
  332. v_scale(&sigbuf[BEGIN],pulse_gain,length);    /* sigbuf in Q0 */
  333. /* Filter and scale pulse excitation */
  334. v_equ(&sigbuf[BEGIN-MIX_ORD],pulse_del,MIX_ORD);
  335. v_equ(pulse_del,&sigbuf[length+BEGIN-MIX_ORD],MIX_ORD);
  336. zerflt_Q(&sigbuf[BEGIN],pulse_cof,&sigbuf[BEGIN],MIX_ORD,length,14);
  337. /* Get scaled noise excitation */
  338. temp1 = shr(mult((Shortword)X1_732_Q14,syn_gain),3);  /* temp1 in Q0 */
  339. rand_num(&sig2[BEGIN],temp1,length);   /* sig2 in Q0 */
  340. /* Filter noise excitation */
  341. v_equ(&sig2[BEGIN-MIX_ORD],noise_del,MIX_ORD);
  342. v_equ(noise_del,&sig2[length+BEGIN-MIX_ORD],MIX_ORD);
  343. zerflt_Q(&sig2[BEGIN],noise_cof,&sig2[BEGIN],MIX_ORD,length,14);
  344. /* Add two excitation signals (mixed excitation) */
  345. v_add(&sigbuf[BEGIN],&sig2[BEGIN],length);   /* sigbuf in Q0 */
  346. /* Adaptive spectral enhancement */
  347. v_equ(&sigbuf[BEGIN-LPC_ORD],ase_del,LPC_ORD);
  348. lpc_synthesis(&sigbuf[BEGIN],&sigbuf[BEGIN],ase_den,LPC_ORD,length);
  349. v_equ(ase_del,&sigbuf[BEGIN+length-LPC_ORD],LPC_ORD);
  350. zerflt(&sigbuf[BEGIN],ase_num,&sigbuf[BEGIN],LPC_ORD,length);
  351. v_equ(&sigbuf[BEGIN-TILT_ORD],tilt_del,TILT_ORD);
  352. v_equ(tilt_del,&sigbuf[length+BEGIN-TILT_ORD],TILT_ORD);
  353. zerflt_Q(&sigbuf[BEGIN],tilt_cof,&sigbuf[BEGIN],TILT_ORD,length,15);
  354. /* Perform LPC synthesis filtering */
  355. v_equ(&sigbuf[BEGIN-LPC_ORD],lpc_del,LPC_ORD);
  356. lpc_synthesis(&sigbuf[BEGIN],&sigbuf[BEGIN],lpc,LPC_ORD,length);
  357. v_equ(lpc_del,&sigbuf[length+BEGIN-LPC_ORD],LPC_ORD);
  358. /* Adjust scaling of synthetic speech */
  359. /* sigbuf in Q0 */
  360. scale_adj(&sigbuf[BEGIN],gain,&prev_scale,length,
  361.   SCALEOVER,(Shortword)INV_SCALEOVER_Q18);
  362. /* Copy processed speech to output array (not past frame end) */
  363. compare_nonzero();
  364. if (add(syn_begin,length) > FRAME) {
  365.     v_equ(&sp_out[BEGIN+syn_begin],&sigbuf[BEGIN],
  366.   (Shortword)(FRAME-syn_begin));
  367.     /* past end: save remainder in sigsave[0] */
  368.     v_equ(&sigsave[0],&sigbuf[BEGIN+FRAME-syn_begin],
  369.   (Shortword)(length-(FRAME-syn_begin)));
  370. }
  371. else
  372.     v_equ(&sp_out[BEGIN+syn_begin],&sigbuf[BEGIN],length);
  373. /* Update syn_begin for next period */
  374. syn_begin = add(syn_begin,length);
  375.     }
  376.     /* Implement pulse dispersion filter on output speech */
  377.     v_equ(&sp_out[BEGIN-DISP_ORD],disp_del,DISP_ORD);
  378.     v_equ(disp_del,&sp_out[FRAME+BEGIN-DISP_ORD],DISP_ORD);
  379.     zerflt_Q(&sp_out[BEGIN],disp_cof,speech_out,DISP_ORD,FRAME,15);
  380.     /* Save previous pulse and noise filters for next frame */
  381.     v_equ(prev_pcof,curr_pcof,MIX_ORD+1);
  382.     v_equ(prev_ncof,curr_ncof,MIX_ORD+1);
  383.     /* Copy current parameters to previous parameters for next time */
  384.     prev_par = *par;
  385.     prev_tilt = curr_tilt;
  386.     prev_lpc_gain = lpc_gain;
  387.     /* Update syn_begin for next frame */
  388.     syn_begin = sub(syn_begin,FRAME);
  389. #if (COMPLEXITY_COUNT)
  390. complexity_count();
  391. #endif
  392. }
  393. /* 
  394.  *
  395.  * Subroutine melp_syn_init(): perform initialization for melp 
  396.  * synthesis
  397.  *
  398.  */
  399. #define INV_LPC_ORD  2979   /* ((1.0/(LPC_ORD+1))*(1<<15)+0.5) */
  400. void melp_syn_init()
  401. {
  402.     Shortword i;
  403.     Shortword w_fs[NUM_HARM];
  404.     v_zap(prev_par.gain,NUM_GAINFR);
  405.     prev_par.pitch = UV_PITCH_Q7;
  406.     prev_par.lsf[0] = 0;
  407.     for (i = 1; i < LPC_ORD+1; i++)
  408.       prev_par.lsf[i] = add(prev_par.lsf[i-1],(Shortword)INV_LPC_ORD);
  409.     prev_par.jitter = 0;
  410.     v_zap(&prev_par.bpvc[0],NUM_BANDS);
  411.     prev_tilt=0;
  412.     prev_gain = 0;
  413.     prev_scale = 0;
  414.     prev_lpc_gain = ONE_Q15;
  415.     syn_begin = 0;
  416.     noise_gain = (Shortword)MIN_NOISE_Q8;
  417.     firstcall = 1;
  418.     prev_gain_err = 0;
  419.     v_zap(pulse_del,MIX_ORD);
  420.     v_zap(noise_del,MIX_ORD);
  421.     v_zap(lpc_del,LPC_ORD);
  422.     v_zap(ase_del,LPC_ORD);
  423.     v_zap(tilt_del,TILT_ORD);
  424.     v_zap(disp_del,DISP_ORD);
  425.     v_zap(sig2,BEGIN+PITCHMAX);
  426.     v_zap(sigbuf,BEGIN+PITCHMAX);
  427.     v_zap(sigsave,PITCHMAX);
  428.     v_zap(prev_pcof,MIX_ORD+1);
  429.     v_zap(prev_ncof,MIX_ORD+1);
  430.     prev_ncof[MIX_ORD/2] = ONE_Q15;
  431.     fill(prev_par.fs_mag,ONE_Q13,NUM_HARM);
  432.     /* 
  433.      * Initialize multi-stage vector quantization (read codebook) 
  434.      */
  435.  
  436.     vq_par.num_best = MSVQ_M;
  437.     vq_par.num_stages = 4;
  438.     vq_par.dimension = 10;
  439.     /* 
  440.      * Allocate memory for number of levels per stage and indices
  441.      * and for number of bits per stage 
  442.      */
  443.  
  444.     MEM_ALLOC(MALLOC,vq_par.num_levels,vq_par.num_stages,Shortword);
  445.     MEM_ALLOC(MALLOC,vq_par.indices,vq_par.num_stages,Shortword);
  446.     MEM_ALLOC(MALLOC,vq_par.num_bits,vq_par.num_stages,Shortword);
  447.     vq_par.num_levels[0] = 128;
  448.     vq_par.num_levels[1] = 64;
  449.     vq_par.num_levels[2] = 64;
  450.     vq_par.num_levels[3] = 64;
  451.     vq_par.num_bits[0] = 7;
  452.     vq_par.num_bits[1] = 6;
  453.     vq_par.num_bits[2] = 6;
  454.     vq_par.num_bits[3] = 6;
  455.     vq_par.cb = msvq_cb;
  456.     vq_par.cb_mean = msvq_cb_mean;
  457.     /* 
  458.      * Initialize Fourier magnitude vector quantization (read codebook) 
  459.      */
  460.  
  461.     fs_vq_par.num_best = 1;
  462.     fs_vq_par.num_stages = 1;
  463.     fs_vq_par.dimension = NUM_HARM;
  464.     /* 
  465.      * Allocate memory for number of levels per stage and indices
  466.      * and for number of bits per stage 
  467.      */
  468.  
  469.     MEM_ALLOC(MALLOC,fs_vq_par.num_levels,fs_vq_par.num_stages,Shortword);
  470.     MEM_ALLOC(MALLOC,fs_vq_par.indices,fs_vq_par.num_stages,Shortword);
  471.     MEM_ALLOC(MALLOC,fs_vq_par.num_bits,fs_vq_par.num_stages,Shortword);
  472.     fs_vq_par.num_levels[0] = FS_LEVELS;
  473.     fs_vq_par.num_bits[0] = FS_BITS;
  474.     fs_vq_par.cb = fsvq_cb;
  475.     /* 
  476.      * Initialize fixed MSE weighting and inverse of weighting 
  477.      */
  478.     vq_fsw(w_fs, NUM_HARM, (Shortword)X60_Q9);
  479.     for (i = 0; i < NUM_HARM; i++)
  480.       w_fs_inv[i] = divide_s(ONE_Q13,w_fs[i]);    /* w_fs_inv in Q14 */
  481.     /* 
  482.      * Pre-weight codebook (assume single stage only) 
  483.      */
  484.     if (fsvq_weighted == 0)
  485.       {
  486.   fsvq_weighted = 1;
  487.   for (i = 0; i < fs_vq_par.num_levels[0]; i++)
  488.     window_Q(&fs_vq_par.cb[i*NUM_HARM],w_fs,
  489.      &fs_vq_par.cb[i*NUM_HARM],NUM_HARM,14);
  490.       }
  491. }