Commit 62e78a91 authored by Devansh Kandpal's avatar Devansh Kandpal
Browse files

midway commit - porting output_fc calculation to fixed point

parent 00bf2d8f
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -939,8 +939,8 @@ void ivas_reverb_interpolate_acoustic_data_fx(
void ivas_reverb_interpolate_energies_fx(
    const Word16 input_table_size,
    const Word32 *pInput_fc,    // input in Q16
    const Word32 *pInput_ene_l, // input in Q28 //Changed to Q31
    const Word32 *pInput_ene_r, // input in Q28 //Changes to q31
    const Word32 *pInput_ene_l, // input in Q28
    const Word32 *pInput_ene_r, // input in Q28
    const Word16 output_table_size,
    const Word32 *pOutput_fc, // Q16
    Word32 *pOutput_ene_l,      // output in Q28
+18 −40
Original line number Diff line number Diff line
@@ -81,13 +81,10 @@ ivas_error ivas_reverb_prepare_cldfb_params(
    Word32 *pOutput_ene )
{
    int16_t idx;
    /*float avg_pwr_left[CLDFB_NO_CHANNELS_MAX];
    float avg_pwr_right[CLDFB_NO_CHANNELS_MAX];
    float delay_diff, ln_1e6_inverted, exp_argument;
    const float dist = DEFAULT_SRC_DIST;
    const float dmx_gain_2 = 4.0f * EVS_PI * dist * dist / 0.001f;*/

    #define CLDFB_BAND_WIDTH (400) // TODO: Fixme!
    const Word16 cldfb_freq_halfstep = MAX_SAMPLING_RATE / ( 4 * CLDFB_NO_CHANNELS_MAX );
    const Word16 halfstep = 13107200; //Q16
    Word32 output_fc_fx[CLDFB_NO_CHANNELS_MAX];
    Word32 output_t60_fx[CLDFB_NO_CHANNELS_MAX];
    Word32 output_ene_fx[CLDFB_NO_CHANNELS_MAX];
@@ -103,27 +100,28 @@ ivas_error ivas_reverb_prepare_cldfb_params(
    FOR ( idx = 0; idx < CLDFB_NO_CHANNELS_MAX; idx++ )
    {
        output_fc_fx[idx] = (Word32) ( ( idx + 0.5f ) * ( MAX_SAMPLING_RATE / ( 2 * CLDFB_NO_CHANNELS_MAX ) ) ) * ONE_IN_Q16;
        //output_fc_fx[idx] = add( shr(CLDFB_BAND_WIDTH, 1), mult(CLDFB_BAND_WIDTH, idx));

        output_fc_fx[idx] = ( ( idx << 1 ) + 1 ) * cldfb_freq_halfstep * ONE_IN_Q16;

        output_fc_fx[idx] = mult( add( ( mult(idx, ONE_IN_Q16) << 1 ), ONE_IN_Q16 ), halfstep );//Debug
        
        int a = 1; // for debugging
    }

    // TODO: Replace all these dedicated interpolate functions with just one taking a single input vector and map it to a single output vector. Period.
    // ivas_interpolate_on_freq_grids_fx( pInput_params->nBands, pInput_params->pFc_input_fx, pInput_params->pAcoustic_rt60_fx, CLDFB_NO_CHANNELS_MAX, output_fc_fx, output_t60_fx );

    ivas_reverb_interpolate_acoustic_data_fx( pInput_params->nBands, pInput_params->pFc_input_fx, pInput_params->pAcoustic_rt60_fx, pInput_params->pAcoustic_dsr_fx,
                                              CLDFB_NO_CHANNELS_MAX, output_fc_fx, output_t60_fx, output_ene_fx );

    //output_t60_fx is in Q26
    //output_ene_fx is in Q30
    //output_t60_fx: Q26, output_ene_fx: Q30

    /* adjust DSR for the delay difference */

    delay_diff_fx = L_sub( pInput_params->inputPreDelay_fx, pInput_params->acousticPreDelay_fx ); //Q27

    ln_1e6_inverted_fx = 155440049; // Q31 /* 1.0f / logf( 1e06f ) */

    for ( idx = 0; idx < CLDFB_NO_CHANNELS_MAX; idx++ )
    {
        L_tmp = Mpy_32_32( output_t60_fx[idx], ln_1e6_inverted_fx );// L_tmp in Q26 // exp = pRt60_e[bin_idx] + 0
        L_tmp = Mpy_32_32( output_t60_fx[idx], ln_1e6_inverted_fx );// L_tmp in Q26

        exp_argument_fx = BASOP_Util_Divide3232_Scale_newton( delay_diff_fx, L_tmp, &tmp_exp );//exp_argument_L_fx in Q30 (not alwys, because tmp_exp values are not constant) //Scale() returns values in the highest possible precision
        exp_argument_fx = L_shr_sat( exp_argument_fx, sub(6,tmp_exp));//exp_argument_L_fx in Q26
@@ -133,7 +131,7 @@ ivas_error ivas_reverb_prepare_cldfb_params(
        {
            exp_argument_fx = 1543503872;
        }
        IF( LT_32( exp_argument_fx, -1543503872 ) )
        IF( LT_32( exp_argument_fx, -1543503872 ) ) //-23 in Q26
        {
              exp_argument_fx = -1543503872;
        }
@@ -141,14 +139,10 @@ ivas_error ivas_reverb_prepare_cldfb_params(
        tmp = Mpy_32_32( 96817114, exp_argument_fx );//Q21 -> multiplication of Q26 by Q26 // x * 1/ln(2) it is ensured that exp_argument_fx is always in Q26

        L_tmp = BASOP_util_Pow2( tmp, 10, &pow_exp );//Q28 -> not always, because pow_exp is not fixed
        // tmp is a 32-bit signed integer in Q21, therefore expoenent to the power function is 10
        //L_tmp = L_shl_sat( L_tmp, pow_exp );// Q28??

        //L_tmp = L_shl( L_tmp, add( 1, pow_exp ) );
        //L_tmp = L_shl( L_tmp, pow_exp );
        L_tmp = Mpy_32_32( L_tmp, output_ene_fx[idx] ); // Q27. Q28 multiplied by Q30 gives Q27 -> Finally, energy should be in Q31 ( this is mandatory for further computations)
        L_tmp = L_shl_sat( L_tmp, add( 1, pow_exp ) );//
        L_tmp = Mpy_32_32( L_tmp, output_ene_fx[idx] ); //Q27 if result of previous step is in Q28
        L_tmp = L_shl_sat( L_tmp, add( 1, pow_exp ) );//L_tmp in Q31

        //Assign energyt o putput -pointer
        pOutput_ene[idx] = L_tmp;
        move32();

@@ -163,24 +157,11 @@ ivas_error ivas_reverb_prepare_cldfb_params(
    {
        Word32 tmp_ene;

        // For fixed-point porting -> energies are stored in Q31, but gain is calcualted in Q16. Final result must be in Q31.

        // Steps for this conversion:
        // Step 1 : add avg_pwr_left and avg_pwr_right (assuming same Q factor) and store in a temp variable -> use L_add : 32bit addition with saturation
        // Step 2 : scale the tmep variable by 0.5 -> can this be done by right shifting by 1?

        tmp_ene = ( L_add( avg_pwr_left_fx[idx], avg_pwr_right_fx[idx] ) ) >> 1; //Q28

        // Step 3 : multiply temp variable by gain, with appropriate scaling for Q31 and Q16 multiplication
        tmp_ene = Mpy_32_32( tmp_ene, dmx_gain_2_fx ); // Mpy_32_32_ss multiplies two 32 bit numberrs in 64-bit with saturation or L_mult -> outputs word16 // Qfactor of tmp_ene?? dmx_gain_2_fx is in Q16
        // incomplete implentation

        // Step 4 : multiply step 3 result with output energy
        pOutput_ene[idx] =Mpy_32_32(pOutput_ene[idx] ,tmp_ene);//result seems to be correct, but in Q13. This should be scaled to Q31 for correct output.


        /*floating-point
        pOutput_ene[idx] *= 0.5f * ( avg_pwr_left[idx] + avg_pwr_right[idx] ) * dmx_gain_2;*/
        tmp_ene = Mpy_32_32( tmp_ene, dmx_gain_2_fx ); //Q13
        pOutput_ene[idx] =Mpy_32_32(pOutput_ene[idx] ,tmp_ene);//Q13
        //Scale final output to Q31 for compatibility with further computations
        pOutput_ene[idx] = L_shl_sat( pOutput_ene[idx], 18 );//Q31
    }

    return IVAS_ERR_OK;
@@ -262,9 +243,6 @@ static void ivas_reverb_set_energies(

    for ( int i = 0; i < 60; i++ )
    {
        // avg_pwr_left[i] = (float)avg_pwr_left_fx[i] / ONE_IN_Q28; //(float) fabs( me2f( avg_pwr_left_fx[i], avg_pwr_left_e[i] ) );
        // avg_pwr_right[i] = (float) avg_pwr_right_fx[i] / ONE_IN_Q28; // (float) fabs( me2f( avg_pwr_right_fx[i], avg_pwr_right_e[i] ) );

        avg_pwr_left[i] = avg_pwr_left_fx[i];
        avg_pwr_right[i] = avg_pwr_right_fx[i];
    }