📄 ntt_scale_coder.c
字号:
short_flag=0; } if(short_delay_flag==1) { mat_shift_min[0]=1; mat_shift_min[1]=2; mat_shift_min[2]=3; mat_shift_min[3]=2; mat_shift_min[4]=2; mat_shift_min[5]=2; mat_shift_max[0]= 1; mat_shift_max[1]= 2; mat_shift_max[2]= 3; mat_shift_max[3]= 3; mat_shift_max[4]= 3; mat_shift_max[5]= 3; } else if(short_delay_flag==0) { mat_shift_min[0]=0; mat_shift_min[1]=1; mat_shift_min[2]=1; mat_shift_min[3]=0; mat_shift_min[4]=0; mat_shift_min[5]=0; mat_shift_max[0]= 1; mat_shift_max[1]= 2; mat_shift_max[2]= 3; mat_shift_max[3]= 3; mat_shift_max[4]= 3; mat_shift_max[5]= 3; } mat_shift_min[0]=0; mat_shift_min[1]=0; mat_shift_min[2]=0; mat_shift_min[3]=0; mat_shift_min[4]=0; mat_shift_min[5]=0; mat_shift_max[0]= 2 /*1 test TM98506 */; mat_shift_max[1]= 3 /*2 test */; mat_shift_max[2]= 3 /*2 test */; mat_shift_max[3]= 3; mat_shift_max[4]= 3; mat_shift_max[5]= 3; switch(iscl) { case 0: mat_wei[0]=mat_ERR_WEI_0*1.1; mat_wei[1]=mat_ERR_WEI_1*4.1; mat_wei[2]=mat_ERR_WEI_2*0.1; mat_wei[3]=mat_ERR_WEI_3*0.1; break; case 1: mat_wei[0]=mat_ERR_WEI_0*0.1; mat_wei[1]=mat_ERR_WEI_1*5.0; mat_wei[2]=mat_ERR_WEI_2*0.1; mat_wei[3]=mat_ERR_WEI_3*0.1; break; case 2: mat_wei[0]=mat_ERR_WEI_0*0.1; mat_wei[1]=mat_ERR_WEI_1*0.1; mat_wei[2]=mat_ERR_WEI_2*8.0; mat_wei[3]=mat_ERR_WEI_3*0.1; break; case 3: mat_wei[0]=mat_ERR_WEI_0*0.1; mat_wei[1]=mat_ERR_WEI_1*0.1; mat_wei[2]=mat_ERR_WEI_2*0.1; mat_wei[3]=mat_ERR_WEI_3*10.0; break; case 4: mat_wei[0]=mat_ERR_WEI_0; mat_wei[1]=mat_ERR_WEI_1; mat_wei[2]=mat_ERR_WEI_2; mat_wei[3]=mat_ERR_WEI_3; break; case 5: mat_wei[0]=mat_ERR_WEI_0; mat_wei[1]=mat_ERR_WEI_1; mat_wei[2]=mat_ERR_WEI_2; mat_wei[3]=mat_ERR_WEI_3; break; case 6: mat_wei[0]=mat_ERR_WEI_0; mat_wei[1]=mat_ERR_WEI_1; mat_wei[2]=mat_ERR_WEI_2; mat_wei[3]=mat_ERR_WEI_3; break; } { int ind, bias, qsample; static int prev[8][2] ={{0,0},{1,1},{2,2}, {3,3}, {0,0}, {0,0},{0,0},{0,0}}; static double prev_pow[8][2] = {{0.0,0.0}, {0.0,0.0},{0.0,0.0},{0.0,0.0}, {0.0,0.0}, {0.0,0.0},{0.0,0.0},{0.0,0.0}}; int smin[2], smax[2]; switch(index->w_type){ case ONLY_LONG_SEQUENCE: case LONG_START_SEQUENCE: case LONG_STOP_SEQUENCE: for(i_ch=0; i_ch<index->numChannel; i_ch++){ { for(ii=0;ii<index->block_size_samples-1;ii++){ tmp_spect[ii] = fabs(0.5*(fabs(spectrum[ii+i_ch*index->block_size_samples])+ fabs(spectrum[ii+i_ch*index->block_size_samples+1])))/ sqrt(sqrt(sqrt(0.5*(fabs(spectrum_org[ii+i_ch*index->block_size_samples])+ fabs(spectrum_org[ii+i_ch*index->block_size_samples+1])))+0.001)); } tmp_spect[index->block_size_samples-1]=fabs(spectrum[index->block_size_samples-1+i_ch*index->block_size_samples])/ sqrt(sqrt(sqrt(fabs(spectrum_org[index->block_size_samples-1+i_ch*index->block_size_samples])+0.001))); { for(ind=0; ind<4; ind++){ float ftmp; ftmp = (float)(index->nttDataScl->ac_top[iscl][i_ch][ind]) - (float)(index->nttDataScl->ac_btm[iscl][i_ch][ind]); ftmp *=(float)(index->block_size_samples); qsample=(int)ftmp; ftmp = (float)(index->nttDataScl->ac_btm[iscl][i_ch][ind]); ftmp *=(float)(index->block_size_samples); bias = (int)ftmp; for(acc=0.0, ii=bias;ii<bias+qsample;ii++){ acc+= (tmp_spect[ii]*tmp_spect[ii]); } if(ind ==0) err[ind] = acc *mat_wei[0]; else if(ind ==1) err[ind] = acc *mat_wei[1]; else if(ind ==2) err[ind] = acc *mat_wei[2]; else if(ind ==3) err[ind] = acc *mat_wei[3]; } } err_max=0.0; smin[i_ch]= smax[i_ch] = prev[iscl][i_ch]; if(prev_pow[iscl][i_ch]*10. < (err[0]+err[1]+err[2]+err[3])){ smin[i_ch] = prev[iscl][i_ch]-1; if(smin[i_ch]<0) smin[i_ch]=0; smax[i_ch] = prev[iscl][i_ch]+1; if(smax[i_ch]>3) smax[i_ch]=3; } if((prev_pow[iscl][i_ch]*30. < (err[0]+err[1]+err[2]+err[3]))|| (prev_pow[iscl][i_ch] > (err[0]+err[1]+err[2]+err[3]))){ smin[i_ch] = 0; smax[i_ch] = 3; } for(ii=smin[i_ch];ii<=smax[i_ch];ii++){ if(err_max<err[ii]){ err_max=err[ii]; mat_shift[iscl][i_ch]=ii; } } prev[iscl][i_ch]= mat_shift[iscl][i_ch]; prev_pow[iscl][i_ch]= prev_pow[iscl][i_ch]*0.6 +(err[0]+err[1]+err[2]+err[3])*0.4; } } break; case EIGHT_SHORT_SEQUENCE: for(i_ch=0; i_ch<index->numChannel; i_ch++) { int isf; for(ii=0;ii<index->block_size_samples;ii++){ tmp_spect[ii] = fabs(spectrum[ii+i_ch*index->block_size_samples])/ sqrt(sqrt(sqrt((fabs(spectrum_org[ii+i_ch*index->block_size_samples])+0.001)))); } for(ind=0; ind<4; ind++){ float ftmp; ftmp = (float)(index->nttDataScl->ac_top[iscl][i_ch][ind]) - (float)(index->nttDataScl->ac_btm[iscl][i_ch][ind]); ftmp *=(float)(index->block_size_samples/8); qsample=(int)ftmp; ftmp = (float)(index->nttDataScl->ac_btm[iscl][i_ch][ind]); ftmp *=(float)(index->block_size_samples/8); bias = (int)ftmp; acc =0.0; for(isf=0; isf<8; isf++){ for(ii=bias;ii<bias+qsample;ii++){ acc+= (tmp_spect[ii+isf*index->block_size_samples/8]* tmp_spect[ii+isf*index->block_size_samples/8]); } } if(ind ==0) err[ind] = acc *mat_wei[0]; else if(ind ==1) err[ind] = acc *mat_wei[1]; else if(ind ==2) err[ind] = acc *mat_wei[2]; else if(ind ==3) err[ind] = acc *mat_wei[3]; } smin[i_ch]= smax[i_ch] = prev[iscl][i_ch]; if(prev_pow[iscl][i_ch]*10. < (err[0]+err[1]+err[2]+err[3])){ smin[i_ch] = prev[iscl][i_ch]-1; if(smin[i_ch]<0) smin[i_ch]=0; smax[i_ch] = prev[iscl][i_ch]+1; if(smax[i_ch]>3) smax[i_ch]=3; } if((prev_pow[iscl][i_ch]*30. < (err[0]+err[1]+err[2]+err[3]))|| (prev_pow[iscl][i_ch] > (err[0]+err[1]+err[2]+err[3]))){ smin[i_ch] = 0; smax[i_ch] = 3; } err_max= err[smin[i_ch]]; for(ii=smin[i_ch];ii<=smax[i_ch];ii++){ if(err_max<=err[ii]){ err_max=err[ii]; mat_shift[iscl][i_ch]=ii; } } prev[iscl][i_ch]= mat_shift[iscl][i_ch]; prev_pow[iscl][i_ch]= prev_pow[iscl][i_ch]*0.6 +(err[0]+err[1]+err[2]+err[3])*0.4;/*fprintf(stderr, "SSSSS %5d %5d %10.3e %10.3e %10.3e %10.3e %10.3e\n",iscl, mat_shift[iscl][i_ch], err_max, err[0], err[1], err[2], err[3]);*/ } break; break; default: fprintf( stderr,"Fatal error! %d: no such window type.\n", index->w_type ); exit(1); } } return(0);} /** end fix_or_flex **/void mat_scale_tf_quantize_spectrum(double spectrum[], double lpc_spectrum[], double bark_env[], double pitch_sequence[], double gain[], double perceptual_weight[], ntt_INDEX *index_scl, int iscl){ /*--- Variables ---*/ int ismp, nfr, nsf, n_can, vq_bits, cb_len_max, subtop, isf, i_ch, subtopq; double add_signal[ntt_T_FR_MAX]; double *sp_cv0, *sp_cv1; double spec_buf[ntt_T_FR_MAX],lpc_buf[ntt_T_FR_MAX],bark_buf[ntt_T_FR_MAX], wei_buf[ntt_T_FR_MAX]; int qsample[MAX_TIME_CHANNELS], bias[MAX_TIME_CHANNELS]; float ftmp; /*--- Parameter settings ---*/ switch (index_scl->w_type){ case ONLY_LONG_SEQUENCE: case LONG_START_SEQUENCE: case LONG_STOP_SEQUENCE: /* available bits */ vq_bits = index_scl->nttDataScl->ntt_VQTOOL_BITS_SCL[iscl]; /* codebooks */ sp_cv0 = index_scl->nttDataScl->ntt_codev0_scl; sp_cv1 = index_scl->nttDataScl->ntt_codev1_scl; cb_len_max = ntt_CB_LEN_READ_SCL + ntt_CB_LEN_MGN; /* number of pre-selection candidates */ n_can = ntt_N_CAN_SCL; /* frame size */ nfr = index_scl->block_size_samples; nsf = index_scl->numChannel; /* additional signal */ for (ismp=0; ismp<index_scl->block_size_samples*index_scl->numChannel; ismp++){ add_signal[ismp] = pitch_sequence[ismp] / lpc_spectrum[ismp]; } break; case EIGHT_SHORT_SEQUENCE: /* available bits */ vq_bits = index_scl->nttDataScl->ntt_VQTOOL_BITS_S_SCL[iscl]; /* codebooks */ sp_cv0 = index_scl->nttDataScl->ntt_codev0s_scl; sp_cv1 = index_scl->nttDataScl->ntt_codev1s_scl; cb_len_max = ntt_CB_LEN_READ_S_SCL + ntt_CB_LEN_MGN; /* number of pre-selection candidates */ n_can = ntt_N_CAN_S_SCL; /* number of subframes in a frame */ nfr = index_scl->block_size_samples/8; nsf = index_scl->numChannel * ntt_N_SHRT; /* additional signal */ ntt_zerod(index_scl->block_size_samples*index_scl->numChannel, add_signal); break; default: fprintf(stderr, "ntt_sencode(): %d: Window mode error.\n", index_scl->w_type); exit(1);} /*--- Vector quantization process ---*//* Base,Enh-1 : fix ,,,, Enh-2... : flex 980302 Tomokazu Ishikawa */ ntt_zerod(index_scl->block_size_samples*index_scl->numChannel,spec_buf); ntt_setd(index_scl->block_size_samples*index_scl->numChannel,1.e5,lpc_buf); ntt_setd(index_scl->block_size_samples*index_scl->numChannel,1.e-1,bark_buf); ntt_setd(index_scl->block_size_samples*index_scl->numChannel,2.e3,wei_buf); for(i_ch = 0; i_ch<index_scl->numChannel; i_ch++){ ftmp = (float) (index_scl->nttDataScl->ac_top[iscl][i_ch][mat_shift[iscl][i_ch]]) -(float) (index_scl->nttDataScl->ac_btm[iscl][i_ch][mat_shift[iscl][i_ch]]); ftmp *= nfr; qsample[i_ch]=(int)ftmp; ftmp = (float) (index_scl->nttDataScl->ac_btm[iscl][i_ch][mat_shift[iscl][i_ch]]); ftmp *= nfr; bias[i_ch] = (int)ftmp; } for(isf=0;isf<nsf;isf++){ subtop= isf*nfr; i_ch = subtop/index_scl->block_size_samples; subtopq= qsample[i_ch]*isf; ntt_movdd(qsample[i_ch],spectrum+subtop+bias[i_ch],spec_buf+subtopq); ntt_movdd(qsample[i_ch],lpc_spectrum+subtop+bias[i_ch],lpc_buf+subtopq); ntt_movdd(qsample[i_ch],bark_env+subtop+bias[i_ch],bark_buf+subtopq); ntt_movdd(qsample[i_ch],perceptual_weight+subtop+bias[i_ch],wei_buf+subtopq); } ntt_movdd(index_scl->block_size_samples*index_scl->numChannel,spec_buf,spectrum); ntt_movdd(index_scl->block_size_samples*index_scl->numChannel,lpc_buf,lpc_spectrum); ntt_movdd(index_scl->block_size_samples*index_scl->numChannel,bark_buf,bark_env); ntt_movdd(index_scl->block_size_samples*index_scl->numChannel,wei_buf,perceptual_weight); /* Fix or Flex 980302 Tomokazu Ishikawa*/ ntt_vq_pn(qsample[0], nsf, vq_bits, n_can, sp_cv0, sp_cv1, cb_len_max, spectrum, lpc_spectrum, bark_env, add_signal, gain, perceptual_weight, index_scl->wvq, index_scl->pow);}/** moriya informed and add **/void ntt_scalebase_enc_lpc_spectrum /* Takehiro Moriya*/ (/* Parameters */ int nfr, int nsf, int block_size_samples, int n_ch, int n_pr, double *lsp_code, double *lsp_fgcode, int *lsp_csize, int *lsp_cdim, int prev_lsp_code[], /* double prev_buf[MAX_TIME_CHANNELS][ntt_MA_NP][ntt_N_PR_MAX+1], */ int ma_np, /* Input */ double spectrum[], int w_type, int current_block, /* Output */ int index_lsp[MAX_TIME_CHANNELS][ntt_LSP_NIDX_MAX], double lpc_spectrum[], double band_lower, double band_upper, double *cos_TT ){ /*--- Variables ---*/ double resid, alf[MAX_TIME_CHANNELS][ntt_N_PR_MAX+1], lsp[ntt_N_PR_MAX+1], lspq[MAX_TIME_CHANNELS*(ntt_N_PR_MAX+1)]; double spectrum_wide[ntt_T_FR_MAX], lpc_spectrum_re[ntt_T_FR_MAX]; int i_sup, top, subtop, w_type_t, isf, lsptop, ismp; int block_size, nfr_l, nfr_u, nfr_lu; float ftmp; block_size = nfr * nsf; ftmp = (float)band_lower; ftmp *= (float)nfr; nfr_l = (int)ftmp; ftmp = (float)band_upper-(float)band_lower; ftmp *= (float)nfr; nfr_lu = (int)ftmp; nfr_u = nfr_l+nfr_lu; /*--- encode LSP coefficients ---*/ if (current_block == ntt_BLOCK_LONG) w_type_t = w_type; else w_type_t = ONLY_LONG_SEQUENCE; ftmp = (float)band_upper-(float)band_lower; ftmp =1./ftmp; for ( i_sup=0; i_sup<n_ch; i_sup++ ){ float fftmp; top = i_sup*block_size; ntt_zerod(block_size, spectrum_wide+top); ntt_setd(block_size,1.e5, lpc_spectrum+top); /*--- band extension for nallow band lpc (by A.Jin 1997.6.9) ---*/ for(isf=0; isf<nsf; isf++){ subtop = top + isf*nfr; for(ismp=nfr_l; ismp<nfr_l+nfr_lu; ismp++){ fftmp = ftmp * (float)(ismp-nfr_l); spectrum_wide[(int)fftmp+subtop]=spectrum[ismp+subtop]; } } /* calculate LSP coefficients */ ntt_scale_fgetalf(nfr, nsf, block_size_samples, spectrum_wide+top,alf[i_sup],&resid, n_pr, ntt_BAND_EXPN, cos_TT); ntt_alflsp(n_pr, alf[i_sup], lsp); /* quantize LSP coefficients */ ntt_lsp_encw(n_pr, (double (*)[ntt_N_PR_MAX])lsp_code, (double (*)[ntt_MA_NP][ntt_N_PR_MAX])lsp_fgcode, lsp_csize, prev_lsp_code+i_sup*ntt_LSP_NIDX_MAX, /*prev_buf[i_sup], */ ma_np, lsp, lspq+i_sup*n_pr, index_lsp[i_sup], ntt_LSP_SPLIT); } /*--- reconstruct LPC spectrum ---*/ for (i_sup=0; i_sup<n_ch; i_sup++){ top = i_sup * block_size; lsptop = i_sup * n_pr; ntt_lsptowt(nfr, n_pr, block_size_samples, lspq+lsptop, lpc_spectrum+top, cos_TT); /*--- band re-extension for nallow band lpc (by A.Jin 1997.6.9) ---*/ ftmp = (float)band_upper-(float)band_lower; ftmp = 1./ftmp; for(ismp=0; ismp<nfr_lu; ismp++){ float fftmp; fftmp = ftmp*(float)ismp; lpc_spectrum_re[ismp+top+nfr_l] = lpc_spectrum[(int)fftmp+top]; } ntt_movdd(nfr_lu,lpc_spectrum_re+top+nfr_l, lpc_spectrum+top+nfr_l); ntt_setd(nfr_l, 999., lpc_spectrum+top); ntt_setd(nfr- nfr_u, 999., lpc_spectrum+top+nfr_u); for (isf=1; isf<nsf; isf++){ subtop = top + isf*nfr; ntt_movdd(nfr, lpc_spectrum+top, lpc_spectrum+subtop); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -