📄 l3psy.c
字号:
ecb[j] = 0; cb[j] = 0; for(k=0;k<CBANDS;k++){ if(s[j][k] != 0.0){ ecb[j] += s[j][k]*grouped_e[k]; cb[j] += s[j][k]*grouped_c[k]; } } if(ecb[j] !=0)cb[j] = cb[j]/ecb[j]; else cb[j] = 0; }/***************************************************************************** * Calculate the required SNR for each of the frequency partitions * * this whole section can be accomplished by a table lookup * *****************************************************************************/ for(j=0;j<CBANDS;j++){ if(cb[j]<.05)cb[j]=0.05; else if(cb[j]>.5)cb[j]=0.5;/* tb = -0.434294482*log((double) cb[j])-0.301029996; */ tb = -0.43 *log((double) cb[j]) - 0.29 ; if(tb<0.0) tb=0.0; else if(tb>1.0) tb=1.0; bc[j] = tmn[j]*tb + nmt*(1.0-tb); k = cbval[j] + 0.5; bc[j] = (bc[j] > bmax[k]) ? bc[j] : bmax[k]; bc[j] = exp((double) -bc[j]*LN_TO_LOG10); }/***************************************************************************** * Calculate the permissible noise energy level in each of the frequency * * partitions. Include absolute threshold and pre-echo controls * * this whole section can be accomplished by a table lookup * *****************************************************************************/ for(j=0;j<CBANDS;j++) if(rnorm[j] && numlines[j]) nb[j] = ecb[j]*bc[j]/(rnorm[j]*numlines[j]); else nb[j] = 0; for(j=0;j<HBLKSIZE;j++){ temp1=nb[partition[j]]; /* preliminary threshold */ temp1=(temp1>absthr[j])?temp1:absthr[j];/*do not use pre-echo control for layer 2 because it may do bad things to the*//* MUSICAM bit allocation algorithm */ if(lay==1){ fthr[j] = (temp1 < lthr[chn][j]) ? temp1 : lthr[chn][j]; temp2 = temp1 * 0.00316; fthr[j] = (temp2 > fthr[j]) ? temp2 : fthr[j]; } else fthr[j] = temp1; lthr[chn][j] = LXMIN*temp1; }/***************************************************************************** * Translate the 512 threshold values to the 32 filter bands of the coder * *****************************************************************************/ for(j=0;j<193;j += 16){ min_thres = fthr[j]; sum_energy = energy[j]; for(k=1;k<17;k++){ if(min_thres>fthr[j+k]) min_thres = fthr[j+k]; sum_energy += energy[j+k]; } snrtmp[i][j/16] = sum_energy/(min_thres * 17.0); snrtmp[i][j/16] = 4.342944819 * log((double)snrtmp[i][j/16]); } for(j=208;j<(HBLKSIZE-1);j += 16){ min_thres = 0.0; sum_energy = 0.0; for(k=0;k<17;k++){ min_thres += fthr[j+k]; sum_energy += energy[j+k]; } snrtmp[i][j/16] = sum_energy/min_thres; snrtmp[i][j/16] = 4.342944819 * log((double)snrtmp[i][j/16]); }/***************************************************************************** * End of Psychoacuostic calculation loop * *****************************************************************************/ } for(i=0; i<32; i++){ if(lay==2) /* if(lay==2 && chn==2) MI */ snr32[i]=(snrtmp[0][i]>snrtmp[1][i])?snrtmp[0][i]:snrtmp[1][i]; else snr32[i]=snrtmp[0][i]; } break;/*************************************************************************//** LAYER 3 *//*************************************************************************/ case 3: if ( init_L3 == 0 ) { L3para_read( sfreq,numlines,partition_l,minval,qthr_l,norm_l,s3_l, partition_s,qthr_s,norm_s,SNR_s, cbw_l,bu_l,bo_l,w1_l,w2_l, cbw_s,bu_s,bo_s,w1_s,w2_s ); init_L3 ++ ; } for ( j = 0; j < 21; j++ ) ratio_d[j] = ratio[chn][j]; for ( j = 0; j < 12; j++ ) for ( i = 0; i < 3; i++ ) ratio_ds[j][i] = ratio_s[chn][j][i]; if ( chn == 0 ) if ( new == 0 ) { new = 1; old = 0; oldest = 1; } else { new = 0; old = 1; oldest = 0; }/*********************************************************************** Delay signal by sync_flush=768 samples ***********************************************************************/ for ( j = 0; j < sync_flush; j++ ) /* for long window samples */ savebuf[j] = savebuf[j+flush]; for ( j = sync_flush; j < syncsize; j++ ) savebuf[j] = *buffer++; for ( j = 0; j < BLKSIZE; j++ ) { /**window data with HANN window**/ wsamp_r[j] = window[j] * savebuf[j]; wsamp_i[j] = 0.0; }/*********************************************************************** compute unpredicatability of first six spectral lines * **********************************************************************/ fft( wsamp_r, wsamp_i, energy, phi, 1024 ); /**long FFT**/ for ( j = 0; j < 6; j++ ) { /* calculate unpredictability measure cw */ r_prime = 2.0 * r[chn][old][j] - r[chn][oldest][j]; phi_prime = 2.0 * phi_sav[chn][old][j]-phi_sav[chn][oldest][j]; r[chn][new][j] = sqrt((double) energy[j]); phi_sav[chn][new][j] = phi[j]; temp1 = r[chn][new][j] * cos((double) phi[j]) - r_prime * cos(phi_prime); temp2 = r[chn][new][j] * sin((double) phi[j]) - r_prime * sin(phi_prime); temp3=r[chn][new][j] + fabs(r_prime); if ( temp3 != 0.0 ) cw[j] = sqrt( temp1*temp1+temp2*temp2 ) / temp3; else cw[j] = 0; }/*********************************************************************** compute unpredicatibility of next 200 spectral lines ***********************************************************************/ for ( sblock = 0; sblock < 3; sblock++ ) { /**window data with HANN window**/ for ( j = 0, k = 128 * (2 + sblock); j < 256; j++, k++ ) { wsamp_r[j] = window_s[j]* savebuf[k]; wsamp_i[j] = 0.0; } /* short FFT*/ fft( wsamp_r, wsamp_i, &energy_s[sblock][0], &phi_s[sblock][0], 256 ); } sblock = 1; for ( j = 6; j < 206; j += 4 ) {/* calculate unpredictability measure cw */ double r2, phi2, temp1, temp2, temp3; k = (j+2) / 4; r_prime = 2.0 * sqrt((double) energy_s[0][k]) - sqrt((double) energy_s[2][k]); phi_prime = 2.0 * phi_s[0][k] - phi_s[2][k]; r2 = sqrt((double) energy_s[1][k]); phi2 = phi_s[1][k]; temp1 = r2 * cos( phi2 ) - r_prime * cos( phi_prime ); temp2 = r2 * sin( phi2 ) - r_prime * sin( phi_prime ); temp3 = r2 + fabs( r_prime ); if ( temp3 != 0.0 ) cw[j] = sqrt( temp1 * temp1 + temp2 * temp2 ) / temp3; else cw[j] = 0.0; cw[j+1] = cw[j+2] = cw[j+3] = cw[j]; }/*********************************************************************** Set unpredicatiblility of remaining spectral lines to 0.4 ***********************************************************************/ for ( j = 206; j < HBLKSIZE; j++ ) cw[j] = 0.4; /*********************************************************************** Calculate the energy and the unpredictability in the threshold ** calculation partitions ***********************************************************************/ for ( b = 0; b < CBANDS; b++ ) { eb[b] = 0.0; cb[b] = 0.0; } for ( j = 0; j < HBLKSIZE; j++ ) { int tp = partition_l[j]; if ( tp >= 0 ) { eb[tp] += energy[j]; cb[tp] += cw[j] * energy[j]; } }/*********************************************************************** convolve the partitioned energy and unpredictability ** with the spreading function, s3_l[b][k] ********************************************************************* */ for ( b = 0; b < CBANDS; b++ ) { ecb[b] = 0.0; ctb[b] = 0.0; } for ( b = 0;b < CBANDS; b++ ) { for ( k = 0; k < CBANDS; k++ ) { ecb[b] += s3_l[b][k] * eb[k]; /* sprdngf for Layer III */ ctb[b] += s3_l[b][k] * cb[k]; } } /* calculate the tonality of each threshold calculation partition */ /* calculate the SNR in each threshhold calculation partition */ for ( b = 0; b < CBANDS; b++ ) { double cbb,tbb; if (ecb[b] != 0.0 ) { cbb = ctb[b]/ecb[b]; if (cbb <0.01) cbb = 0.01; cbb = log( cbb); } else cbb = 0.0 ; tbb = -0.299 - 0.43*cbb; /* conv1=-0.299, conv2=-0.43 */ tbb = minimum( 1.0, maximum( 0.0, tbb) ) ; /* 0<tbb<1 */ SNR_l[b] = maximum( minval[b], 29.0*tbb+6.0*(1.0-tbb) ); } /* TMN=29.0,NMT=6.0 for all calculation partitions */ for ( b = 0; b < CBANDS; b++ ) /* calculate the threshold for each partition */ nb[b] = ecb[b] * norm_l[b] * exp( -SNR_l[b] * LN_TO_LOG10 ); for ( b = 0; b < CBANDS; b++ ) { /* pre-echo control */ double temp_1; /* BUG of IS */ temp_1 = minimum( nb[b], minimum(2.0*nb_1[chn][b],16.0*nb_2[chn][b]) ); thr[b] = maximum( qthr_l[b], temp_1 );/* rpelev=2.0, rpelev2=16.0 */ nb_2[chn][b] = nb_1[chn][b]; nb_1[chn][b] = nb[b]; } *pe = 0.0; /* calculate percetual entropy */ for ( b = 0; b < CBANDS; b++ ) { double tp ; tp = minimum( 0.0, log((thr[b]+1.0) / (eb[b]+1.0) ) ) ; /*not log*/ *pe -= numlines[b] * tp ; } /* thr[b] -> thr[b]+1.0 : for non sound portition */ #define switch_pe 1800 blocktype = NORM_TYPE; if ( *pe < switch_pe ) { /* no attack : use long blocks */ switch( blocktype_old[chn] ) { case NORM_TYPE: case STOP_TYPE: blocktype = NORM_TYPE; break; case SHORT_TYPE: blocktype = STOP_TYPE; break; case START_TYPE: fprintf( stderr, "Error in block selecting\n" ); abort(); break; /* problem */ } /* threshold calculation (part 2) */ for ( sb = 0; sb < SBMAX_l; sb++ ) { en[sb] = w1_l[sb] * eb[bu_l[sb]] + w2_l[sb] * eb[bo_l[sb]]; thm[sb] = w1_l[sb] *thr[bu_l[sb]] + w2_l[sb] * thr[bo_l[sb]]; for ( b = bu_l[sb]+1; b < bo_l[sb]; b++ ) { en[sb] += eb[b]; thm[sb] += thr[b]; } if ( en[sb] != 0.0 ) ratio[chn][sb] = thm[sb]/en[sb]; else ratio[chn][sb] = 0.0; } } else { /* attack : use short blocks */ blocktype = SHORT_TYPE; if ( blocktype_old[chn] == NORM_TYPE ) blocktype_old[chn] = START_TYPE; if ( blocktype_old[chn] == STOP_TYPE ) blocktype_old[chn] = SHORT_TYPE ; /* threshold calculation for short blocks */ for ( sblock = 0; sblock < 3; sblock++ ) { for ( b = 0; b < CBANDS_s; b++ ) { eb[b] = 0.0; ecb[b] = 0.0; } for ( j = 0; j < HBLKSIZE_s; j++ ) eb[partition_s[j]] += energy_s[sblock][j]; for ( b = 0; b < CBANDS_s; b++ ) for ( k = 0; k < CBANDS_s; k++ ) ecb[b] += s3_l[b][k] * eb[k];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -