📄 agc.cpp
字号:
agc.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001------------------------------------------------------------------------------ PSEUDO-CODEstatic Word32 energy_new( // o : return energy of signal Word16 in[], // i : input signal (length l_trm) Word16 l_trm ) // i : signal length{ Word32 s; Word16 i; Flag ov_save; ov_save = Overflow; //save overflow flag in case energy_old // must be called s = L_mult(in[0], in[0]); for (i = 1; i < l_trm; i++) { s = L_mac(s, in[i], in[i]); } // check for overflow if (L_sub (s, MAX_32) == 0L) { Overflow = ov_save; // restore overflow flag s = energy_old (in, l_trm); // function result } else { s = L_shr(s, 4); } return(s);}------------------------------------------------------------------------------ RESOURCES USED [optional] When the code is written for a specific target processor the the resources used should be documented below. HEAP MEMORY USED: x bytes STACK MEMORY USED: x bytes CLOCK CYCLES: (cycle count equation for this function) + (variable used to represent cycle count for each subroutine called) where: (cycle count variable) = cycle count for [subroutine name]------------------------------------------------------------------------------ CAUTION [optional] [State any special notes, constraints or cautions for users of this function]------------------------------------------------------------------------------*/static Word32 energy_new( /* o : return energy of signal */ Word16 in[], /* i : input signal (length l_trm) */ Word16 l_trm, /* i : signal length */ Flag *pOverflow /* i : overflow flag */){ Word32 s = 0; Word32 L_temp; Word16 i; Flag ov_save; ov_save = *(pOverflow); /* save overflow flag in case energy_old */ /* must be called */ for (i = l_trm - 1; i >= 0; i--) { L_temp = ((Word32) in[i]) * in[i]; if (L_temp != (Word32) 0x40000000L) { L_temp = L_temp << 1; } else { L_temp = MAX_32; } s = L_add(s, L_temp, pOverflow); } /* check for overflow */ if (s != MAX_32) { /* s is a sum of squares, so it won't be negative */ s = s >> 4; } else { *(pOverflow) = ov_save; /* restore overflow flag */ s = energy_old(in, l_trm, pOverflow); /* function result */ } return (s);}/*--------------------------------------------------------------------------*//*------------------------------------------------------------------------------ FUNCTION NAME: energy_new__Wrapper------------------------------------------------------------------------------ INPUT AND OUTPUT DEFINITIONS Inputs: in = input signal (Word16) l_trm = input signal length (Word16) overflow = address of overflow (Flag) Outputs: pOverflow -> 1 if the energy computation saturates Returns: s = return energy of signal (Word32) Global Variables Used: None. Local Variables Needed: None.------------------------------------------------------------------------------ FUNCTION DESCRIPTION This function provides external access to the static function energy_new.------------------------------------------------------------------------------ REQUIREMENTS None------------------------------------------------------------------------------ REFERENCES None------------------------------------------------------------------------------ PSEUDO-CODE CALL energy_new ( in = in l_trm = l_trm pOverflow = pOverflow ) MODIFYING(nothing) RETURNING(energy_new_value = s)------------------------------------------------------------------------------ RESOURCES USED [optional] When the code is written for a specific target processor the the resources used should be documented below. HEAP MEMORY USED: x bytes STACK MEMORY USED: x bytes CLOCK CYCLES: (cycle count equation for this function) + (variable used to represent cycle count for each subroutine called) where: (cycle count variable) = cycle count for [subroutine name]------------------------------------------------------------------------------ CAUTION [optional] [State any special notes, constraints or cautions for users of this function]------------------------------------------------------------------------------*/Word32 energy_new_Wrapper(Word16 in[], Word16 l_trm, Flag *pOverflow){ Word32 energy_new_value; /*---------------------------------------------------------------------------- CALL energy_new ( in = in l_trm = l_trm pOverflow = pOverflow ) MODIFYING(nothing) RETURNING(energy_new_value = s) ----------------------------------------------------------------------------*/ energy_new_value = energy_new(in, l_trm, pOverflow); return(energy_new_value);}/*--------------------------------------------------------------------------*//*------------------------------------------------------------------------------ FUNCTION NAME: agc_reset------------------------------------------------------------------------------ INPUT AND OUTPUT DEFINITIONS Inputs: state = pointer to a structure of type agcState Outputs: Structure pointed to by state is initialized to zeros Returns: Returns 0 if memory was successfully initialized, otherwise returns -1. Global Variables Used: None. Local Variables Needed: None.------------------------------------------------------------------------------ FUNCTION DESCRIPTION Reset of agc (i.e. set state memory to 1.0).------------------------------------------------------------------------------ REQUIREMENTS None.------------------------------------------------------------------------------ REFERENCES agc.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001------------------------------------------------------------------------------ PSEUDO-CODEint agc_reset (agcState *state){ if (state == (agcState *) NULL) { fprintf(stderr, "agc_reset: invalid parameter\n"); return -1; } state->past_gain = 4096; // initial value of past_gain = 1.0 return 0;}------------------------------------------------------------------------------ RESOURCES USED [optional] When the code is written for a specific target processor the the resources used should be documented below. HEAP MEMORY USED: x bytes STACK MEMORY USED: x bytes CLOCK CYCLES: (cycle count equation for this function) + (variable used to represent cycle count for each subroutine called) where: (cycle count variable) = cycle count for [subroutine name]------------------------------------------------------------------------------ CAUTION [optional] [State any special notes, constraints or cautions for users of this function]------------------------------------------------------------------------------*/Word16 agc_reset(agcState *state){ if (state == (agcState *) NULL) { /* fprintf(stderr, "agc_reset: invalid parameter\n"); */ return(-1); } state->past_gain = 4096; /* initial value of past_gain = 1.0 */ return(0);}/*--------------------------------------------------------------------------*//*------------------------------------------------------------------------------ FUNCTION NAME: agc------------------------------------------------------------------------------ INPUT AND OUTPUT DEFINITIONS Inputs: st = pointer to agc state sig_in = pointer to a buffer containing the postfilter input signal sig_out = pointer to a buffer containing the postfilter output signal agc_fac = AGC factor l_trm = subframe size pOverflow = pointer to the overflow flag Outputs: st->past_gain = gain buffer pointed to by sig_out contains the new postfilter output signal pOverflow -> 1 if the agc computation saturates Returns: return = 0 Global Variables Used: none. Local Variables Needed: none.------------------------------------------------------------------------------ FUNCTION DESCRIPTION Scales the postfilter output on a subframe basis using: sig_out[n] = sig_out[n] * gain[n] gain[n] = agc_fac * gain[n-1] + (1 - agc_fac) g_in/g_out where: gain[n] = gain at the nth sample given by g_in/g_out = square root of the ratio of energy at the input and output of the postfilter.------------------------------------------------------------------------------ REQUIREMENTS None.------------------------------------------------------------------------------ REFERENCES agc.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001------------------------------------------------------------------------------ PSEUDO-CODEint agc ( agcState *st, // i/o : agc state Word16 *sig_in, // i : postfilter input signal (l_trm) Word16 *sig_out, // i/o : postfilter output signal (l_trm) Word16 agc_fac, // i : AGC factor Word16 l_trm // i : subframe size){ Word16 i, exp; Word16 gain_in, gain_out, g0, gain; Word32 s; // calculate gain_out with exponent s = energy_new(sig_out, l_trm); // function result if (s == 0) { st->past_gain = 0; return 0; } exp = sub (norm_l (s), 1); gain_out = pv_round (L_shl (s, exp)); // calculate gain_in with exponent s = energy_new(sig_in, l_trm); // function result if (s == 0) { g0 = 0; } else { i = norm_l (s); gain_in = pv_round (L_shl (s, i)); exp = sub (exp, i); *---------------------------------------------------* * g0 = (1-agc_fac) * sqrt(gain_in/gain_out); * *---------------------------------------------------* s = L_deposit_l (div_s (gain_out, gain_in));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -