FreeCalypso > hg > gsm-codec-lib
view libgsmefr/agc.c @ 242:f081a6850fb5
libgsmfrp: new refined implementation
The previous implementation exhibited the following defects,
which are now fixed:
1) The last received valid SID was cached forever for the purpose of
handling future invalid SIDs - we could have received some valid
SID ages ago, then lots of speech or NO_DATA, and if we then get
an invalid SID, we would resurrect the last valid SID from ancient
history - a bad design. In our new design, we handle invalid SID
based on the current state, much like BFI.
2) GSM 06.11 spec says clearly that after the second lost SID
(received BFI=1 && TAF=1 in CN state) we need to gradually decrease
the output level, rather than jump directly to emitting silence
frames - we previously failed to implement such logic.
3) Per GSM 06.12 section 5.2, Xmaxc should be the same in all 4 subframes
in a SID frame. What should we do if we receive an otherwise valid
SID frame with different Xmaxc? Our previous approach would
replicate this Xmaxc oddity in every subsequent generated CN frame,
which is rather bad. In our new design, the very first CN frame
(which can be seen as a transformation of the SID frame itself)
retains the original 4 distinct Xmaxc, but all subsequent CN frames
are based on the Xmaxc from the last subframe of the most recent SID.
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Tue, 09 May 2023 05:16:31 +0000 |
parents | 7b11cbe99a0e |
children |
line wrap: on
line source
/************************************************************************* * * FUNCTION: agc * * PURPOSE: Scales the postfilter output on a subframe basis by automatic * control of the subframe gain. * * DESCRIPTION: * sig_out[n] = sig_out[n] * gain[n]; * where gain[n] is the gain at the nth sample given by * gain[n] = agc_fac * gain[n-1] + (1 - agc_fac) g_in/g_out * g_in/g_out is the square root of the ratio of energy at the input * and output of the postfilter. * *************************************************************************/ #include "gsm_efr.h" #include "typedef.h" #include "namespace.h" #include "basic_op.h" #include "no_count.h" #include "cnst.h" #include "dec_state.h" #include "sig_proc.h" void agc ( struct EFR_decoder_state *st, Word16 *sig_in, /* (i) : postfilter input signal */ Word16 *sig_out, /* (i/o) : postfilter output signal */ Word16 agc_fac, /* (i) : AGC factor */ Word16 l_trm /* (i) : subframe size */ ) { Word16 i, exp; Word16 gain_in, gain_out, g0, gain; Word32 s; Word16 temp; /* calculate gain_out with exponent */ temp = shr (sig_out[0], 2); s = L_mult (temp, temp); for (i = 1; i < l_trm; i++) { temp = shr (sig_out[i], 2); s = L_mac (s, temp, temp); } if (s == 0) { st->past_gain = 0; return; } exp = sub (norm_l (s), 1); gain_out = round (L_shl (s, exp)); /* calculate gain_in with exponent */ temp = shr (sig_in[0], 2); s = L_mult (temp, temp); for (i = 1; i < l_trm; i++) { temp = shr (sig_in[i], 2); s = L_mac (s, temp, temp); } if (s == 0) { g0 = 0; } else { i = norm_l (s); gain_in = 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)); s = L_shl (s, 7); /* s = gain_out / gain_in */ s = L_shr (s, exp); /* add exponent */ s = Inv_sqrt (s); i = round (L_shl (s, 9)); /* g0 = i * (1-agc_fac) */ g0 = mult (i, sub (32767, agc_fac)); } /* compute gain[n] = agc_fac * gain[n-1] + (1-agc_fac) * sqrt(gain_in/gain_out) */ /* sig_out[n] = gain[n] * sig_out[n] */ gain = st->past_gain; for (i = 0; i < l_trm; i++) { gain = mult (gain, agc_fac); gain = add (gain, g0); sig_out[i] = extract_h (L_shl (L_mult (sig_out[i], gain), 3)); } st->past_gain = gain; return; } void agc2 ( Word16 *sig_in, /* (i) : postfilter input signal */ Word16 *sig_out, /* (i/o) : postfilter output signal */ Word16 l_trm /* (i) : subframe size */ ) { Word16 i, exp; Word16 gain_in, gain_out, g0; Word32 s; Word16 temp; /* calculate gain_out with exponent */ temp = shr (sig_out[0], 2); s = L_mult (temp, temp); for (i = 1; i < l_trm; i++) { temp = shr (sig_out[i], 2); s = L_mac (s, temp, temp); } test (); if (s == 0) { return; } exp = sub (norm_l (s), 1); gain_out = round (L_shl (s, exp)); /* calculate gain_in with exponent */ temp = shr (sig_in[0], 2); s = L_mult (temp, temp); for (i = 1; i < l_trm; i++) { temp = shr (sig_in[i], 2); s = L_mac (s, temp, temp); } test (); if (s == 0) { g0 = 0; move16 (); } else { i = norm_l (s); gain_in = round (L_shl (s, i)); exp = sub (exp, i); /*---------------------------------------------------* * g0 = sqrt(gain_in/gain_out); * *---------------------------------------------------*/ s = L_deposit_l (div_s (gain_out, gain_in)); s = L_shl (s, 7); /* s = gain_out / gain_in */ s = L_shr (s, exp); /* add exponent */ s = Inv_sqrt (s); g0 = round (L_shl (s, 9)); } /* sig_out(n) = gain(n) sig_out(n) */ for (i = 0; i < l_trm; i++) { sig_out[i] = extract_h (L_shl (L_mult (sig_out[i], g0), 3)); move16 (); } return; }