https://wiki.multimedia.cx/api.php?action=feedcontributions&user=Adam+Quantrill&feedformat=atomMultimediaWiki - User contributions [en]2023-09-24T22:48:41ZUser contributionsMediaWiki 1.39.3https://wiki.multimedia.cx/index.php?title=User:Adam_Quantrill&diff=14383User:Adam Quantrill2013-03-05T18:09:26Z<p>Adam Quantrill: </p>
<hr />
<div>Adam is a computing and electronics engineer currently working in Telecoms, specifically GSM basestations at IP Access Ltd near Cambridge, UK.<br />
<br />
Hobbies include engineering, electronics, computing and fine real ale.</div>Adam Quantrillhttps://wiki.multimedia.cx/index.php?title=AMR-NB&diff=14382AMR-NB2013-03-05T18:06:26Z<p>Adam Quantrill: /* GSM OTA format */</p>
<hr />
<div>* samples: http://samples.mplayerhq.hu/A-codecs/amr/<br />
* specification: http://www.3gpp.org/ftp/Specs/html-info/26-series.htm<br />
* Wikipedia article: http://en.wikipedia.org/wiki/Adaptive_Multi-Rate<br />
<br />
AMR-NB (Adaptive Multi-Rate Narrowband) is a [[vocoder]] employed in low-bitrate applications like mobile phones. It is a form of ACELP where A stands for algebraic.<br />
<br />
----<br />
<br />
The following text aims to be a simpler and more explicit document of the AMR narrow band decoding processes to aid in development of a decoder. Reference to sections of the specification will be made in the following format: (c.f. §5.2.5). Happy reading.<br />
<br />
<br />
<br />
== Nomenclature weirdness ==<br />
<br />
Throughout the specification, a number of references are made to the same (or very similar) items with fairly confusing variation. They are listed below to aid understanding of the following text but efforts will be made to consistently use one item name throughout or to use both with the lesser used name in parenthesis.<br />
<br />
* Pitch / Adaptive codebook<br />
* Fixed / Innovative (also algebraic when referring to the codebook)<br />
* Quantified means quantized<br />
<br />
<br />
== Summary ==<br />
<br />
* Mode dependent bitstream parsing<br />
* Indices parsed from bitstream<br />
* Indices decoded to give LSF vectors, fractional pitch lags, innovative code vectors and the pitch and innovative gains<br />
* LSF vectors converted to LP filter coefficients at each subframe<br />
* Subframe decoding<br />
** Excitation vector = adaptive code vector * adaptive (pitch) gain + innovative code vector * innovative gain<br />
** Excitation vector filtered through an LP synthesis filter to reconstruct speech<br />
** Speech signal filtered with adaptive postfilter<br />
<br />
<br />
<br />
== Bit stream frame format ==<br />
Specification ([http://www.3gpp.org/ftp/Specs/html-info/26101.htm 26.101]) describes two possible frame types - interface formats 1 and 2 (often abbreviated IF1 and IF2). IF2 is byte-aligned. The following tables of data are taken from this specification unless otherwise stated. AMR-NB over GSM uses a third slightly different format whose bit-packing is tightly defined by the GSM specs.<br />
<br />
=== IF1 format ===<br />
<br />
{| border=1<br />
! bits !! low level meaning || high level meaning<br />
|-<br />
| 4<br />
| Frame type<br />
| rowspan=2 | AMR header<br />
|-<br />
| 1<br />
| Frame quality indicator (0 bad/1 good)<br />
|-<br />
| 3<br />
| Mode indication<br />
| rowspan=3 | AMR auxiliary information<br />
|-<br />
| 3<br />
| Mode request<br />
|-<br />
| 8<br />
| CRC<br />
|-<br />
| &nbsp;<br />
| Class A bits<br />
| rowspan=3 | AMR core frame<br />
|-<br />
| &nbsp;<br />
| Class B bits<br />
|-<br />
| &nbsp;<br />
| Class C bits<br />
|}<br />
<br />
=== IF2 format ===<br />
<br />
{| border=1<br />
! bits !! low level meaning || high level meaning<br />
|-<br />
| 4<br />
| Frame type<br />
| AMR header<br />
|-<br />
| &nbsp;<br />
| Class A bits<br />
| rowspan=3 | AMR core frame<br />
|-<br />
| &nbsp;<br />
| Class B bits<br />
|-<br />
| &nbsp;<br />
| Class C bits<br />
|-<br />
| &nbsp;<br />
| Padding (called "Bit stuffing" in the specification)<br />
| &nbsp;<br />
|}<br />
<br />
=== GSM OTA format ===<br />
<br />
{| border=1<br />
! bits !! low level meaning || high level meaning<br />
|-<br />
| 4<br />
| Mode request<br />
| rowspan=1 | AMR auxiliary information <br />
|-<br />
| 1<br />
| F bit (0 good/1 unusual)<br />
| rowspan=3 | AMR header<br />
|-<br />
| 4<br />
| Frame Type<br />
|-<br />
| 1<br />
| Frame quality indicator (0 bad/1 good)<br />
|-<br />
| &nbsp;<br />
| Class A bits<br />
| rowspan=3 | AMR core frame<br />
|-<br />
| 6<br />
| CRC<br />
|-<br />
| &nbsp;<br />
| Class B bits<br />
|}<br />
<br />
=== Field meaning ===<br />
<br />
{| border=1<br />
! Frame type !! Frame content<br />
|-<br />
| 0 || AMR 4.75kbps<br />
|-<br />
| 1 || AMR 5.15kbps<br />
|-<br />
| 2 || AMR 5.90kbps<br />
|-<br />
| 3 || AMR 6.70kbps (PDC-EFR)<br />
|-<br />
| 4 || AMR 7.40kbps (TDMA-EFR)<br />
|-<br />
| 5 || AMR 7.95kbps<br />
|-<br />
| 6 || AMR 10.2kbps<br />
|-<br />
| 7 || AMR 12.2kbps (GSM-EFR)<br />
|-<br />
| 8 || AMR SID<br />
|-<br />
| 9 || GSM-EFR SID<br />
|-<br />
| 10 || TDMA-EFR SID<br />
|-<br />
| 11 || PDC-EFR SID<br />
|-<br />
| 12-14 || Reserved for future use<br />
|-<br />
| 15 || No data (no transmission/no reception)<br />
|}<br />
<br />
=== Classes ===<br />
<br />
{| border=1<br />
! Class !! Importance explanation<br />
|-<br />
| A || Data that is most sensitive to error. Any error in these bits leads to a corrupted speech frame that should not be decoded without appropriate error concealment. This class of bits is protected by an 8-bit CRC.<br />
|-<br />
| B || Less sensitive data that are present in all speech frames.<br />
|-<br />
| C || Least sensitive data present only in higher bit rate frames.<br />
|}<br />
<br />
* Class A is protected by an 8-bit CRC with polynomial x^8+x^6+x^5+x^4+1 computed over the Class A bits.<br />
* However for AMR-NB over GSM the CRC is a 6-bit with polynomial x^6+x^5+x^3+x^2+x+1 computed over the Class A bits, and it immediately follows the Class A bits, rather than preceding them. That is, it's between the Class A and Class B bits. [see 3GPP TS 05.03 sec 3.9.4.3]<br />
* There is no significant step-wise change in subjective importance at class boundaries.<br />
* The distribution of bits is ordered from most to least subjective importance at both the class level and within the classes.<br />
<br />
{| border=1<br />
! Frame type !! Total bits !! Class A bits !! Class B bits !! Class C bits<br />
|-<br />
| 0 || 95 || 42 || 53 || 0<br />
|-<br />
| 1 || 103 || 49 || 54 || 0<br />
|-<br />
| 2 || 118 || 55 || 63 || 0<br />
|-<br />
| 3 || 134 || 58 || 76 || 0<br />
|-<br />
| 4 || 148 || 61 || 87 || 0<br />
|-<br />
| 5 || 159 || 75 || 84 || 0<br />
|-<br />
| 6 || 204 || 65 || 99 || 40<br />
|-<br />
| 7 || 244 || 81 || 103 || 60<br />
|-<br />
| 8 || 39 || 39 || 0 || 0<br />
|-<br />
| 9 || 43 || 43 || 0 || 0<br />
|-<br />
| 10 || 38 || 38 || 0 || 0<br />
|-<br />
| 11 || 37 || 37 || 0 || 0<br />
|}<br />
<br />
For the specifics of the bit stream layout (i.e. for the bits to parameter mappings) see [http://www.3gpp.org/ftp/Specs/html-info/26101.htm 26.101 AMR speech codec frame structure] and [http://www.3gpp.org/ftp/Specs/html-info/26090.htm 26.090 AMR speech codec transcoding functions].<br />
<br />
== Decoding of LP filter parameters ==<br />
<br />
The received indices of LSP quantization are used to reconstruct the quantified<br />
LSP vectors. (c.f. §5.2.5)<br />
<br />
<br />
=== 12.2kbps mode summary ===<br />
<br />
* indices into code books are parsed from the bit stream<br />
* indices give elements of split matrix quantised (SMQ) residual LSF vectors from the relevant code books<br />
* prediction from the previous frame is added to obtain the mean-removed LSF vectors<br />
* the mean is added<br />
* the LSF vectors are converted to cosine domain LSP vectors<br />
<br />
==== Decoding SMQ residual LSF vectors ====<br />
<br />
The elements of the SMQ vectors are stored at an index into a code book that<br />
varies according to the mode. There are 5 code books for the 12.2kbps mode<br />
corresponding to the 5 indices. These tables will be referred to as:<br />
<br />
<code>lsf_m_n</code><br />
<br />
; m : the number of indices parsed according to the mode<br />
; n : the index 'position' i.e. 1 for the first index, etc<br />
<br />
The 5 indices are stored using 7, 8, 8 + sign bit, 8, 6 bits respectively.<br />
The four elements of a 'split quantized sub-matrix' are stored at the index<br />
position in the appropriate code book are reassigned as follows:<br />
<br />
; lsf_5_1[index1] : r1_1, r1_2, r2_1, r2_2<br />
; lsf_5_2[index2] : r1_3, r1_4, r2_3, r2_4<br />
; lsf_5_3[index3] : r1_5, r1_6, r2_5, r2_6<br />
; lsf_5_4[index4] : r1_7, r1_8, r2_7, r2_8<br />
; lsf_5_5[index5] : r1_9, r1_10, r2_9, r2_10<br />
<br />
With rj_i :<br />
; j : the first or second residual lsf vector<br />
; i : the coefficient of a residual lsf vector ( i = 1, ..., 10 )<br />
<br />
; rj_i : residual line spectral frequencies (LSFs) in Hz<br />
<br />
==== Mean-removed LSF vector prediction ====<br />
<br />
[[Image: Amrnb_lsfmeanrem.gif]]<br />
<br />
; z_j(n) : the mean-removed LSF vector at the jth subframe<br />
; r_j(n) : prediction residual vector of frame n at the jth subframe<br />
; ^r_2(n-1) : the quantified residual vector from the previous frame at the 2nd subframe<br />
<br />
==== The mean is added ====<br />
<br />
[[Image: Amrnb_lsfvec.gif]]<br />
<br />
; lsf_mean_m : a table of the means of the LSF coefficients<br />
; m : the number of indices parsed according to the mode<br />
; fj : the LSF vectors<br />
<br />
==== LSF to LSP vector conversion ====<br />
<br />
[[Image: Amrnb_lspvec.gif]]<br />
<br />
; q_k[i] : the ith coefficient of the kth line spectral pair (LSP) in the cosine domain<br />
; k : the two lsf vectors give the LSP vectors q2, q4 at the 2nd and 4th subframes; k = 2*j<br />
; f_j[i] : ith coefficient of the jth LSF vector; [0,4000] Hz<br />
; f_s : sampling frequency in Hz (8kHz)<br />
<br />
=== Other active modes summary ===<br />
<br />
The process for the other modes is similar to that for the 12.2kbps mode.<br />
<br />
* indices into code books are parsed from the bit stream<br />
* indices give elements of a split matrix quantised (SMQ) residual LSF vector from the relevant code books<br />
* prediction from the previous frame is added to obtain the mean-removed LSF vector<br />
* the mean is added<br />
* the LSF vector is converted to a cosine domain LSP vector<br />
<br />
==== Decoding the SMQ residual LSF vector ====<br />
<br />
The 3 indices are stored with the following numbers of bits:<br />
<br />
{| border=1<br />
! Mode (kbps) !! 1st index (bits) !! 2nd index (bits) !! 3rd index (bits)<br />
|-<br />
| 10.2 || 8 || 9 || 9<br />
|-<br />
| 7.95 || 9 || 9 || 9<br />
|-<br />
| 7.40 || 8 || 9 || 9<br />
|-<br />
| 6.70 || 8 || 9 || 9<br />
|-<br />
| 5.90 || 8 || 9 || 9<br />
|-<br />
| 5.15 || 8 || 8 || 7<br />
|-<br />
| 4.75 || 8 || 8 || 7<br />
|}<br />
<br />
The four elements of a 'split quantized sub-matrix' are stored at the index<br />
position in the appropriate code book are:<br />
<br />
; 1st index in 1st code book : r_1, r_2, r_3<br />
; 2nd index in 2nd code book : r_4, r_5, r_6<br />
; 3rd index in 3rd code book : r_7, r_8, r_9, r_10<br />
<br />
; r_i : residual LSF vector (Hz)<br />
; i : the coefficient of vector ( i = 1, ..., 10 )<br />
<br />
==== Mean-removed LSF vector prediction ====<br />
<br />
[[Image: Amrnb_lsfmeanrem2.gif]]<br />
<br />
; z_j(n)[i] : the ith coefficient of the mean-removed LSF vector at the jth subframe<br />
; r_j(n)[i] : the ith coefficient of the prediction residual vector of frame n at the jth subframe<br />
; pred_fac[i] : the ith coefficient of the prediction factor<br />
; ^r_j(n-1)[i] : the ith coefficient of the the quantified residual vector of the previous frame at the jth subframe<br />
<br />
These processes give the LSP vector at the 4th subframe (q4)<br />
<br />
=== LSP vector interpolation (c.f. §5.2.6) ===<br />
<br />
==== 12.2 kbps mode ====<br />
<br />
[[Image: Amrnb_lspinterpq1-1.gif]]<br />
<br />
[[Image: Amrnb_lspinterpq3-1.gif]]<br />
<br />
==== Other modes ====<br />
<br />
[[Image: Amrnb_lspinterpq1-2.gif]]<br />
<br />
[[Image: Amrnb_lspinterpq2.gif]]<br />
<br />
[[Image: Amrnb_lspinterpq3-2.gif]]<br />
<br />
=== LSP vector to LP filter coefficient conversion (c.f. §5.2.4) ===<br />
<br />
for i=1..5<br />
f1[i] = 2*f1[i-2] - 2*q[2i-1]*f1[i-1]<br />
for j=i-1..1<br />
f1[j] += f1[j-2] - 2*q[2i-1]*f1[j-1]<br />
end<br />
end<br />
<br />
f1[-1] = 0; f1[0] = 0;<br />
<br />
Same for f2[i] with q[2i] instead of q[2i-1]<br />
<br />
for i=1..5<br />
f'1[i] = f1[i] + f1[i-1]<br />
f'2[i] = f2[i] - f2[i-1]<br />
end<br />
<br />
[[Image: Amrnb_lpccoeff.gif]]<br />
<br />
; a_i : the LP filter coefficients<br />
<br />
== Decoding of the pitch (adaptive codebook) vector ==<br />
<br />
* indices parsed from bitstream<br />
* indices give integer and fractional parts of the pitch lag<br />
* pitch vector v(n) is found by interpolating the past excitation u(n) at the pitch lag using an FIR filter. (c.f. §5.6)<br />
<br />
<br />
=== Decode pitch lag ===<br />
<br />
'''Note: division in this section is integer division!'''<br />
<br />
==== 12.2kbps mode - 1/6 resolution pitch lag ====<br />
<br />
===== First and third subframes =====<br />
<br />
In the first and third subframes, a fractional pitch lag is used with resolutions:<br />
<br />
* 1/6 in the range [17 3/6, 94 3/6]<br />
* 1 in the range [95, 143]<br />
<br />
...encoded using 9 bits.<br />
<br />
For [17 3/6, 94 3/6] the pitch index is encoded as:<br />
<br />
pitch_index = (pitch_lag_int - 17)*6 + pitch_lag_frac - 3;<br />
<br />
; pitch_lag_int : integer part of the pitch lag in the range [17, 94]<br />
; pitch_lag_frac : fractional part of the pitch lag in 1/6 units in the range [-2, 3]<br />
<br />
so...<br />
<br />
if(pitch_index < (94 4/6 - 17 3/6)*6)<br />
// fractional part is encoded in range [17 3/6, 94 3/6]<br />
pitch_lag_int = (pitch_index + 5)/6 + 17;<br />
pitch_lag_frac = pitch_index - pitch_lag_int*6 + (17 3/6)*6;<br />
<br />
And for [95, 143] the pitch index is encoded as:<br />
<br />
pitch_index = (pitch_lag_int - 95) + (94 4/6 - 17 3/6)*6;<br />
<br />
; pitch_lag_int : integer pitch lag in the range [95, 143]<br />
<br />
so...<br />
<br />
else<br />
// only integer part encoded in range [95, 143], no fractional part<br />
pitch_lag_int = pitch_index - (94 4/6 - 17 3/6)*6 + 95;<br />
pitch_lag_frac = 0;<br />
<br />
===== Second and fourth subframes =====<br />
<br />
In the second and fourth subframes, a pitch lag resolution of 1/6 is always used in the range [T1 - 5 3/6, T1 + 4 3/6], where T1 is nearest integer to the fractional pitch lag of the previous (1st or 3rd) subframe. The search range is bounded by [18, 143]. In this case the pitch delay is encoded using 6 bits and is therefore in the range [0,63].<br />
<br />
So the search range for the pitch lag is:<br />
<br />
search_range_min = max(pitch_lag_int_prev - 5, 18);<br />
search_range_max = search_range_min + 9;<br />
if(search_range_max > 143) {<br />
search_range_max = 143;<br />
search_range_min = search_range_max - 9;<br />
}<br />
<br />
; pitch_lag_int_prev : the integer part of the pitch lag from the previous sub frame<br />
<br />
The pitch index is encoded as:<br />
<br />
pitch_index = (pitch_lag_int - (search_range_min - 1))*6 + pitch_lag_frac - 3;<br />
<br />
; pitch_lag_int : the integer part of the pitch lag in the range [search_range_min - 1, search_range_max]<br />
; pitch_lag_frac : the fractional part of the pitch lag in the range [-2, 3]<br />
<br />
The formula for the pitch_index has been chosen to map pitch_lag_int [search_range_min - 1, search_range_max] and pitch_lag_frac [-2, 3] to [0,60]. (pitch_index = [0, 10]*6 + [-2, 3] - 3 = [0, 6, ..., 60] + [-5, 0] = [0,60])<br />
<br />
So the pitch lag is calculated through:<br />
<br />
// integer part of pitch lag = position in range [search_range_min - 1, search_range_max] + lower bound of range<br />
pitch_lag_int = (pitch_index + 5)/6 + search_range_min - 1;<br />
// fractional part of pitch lag = pitch index - (integer part without offset)*6 - 1 3/6 offset to bring the values to the correct range<br />
pitch_lag_frac = pitch_index - ((pitch_index + 5)/6)*6 - 9;<br />
<br />
Note that when using integers and integer division to conduct (pitch_index + 5)/6 the result is similar to taking the ceiling of pitch_index/6.0.<br />
<br />
==== Others modes - 1/3 resolution pitch lag ====<br />
<br />
===== First and third subframes =====<br />
<br />
In the first and third subframes, a fractional pitch lag is used with resolutions:<br />
<br />
* 1/3 in the range [19 1/3, 84 2/3]<br />
* 1 in the range [85, 143]<br />
<br />
...encoded using 8 bits.<br />
<br />
For [19 1/3, 84 2/3] the pitch lag is encoded as:<br />
<br />
pitch_index = pitch_lag_int*3 + pitch_lag_frac - (19 1/3)*3;<br />
<br />
; pitch_lag_int : integer part of the pitch lag in the range [19, 84]<br />
; pitch_lag_frac : fractional part of the pitch lag in 1/3 units in the range [0, 2]<br />
<br />
so...<br />
<br />
if(pitch_index < (85 - 19 1/3)*3)<br />
// fractional part is encoded in range [19 1/3, 84 2/3]<br />
pitch_lag_int = (pitch_index + 2)/3 + 19;<br />
pitch_lag_frac = pitch_index - pitch_lag_int*3 + (19 1/3)*3;<br />
<br />
And for [85, 143] the pitch index is encoded as:<br />
<br />
pitch_index = pitch_lag_int - 85 + (85 - 19 1/3)*3;<br />
<br />
; pitch_lag_int : integer pitch lag in the range [85, 143]<br />
<br />
so...<br />
<br />
else<br />
// only integer part encoded in range [85, 143], no fractional part<br />
pitch_lag_int = pitch_index - (85 - 19 1/3)*3 + 85;<br />
pitch_lag_frac = 0;<br />
<br />
===== Second and fourth subframes =====<br />
<br />
In the second and fourth subframes, the pitch lag resolution varies depending on the mode as follows:<br />
<br />
* 7.95 kbps mode<br />
** resolution of 1/3 is always used in the range [T1 - 10 2/3, T1 + 9 2/3]<br />
** encoded using 6 bits => pitch_index is in the range [0, 63]<br />
* 10.2 and 7.40 kbps modes<br />
** resolution of 1/3 is always used in the range [T1 - 5 2/3, T1 + 4 2/3]<br />
** encoded using 5 bits => pitch_index is in the range [0, 31]<br />
* 6.70, 5.90, 5.15 and 4.75 kbps modes<br />
** resolution of 1 is used in the range [T1 - 5, T1 + 4]<br />
** resolution of 1/3 is always used in the range [T1 - 1 2/3, T1 + 2/3]<br />
** encoded using 4 bits => pitch_index is in the range [0, 15]<br />
<br />
Where T1 is nearest integer to the fractional pitch lag of the previous (1st or 3rd) subframe. The search range is bounded by [20, 143].<br />
<br />
So the search range for the pitch lag is:<br />
<br />
lower_bound = 5;<br />
range = 9;<br />
if(mode == 7.95) {<br />
lower_bound = 10;<br />
range = 19;<br />
}<br />
search_range_min = max(pitch_lag_int_prev - lower_bound, 20);<br />
search_range_max = search_range_min + range;<br />
if(search_range_max > 143) {<br />
search_range_max = 143;<br />
search_range_min = search_range_max - range;<br />
}<br />
<br />
; pitch_lag_int_prev : the integer part of the pitch lag from the previous sub frame<br />
<br />
<br />
For modes 7.40, 7.95 and 10.2 the pitch index is encoded as:<br />
<br />
pitch_index = (pitch_lag_int - search_range_min)*3 + pitch_lag_frac + 2;<br />
<br />
; pitch_lag_int : the integer part of the pitch lag in the range [search_range_min, search_range_max]<br />
; pitch_lag_frac : the fractional part of the pitch lag in the range [-1, 1]<br />
<br />
So the pitch lag is calculated through:<br />
<br />
// integer part of pitch lag = position of pitch lag in range [search_range_min, search_range_max] + lower bound of the range<br />
pitch_lag_int = (pitch_index + 2)/3 - 1 + search_range_min;<br />
// fractional part of pitch lag = pitch index - (integer part without offset)*3 - 2/3 to bring the values to the correct range<br />
pitch_lag_frac = pitch_index - ((pitch_index + 2)/3 - 1)*3 - 2;<br />
<br />
<br />
For modes 4.75, 5.15, 5.90 and 6.70:<br />
<br />
t1_temp = max( min(pitch_lag_int_prev, search_range_min + 5), search_range_max - 4 );<br />
<br />
; t1_temp : predicted pitch lag from the previous frame adjusted to fit into the 0 position of the search range<br />
<br />
The pitch index is encoded as:<br />
<br />
// if pitch lag is below T1 - 1 2/3<br />
if( pitch_lag_int*3 + pitch_lag_frac <= (t1_temp - 2)*3 ) {<br />
// encode with resolution 1<br />
index = (pitch_lag_int - t1_temp) + 5;<br />
// else if pitch lag is below T1 + 1<br />
}else if( pitch_lag_int*3 + pitch_lag_frac < (t1_temp + 1)*3 ) {<br />
// encode with resolution 1/3<br />
index = ( pitch_lag_int*3 + pitch_lag_frac - (t1_temp - 2)*3 ) + 3;<br />
// else pitch lag is above T1 + 2/3<br />
}else {<br />
// encode with resolution 1<br />
index = (pitch_lag_int - t1_temp) + 11;<br />
}<br />
<br />
; pitch_lag_int : the integer part of the pitch lag in the range [search_range_min, search_range_max]<br />
; pitch_lag_frac : the fractional part of the pitch lag in the range [-1, 1]<br />
<br />
The possible pitch indices and values are:<br />
<br />
{| border=1<br />
|-<br />
| 0 || 1 || 2 || 3 || 4 || 5 || 6 || 7 || 8 || 9 || 10 || 11 || 12 || 13 || 14 || 15<br />
|-<br />
| -5 || -4 || -3 || -2 || -1 2/3 || -1 1/3 || -1 || -2/3 || -1/3 || 0 || 1/3 || 2/3 || 1 || 2 || 3 || 4<br />
|-<br />
|}<br />
<br />
So the pitch lag is calculated through:<br />
<br />
if(pitch_index < 4) {<br />
// integer part of pitch lag = pitch lag position in range [t1_temp - 5, t1_temp - 2] + lower bound of range<br />
pitch_lag_int = pitch_index + (t1_temp - 5);<br />
// this range is coded with resolution 1 so no fractional part<br />
pitch_lag_frac = 0;<br />
}else if(pitch_index < 12) {<br />
pitch_lag_int = (pitch_index - 2)/3 + (t1_temp - 2);<br />
pitch_lag_frac = (pitch_index - 4) - ((pitch_index - 2)/3)*3 - 11;<br />
}else {<br />
// integer part of pitch lag = pitch lag position in range [t1_temp + 1, t1_temp + 4] + lower bound of range<br />
pitch_lag_int = pitch_index - 12 + t1_temp + 1;<br />
// this range is coded with resolution 1 so no fractional part<br />
pitch_lag_frac = 0;<br />
}<br />
<br />
<br />
=== Calculate pitch vector ===<br />
<br />
[[Image: Amrnb_pitchvector.gif]]<br />
<br />
; k : integer pitch lag<br />
; n : sample position in the vectors 0, ..., 39<br />
; t : 0, ..., 5 corresponding to fractions 0, 1/6, 2/6, 3/6, -2/6, -1/6 respectively<br />
<br />
This equation can be used for both 1/3 and 1/6 resolution simply by multiplying t by 2 in the 1/3 case.<br />
<br />
(Note: the coefficients b60 are in the reference source in an array called inter6)<br />
<br />
<br />
<br />
== Decoding of the fixed (innovative or algebraic) vector ==<br />
<br />
* the excitation pulse positions and signs are parsed from the bit stream<br />
* the pulse positions and signs are encoded differently depending on the mode<br />
* the fixed code book vector, c(n), is then constructed from the pulse positions and signs<br />
* if pitch_lag_int is less than the subframe size (40), the pitch sharpening procedure is applied<br />
<br />
<br />
=== Decoding the pulse positions ===<br />
<br />
==== 12.2 kbps mode ====<br />
<br />
* 10 pulse positions each coded using 3 bit Gray codes<br />
* signs coded using 1 bit each for 5 pulse pairs<br />
<br />
<br />
{| border=1<br />
! Pulse !! Positions<br />
|-<br />
| i0,i5 || 0, 5, 10, 15, 20, 25, 30, 35<br />
|-<br />
| i1,i6 || 1, 6, 11, 16, 21, 26, 31, 36<br />
|-<br />
| i2,i7 || 2, 7, 12, 17, 22, 27, 32, 37<br />
|-<br />
| i3,i8 || 3, 8, 13, 18, 23, 28, 33, 38<br />
|-<br />
| i4,i9 || 4, 9, 14, 19, 24, 29, 34, 39<br />
|-<br />
|}<br />
<br />
<br />
==== 10.2 kbps mode ====<br />
<br />
* 8 pulse positions, 4 pairs, coded as 3 values using 10, 10 and 7 bits<br />
* signs coded using 1 bit each for 4 pulse pairs<br />
<br />
<br />
{| border=1<br />
! Pulse !! Positions<br />
|-<br />
| i0,i4 || 0, 4, 8, 12, 16, 20, 24, 28, 32, 36<br />
|-<br />
| i1,i5 || 1, 5, 9, 13, 17, 21, 25, 29, 33, 37<br />
|-<br />
| i2,i6 || 2, 6, 10, 14, 18, 22, 26, 30, 34, 38<br />
|-<br />
| i3,i7 || 3, 7, 11, 15, 19, 23, 27, 31, 35, 39<br />
|-<br />
|}<br />
<br />
<br />
==== 7.95 and 7.40 kbps modes ====<br />
<br />
* 4 pulse positions Gray coded using 3, 3, 3 and 4 bits<br />
* signs coded using 1 bit for each pulse<br />
<br />
<br />
{| border=1<br />
! Pulse !! Positions<br />
|-<br />
| i0 || 0, 5, 10, 15, 20, 25, 30, 35<br />
|-<br />
| i1 || 1, 6, 11, 16, 21, 26, 31, 36<br />
|-<br />
| i2 || 2, 7, 12, 17, 22, 27, 32, 37<br />
|-<br />
| i3 || 3, 8, 13, 18, 23, 28, 33, 38<br />
4, 9, 14, 19, 24, 29, 34, 39<br />
|-<br />
|}<br />
<br />
==== 6.70 kbps mode ====<br />
<br />
* 3 pulse positions coded using 3, 4 and 4 bits<br />
* signs coded using 1 bit for each pulse<br />
<br />
<br />
{| border=1<br />
! Pulse !! Positions<br />
|-<br />
| i0 || 0, 5, 10, 15, 20, 25, 30, 35<br />
|-<br />
| i1 || 1, 6, 11, 16, 21, 26, 31, 36<br />
3, 8, 13, 18, 23, 28, 33, 38<br />
|-<br />
| i2 || 2, 7, 12, 17, 22, 27, 32, 37<br />
4, 9, 14, 19, 24, 29, 34, 39<br />
|-<br />
|}<br />
<br />
<br />
==== 5.90 kbps mode ====<br />
<br />
* 2 pulse positions coded using 4 and 5 bits<br />
* signs coded using 1 bit for each pulse<br />
<br />
<br />
{| border=1<br />
! Pulse !! Positions<br />
|-<br />
| i0 || 1, 6, 11, 16, 21, 26, 31, 36<br />
3, 8, 13, 18, 23, 28, 33, 38<br />
|-<br />
| i1 || 0, 5, 10, 15, 20, 25, 30, 35<br />
1, 6, 11, 16, 21, 26, 31, 36<br><br />
2, 7, 12, 17, 22, 27, 32, 37<br><br />
4, 9, 14, 19, 24, 29, 34, 39<br />
|-<br />
|}<br />
<br />
==== 5.15 and 4.75 kbps modes ====<br />
<br />
* 2 pulse positions coded using 1 bit for the position subset and 3 bits per pulse<br />
* signs coded using 1 bit for each pulse<br />
<br />
<br />
{| border=1<br />
! Subframe !! Subset !! Pulse !! Positions<br />
|-<br />
| rowspan="4" align="center" | 1<br />
| rowspan="2" align="center" | 1<br />
| i0 || 0, 5, 10, 15, 20, 25, 30, 35<br />
|-<br />
| i1 || 2, 7, 12, 17, 22, 27, 32, 37<br />
|-<br />
| rowspan="2" align="center" | 2<br />
| i0 || 1, 6, 11, 16, 21, 26, 31, 36<br />
|-<br />
| i1 || 3, 8, 13, 18, 23, 28, 33, 38<br />
|-<br />
| rowspan="4" align="center" | 2<br />
| rowspan="2" align="center" | 1<br />
| i0 || 0, 5, 10, 15, 20, 25, 30, 35<br />
|-<br />
| i1 || 3, 8, 13, 18, 23, 28, 33, 38<br />
|-<br />
| rowspan="2" align="center" | 2<br />
| i0 || 2, 7, 12, 17, 22, 27, 32, 37<br />
|-<br />
| i1 || 4, 9, 14, 19, 24, 29, 34, 39<br />
|-<br />
| rowspan="4" align="center" | 3<br />
| rowspan="2" align="center" | 1<br />
| i0 || 0, 5, 10, 15, 20, 25, 30, 35<br />
|-<br />
| i1 || 2, 7, 12, 17, 22, 27, 32, 37<br />
|-<br />
| rowspan="2" align="center" | 2<br />
| i0 || 1, 6, 11, 16, 21, 26, 31, 36<br />
|-<br />
| i1 || 4, 9, 14, 19, 24, 29, 34, 39<br />
|-<br />
| rowspan="4" align="center" | 4<br />
| rowspan="2" align="center" | 1<br />
| i0 || 0, 5, 10, 15, 20, 25, 30, 35<br />
|-<br />
| i1 || 3, 8, 13, 18, 23, 28, 33, 38<br />
|-<br />
| rowspan="2" align="center" | 2<br />
| i0 || 1, 6, 11, 16, 21, 26, 31, 36<br />
|-<br />
| i1 || 4, 9, 14, 19, 24, 29, 34, 39<br />
|-<br />
|}<br />
<br />
<br />
=== Fixed codebook vector construction ===<br />
<br />
All c(n) are zero if there is no pulse at position n. If there is a pulse at position n then it has the corresponding sign as parsed above.<br />
<br />
<br />
=== Pitch sharpening ===<br />
<br />
[[Image: Amrnb fixvecpitshp.gif]]<br />
<br />
; β : the decoded pitch gain, ^g_p, bounded by [0.0,1.0] for 12.2. kbps or [0.0,0.8] for other modes<br />
<br />
Note: Only sharpen n in [pitch_lag_int-1, 39]. The reference source considers previous fixed vector elements (n<0) to be 0. I think this is also the reason pitch sharpening is not conducted for pitch_lag_int > 40 as all the sharpening contributions would be 0.<br />
<br />
== Decoding of the pitch and fixed codebook gains ==<br />
<br />
<br />
=== Fixed gain prediction ===<br />
<br />
A moving average prediction of the innovation (fixed) energy is conducted.<br />
<br />
<br />
[[Image: Amrnb_fixedgainpred.gif]]<br />
<br />
; g_c' : fixed gain prediction<br />
; \tilde{E} : predicted energy [dB]<br />
; \bar{E} : desired mean innovation (fixed) energy [dB]<br />
; E_I : calculated mean innovation (fixed) energy [dB]<br />
<br />
<br />
[[Image: Amrnb_predener.gif]]<br />
<br />
; b : 4-tap MA prediction coefficients [0.68, 0.58, 0.34, 0.19]<br />
; ^R(k) : quantified prediction errors at subframe k 20*log10(^γ_gc(k))<br />
<br />
<br />
Desired mean innovation (fixed) energy:<br />
<br />
{| border=1<br />
! Mode (kbps) !! Mean energy (dB)<br />
|-<br />
| 12.2 || 36<br />
|-<br />
| 10.2 || 33<br />
|-<br />
| 7.95 || 36<br />
|-<br />
| 7.40 || 30<br />
|-<br />
| 6.70 || 28.75<br />
|-<br />
| 5.90 || 33<br />
|-<br />
| 5.15 || 33<br />
|-<br />
| 4.75 || 33<br />
|-<br />
|}<br />
<br />
<br />
[[Image: Amrnb_meaniner.gif]]<br />
<br />
; E_I : calculated mean innovation (fixed) energy [dB]<br />
; N : subframe size 40<br />
; c(n) : fixed codebook vector<br />
<br />
=== Dequantisation of the gains ===<br />
<br />
==== 12.2kbps and 7.95kbps - scalar quantised gains ====<br />
<br />
The received indices are used to find the quantified pitch gain, ^g_p, and the quantified fixed gain correction factor, ^γ_gc.<br />
<br />
===== Pitch gain =====<br />
<br />
The parsed gain index is used to obtain the quantified pitch gain, ^g_p, from the corresponding codebook. (qua_gain_pitch in the reference source.)<br />
<br />
===== Fixed gain correction factor =====<br />
<br />
The parsed gain index is used to obtain the quantified fixed gain, ^g_c, from the corresponding codebook. (qua_gain_code in the reference source.) The table stores ^γ_gc, and the quantised energy error in two forms (these are needed for the moving average calculation of the predicted fixed gain):<br />
<br />
qua_ener_MR122 = log2(^γ_gc)<br />
qua_ener = 20*log10(^γ_gc)<br />
<br />
^γ_gc is stored at Q11 (i.e. it's multiplied by 2^11.) qua_ener_MR122 and qua_ener are stored at Q10.<br />
<br />
==== Other modes - vector quantised gains ====<br />
<br />
The received index gives both the quantified adaptive codebook gain, ^g_p, and the quantified algebraic codebook gain correction factor, ^γ_gc.<br />
<br />
The tables contains the following data:<br />
<br />
^g_p (Q14),<br />
^γ_gc (Q12), (^g_c = g_c'*^γ_gc),<br />
qua_ener_MR122 (Q10), (log2(^γ_gc))<br />
qua_ener (Q10) (20*log10(^γ_gc))<br />
<br />
The log2() and log10() values are calculated on the fixed point value<br />
(g_fac Q12) and not on the original floating point value of g_fac<br />
to make the quantizer/MA predictdor use corresponding values.<br />
<br />
The codebook used depends on the mode:<br />
<br />
6.70, 7.40, 10.2 kbps modes - table_gain_highrates in the reference source<br />
Four consecutive entries give ^g_p, ^γ_gc, qua_ener_MR122 and qua_ener. Apparently the values for qua_ener are the original ones from IS641 to ensure bit-exactness but are not exactly the rounded value of 20log10(^γ_gc).<br />
<br />
5.15, 5.90 kbps modes - table_gain_lowrates<br />
Similar to table_gain_highrates, four consecutive entries give ^g_p, ^γ_gc, qua_ener_MR122 and qua_ener. There are no special notes for this table.<br />
<br />
4.75 kbps mode - table_gain_MR475<br />
Unlike the above mentioned tables, four consecutive values give ^g_p, ^γ_gc for subframes 0,2, and then ^g_p, ^γ_gc for subframes 1,3.<br />
<br />
At the very least I think these tables could be redesigned a little.<br />
<br />
=== Calculation of the quantified fixed gain ===<br />
<br />
[[Image: Amrnb_fixedgain.gif]]<br />
<br />
== Smoothing of the fixed codebook gain ==<br />
<br />
<br />
'''10.2, 6.70, 5.90, 5.15, 4.75 kbit/s modes only'''<br />
<br />
<br />
=== Calculate averaged LSP vector ===<br />
<br />
[[Image: Amrnb_lspsmooth.gif]]<br />
<br />
; \bar{q}(n) : averaged LSP vector at frame n<br />
; ^q_4(n) : quantified LSP vector for the 4th subframe at frame n<br />
<br />
<br />
=== Calculate fixed gain smoothing factor ===<br />
<br />
[[Image: Amrnb_lspdiff.gif]]<br />
<br />
; diff_m : difference measure at subframe m<br />
; j : loops over LSPs<br />
; m : loops over subframes<br />
; ^q_m : quantified LSP vector at subframe m<br />
<br />
Note: I think the sum over the subframes is an error in the specification and they actually mean to use the quantified LSP vector from subframe m for the calculation of diff for subframe m. I think this is what they do in the reference source and the sum over m is an error.<br />
<br />
<br />
[[Image: Amrnb_smoothfac.gif]]<br />
<br />
; k_m : fixed gain smoothing factor<br />
; K_1 : 0.4<br />
; K_2 : 0.25<br />
; diff_m : difference measure at subframe m<br />
<br />
If diff_m has been greater than 0.65 for 10 subframes, k_m is set to 1.0 (i.e. no smoothing) for 40 subframes.<br />
<br />
=== Calculate mean fixed gain ===<br />
<br />
[[Image: Amrnb_meangc.gif]]<br />
<br />
; \bar{g_c}(m) : mean fixed gain at subframe m<br />
; ^g_c(k) : quantified fixed gain at subframe k<br />
<br />
=== Calculate smoothed fixed gain ===<br />
<br />
[[Image: Amrnb_gcsmooth.gif]]<br />
<br />
; ^g_c : quantified fixed gain<br />
; \bar{g_c} : averaged fixed gain<br />
<br />
== Anti-sparseness processing ==<br />
<br />
<br />
'''7.95, 6.70, 5.90, 5.15, 4.75 kbit/s modes only'''<br />
<br />
<br />
=== Evaluate impulse response filter strength ===<br />
<br />
The fixed vector, c(n), has only a few pulses per subframe. In certain conditions, to reduce perceptual artifacts arising from this, the vector is circularly convolved with a predefined impulse response. The selection of the strength of filter is made based on the decoded gains.<br />
<br />
if ^g_p < 0.6<br />
impNr = 0<br />
else if ^g_p < 0.9<br />
impNr = 1<br />
else<br />
impNr = 2<br />
<br />
; impNr = 0 : strong impulse response filter<br />
; impNr = 1 : medium impulse response filter<br />
; impNr = 2 : no filtering<br />
<br />
if ^g_c(k) > 2 * ^g_c(k-1)<br />
impNr = min( impNr + 1, 2 )<br />
else if impNr = 0 AND median of last five ^g_p >= 0.6<br />
impNr = min( impNr(k), min(impNr(k-1) + 1, 2) )<br />
<br />
<br />
=== Circular convolution of fixed vector and impulse response filter ===<br />
<br />
[[Image: Amrnb_circconv.gif]]<br />
<br />
; (c * h) : convolution of vectors c and h, in this case circular convolution<br />
; h[n] : nth coefficient of the impulse response used for filtering<br />
; c[n] : nth coefficient of the fixed vector<br />
<br />
n = 0, ..., 39<br />
<br />
To make the convolution circular, make the impulse response circular by taking h[-m] = h[39-m]<br />
<br />
== Computing the reconstructed speech ==<br />
<br />
<br />
=== Construct excitation ===<br />
<br />
[[Image: Amrnb_excitation.gif]]<br />
<br />
; u(n) : excitation vector<br />
; ^g_p : quantified pitch gain<br />
; v(n) : pitch vector<br />
; ^g_c : quantified fixed gain<br />
; c(n) : fixed vector<br />
<br />
<br />
=== Emphasise pitch vector contribution ===<br />
<br />
This is apparently a post-processing technique.<br />
<br />
[[Image: Amrnb_pitchvecemph.gif]]<br />
<br />
; ^u(n) : excitation vector with emphasised pitch vector contribution<br />
; β : ^g_p bounded by [0.0, 0.8] or [0.0, 1.0] depending on mode<br />
<br />
<br />
=== Apply adaptive gain control (AGC) through gain scaling ===<br />
<br />
[[Image: Amrnb_gainfac.gif]]<br />
<br />
; η : gain scaling factor for emphasised excitation<br />
<br />
[[Image: Amrnb_gainscalexc.gif]]<br />
<br />
; ^u'(n) : gain-scaled emphasised excitation<br />
<br />
<br />
=== Calculate reconstructed speech samples ===<br />
<br />
[[Image: Amrnb_synthfilter.gif]]<br />
<br />
; ^s(n) : reconstructed speech samples<br />
; ^a_i : LP filter coefficients<br />
<br />
Note: for n-i < 0, ^s(n-i) should be take from previous speech samples if existing, else we will consider them 0 as this behaviour is undefined the specification.<br />
<br />
== Additional instability protection ==<br />
<br />
If an overflow occurs during synthesis, the pitch vector, v(n), is scaled down by a factor of 4 and synthesis is conducted again bypassing emphasising the pitch vector contribution and adaptive gain control.<br />
<br />
Q: What classifies an overflow?<br />
A: s(n) < -32768 or s(n) > 32767 (i.e. 16-bit signed int)<br />
<br />
== Post-processing ==<br />
<br />
=== Adaptive post-filtering ===<br />
<br />
(c.f. §6.2.1)<br />
<br />
==== IIR filtering ====<br />
<br />
The speech samples, ^s(n), are filtered through a formant filter and a tilt compensation filter.<br />
<br />
[[Image: Amrnb_pfformant.gif]]<br />
<br />
; γ_n, γ_d : control the amount of formant post-filtering<br />
<br />
The speech samples, ^s(n), are filtered through ^A(z/γ_n) to produce the residual signal ^r(n).<br />
<br />
[[Image: Amrnb_residual.gif]]<br />
<br />
^r(n) is filtered through 1/^A(z/γ_d).<br />
<br />
[[Image: Amrnb_fmntfilt.gif]]<br />
<br />
The output is filtered through H_t(z) (the tilt compensation filter) resulting in the post-filtered speech signal, ^s_f(n).<br />
<br />
[[Image: Amrnb_pftilt.gif]]<br />
<br />
[[Image: Amrnb_mu.gif]]<br />
<br />
[[Image: Amrnb_reffac.gif]]<br />
<br />
[[Image: Amrnb_r_h.gif]]<br />
<br />
; L_h : 22 - the truncation of the impulse response<br />
; h_f : impulse response of the formant filter H_f<br />
<br />
So, in summation notation:<br />
<br />
[[Image: Amrnb_pftiltsum.gif]]<br />
<br />
===== 12.2 and 10.2 kbps modes =====<br />
<br />
γ_n = 0.7<br />
<br />
γ_d = 0.75<br />
<br />
γ_t = 0.8 if k_1' > 0; 0 otherwise<br />
<br />
===== Other modes =====<br />
<br />
γ_n = 0.55<br />
<br />
γ_d = 0.7<br />
<br />
γ_t = 0.8<br />
<br />
==== Adaptive gain control ====<br />
<br />
Adaptive gain control is used to compensate for the gain difference between the filtered and synthesised speech signals.<br />
<br />
[[Image: Amrnb_gainscalfac.gif]]<br />
<br />
[[Image: Amrnb_gainscalfacwtd.gif]]<br />
<br />
[[Image: Amrnb_gainscalspch.gif]]<br />
<br />
; \alpha : adaptive gain control factor equal to 0.9<br />
<br />
=== High-pass filtering and upscaling ===<br />
<br />
(c.f. §6.2.2)<br />
<br />
[[Image: Amrnb_highpass.gif]]<br />
<br />
After completing all filtering, the samples are scaled up by a factor of 2.<br />
<br />
<br />
<br />
<br />
[[Category:Vocoders]]<br />
[[Category:Audio Codecs]]</div>Adam Quantrillhttps://wiki.multimedia.cx/index.php?title=AMR-NB&diff=14381AMR-NB2013-03-05T18:04:07Z<p>Adam Quantrill: /* GSM OTA format */</p>
<hr />
<div>* samples: http://samples.mplayerhq.hu/A-codecs/amr/<br />
* specification: http://www.3gpp.org/ftp/Specs/html-info/26-series.htm<br />
* Wikipedia article: http://en.wikipedia.org/wiki/Adaptive_Multi-Rate<br />
<br />
AMR-NB (Adaptive Multi-Rate Narrowband) is a [[vocoder]] employed in low-bitrate applications like mobile phones. It is a form of ACELP where A stands for algebraic.<br />
<br />
----<br />
<br />
The following text aims to be a simpler and more explicit document of the AMR narrow band decoding processes to aid in development of a decoder. Reference to sections of the specification will be made in the following format: (c.f. §5.2.5). Happy reading.<br />
<br />
<br />
<br />
== Nomenclature weirdness ==<br />
<br />
Throughout the specification, a number of references are made to the same (or very similar) items with fairly confusing variation. They are listed below to aid understanding of the following text but efforts will be made to consistently use one item name throughout or to use both with the lesser used name in parenthesis.<br />
<br />
* Pitch / Adaptive codebook<br />
* Fixed / Innovative (also algebraic when referring to the codebook)<br />
* Quantified means quantized<br />
<br />
<br />
== Summary ==<br />
<br />
* Mode dependent bitstream parsing<br />
* Indices parsed from bitstream<br />
* Indices decoded to give LSF vectors, fractional pitch lags, innovative code vectors and the pitch and innovative gains<br />
* LSF vectors converted to LP filter coefficients at each subframe<br />
* Subframe decoding<br />
** Excitation vector = adaptive code vector * adaptive (pitch) gain + innovative code vector * innovative gain<br />
** Excitation vector filtered through an LP synthesis filter to reconstruct speech<br />
** Speech signal filtered with adaptive postfilter<br />
<br />
<br />
<br />
== Bit stream frame format ==<br />
Specification ([http://www.3gpp.org/ftp/Specs/html-info/26101.htm 26.101]) describes two possible frame types - interface formats 1 and 2 (often abbreviated IF1 and IF2). IF2 is byte-aligned. The following tables of data are taken from this specification unless otherwise stated. AMR-NB over GSM uses a third slightly different format whose bit-packing is tightly defined by the GSM specs.<br />
<br />
=== IF1 format ===<br />
<br />
{| border=1<br />
! bits !! low level meaning || high level meaning<br />
|-<br />
| 4<br />
| Frame type<br />
| rowspan=2 | AMR header<br />
|-<br />
| 1<br />
| Frame quality indicator (0 bad/1 good)<br />
|-<br />
| 3<br />
| Mode indication<br />
| rowspan=3 | AMR auxiliary information<br />
|-<br />
| 3<br />
| Mode request<br />
|-<br />
| 8<br />
| CRC<br />
|-<br />
| &nbsp;<br />
| Class A bits<br />
| rowspan=3 | AMR core frame<br />
|-<br />
| &nbsp;<br />
| Class B bits<br />
|-<br />
| &nbsp;<br />
| Class C bits<br />
|}<br />
<br />
=== IF2 format ===<br />
<br />
{| border=1<br />
! bits !! low level meaning || high level meaning<br />
|-<br />
| 4<br />
| Frame type<br />
| AMR header<br />
|-<br />
| &nbsp;<br />
| Class A bits<br />
| rowspan=3 | AMR core frame<br />
|-<br />
| &nbsp;<br />
| Class B bits<br />
|-<br />
| &nbsp;<br />
| Class C bits<br />
|-<br />
| &nbsp;<br />
| Padding (called "Bit stuffing" in the specification)<br />
| &nbsp;<br />
|}<br />
<br />
=== GSM OTA format ===<br />
<br />
{| border=1<br />
! bits !! low level meaning || high level meaning<br />
|-<br />
| 4<br />
| Mode request<br />
| rowspan=1 | AMR auxiliary information <br />
|-<br />
| 1<br />
| F bit (0 good/1 unusual)<br />
| rowspan=3 | AMR header<br />
|-<br />
| 4<br />
| Mode indication<br />
|-<br />
| 1<br />
| Frame quality indicator (0 bad/1 good)<br />
|-<br />
| &nbsp;<br />
| Class A bits<br />
| rowspan=3 | AMR core frame<br />
|-<br />
| 6<br />
| CRC<br />
|-<br />
| &nbsp;<br />
| Class B bits<br />
|}<br />
<br />
=== Field meaning ===<br />
<br />
{| border=1<br />
! Frame type !! Frame content<br />
|-<br />
| 0 || AMR 4.75kbps<br />
|-<br />
| 1 || AMR 5.15kbps<br />
|-<br />
| 2 || AMR 5.90kbps<br />
|-<br />
| 3 || AMR 6.70kbps (PDC-EFR)<br />
|-<br />
| 4 || AMR 7.40kbps (TDMA-EFR)<br />
|-<br />
| 5 || AMR 7.95kbps<br />
|-<br />
| 6 || AMR 10.2kbps<br />
|-<br />
| 7 || AMR 12.2kbps (GSM-EFR)<br />
|-<br />
| 8 || AMR SID<br />
|-<br />
| 9 || GSM-EFR SID<br />
|-<br />
| 10 || TDMA-EFR SID<br />
|-<br />
| 11 || PDC-EFR SID<br />
|-<br />
| 12-14 || Reserved for future use<br />
|-<br />
| 15 || No data (no transmission/no reception)<br />
|}<br />
<br />
=== Classes ===<br />
<br />
{| border=1<br />
! Class !! Importance explanation<br />
|-<br />
| A || Data that is most sensitive to error. Any error in these bits leads to a corrupted speech frame that should not be decoded without appropriate error concealment. This class of bits is protected by an 8-bit CRC.<br />
|-<br />
| B || Less sensitive data that are present in all speech frames.<br />
|-<br />
| C || Least sensitive data present only in higher bit rate frames.<br />
|}<br />
<br />
* Class A is protected by an 8-bit CRC with polynomial x^8+x^6+x^5+x^4+1 computed over the Class A bits.<br />
* However for AMR-NB over GSM the CRC is a 6-bit with polynomial x^6+x^5+x^3+x^2+x+1 computed over the Class A bits, and it immediately follows the Class A bits, rather than preceding them. That is, it's between the Class A and Class B bits. [see 3GPP TS 05.03 sec 3.9.4.3]<br />
* There is no significant step-wise change in subjective importance at class boundaries.<br />
* The distribution of bits is ordered from most to least subjective importance at both the class level and within the classes.<br />
<br />
{| border=1<br />
! Frame type !! Total bits !! Class A bits !! Class B bits !! Class C bits<br />
|-<br />
| 0 || 95 || 42 || 53 || 0<br />
|-<br />
| 1 || 103 || 49 || 54 || 0<br />
|-<br />
| 2 || 118 || 55 || 63 || 0<br />
|-<br />
| 3 || 134 || 58 || 76 || 0<br />
|-<br />
| 4 || 148 || 61 || 87 || 0<br />
|-<br />
| 5 || 159 || 75 || 84 || 0<br />
|-<br />
| 6 || 204 || 65 || 99 || 40<br />
|-<br />
| 7 || 244 || 81 || 103 || 60<br />
|-<br />
| 8 || 39 || 39 || 0 || 0<br />
|-<br />
| 9 || 43 || 43 || 0 || 0<br />
|-<br />
| 10 || 38 || 38 || 0 || 0<br />
|-<br />
| 11 || 37 || 37 || 0 || 0<br />
|}<br />
<br />
For the specifics of the bit stream layout (i.e. for the bits to parameter mappings) see [http://www.3gpp.org/ftp/Specs/html-info/26101.htm 26.101 AMR speech codec frame structure] and [http://www.3gpp.org/ftp/Specs/html-info/26090.htm 26.090 AMR speech codec transcoding functions].<br />
<br />
== Decoding of LP filter parameters ==<br />
<br />
The received indices of LSP quantization are used to reconstruct the quantified<br />
LSP vectors. (c.f. §5.2.5)<br />
<br />
<br />
=== 12.2kbps mode summary ===<br />
<br />
* indices into code books are parsed from the bit stream<br />
* indices give elements of split matrix quantised (SMQ) residual LSF vectors from the relevant code books<br />
* prediction from the previous frame is added to obtain the mean-removed LSF vectors<br />
* the mean is added<br />
* the LSF vectors are converted to cosine domain LSP vectors<br />
<br />
==== Decoding SMQ residual LSF vectors ====<br />
<br />
The elements of the SMQ vectors are stored at an index into a code book that<br />
varies according to the mode. There are 5 code books for the 12.2kbps mode<br />
corresponding to the 5 indices. These tables will be referred to as:<br />
<br />
<code>lsf_m_n</code><br />
<br />
; m : the number of indices parsed according to the mode<br />
; n : the index 'position' i.e. 1 for the first index, etc<br />
<br />
The 5 indices are stored using 7, 8, 8 + sign bit, 8, 6 bits respectively.<br />
The four elements of a 'split quantized sub-matrix' are stored at the index<br />
position in the appropriate code book are reassigned as follows:<br />
<br />
; lsf_5_1[index1] : r1_1, r1_2, r2_1, r2_2<br />
; lsf_5_2[index2] : r1_3, r1_4, r2_3, r2_4<br />
; lsf_5_3[index3] : r1_5, r1_6, r2_5, r2_6<br />
; lsf_5_4[index4] : r1_7, r1_8, r2_7, r2_8<br />
; lsf_5_5[index5] : r1_9, r1_10, r2_9, r2_10<br />
<br />
With rj_i :<br />
; j : the first or second residual lsf vector<br />
; i : the coefficient of a residual lsf vector ( i = 1, ..., 10 )<br />
<br />
; rj_i : residual line spectral frequencies (LSFs) in Hz<br />
<br />
==== Mean-removed LSF vector prediction ====<br />
<br />
[[Image: Amrnb_lsfmeanrem.gif]]<br />
<br />
; z_j(n) : the mean-removed LSF vector at the jth subframe<br />
; r_j(n) : prediction residual vector of frame n at the jth subframe<br />
; ^r_2(n-1) : the quantified residual vector from the previous frame at the 2nd subframe<br />
<br />
==== The mean is added ====<br />
<br />
[[Image: Amrnb_lsfvec.gif]]<br />
<br />
; lsf_mean_m : a table of the means of the LSF coefficients<br />
; m : the number of indices parsed according to the mode<br />
; fj : the LSF vectors<br />
<br />
==== LSF to LSP vector conversion ====<br />
<br />
[[Image: Amrnb_lspvec.gif]]<br />
<br />
; q_k[i] : the ith coefficient of the kth line spectral pair (LSP) in the cosine domain<br />
; k : the two lsf vectors give the LSP vectors q2, q4 at the 2nd and 4th subframes; k = 2*j<br />
; f_j[i] : ith coefficient of the jth LSF vector; [0,4000] Hz<br />
; f_s : sampling frequency in Hz (8kHz)<br />
<br />
=== Other active modes summary ===<br />
<br />
The process for the other modes is similar to that for the 12.2kbps mode.<br />
<br />
* indices into code books are parsed from the bit stream<br />
* indices give elements of a split matrix quantised (SMQ) residual LSF vector from the relevant code books<br />
* prediction from the previous frame is added to obtain the mean-removed LSF vector<br />
* the mean is added<br />
* the LSF vector is converted to a cosine domain LSP vector<br />
<br />
==== Decoding the SMQ residual LSF vector ====<br />
<br />
The 3 indices are stored with the following numbers of bits:<br />
<br />
{| border=1<br />
! Mode (kbps) !! 1st index (bits) !! 2nd index (bits) !! 3rd index (bits)<br />
|-<br />
| 10.2 || 8 || 9 || 9<br />
|-<br />
| 7.95 || 9 || 9 || 9<br />
|-<br />
| 7.40 || 8 || 9 || 9<br />
|-<br />
| 6.70 || 8 || 9 || 9<br />
|-<br />
| 5.90 || 8 || 9 || 9<br />
|-<br />
| 5.15 || 8 || 8 || 7<br />
|-<br />
| 4.75 || 8 || 8 || 7<br />
|}<br />
<br />
The four elements of a 'split quantized sub-matrix' are stored at the index<br />
position in the appropriate code book are:<br />
<br />
; 1st index in 1st code book : r_1, r_2, r_3<br />
; 2nd index in 2nd code book : r_4, r_5, r_6<br />
; 3rd index in 3rd code book : r_7, r_8, r_9, r_10<br />
<br />
; r_i : residual LSF vector (Hz)<br />
; i : the coefficient of vector ( i = 1, ..., 10 )<br />
<br />
==== Mean-removed LSF vector prediction ====<br />
<br />
[[Image: Amrnb_lsfmeanrem2.gif]]<br />
<br />
; z_j(n)[i] : the ith coefficient of the mean-removed LSF vector at the jth subframe<br />
; r_j(n)[i] : the ith coefficient of the prediction residual vector of frame n at the jth subframe<br />
; pred_fac[i] : the ith coefficient of the prediction factor<br />
; ^r_j(n-1)[i] : the ith coefficient of the the quantified residual vector of the previous frame at the jth subframe<br />
<br />
These processes give the LSP vector at the 4th subframe (q4)<br />
<br />
=== LSP vector interpolation (c.f. §5.2.6) ===<br />
<br />
==== 12.2 kbps mode ====<br />
<br />
[[Image: Amrnb_lspinterpq1-1.gif]]<br />
<br />
[[Image: Amrnb_lspinterpq3-1.gif]]<br />
<br />
==== Other modes ====<br />
<br />
[[Image: Amrnb_lspinterpq1-2.gif]]<br />
<br />
[[Image: Amrnb_lspinterpq2.gif]]<br />
<br />
[[Image: Amrnb_lspinterpq3-2.gif]]<br />
<br />
=== LSP vector to LP filter coefficient conversion (c.f. §5.2.4) ===<br />
<br />
for i=1..5<br />
f1[i] = 2*f1[i-2] - 2*q[2i-1]*f1[i-1]<br />
for j=i-1..1<br />
f1[j] += f1[j-2] - 2*q[2i-1]*f1[j-1]<br />
end<br />
end<br />
<br />
f1[-1] = 0; f1[0] = 0;<br />
<br />
Same for f2[i] with q[2i] instead of q[2i-1]<br />
<br />
for i=1..5<br />
f'1[i] = f1[i] + f1[i-1]<br />
f'2[i] = f2[i] - f2[i-1]<br />
end<br />
<br />
[[Image: Amrnb_lpccoeff.gif]]<br />
<br />
; a_i : the LP filter coefficients<br />
<br />
== Decoding of the pitch (adaptive codebook) vector ==<br />
<br />
* indices parsed from bitstream<br />
* indices give integer and fractional parts of the pitch lag<br />
* pitch vector v(n) is found by interpolating the past excitation u(n) at the pitch lag using an FIR filter. (c.f. §5.6)<br />
<br />
<br />
=== Decode pitch lag ===<br />
<br />
'''Note: division in this section is integer division!'''<br />
<br />
==== 12.2kbps mode - 1/6 resolution pitch lag ====<br />
<br />
===== First and third subframes =====<br />
<br />
In the first and third subframes, a fractional pitch lag is used with resolutions:<br />
<br />
* 1/6 in the range [17 3/6, 94 3/6]<br />
* 1 in the range [95, 143]<br />
<br />
...encoded using 9 bits.<br />
<br />
For [17 3/6, 94 3/6] the pitch index is encoded as:<br />
<br />
pitch_index = (pitch_lag_int - 17)*6 + pitch_lag_frac - 3;<br />
<br />
; pitch_lag_int : integer part of the pitch lag in the range [17, 94]<br />
; pitch_lag_frac : fractional part of the pitch lag in 1/6 units in the range [-2, 3]<br />
<br />
so...<br />
<br />
if(pitch_index < (94 4/6 - 17 3/6)*6)<br />
// fractional part is encoded in range [17 3/6, 94 3/6]<br />
pitch_lag_int = (pitch_index + 5)/6 + 17;<br />
pitch_lag_frac = pitch_index - pitch_lag_int*6 + (17 3/6)*6;<br />
<br />
And for [95, 143] the pitch index is encoded as:<br />
<br />
pitch_index = (pitch_lag_int - 95) + (94 4/6 - 17 3/6)*6;<br />
<br />
; pitch_lag_int : integer pitch lag in the range [95, 143]<br />
<br />
so...<br />
<br />
else<br />
// only integer part encoded in range [95, 143], no fractional part<br />
pitch_lag_int = pitch_index - (94 4/6 - 17 3/6)*6 + 95;<br />
pitch_lag_frac = 0;<br />
<br />
===== Second and fourth subframes =====<br />
<br />
In the second and fourth subframes, a pitch lag resolution of 1/6 is always used in the range [T1 - 5 3/6, T1 + 4 3/6], where T1 is nearest integer to the fractional pitch lag of the previous (1st or 3rd) subframe. The search range is bounded by [18, 143]. In this case the pitch delay is encoded using 6 bits and is therefore in the range [0,63].<br />
<br />
So the search range for the pitch lag is:<br />
<br />
search_range_min = max(pitch_lag_int_prev - 5, 18);<br />
search_range_max = search_range_min + 9;<br />
if(search_range_max > 143) {<br />
search_range_max = 143;<br />
search_range_min = search_range_max - 9;<br />
}<br />
<br />
; pitch_lag_int_prev : the integer part of the pitch lag from the previous sub frame<br />
<br />
The pitch index is encoded as:<br />
<br />
pitch_index = (pitch_lag_int - (search_range_min - 1))*6 + pitch_lag_frac - 3;<br />
<br />
; pitch_lag_int : the integer part of the pitch lag in the range [search_range_min - 1, search_range_max]<br />
; pitch_lag_frac : the fractional part of the pitch lag in the range [-2, 3]<br />
<br />
The formula for the pitch_index has been chosen to map pitch_lag_int [search_range_min - 1, search_range_max] and pitch_lag_frac [-2, 3] to [0,60]. (pitch_index = [0, 10]*6 + [-2, 3] - 3 = [0, 6, ..., 60] + [-5, 0] = [0,60])<br />
<br />
So the pitch lag is calculated through:<br />
<br />
// integer part of pitch lag = position in range [search_range_min - 1, search_range_max] + lower bound of range<br />
pitch_lag_int = (pitch_index + 5)/6 + search_range_min - 1;<br />
// fractional part of pitch lag = pitch index - (integer part without offset)*6 - 1 3/6 offset to bring the values to the correct range<br />
pitch_lag_frac = pitch_index - ((pitch_index + 5)/6)*6 - 9;<br />
<br />
Note that when using integers and integer division to conduct (pitch_index + 5)/6 the result is similar to taking the ceiling of pitch_index/6.0.<br />
<br />
==== Others modes - 1/3 resolution pitch lag ====<br />
<br />
===== First and third subframes =====<br />
<br />
In the first and third subframes, a fractional pitch lag is used with resolutions:<br />
<br />
* 1/3 in the range [19 1/3, 84 2/3]<br />
* 1 in the range [85, 143]<br />
<br />
...encoded using 8 bits.<br />
<br />
For [19 1/3, 84 2/3] the pitch lag is encoded as:<br />
<br />
pitch_index = pitch_lag_int*3 + pitch_lag_frac - (19 1/3)*3;<br />
<br />
; pitch_lag_int : integer part of the pitch lag in the range [19, 84]<br />
; pitch_lag_frac : fractional part of the pitch lag in 1/3 units in the range [0, 2]<br />
<br />
so...<br />
<br />
if(pitch_index < (85 - 19 1/3)*3)<br />
// fractional part is encoded in range [19 1/3, 84 2/3]<br />
pitch_lag_int = (pitch_index + 2)/3 + 19;<br />
pitch_lag_frac = pitch_index - pitch_lag_int*3 + (19 1/3)*3;<br />
<br />
And for [85, 143] the pitch index is encoded as:<br />
<br />
pitch_index = pitch_lag_int - 85 + (85 - 19 1/3)*3;<br />
<br />
; pitch_lag_int : integer pitch lag in the range [85, 143]<br />
<br />
so...<br />
<br />
else<br />
// only integer part encoded in range [85, 143], no fractional part<br />
pitch_lag_int = pitch_index - (85 - 19 1/3)*3 + 85;<br />
pitch_lag_frac = 0;<br />
<br />
===== Second and fourth subframes =====<br />
<br />
In the second and fourth subframes, the pitch lag resolution varies depending on the mode as follows:<br />
<br />
* 7.95 kbps mode<br />
** resolution of 1/3 is always used in the range [T1 - 10 2/3, T1 + 9 2/3]<br />
** encoded using 6 bits => pitch_index is in the range [0, 63]<br />
* 10.2 and 7.40 kbps modes<br />
** resolution of 1/3 is always used in the range [T1 - 5 2/3, T1 + 4 2/3]<br />
** encoded using 5 bits => pitch_index is in the range [0, 31]<br />
* 6.70, 5.90, 5.15 and 4.75 kbps modes<br />
** resolution of 1 is used in the range [T1 - 5, T1 + 4]<br />
** resolution of 1/3 is always used in the range [T1 - 1 2/3, T1 + 2/3]<br />
** encoded using 4 bits => pitch_index is in the range [0, 15]<br />
<br />
Where T1 is nearest integer to the fractional pitch lag of the previous (1st or 3rd) subframe. The search range is bounded by [20, 143].<br />
<br />
So the search range for the pitch lag is:<br />
<br />
lower_bound = 5;<br />
range = 9;<br />
if(mode == 7.95) {<br />
lower_bound = 10;<br />
range = 19;<br />
}<br />
search_range_min = max(pitch_lag_int_prev - lower_bound, 20);<br />
search_range_max = search_range_min + range;<br />
if(search_range_max > 143) {<br />
search_range_max = 143;<br />
search_range_min = search_range_max - range;<br />
}<br />
<br />
; pitch_lag_int_prev : the integer part of the pitch lag from the previous sub frame<br />
<br />
<br />
For modes 7.40, 7.95 and 10.2 the pitch index is encoded as:<br />
<br />
pitch_index = (pitch_lag_int - search_range_min)*3 + pitch_lag_frac + 2;<br />
<br />
; pitch_lag_int : the integer part of the pitch lag in the range [search_range_min, search_range_max]<br />
; pitch_lag_frac : the fractional part of the pitch lag in the range [-1, 1]<br />
<br />
So the pitch lag is calculated through:<br />
<br />
// integer part of pitch lag = position of pitch lag in range [search_range_min, search_range_max] + lower bound of the range<br />
pitch_lag_int = (pitch_index + 2)/3 - 1 + search_range_min;<br />
// fractional part of pitch lag = pitch index - (integer part without offset)*3 - 2/3 to bring the values to the correct range<br />
pitch_lag_frac = pitch_index - ((pitch_index + 2)/3 - 1)*3 - 2;<br />
<br />
<br />
For modes 4.75, 5.15, 5.90 and 6.70:<br />
<br />
t1_temp = max( min(pitch_lag_int_prev, search_range_min + 5), search_range_max - 4 );<br />
<br />
; t1_temp : predicted pitch lag from the previous frame adjusted to fit into the 0 position of the search range<br />
<br />
The pitch index is encoded as:<br />
<br />
// if pitch lag is below T1 - 1 2/3<br />
if( pitch_lag_int*3 + pitch_lag_frac <= (t1_temp - 2)*3 ) {<br />
// encode with resolution 1<br />
index = (pitch_lag_int - t1_temp) + 5;<br />
// else if pitch lag is below T1 + 1<br />
}else if( pitch_lag_int*3 + pitch_lag_frac < (t1_temp + 1)*3 ) {<br />
// encode with resolution 1/3<br />
index = ( pitch_lag_int*3 + pitch_lag_frac - (t1_temp - 2)*3 ) + 3;<br />
// else pitch lag is above T1 + 2/3<br />
}else {<br />
// encode with resolution 1<br />
index = (pitch_lag_int - t1_temp) + 11;<br />
}<br />
<br />
; pitch_lag_int : the integer part of the pitch lag in the range [search_range_min, search_range_max]<br />
; pitch_lag_frac : the fractional part of the pitch lag in the range [-1, 1]<br />
<br />
The possible pitch indices and values are:<br />
<br />
{| border=1<br />
|-<br />
| 0 || 1 || 2 || 3 || 4 || 5 || 6 || 7 || 8 || 9 || 10 || 11 || 12 || 13 || 14 || 15<br />
|-<br />
| -5 || -4 || -3 || -2 || -1 2/3 || -1 1/3 || -1 || -2/3 || -1/3 || 0 || 1/3 || 2/3 || 1 || 2 || 3 || 4<br />
|-<br />
|}<br />
<br />
So the pitch lag is calculated through:<br />
<br />
if(pitch_index < 4) {<br />
// integer part of pitch lag = pitch lag position in range [t1_temp - 5, t1_temp - 2] + lower bound of range<br />
pitch_lag_int = pitch_index + (t1_temp - 5);<br />
// this range is coded with resolution 1 so no fractional part<br />
pitch_lag_frac = 0;<br />
}else if(pitch_index < 12) {<br />
pitch_lag_int = (pitch_index - 2)/3 + (t1_temp - 2);<br />
pitch_lag_frac = (pitch_index - 4) - ((pitch_index - 2)/3)*3 - 11;<br />
}else {<br />
// integer part of pitch lag = pitch lag position in range [t1_temp + 1, t1_temp + 4] + lower bound of range<br />
pitch_lag_int = pitch_index - 12 + t1_temp + 1;<br />
// this range is coded with resolution 1 so no fractional part<br />
pitch_lag_frac = 0;<br />
}<br />
<br />
<br />
=== Calculate pitch vector ===<br />
<br />
[[Image: Amrnb_pitchvector.gif]]<br />
<br />
; k : integer pitch lag<br />
; n : sample position in the vectors 0, ..., 39<br />
; t : 0, ..., 5 corresponding to fractions 0, 1/6, 2/6, 3/6, -2/6, -1/6 respectively<br />
<br />
This equation can be used for both 1/3 and 1/6 resolution simply by multiplying t by 2 in the 1/3 case.<br />
<br />
(Note: the coefficients b60 are in the reference source in an array called inter6)<br />
<br />
<br />
<br />
== Decoding of the fixed (innovative or algebraic) vector ==<br />
<br />
* the excitation pulse positions and signs are parsed from the bit stream<br />
* the pulse positions and signs are encoded differently depending on the mode<br />
* the fixed code book vector, c(n), is then constructed from the pulse positions and signs<br />
* if pitch_lag_int is less than the subframe size (40), the pitch sharpening procedure is applied<br />
<br />
<br />
=== Decoding the pulse positions ===<br />
<br />
==== 12.2 kbps mode ====<br />
<br />
* 10 pulse positions each coded using 3 bit Gray codes<br />
* signs coded using 1 bit each for 5 pulse pairs<br />
<br />
<br />
{| border=1<br />
! Pulse !! Positions<br />
|-<br />
| i0,i5 || 0, 5, 10, 15, 20, 25, 30, 35<br />
|-<br />
| i1,i6 || 1, 6, 11, 16, 21, 26, 31, 36<br />
|-<br />
| i2,i7 || 2, 7, 12, 17, 22, 27, 32, 37<br />
|-<br />
| i3,i8 || 3, 8, 13, 18, 23, 28, 33, 38<br />
|-<br />
| i4,i9 || 4, 9, 14, 19, 24, 29, 34, 39<br />
|-<br />
|}<br />
<br />
<br />
==== 10.2 kbps mode ====<br />
<br />
* 8 pulse positions, 4 pairs, coded as 3 values using 10, 10 and 7 bits<br />
* signs coded using 1 bit each for 4 pulse pairs<br />
<br />
<br />
{| border=1<br />
! Pulse !! Positions<br />
|-<br />
| i0,i4 || 0, 4, 8, 12, 16, 20, 24, 28, 32, 36<br />
|-<br />
| i1,i5 || 1, 5, 9, 13, 17, 21, 25, 29, 33, 37<br />
|-<br />
| i2,i6 || 2, 6, 10, 14, 18, 22, 26, 30, 34, 38<br />
|-<br />
| i3,i7 || 3, 7, 11, 15, 19, 23, 27, 31, 35, 39<br />
|-<br />
|}<br />
<br />
<br />
==== 7.95 and 7.40 kbps modes ====<br />
<br />
* 4 pulse positions Gray coded using 3, 3, 3 and 4 bits<br />
* signs coded using 1 bit for each pulse<br />
<br />
<br />
{| border=1<br />
! Pulse !! Positions<br />
|-<br />
| i0 || 0, 5, 10, 15, 20, 25, 30, 35<br />
|-<br />
| i1 || 1, 6, 11, 16, 21, 26, 31, 36<br />
|-<br />
| i2 || 2, 7, 12, 17, 22, 27, 32, 37<br />
|-<br />
| i3 || 3, 8, 13, 18, 23, 28, 33, 38<br />
4, 9, 14, 19, 24, 29, 34, 39<br />
|-<br />
|}<br />
<br />
==== 6.70 kbps mode ====<br />
<br />
* 3 pulse positions coded using 3, 4 and 4 bits<br />
* signs coded using 1 bit for each pulse<br />
<br />
<br />
{| border=1<br />
! Pulse !! Positions<br />
|-<br />
| i0 || 0, 5, 10, 15, 20, 25, 30, 35<br />
|-<br />
| i1 || 1, 6, 11, 16, 21, 26, 31, 36<br />
3, 8, 13, 18, 23, 28, 33, 38<br />
|-<br />
| i2 || 2, 7, 12, 17, 22, 27, 32, 37<br />
4, 9, 14, 19, 24, 29, 34, 39<br />
|-<br />
|}<br />
<br />
<br />
==== 5.90 kbps mode ====<br />
<br />
* 2 pulse positions coded using 4 and 5 bits<br />
* signs coded using 1 bit for each pulse<br />
<br />
<br />
{| border=1<br />
! Pulse !! Positions<br />
|-<br />
| i0 || 1, 6, 11, 16, 21, 26, 31, 36<br />
3, 8, 13, 18, 23, 28, 33, 38<br />
|-<br />
| i1 || 0, 5, 10, 15, 20, 25, 30, 35<br />
1, 6, 11, 16, 21, 26, 31, 36<br><br />
2, 7, 12, 17, 22, 27, 32, 37<br><br />
4, 9, 14, 19, 24, 29, 34, 39<br />
|-<br />
|}<br />
<br />
==== 5.15 and 4.75 kbps modes ====<br />
<br />
* 2 pulse positions coded using 1 bit for the position subset and 3 bits per pulse<br />
* signs coded using 1 bit for each pulse<br />
<br />
<br />
{| border=1<br />
! Subframe !! Subset !! Pulse !! Positions<br />
|-<br />
| rowspan="4" align="center" | 1<br />
| rowspan="2" align="center" | 1<br />
| i0 || 0, 5, 10, 15, 20, 25, 30, 35<br />
|-<br />
| i1 || 2, 7, 12, 17, 22, 27, 32, 37<br />
|-<br />
| rowspan="2" align="center" | 2<br />
| i0 || 1, 6, 11, 16, 21, 26, 31, 36<br />
|-<br />
| i1 || 3, 8, 13, 18, 23, 28, 33, 38<br />
|-<br />
| rowspan="4" align="center" | 2<br />
| rowspan="2" align="center" | 1<br />
| i0 || 0, 5, 10, 15, 20, 25, 30, 35<br />
|-<br />
| i1 || 3, 8, 13, 18, 23, 28, 33, 38<br />
|-<br />
| rowspan="2" align="center" | 2<br />
| i0 || 2, 7, 12, 17, 22, 27, 32, 37<br />
|-<br />
| i1 || 4, 9, 14, 19, 24, 29, 34, 39<br />
|-<br />
| rowspan="4" align="center" | 3<br />
| rowspan="2" align="center" | 1<br />
| i0 || 0, 5, 10, 15, 20, 25, 30, 35<br />
|-<br />
| i1 || 2, 7, 12, 17, 22, 27, 32, 37<br />
|-<br />
| rowspan="2" align="center" | 2<br />
| i0 || 1, 6, 11, 16, 21, 26, 31, 36<br />
|-<br />
| i1 || 4, 9, 14, 19, 24, 29, 34, 39<br />
|-<br />
| rowspan="4" align="center" | 4<br />
| rowspan="2" align="center" | 1<br />
| i0 || 0, 5, 10, 15, 20, 25, 30, 35<br />
|-<br />
| i1 || 3, 8, 13, 18, 23, 28, 33, 38<br />
|-<br />
| rowspan="2" align="center" | 2<br />
| i0 || 1, 6, 11, 16, 21, 26, 31, 36<br />
|-<br />
| i1 || 4, 9, 14, 19, 24, 29, 34, 39<br />
|-<br />
|}<br />
<br />
<br />
=== Fixed codebook vector construction ===<br />
<br />
All c(n) are zero if there is no pulse at position n. If there is a pulse at position n then it has the corresponding sign as parsed above.<br />
<br />
<br />
=== Pitch sharpening ===<br />
<br />
[[Image: Amrnb fixvecpitshp.gif]]<br />
<br />
; β : the decoded pitch gain, ^g_p, bounded by [0.0,1.0] for 12.2. kbps or [0.0,0.8] for other modes<br />
<br />
Note: Only sharpen n in [pitch_lag_int-1, 39]. The reference source considers previous fixed vector elements (n<0) to be 0. I think this is also the reason pitch sharpening is not conducted for pitch_lag_int > 40 as all the sharpening contributions would be 0.<br />
<br />
== Decoding of the pitch and fixed codebook gains ==<br />
<br />
<br />
=== Fixed gain prediction ===<br />
<br />
A moving average prediction of the innovation (fixed) energy is conducted.<br />
<br />
<br />
[[Image: Amrnb_fixedgainpred.gif]]<br />
<br />
; g_c' : fixed gain prediction<br />
; \tilde{E} : predicted energy [dB]<br />
; \bar{E} : desired mean innovation (fixed) energy [dB]<br />
; E_I : calculated mean innovation (fixed) energy [dB]<br />
<br />
<br />
[[Image: Amrnb_predener.gif]]<br />
<br />
; b : 4-tap MA prediction coefficients [0.68, 0.58, 0.34, 0.19]<br />
; ^R(k) : quantified prediction errors at subframe k 20*log10(^γ_gc(k))<br />
<br />
<br />
Desired mean innovation (fixed) energy:<br />
<br />
{| border=1<br />
! Mode (kbps) !! Mean energy (dB)<br />
|-<br />
| 12.2 || 36<br />
|-<br />
| 10.2 || 33<br />
|-<br />
| 7.95 || 36<br />
|-<br />
| 7.40 || 30<br />
|-<br />
| 6.70 || 28.75<br />
|-<br />
| 5.90 || 33<br />
|-<br />
| 5.15 || 33<br />
|-<br />
| 4.75 || 33<br />
|-<br />
|}<br />
<br />
<br />
[[Image: Amrnb_meaniner.gif]]<br />
<br />
; E_I : calculated mean innovation (fixed) energy [dB]<br />
; N : subframe size 40<br />
; c(n) : fixed codebook vector<br />
<br />
=== Dequantisation of the gains ===<br />
<br />
==== 12.2kbps and 7.95kbps - scalar quantised gains ====<br />
<br />
The received indices are used to find the quantified pitch gain, ^g_p, and the quantified fixed gain correction factor, ^γ_gc.<br />
<br />
===== Pitch gain =====<br />
<br />
The parsed gain index is used to obtain the quantified pitch gain, ^g_p, from the corresponding codebook. (qua_gain_pitch in the reference source.)<br />
<br />
===== Fixed gain correction factor =====<br />
<br />
The parsed gain index is used to obtain the quantified fixed gain, ^g_c, from the corresponding codebook. (qua_gain_code in the reference source.) The table stores ^γ_gc, and the quantised energy error in two forms (these are needed for the moving average calculation of the predicted fixed gain):<br />
<br />
qua_ener_MR122 = log2(^γ_gc)<br />
qua_ener = 20*log10(^γ_gc)<br />
<br />
^γ_gc is stored at Q11 (i.e. it's multiplied by 2^11.) qua_ener_MR122 and qua_ener are stored at Q10.<br />
<br />
==== Other modes - vector quantised gains ====<br />
<br />
The received index gives both the quantified adaptive codebook gain, ^g_p, and the quantified algebraic codebook gain correction factor, ^γ_gc.<br />
<br />
The tables contains the following data:<br />
<br />
^g_p (Q14),<br />
^γ_gc (Q12), (^g_c = g_c'*^γ_gc),<br />
qua_ener_MR122 (Q10), (log2(^γ_gc))<br />
qua_ener (Q10) (20*log10(^γ_gc))<br />
<br />
The log2() and log10() values are calculated on the fixed point value<br />
(g_fac Q12) and not on the original floating point value of g_fac<br />
to make the quantizer/MA predictdor use corresponding values.<br />
<br />
The codebook used depends on the mode:<br />
<br />
6.70, 7.40, 10.2 kbps modes - table_gain_highrates in the reference source<br />
Four consecutive entries give ^g_p, ^γ_gc, qua_ener_MR122 and qua_ener. Apparently the values for qua_ener are the original ones from IS641 to ensure bit-exactness but are not exactly the rounded value of 20log10(^γ_gc).<br />
<br />
5.15, 5.90 kbps modes - table_gain_lowrates<br />
Similar to table_gain_highrates, four consecutive entries give ^g_p, ^γ_gc, qua_ener_MR122 and qua_ener. There are no special notes for this table.<br />
<br />
4.75 kbps mode - table_gain_MR475<br />
Unlike the above mentioned tables, four consecutive values give ^g_p, ^γ_gc for subframes 0,2, and then ^g_p, ^γ_gc for subframes 1,3.<br />
<br />
At the very least I think these tables could be redesigned a little.<br />
<br />
=== Calculation of the quantified fixed gain ===<br />
<br />
[[Image: Amrnb_fixedgain.gif]]<br />
<br />
== Smoothing of the fixed codebook gain ==<br />
<br />
<br />
'''10.2, 6.70, 5.90, 5.15, 4.75 kbit/s modes only'''<br />
<br />
<br />
=== Calculate averaged LSP vector ===<br />
<br />
[[Image: Amrnb_lspsmooth.gif]]<br />
<br />
; \bar{q}(n) : averaged LSP vector at frame n<br />
; ^q_4(n) : quantified LSP vector for the 4th subframe at frame n<br />
<br />
<br />
=== Calculate fixed gain smoothing factor ===<br />
<br />
[[Image: Amrnb_lspdiff.gif]]<br />
<br />
; diff_m : difference measure at subframe m<br />
; j : loops over LSPs<br />
; m : loops over subframes<br />
; ^q_m : quantified LSP vector at subframe m<br />
<br />
Note: I think the sum over the subframes is an error in the specification and they actually mean to use the quantified LSP vector from subframe m for the calculation of diff for subframe m. I think this is what they do in the reference source and the sum over m is an error.<br />
<br />
<br />
[[Image: Amrnb_smoothfac.gif]]<br />
<br />
; k_m : fixed gain smoothing factor<br />
; K_1 : 0.4<br />
; K_2 : 0.25<br />
; diff_m : difference measure at subframe m<br />
<br />
If diff_m has been greater than 0.65 for 10 subframes, k_m is set to 1.0 (i.e. no smoothing) for 40 subframes.<br />
<br />
=== Calculate mean fixed gain ===<br />
<br />
[[Image: Amrnb_meangc.gif]]<br />
<br />
; \bar{g_c}(m) : mean fixed gain at subframe m<br />
; ^g_c(k) : quantified fixed gain at subframe k<br />
<br />
=== Calculate smoothed fixed gain ===<br />
<br />
[[Image: Amrnb_gcsmooth.gif]]<br />
<br />
; ^g_c : quantified fixed gain<br />
; \bar{g_c} : averaged fixed gain<br />
<br />
== Anti-sparseness processing ==<br />
<br />
<br />
'''7.95, 6.70, 5.90, 5.15, 4.75 kbit/s modes only'''<br />
<br />
<br />
=== Evaluate impulse response filter strength ===<br />
<br />
The fixed vector, c(n), has only a few pulses per subframe. In certain conditions, to reduce perceptual artifacts arising from this, the vector is circularly convolved with a predefined impulse response. The selection of the strength of filter is made based on the decoded gains.<br />
<br />
if ^g_p < 0.6<br />
impNr = 0<br />
else if ^g_p < 0.9<br />
impNr = 1<br />
else<br />
impNr = 2<br />
<br />
; impNr = 0 : strong impulse response filter<br />
; impNr = 1 : medium impulse response filter<br />
; impNr = 2 : no filtering<br />
<br />
if ^g_c(k) > 2 * ^g_c(k-1)<br />
impNr = min( impNr + 1, 2 )<br />
else if impNr = 0 AND median of last five ^g_p >= 0.6<br />
impNr = min( impNr(k), min(impNr(k-1) + 1, 2) )<br />
<br />
<br />
=== Circular convolution of fixed vector and impulse response filter ===<br />
<br />
[[Image: Amrnb_circconv.gif]]<br />
<br />
; (c * h) : convolution of vectors c and h, in this case circular convolution<br />
; h[n] : nth coefficient of the impulse response used for filtering<br />
; c[n] : nth coefficient of the fixed vector<br />
<br />
n = 0, ..., 39<br />
<br />
To make the convolution circular, make the impulse response circular by taking h[-m] = h[39-m]<br />
<br />
== Computing the reconstructed speech ==<br />
<br />
<br />
=== Construct excitation ===<br />
<br />
[[Image: Amrnb_excitation.gif]]<br />
<br />
; u(n) : excitation vector<br />
; ^g_p : quantified pitch gain<br />
; v(n) : pitch vector<br />
; ^g_c : quantified fixed gain<br />
; c(n) : fixed vector<br />
<br />
<br />
=== Emphasise pitch vector contribution ===<br />
<br />
This is apparently a post-processing technique.<br />
<br />
[[Image: Amrnb_pitchvecemph.gif]]<br />
<br />
; ^u(n) : excitation vector with emphasised pitch vector contribution<br />
; β : ^g_p bounded by [0.0, 0.8] or [0.0, 1.0] depending on mode<br />
<br />
<br />
=== Apply adaptive gain control (AGC) through gain scaling ===<br />
<br />
[[Image: Amrnb_gainfac.gif]]<br />
<br />
; η : gain scaling factor for emphasised excitation<br />
<br />
[[Image: Amrnb_gainscalexc.gif]]<br />
<br />
; ^u'(n) : gain-scaled emphasised excitation<br />
<br />
<br />
=== Calculate reconstructed speech samples ===<br />
<br />
[[Image: Amrnb_synthfilter.gif]]<br />
<br />
; ^s(n) : reconstructed speech samples<br />
; ^a_i : LP filter coefficients<br />
<br />
Note: for n-i < 0, ^s(n-i) should be take from previous speech samples if existing, else we will consider them 0 as this behaviour is undefined the specification.<br />
<br />
== Additional instability protection ==<br />
<br />
If an overflow occurs during synthesis, the pitch vector, v(n), is scaled down by a factor of 4 and synthesis is conducted again bypassing emphasising the pitch vector contribution and adaptive gain control.<br />
<br />
Q: What classifies an overflow?<br />
A: s(n) < -32768 or s(n) > 32767 (i.e. 16-bit signed int)<br />
<br />
== Post-processing ==<br />
<br />
=== Adaptive post-filtering ===<br />
<br />
(c.f. §6.2.1)<br />
<br />
==== IIR filtering ====<br />
<br />
The speech samples, ^s(n), are filtered through a formant filter and a tilt compensation filter.<br />
<br />
[[Image: Amrnb_pfformant.gif]]<br />
<br />
; γ_n, γ_d : control the amount of formant post-filtering<br />
<br />
The speech samples, ^s(n), are filtered through ^A(z/γ_n) to produce the residual signal ^r(n).<br />
<br />
[[Image: Amrnb_residual.gif]]<br />
<br />
^r(n) is filtered through 1/^A(z/γ_d).<br />
<br />
[[Image: Amrnb_fmntfilt.gif]]<br />
<br />
The output is filtered through H_t(z) (the tilt compensation filter) resulting in the post-filtered speech signal, ^s_f(n).<br />
<br />
[[Image: Amrnb_pftilt.gif]]<br />
<br />
[[Image: Amrnb_mu.gif]]<br />
<br />
[[Image: Amrnb_reffac.gif]]<br />
<br />
[[Image: Amrnb_r_h.gif]]<br />
<br />
; L_h : 22 - the truncation of the impulse response<br />
; h_f : impulse response of the formant filter H_f<br />
<br />
So, in summation notation:<br />
<br />
[[Image: Amrnb_pftiltsum.gif]]<br />
<br />
===== 12.2 and 10.2 kbps modes =====<br />
<br />
γ_n = 0.7<br />
<br />
γ_d = 0.75<br />
<br />
γ_t = 0.8 if k_1' > 0; 0 otherwise<br />
<br />
===== Other modes =====<br />
<br />
γ_n = 0.55<br />
<br />
γ_d = 0.7<br />
<br />
γ_t = 0.8<br />
<br />
==== Adaptive gain control ====<br />
<br />
Adaptive gain control is used to compensate for the gain difference between the filtered and synthesised speech signals.<br />
<br />
[[Image: Amrnb_gainscalfac.gif]]<br />
<br />
[[Image: Amrnb_gainscalfacwtd.gif]]<br />
<br />
[[Image: Amrnb_gainscalspch.gif]]<br />
<br />
; \alpha : adaptive gain control factor equal to 0.9<br />
<br />
=== High-pass filtering and upscaling ===<br />
<br />
(c.f. §6.2.2)<br />
<br />
[[Image: Amrnb_highpass.gif]]<br />
<br />
After completing all filtering, the samples are scaled up by a factor of 2.<br />
<br />
<br />
<br />
<br />
[[Category:Vocoders]]<br />
[[Category:Audio Codecs]]</div>Adam Quantrillhttps://wiki.multimedia.cx/index.php?title=AMR-NB&diff=14380AMR-NB2013-03-05T18:03:03Z<p>Adam Quantrill: /* IF2 format */</p>
<hr />
<div>* samples: http://samples.mplayerhq.hu/A-codecs/amr/<br />
* specification: http://www.3gpp.org/ftp/Specs/html-info/26-series.htm<br />
* Wikipedia article: http://en.wikipedia.org/wiki/Adaptive_Multi-Rate<br />
<br />
AMR-NB (Adaptive Multi-Rate Narrowband) is a [[vocoder]] employed in low-bitrate applications like mobile phones. It is a form of ACELP where A stands for algebraic.<br />
<br />
----<br />
<br />
The following text aims to be a simpler and more explicit document of the AMR narrow band decoding processes to aid in development of a decoder. Reference to sections of the specification will be made in the following format: (c.f. §5.2.5). Happy reading.<br />
<br />
<br />
<br />
== Nomenclature weirdness ==<br />
<br />
Throughout the specification, a number of references are made to the same (or very similar) items with fairly confusing variation. They are listed below to aid understanding of the following text but efforts will be made to consistently use one item name throughout or to use both with the lesser used name in parenthesis.<br />
<br />
* Pitch / Adaptive codebook<br />
* Fixed / Innovative (also algebraic when referring to the codebook)<br />
* Quantified means quantized<br />
<br />
<br />
== Summary ==<br />
<br />
* Mode dependent bitstream parsing<br />
* Indices parsed from bitstream<br />
* Indices decoded to give LSF vectors, fractional pitch lags, innovative code vectors and the pitch and innovative gains<br />
* LSF vectors converted to LP filter coefficients at each subframe<br />
* Subframe decoding<br />
** Excitation vector = adaptive code vector * adaptive (pitch) gain + innovative code vector * innovative gain<br />
** Excitation vector filtered through an LP synthesis filter to reconstruct speech<br />
** Speech signal filtered with adaptive postfilter<br />
<br />
<br />
<br />
== Bit stream frame format ==<br />
Specification ([http://www.3gpp.org/ftp/Specs/html-info/26101.htm 26.101]) describes two possible frame types - interface formats 1 and 2 (often abbreviated IF1 and IF2). IF2 is byte-aligned. The following tables of data are taken from this specification unless otherwise stated. AMR-NB over GSM uses a third slightly different format whose bit-packing is tightly defined by the GSM specs.<br />
<br />
=== IF1 format ===<br />
<br />
{| border=1<br />
! bits !! low level meaning || high level meaning<br />
|-<br />
| 4<br />
| Frame type<br />
| rowspan=2 | AMR header<br />
|-<br />
| 1<br />
| Frame quality indicator (0 bad/1 good)<br />
|-<br />
| 3<br />
| Mode indication<br />
| rowspan=3 | AMR auxiliary information<br />
|-<br />
| 3<br />
| Mode request<br />
|-<br />
| 8<br />
| CRC<br />
|-<br />
| &nbsp;<br />
| Class A bits<br />
| rowspan=3 | AMR core frame<br />
|-<br />
| &nbsp;<br />
| Class B bits<br />
|-<br />
| &nbsp;<br />
| Class C bits<br />
|}<br />
<br />
=== IF2 format ===<br />
<br />
{| border=1<br />
! bits !! low level meaning || high level meaning<br />
|-<br />
| 4<br />
| Frame type<br />
| AMR header<br />
|-<br />
| &nbsp;<br />
| Class A bits<br />
| rowspan=3 | AMR core frame<br />
|-<br />
| &nbsp;<br />
| Class B bits<br />
|-<br />
| &nbsp;<br />
| Class C bits<br />
|-<br />
| &nbsp;<br />
| Padding (called "Bit stuffing" in the specification)<br />
| &nbsp;<br />
|}<br />
<br />
=== GSM OTA format ===<br />
<br />
{| border=1<br />
! bits !! low level meaning || high level meaning<br />
|-<br />
| 4<br />
| Mode request<br />
| rowspan=1 | AMR auxiliary information <br />
|-<br />
| 1<br />
| F bit (0 good/1 unusual)<br />
| rowspan=3 | AMR header<br />
|-<br />
| 3<br />
| Mode indication<br />
|-<br />
| 1<br />
| Frame quality indicator (0 bad/1 good)<br />
|-<br />
| &nbsp;<br />
| Class A bits<br />
| rowspan=3 | AMR core frame<br />
|-<br />
| 6<br />
| CRC<br />
|-<br />
| &nbsp;<br />
| Class B bits<br />
|}<br />
<br />
=== Field meaning ===<br />
<br />
{| border=1<br />
! Frame type !! Frame content<br />
|-<br />
| 0 || AMR 4.75kbps<br />
|-<br />
| 1 || AMR 5.15kbps<br />
|-<br />
| 2 || AMR 5.90kbps<br />
|-<br />
| 3 || AMR 6.70kbps (PDC-EFR)<br />
|-<br />
| 4 || AMR 7.40kbps (TDMA-EFR)<br />
|-<br />
| 5 || AMR 7.95kbps<br />
|-<br />
| 6 || AMR 10.2kbps<br />
|-<br />
| 7 || AMR 12.2kbps (GSM-EFR)<br />
|-<br />
| 8 || AMR SID<br />
|-<br />
| 9 || GSM-EFR SID<br />
|-<br />
| 10 || TDMA-EFR SID<br />
|-<br />
| 11 || PDC-EFR SID<br />
|-<br />
| 12-14 || Reserved for future use<br />
|-<br />
| 15 || No data (no transmission/no reception)<br />
|}<br />
<br />
=== Classes ===<br />
<br />
{| border=1<br />
! Class !! Importance explanation<br />
|-<br />
| A || Data that is most sensitive to error. Any error in these bits leads to a corrupted speech frame that should not be decoded without appropriate error concealment. This class of bits is protected by an 8-bit CRC.<br />
|-<br />
| B || Less sensitive data that are present in all speech frames.<br />
|-<br />
| C || Least sensitive data present only in higher bit rate frames.<br />
|}<br />
<br />
* Class A is protected by an 8-bit CRC with polynomial x^8+x^6+x^5+x^4+1 computed over the Class A bits.<br />
* However for AMR-NB over GSM the CRC is a 6-bit with polynomial x^6+x^5+x^3+x^2+x+1 computed over the Class A bits, and it immediately follows the Class A bits, rather than preceding them. That is, it's between the Class A and Class B bits. [see 3GPP TS 05.03 sec 3.9.4.3]<br />
* There is no significant step-wise change in subjective importance at class boundaries.<br />
* The distribution of bits is ordered from most to least subjective importance at both the class level and within the classes.<br />
<br />
{| border=1<br />
! Frame type !! Total bits !! Class A bits !! Class B bits !! Class C bits<br />
|-<br />
| 0 || 95 || 42 || 53 || 0<br />
|-<br />
| 1 || 103 || 49 || 54 || 0<br />
|-<br />
| 2 || 118 || 55 || 63 || 0<br />
|-<br />
| 3 || 134 || 58 || 76 || 0<br />
|-<br />
| 4 || 148 || 61 || 87 || 0<br />
|-<br />
| 5 || 159 || 75 || 84 || 0<br />
|-<br />
| 6 || 204 || 65 || 99 || 40<br />
|-<br />
| 7 || 244 || 81 || 103 || 60<br />
|-<br />
| 8 || 39 || 39 || 0 || 0<br />
|-<br />
| 9 || 43 || 43 || 0 || 0<br />
|-<br />
| 10 || 38 || 38 || 0 || 0<br />
|-<br />
| 11 || 37 || 37 || 0 || 0<br />
|}<br />
<br />
For the specifics of the bit stream layout (i.e. for the bits to parameter mappings) see [http://www.3gpp.org/ftp/Specs/html-info/26101.htm 26.101 AMR speech codec frame structure] and [http://www.3gpp.org/ftp/Specs/html-info/26090.htm 26.090 AMR speech codec transcoding functions].<br />
<br />
== Decoding of LP filter parameters ==<br />
<br />
The received indices of LSP quantization are used to reconstruct the quantified<br />
LSP vectors. (c.f. §5.2.5)<br />
<br />
<br />
=== 12.2kbps mode summary ===<br />
<br />
* indices into code books are parsed from the bit stream<br />
* indices give elements of split matrix quantised (SMQ) residual LSF vectors from the relevant code books<br />
* prediction from the previous frame is added to obtain the mean-removed LSF vectors<br />
* the mean is added<br />
* the LSF vectors are converted to cosine domain LSP vectors<br />
<br />
==== Decoding SMQ residual LSF vectors ====<br />
<br />
The elements of the SMQ vectors are stored at an index into a code book that<br />
varies according to the mode. There are 5 code books for the 12.2kbps mode<br />
corresponding to the 5 indices. These tables will be referred to as:<br />
<br />
<code>lsf_m_n</code><br />
<br />
; m : the number of indices parsed according to the mode<br />
; n : the index 'position' i.e. 1 for the first index, etc<br />
<br />
The 5 indices are stored using 7, 8, 8 + sign bit, 8, 6 bits respectively.<br />
The four elements of a 'split quantized sub-matrix' are stored at the index<br />
position in the appropriate code book are reassigned as follows:<br />
<br />
; lsf_5_1[index1] : r1_1, r1_2, r2_1, r2_2<br />
; lsf_5_2[index2] : r1_3, r1_4, r2_3, r2_4<br />
; lsf_5_3[index3] : r1_5, r1_6, r2_5, r2_6<br />
; lsf_5_4[index4] : r1_7, r1_8, r2_7, r2_8<br />
; lsf_5_5[index5] : r1_9, r1_10, r2_9, r2_10<br />
<br />
With rj_i :<br />
; j : the first or second residual lsf vector<br />
; i : the coefficient of a residual lsf vector ( i = 1, ..., 10 )<br />
<br />
; rj_i : residual line spectral frequencies (LSFs) in Hz<br />
<br />
==== Mean-removed LSF vector prediction ====<br />
<br />
[[Image: Amrnb_lsfmeanrem.gif]]<br />
<br />
; z_j(n) : the mean-removed LSF vector at the jth subframe<br />
; r_j(n) : prediction residual vector of frame n at the jth subframe<br />
; ^r_2(n-1) : the quantified residual vector from the previous frame at the 2nd subframe<br />
<br />
==== The mean is added ====<br />
<br />
[[Image: Amrnb_lsfvec.gif]]<br />
<br />
; lsf_mean_m : a table of the means of the LSF coefficients<br />
; m : the number of indices parsed according to the mode<br />
; fj : the LSF vectors<br />
<br />
==== LSF to LSP vector conversion ====<br />
<br />
[[Image: Amrnb_lspvec.gif]]<br />
<br />
; q_k[i] : the ith coefficient of the kth line spectral pair (LSP) in the cosine domain<br />
; k : the two lsf vectors give the LSP vectors q2, q4 at the 2nd and 4th subframes; k = 2*j<br />
; f_j[i] : ith coefficient of the jth LSF vector; [0,4000] Hz<br />
; f_s : sampling frequency in Hz (8kHz)<br />
<br />
=== Other active modes summary ===<br />
<br />
The process for the other modes is similar to that for the 12.2kbps mode.<br />
<br />
* indices into code books are parsed from the bit stream<br />
* indices give elements of a split matrix quantised (SMQ) residual LSF vector from the relevant code books<br />
* prediction from the previous frame is added to obtain the mean-removed LSF vector<br />
* the mean is added<br />
* the LSF vector is converted to a cosine domain LSP vector<br />
<br />
==== Decoding the SMQ residual LSF vector ====<br />
<br />
The 3 indices are stored with the following numbers of bits:<br />
<br />
{| border=1<br />
! Mode (kbps) !! 1st index (bits) !! 2nd index (bits) !! 3rd index (bits)<br />
|-<br />
| 10.2 || 8 || 9 || 9<br />
|-<br />
| 7.95 || 9 || 9 || 9<br />
|-<br />
| 7.40 || 8 || 9 || 9<br />
|-<br />
| 6.70 || 8 || 9 || 9<br />
|-<br />
| 5.90 || 8 || 9 || 9<br />
|-<br />
| 5.15 || 8 || 8 || 7<br />
|-<br />
| 4.75 || 8 || 8 || 7<br />
|}<br />
<br />
The four elements of a 'split quantized sub-matrix' are stored at the index<br />
position in the appropriate code book are:<br />
<br />
; 1st index in 1st code book : r_1, r_2, r_3<br />
; 2nd index in 2nd code book : r_4, r_5, r_6<br />
; 3rd index in 3rd code book : r_7, r_8, r_9, r_10<br />
<br />
; r_i : residual LSF vector (Hz)<br />
; i : the coefficient of vector ( i = 1, ..., 10 )<br />
<br />
==== Mean-removed LSF vector prediction ====<br />
<br />
[[Image: Amrnb_lsfmeanrem2.gif]]<br />
<br />
; z_j(n)[i] : the ith coefficient of the mean-removed LSF vector at the jth subframe<br />
; r_j(n)[i] : the ith coefficient of the prediction residual vector of frame n at the jth subframe<br />
; pred_fac[i] : the ith coefficient of the prediction factor<br />
; ^r_j(n-1)[i] : the ith coefficient of the the quantified residual vector of the previous frame at the jth subframe<br />
<br />
These processes give the LSP vector at the 4th subframe (q4)<br />
<br />
=== LSP vector interpolation (c.f. §5.2.6) ===<br />
<br />
==== 12.2 kbps mode ====<br />
<br />
[[Image: Amrnb_lspinterpq1-1.gif]]<br />
<br />
[[Image: Amrnb_lspinterpq3-1.gif]]<br />
<br />
==== Other modes ====<br />
<br />
[[Image: Amrnb_lspinterpq1-2.gif]]<br />
<br />
[[Image: Amrnb_lspinterpq2.gif]]<br />
<br />
[[Image: Amrnb_lspinterpq3-2.gif]]<br />
<br />
=== LSP vector to LP filter coefficient conversion (c.f. §5.2.4) ===<br />
<br />
for i=1..5<br />
f1[i] = 2*f1[i-2] - 2*q[2i-1]*f1[i-1]<br />
for j=i-1..1<br />
f1[j] += f1[j-2] - 2*q[2i-1]*f1[j-1]<br />
end<br />
end<br />
<br />
f1[-1] = 0; f1[0] = 0;<br />
<br />
Same for f2[i] with q[2i] instead of q[2i-1]<br />
<br />
for i=1..5<br />
f'1[i] = f1[i] + f1[i-1]<br />
f'2[i] = f2[i] - f2[i-1]<br />
end<br />
<br />
[[Image: Amrnb_lpccoeff.gif]]<br />
<br />
; a_i : the LP filter coefficients<br />
<br />
== Decoding of the pitch (adaptive codebook) vector ==<br />
<br />
* indices parsed from bitstream<br />
* indices give integer and fractional parts of the pitch lag<br />
* pitch vector v(n) is found by interpolating the past excitation u(n) at the pitch lag using an FIR filter. (c.f. §5.6)<br />
<br />
<br />
=== Decode pitch lag ===<br />
<br />
'''Note: division in this section is integer division!'''<br />
<br />
==== 12.2kbps mode - 1/6 resolution pitch lag ====<br />
<br />
===== First and third subframes =====<br />
<br />
In the first and third subframes, a fractional pitch lag is used with resolutions:<br />
<br />
* 1/6 in the range [17 3/6, 94 3/6]<br />
* 1 in the range [95, 143]<br />
<br />
...encoded using 9 bits.<br />
<br />
For [17 3/6, 94 3/6] the pitch index is encoded as:<br />
<br />
pitch_index = (pitch_lag_int - 17)*6 + pitch_lag_frac - 3;<br />
<br />
; pitch_lag_int : integer part of the pitch lag in the range [17, 94]<br />
; pitch_lag_frac : fractional part of the pitch lag in 1/6 units in the range [-2, 3]<br />
<br />
so...<br />
<br />
if(pitch_index < (94 4/6 - 17 3/6)*6)<br />
// fractional part is encoded in range [17 3/6, 94 3/6]<br />
pitch_lag_int = (pitch_index + 5)/6 + 17;<br />
pitch_lag_frac = pitch_index - pitch_lag_int*6 + (17 3/6)*6;<br />
<br />
And for [95, 143] the pitch index is encoded as:<br />
<br />
pitch_index = (pitch_lag_int - 95) + (94 4/6 - 17 3/6)*6;<br />
<br />
; pitch_lag_int : integer pitch lag in the range [95, 143]<br />
<br />
so...<br />
<br />
else<br />
// only integer part encoded in range [95, 143], no fractional part<br />
pitch_lag_int = pitch_index - (94 4/6 - 17 3/6)*6 + 95;<br />
pitch_lag_frac = 0;<br />
<br />
===== Second and fourth subframes =====<br />
<br />
In the second and fourth subframes, a pitch lag resolution of 1/6 is always used in the range [T1 - 5 3/6, T1 + 4 3/6], where T1 is nearest integer to the fractional pitch lag of the previous (1st or 3rd) subframe. The search range is bounded by [18, 143]. In this case the pitch delay is encoded using 6 bits and is therefore in the range [0,63].<br />
<br />
So the search range for the pitch lag is:<br />
<br />
search_range_min = max(pitch_lag_int_prev - 5, 18);<br />
search_range_max = search_range_min + 9;<br />
if(search_range_max > 143) {<br />
search_range_max = 143;<br />
search_range_min = search_range_max - 9;<br />
}<br />
<br />
; pitch_lag_int_prev : the integer part of the pitch lag from the previous sub frame<br />
<br />
The pitch index is encoded as:<br />
<br />
pitch_index = (pitch_lag_int - (search_range_min - 1))*6 + pitch_lag_frac - 3;<br />
<br />
; pitch_lag_int : the integer part of the pitch lag in the range [search_range_min - 1, search_range_max]<br />
; pitch_lag_frac : the fractional part of the pitch lag in the range [-2, 3]<br />
<br />
The formula for the pitch_index has been chosen to map pitch_lag_int [search_range_min - 1, search_range_max] and pitch_lag_frac [-2, 3] to [0,60]. (pitch_index = [0, 10]*6 + [-2, 3] - 3 = [0, 6, ..., 60] + [-5, 0] = [0,60])<br />
<br />
So the pitch lag is calculated through:<br />
<br />
// integer part of pitch lag = position in range [search_range_min - 1, search_range_max] + lower bound of range<br />
pitch_lag_int = (pitch_index + 5)/6 + search_range_min - 1;<br />
// fractional part of pitch lag = pitch index - (integer part without offset)*6 - 1 3/6 offset to bring the values to the correct range<br />
pitch_lag_frac = pitch_index - ((pitch_index + 5)/6)*6 - 9;<br />
<br />
Note that when using integers and integer division to conduct (pitch_index + 5)/6 the result is similar to taking the ceiling of pitch_index/6.0.<br />
<br />
==== Others modes - 1/3 resolution pitch lag ====<br />
<br />
===== First and third subframes =====<br />
<br />
In the first and third subframes, a fractional pitch lag is used with resolutions:<br />
<br />
* 1/3 in the range [19 1/3, 84 2/3]<br />
* 1 in the range [85, 143]<br />
<br />
...encoded using 8 bits.<br />
<br />
For [19 1/3, 84 2/3] the pitch lag is encoded as:<br />
<br />
pitch_index = pitch_lag_int*3 + pitch_lag_frac - (19 1/3)*3;<br />
<br />
; pitch_lag_int : integer part of the pitch lag in the range [19, 84]<br />
; pitch_lag_frac : fractional part of the pitch lag in 1/3 units in the range [0, 2]<br />
<br />
so...<br />
<br />
if(pitch_index < (85 - 19 1/3)*3)<br />
// fractional part is encoded in range [19 1/3, 84 2/3]<br />
pitch_lag_int = (pitch_index + 2)/3 + 19;<br />
pitch_lag_frac = pitch_index - pitch_lag_int*3 + (19 1/3)*3;<br />
<br />
And for [85, 143] the pitch index is encoded as:<br />
<br />
pitch_index = pitch_lag_int - 85 + (85 - 19 1/3)*3;<br />
<br />
; pitch_lag_int : integer pitch lag in the range [85, 143]<br />
<br />
so...<br />
<br />
else<br />
// only integer part encoded in range [85, 143], no fractional part<br />
pitch_lag_int = pitch_index - (85 - 19 1/3)*3 + 85;<br />
pitch_lag_frac = 0;<br />
<br />
===== Second and fourth subframes =====<br />
<br />
In the second and fourth subframes, the pitch lag resolution varies depending on the mode as follows:<br />
<br />
* 7.95 kbps mode<br />
** resolution of 1/3 is always used in the range [T1 - 10 2/3, T1 + 9 2/3]<br />
** encoded using 6 bits => pitch_index is in the range [0, 63]<br />
* 10.2 and 7.40 kbps modes<br />
** resolution of 1/3 is always used in the range [T1 - 5 2/3, T1 + 4 2/3]<br />
** encoded using 5 bits => pitch_index is in the range [0, 31]<br />
* 6.70, 5.90, 5.15 and 4.75 kbps modes<br />
** resolution of 1 is used in the range [T1 - 5, T1 + 4]<br />
** resolution of 1/3 is always used in the range [T1 - 1 2/3, T1 + 2/3]<br />
** encoded using 4 bits => pitch_index is in the range [0, 15]<br />
<br />
Where T1 is nearest integer to the fractional pitch lag of the previous (1st or 3rd) subframe. The search range is bounded by [20, 143].<br />
<br />
So the search range for the pitch lag is:<br />
<br />
lower_bound = 5;<br />
range = 9;<br />
if(mode == 7.95) {<br />
lower_bound = 10;<br />
range = 19;<br />
}<br />
search_range_min = max(pitch_lag_int_prev - lower_bound, 20);<br />
search_range_max = search_range_min + range;<br />
if(search_range_max > 143) {<br />
search_range_max = 143;<br />
search_range_min = search_range_max - range;<br />
}<br />
<br />
; pitch_lag_int_prev : the integer part of the pitch lag from the previous sub frame<br />
<br />
<br />
For modes 7.40, 7.95 and 10.2 the pitch index is encoded as:<br />
<br />
pitch_index = (pitch_lag_int - search_range_min)*3 + pitch_lag_frac + 2;<br />
<br />
; pitch_lag_int : the integer part of the pitch lag in the range [search_range_min, search_range_max]<br />
; pitch_lag_frac : the fractional part of the pitch lag in the range [-1, 1]<br />
<br />
So the pitch lag is calculated through:<br />
<br />
// integer part of pitch lag = position of pitch lag in range [search_range_min, search_range_max] + lower bound of the range<br />
pitch_lag_int = (pitch_index + 2)/3 - 1 + search_range_min;<br />
// fractional part of pitch lag = pitch index - (integer part without offset)*3 - 2/3 to bring the values to the correct range<br />
pitch_lag_frac = pitch_index - ((pitch_index + 2)/3 - 1)*3 - 2;<br />
<br />
<br />
For modes 4.75, 5.15, 5.90 and 6.70:<br />
<br />
t1_temp = max( min(pitch_lag_int_prev, search_range_min + 5), search_range_max - 4 );<br />
<br />
; t1_temp : predicted pitch lag from the previous frame adjusted to fit into the 0 position of the search range<br />
<br />
The pitch index is encoded as:<br />
<br />
// if pitch lag is below T1 - 1 2/3<br />
if( pitch_lag_int*3 + pitch_lag_frac <= (t1_temp - 2)*3 ) {<br />
// encode with resolution 1<br />
index = (pitch_lag_int - t1_temp) + 5;<br />
// else if pitch lag is below T1 + 1<br />
}else if( pitch_lag_int*3 + pitch_lag_frac < (t1_temp + 1)*3 ) {<br />
// encode with resolution 1/3<br />
index = ( pitch_lag_int*3 + pitch_lag_frac - (t1_temp - 2)*3 ) + 3;<br />
// else pitch lag is above T1 + 2/3<br />
}else {<br />
// encode with resolution 1<br />
index = (pitch_lag_int - t1_temp) + 11;<br />
}<br />
<br />
; pitch_lag_int : the integer part of the pitch lag in the range [search_range_min, search_range_max]<br />
; pitch_lag_frac : the fractional part of the pitch lag in the range [-1, 1]<br />
<br />
The possible pitch indices and values are:<br />
<br />
{| border=1<br />
|-<br />
| 0 || 1 || 2 || 3 || 4 || 5 || 6 || 7 || 8 || 9 || 10 || 11 || 12 || 13 || 14 || 15<br />
|-<br />
| -5 || -4 || -3 || -2 || -1 2/3 || -1 1/3 || -1 || -2/3 || -1/3 || 0 || 1/3 || 2/3 || 1 || 2 || 3 || 4<br />
|-<br />
|}<br />
<br />
So the pitch lag is calculated through:<br />
<br />
if(pitch_index < 4) {<br />
// integer part of pitch lag = pitch lag position in range [t1_temp - 5, t1_temp - 2] + lower bound of range<br />
pitch_lag_int = pitch_index + (t1_temp - 5);<br />
// this range is coded with resolution 1 so no fractional part<br />
pitch_lag_frac = 0;<br />
}else if(pitch_index < 12) {<br />
pitch_lag_int = (pitch_index - 2)/3 + (t1_temp - 2);<br />
pitch_lag_frac = (pitch_index - 4) - ((pitch_index - 2)/3)*3 - 11;<br />
}else {<br />
// integer part of pitch lag = pitch lag position in range [t1_temp + 1, t1_temp + 4] + lower bound of range<br />
pitch_lag_int = pitch_index - 12 + t1_temp + 1;<br />
// this range is coded with resolution 1 so no fractional part<br />
pitch_lag_frac = 0;<br />
}<br />
<br />
<br />
=== Calculate pitch vector ===<br />
<br />
[[Image: Amrnb_pitchvector.gif]]<br />
<br />
; k : integer pitch lag<br />
; n : sample position in the vectors 0, ..., 39<br />
; t : 0, ..., 5 corresponding to fractions 0, 1/6, 2/6, 3/6, -2/6, -1/6 respectively<br />
<br />
This equation can be used for both 1/3 and 1/6 resolution simply by multiplying t by 2 in the 1/3 case.<br />
<br />
(Note: the coefficients b60 are in the reference source in an array called inter6)<br />
<br />
<br />
<br />
== Decoding of the fixed (innovative or algebraic) vector ==<br />
<br />
* the excitation pulse positions and signs are parsed from the bit stream<br />
* the pulse positions and signs are encoded differently depending on the mode<br />
* the fixed code book vector, c(n), is then constructed from the pulse positions and signs<br />
* if pitch_lag_int is less than the subframe size (40), the pitch sharpening procedure is applied<br />
<br />
<br />
=== Decoding the pulse positions ===<br />
<br />
==== 12.2 kbps mode ====<br />
<br />
* 10 pulse positions each coded using 3 bit Gray codes<br />
* signs coded using 1 bit each for 5 pulse pairs<br />
<br />
<br />
{| border=1<br />
! Pulse !! Positions<br />
|-<br />
| i0,i5 || 0, 5, 10, 15, 20, 25, 30, 35<br />
|-<br />
| i1,i6 || 1, 6, 11, 16, 21, 26, 31, 36<br />
|-<br />
| i2,i7 || 2, 7, 12, 17, 22, 27, 32, 37<br />
|-<br />
| i3,i8 || 3, 8, 13, 18, 23, 28, 33, 38<br />
|-<br />
| i4,i9 || 4, 9, 14, 19, 24, 29, 34, 39<br />
|-<br />
|}<br />
<br />
<br />
==== 10.2 kbps mode ====<br />
<br />
* 8 pulse positions, 4 pairs, coded as 3 values using 10, 10 and 7 bits<br />
* signs coded using 1 bit each for 4 pulse pairs<br />
<br />
<br />
{| border=1<br />
! Pulse !! Positions<br />
|-<br />
| i0,i4 || 0, 4, 8, 12, 16, 20, 24, 28, 32, 36<br />
|-<br />
| i1,i5 || 1, 5, 9, 13, 17, 21, 25, 29, 33, 37<br />
|-<br />
| i2,i6 || 2, 6, 10, 14, 18, 22, 26, 30, 34, 38<br />
|-<br />
| i3,i7 || 3, 7, 11, 15, 19, 23, 27, 31, 35, 39<br />
|-<br />
|}<br />
<br />
<br />
==== 7.95 and 7.40 kbps modes ====<br />
<br />
* 4 pulse positions Gray coded using 3, 3, 3 and 4 bits<br />
* signs coded using 1 bit for each pulse<br />
<br />
<br />
{| border=1<br />
! Pulse !! Positions<br />
|-<br />
| i0 || 0, 5, 10, 15, 20, 25, 30, 35<br />
|-<br />
| i1 || 1, 6, 11, 16, 21, 26, 31, 36<br />
|-<br />
| i2 || 2, 7, 12, 17, 22, 27, 32, 37<br />
|-<br />
| i3 || 3, 8, 13, 18, 23, 28, 33, 38<br />
4, 9, 14, 19, 24, 29, 34, 39<br />
|-<br />
|}<br />
<br />
==== 6.70 kbps mode ====<br />
<br />
* 3 pulse positions coded using 3, 4 and 4 bits<br />
* signs coded using 1 bit for each pulse<br />
<br />
<br />
{| border=1<br />
! Pulse !! Positions<br />
|-<br />
| i0 || 0, 5, 10, 15, 20, 25, 30, 35<br />
|-<br />
| i1 || 1, 6, 11, 16, 21, 26, 31, 36<br />
3, 8, 13, 18, 23, 28, 33, 38<br />
|-<br />
| i2 || 2, 7, 12, 17, 22, 27, 32, 37<br />
4, 9, 14, 19, 24, 29, 34, 39<br />
|-<br />
|}<br />
<br />
<br />
==== 5.90 kbps mode ====<br />
<br />
* 2 pulse positions coded using 4 and 5 bits<br />
* signs coded using 1 bit for each pulse<br />
<br />
<br />
{| border=1<br />
! Pulse !! Positions<br />
|-<br />
| i0 || 1, 6, 11, 16, 21, 26, 31, 36<br />
3, 8, 13, 18, 23, 28, 33, 38<br />
|-<br />
| i1 || 0, 5, 10, 15, 20, 25, 30, 35<br />
1, 6, 11, 16, 21, 26, 31, 36<br><br />
2, 7, 12, 17, 22, 27, 32, 37<br><br />
4, 9, 14, 19, 24, 29, 34, 39<br />
|-<br />
|}<br />
<br />
==== 5.15 and 4.75 kbps modes ====<br />
<br />
* 2 pulse positions coded using 1 bit for the position subset and 3 bits per pulse<br />
* signs coded using 1 bit for each pulse<br />
<br />
<br />
{| border=1<br />
! Subframe !! Subset !! Pulse !! Positions<br />
|-<br />
| rowspan="4" align="center" | 1<br />
| rowspan="2" align="center" | 1<br />
| i0 || 0, 5, 10, 15, 20, 25, 30, 35<br />
|-<br />
| i1 || 2, 7, 12, 17, 22, 27, 32, 37<br />
|-<br />
| rowspan="2" align="center" | 2<br />
| i0 || 1, 6, 11, 16, 21, 26, 31, 36<br />
|-<br />
| i1 || 3, 8, 13, 18, 23, 28, 33, 38<br />
|-<br />
| rowspan="4" align="center" | 2<br />
| rowspan="2" align="center" | 1<br />
| i0 || 0, 5, 10, 15, 20, 25, 30, 35<br />
|-<br />
| i1 || 3, 8, 13, 18, 23, 28, 33, 38<br />
|-<br />
| rowspan="2" align="center" | 2<br />
| i0 || 2, 7, 12, 17, 22, 27, 32, 37<br />
|-<br />
| i1 || 4, 9, 14, 19, 24, 29, 34, 39<br />
|-<br />
| rowspan="4" align="center" | 3<br />
| rowspan="2" align="center" | 1<br />
| i0 || 0, 5, 10, 15, 20, 25, 30, 35<br />
|-<br />
| i1 || 2, 7, 12, 17, 22, 27, 32, 37<br />
|-<br />
| rowspan="2" align="center" | 2<br />
| i0 || 1, 6, 11, 16, 21, 26, 31, 36<br />
|-<br />
| i1 || 4, 9, 14, 19, 24, 29, 34, 39<br />
|-<br />
| rowspan="4" align="center" | 4<br />
| rowspan="2" align="center" | 1<br />
| i0 || 0, 5, 10, 15, 20, 25, 30, 35<br />
|-<br />
| i1 || 3, 8, 13, 18, 23, 28, 33, 38<br />
|-<br />
| rowspan="2" align="center" | 2<br />
| i0 || 1, 6, 11, 16, 21, 26, 31, 36<br />
|-<br />
| i1 || 4, 9, 14, 19, 24, 29, 34, 39<br />
|-<br />
|}<br />
<br />
<br />
=== Fixed codebook vector construction ===<br />
<br />
All c(n) are zero if there is no pulse at position n. If there is a pulse at position n then it has the corresponding sign as parsed above.<br />
<br />
<br />
=== Pitch sharpening ===<br />
<br />
[[Image: Amrnb fixvecpitshp.gif]]<br />
<br />
; β : the decoded pitch gain, ^g_p, bounded by [0.0,1.0] for 12.2. kbps or [0.0,0.8] for other modes<br />
<br />
Note: Only sharpen n in [pitch_lag_int-1, 39]. The reference source considers previous fixed vector elements (n<0) to be 0. I think this is also the reason pitch sharpening is not conducted for pitch_lag_int > 40 as all the sharpening contributions would be 0.<br />
<br />
== Decoding of the pitch and fixed codebook gains ==<br />
<br />
<br />
=== Fixed gain prediction ===<br />
<br />
A moving average prediction of the innovation (fixed) energy is conducted.<br />
<br />
<br />
[[Image: Amrnb_fixedgainpred.gif]]<br />
<br />
; g_c' : fixed gain prediction<br />
; \tilde{E} : predicted energy [dB]<br />
; \bar{E} : desired mean innovation (fixed) energy [dB]<br />
; E_I : calculated mean innovation (fixed) energy [dB]<br />
<br />
<br />
[[Image: Amrnb_predener.gif]]<br />
<br />
; b : 4-tap MA prediction coefficients [0.68, 0.58, 0.34, 0.19]<br />
; ^R(k) : quantified prediction errors at subframe k 20*log10(^γ_gc(k))<br />
<br />
<br />
Desired mean innovation (fixed) energy:<br />
<br />
{| border=1<br />
! Mode (kbps) !! Mean energy (dB)<br />
|-<br />
| 12.2 || 36<br />
|-<br />
| 10.2 || 33<br />
|-<br />
| 7.95 || 36<br />
|-<br />
| 7.40 || 30<br />
|-<br />
| 6.70 || 28.75<br />
|-<br />
| 5.90 || 33<br />
|-<br />
| 5.15 || 33<br />
|-<br />
| 4.75 || 33<br />
|-<br />
|}<br />
<br />
<br />
[[Image: Amrnb_meaniner.gif]]<br />
<br />
; E_I : calculated mean innovation (fixed) energy [dB]<br />
; N : subframe size 40<br />
; c(n) : fixed codebook vector<br />
<br />
=== Dequantisation of the gains ===<br />
<br />
==== 12.2kbps and 7.95kbps - scalar quantised gains ====<br />
<br />
The received indices are used to find the quantified pitch gain, ^g_p, and the quantified fixed gain correction factor, ^γ_gc.<br />
<br />
===== Pitch gain =====<br />
<br />
The parsed gain index is used to obtain the quantified pitch gain, ^g_p, from the corresponding codebook. (qua_gain_pitch in the reference source.)<br />
<br />
===== Fixed gain correction factor =====<br />
<br />
The parsed gain index is used to obtain the quantified fixed gain, ^g_c, from the corresponding codebook. (qua_gain_code in the reference source.) The table stores ^γ_gc, and the quantised energy error in two forms (these are needed for the moving average calculation of the predicted fixed gain):<br />
<br />
qua_ener_MR122 = log2(^γ_gc)<br />
qua_ener = 20*log10(^γ_gc)<br />
<br />
^γ_gc is stored at Q11 (i.e. it's multiplied by 2^11.) qua_ener_MR122 and qua_ener are stored at Q10.<br />
<br />
==== Other modes - vector quantised gains ====<br />
<br />
The received index gives both the quantified adaptive codebook gain, ^g_p, and the quantified algebraic codebook gain correction factor, ^γ_gc.<br />
<br />
The tables contains the following data:<br />
<br />
^g_p (Q14),<br />
^γ_gc (Q12), (^g_c = g_c'*^γ_gc),<br />
qua_ener_MR122 (Q10), (log2(^γ_gc))<br />
qua_ener (Q10) (20*log10(^γ_gc))<br />
<br />
The log2() and log10() values are calculated on the fixed point value<br />
(g_fac Q12) and not on the original floating point value of g_fac<br />
to make the quantizer/MA predictdor use corresponding values.<br />
<br />
The codebook used depends on the mode:<br />
<br />
6.70, 7.40, 10.2 kbps modes - table_gain_highrates in the reference source<br />
Four consecutive entries give ^g_p, ^γ_gc, qua_ener_MR122 and qua_ener. Apparently the values for qua_ener are the original ones from IS641 to ensure bit-exactness but are not exactly the rounded value of 20log10(^γ_gc).<br />
<br />
5.15, 5.90 kbps modes - table_gain_lowrates<br />
Similar to table_gain_highrates, four consecutive entries give ^g_p, ^γ_gc, qua_ener_MR122 and qua_ener. There are no special notes for this table.<br />
<br />
4.75 kbps mode - table_gain_MR475<br />
Unlike the above mentioned tables, four consecutive values give ^g_p, ^γ_gc for subframes 0,2, and then ^g_p, ^γ_gc for subframes 1,3.<br />
<br />
At the very least I think these tables could be redesigned a little.<br />
<br />
=== Calculation of the quantified fixed gain ===<br />
<br />
[[Image: Amrnb_fixedgain.gif]]<br />
<br />
== Smoothing of the fixed codebook gain ==<br />
<br />
<br />
'''10.2, 6.70, 5.90, 5.15, 4.75 kbit/s modes only'''<br />
<br />
<br />
=== Calculate averaged LSP vector ===<br />
<br />
[[Image: Amrnb_lspsmooth.gif]]<br />
<br />
; \bar{q}(n) : averaged LSP vector at frame n<br />
; ^q_4(n) : quantified LSP vector for the 4th subframe at frame n<br />
<br />
<br />
=== Calculate fixed gain smoothing factor ===<br />
<br />
[[Image: Amrnb_lspdiff.gif]]<br />
<br />
; diff_m : difference measure at subframe m<br />
; j : loops over LSPs<br />
; m : loops over subframes<br />
; ^q_m : quantified LSP vector at subframe m<br />
<br />
Note: I think the sum over the subframes is an error in the specification and they actually mean to use the quantified LSP vector from subframe m for the calculation of diff for subframe m. I think this is what they do in the reference source and the sum over m is an error.<br />
<br />
<br />
[[Image: Amrnb_smoothfac.gif]]<br />
<br />
; k_m : fixed gain smoothing factor<br />
; K_1 : 0.4<br />
; K_2 : 0.25<br />
; diff_m : difference measure at subframe m<br />
<br />
If diff_m has been greater than 0.65 for 10 subframes, k_m is set to 1.0 (i.e. no smoothing) for 40 subframes.<br />
<br />
=== Calculate mean fixed gain ===<br />
<br />
[[Image: Amrnb_meangc.gif]]<br />
<br />
; \bar{g_c}(m) : mean fixed gain at subframe m<br />
; ^g_c(k) : quantified fixed gain at subframe k<br />
<br />
=== Calculate smoothed fixed gain ===<br />
<br />
[[Image: Amrnb_gcsmooth.gif]]<br />
<br />
; ^g_c : quantified fixed gain<br />
; \bar{g_c} : averaged fixed gain<br />
<br />
== Anti-sparseness processing ==<br />
<br />
<br />
'''7.95, 6.70, 5.90, 5.15, 4.75 kbit/s modes only'''<br />
<br />
<br />
=== Evaluate impulse response filter strength ===<br />
<br />
The fixed vector, c(n), has only a few pulses per subframe. In certain conditions, to reduce perceptual artifacts arising from this, the vector is circularly convolved with a predefined impulse response. The selection of the strength of filter is made based on the decoded gains.<br />
<br />
if ^g_p < 0.6<br />
impNr = 0<br />
else if ^g_p < 0.9<br />
impNr = 1<br />
else<br />
impNr = 2<br />
<br />
; impNr = 0 : strong impulse response filter<br />
; impNr = 1 : medium impulse response filter<br />
; impNr = 2 : no filtering<br />
<br />
if ^g_c(k) > 2 * ^g_c(k-1)<br />
impNr = min( impNr + 1, 2 )<br />
else if impNr = 0 AND median of last five ^g_p >= 0.6<br />
impNr = min( impNr(k), min(impNr(k-1) + 1, 2) )<br />
<br />
<br />
=== Circular convolution of fixed vector and impulse response filter ===<br />
<br />
[[Image: Amrnb_circconv.gif]]<br />
<br />
; (c * h) : convolution of vectors c and h, in this case circular convolution<br />
; h[n] : nth coefficient of the impulse response used for filtering<br />
; c[n] : nth coefficient of the fixed vector<br />
<br />
n = 0, ..., 39<br />
<br />
To make the convolution circular, make the impulse response circular by taking h[-m] = h[39-m]<br />
<br />
== Computing the reconstructed speech ==<br />
<br />
<br />
=== Construct excitation ===<br />
<br />
[[Image: Amrnb_excitation.gif]]<br />
<br />
; u(n) : excitation vector<br />
; ^g_p : quantified pitch gain<br />
; v(n) : pitch vector<br />
; ^g_c : quantified fixed gain<br />
; c(n) : fixed vector<br />
<br />
<br />
=== Emphasise pitch vector contribution ===<br />
<br />
This is apparently a post-processing technique.<br />
<br />
[[Image: Amrnb_pitchvecemph.gif]]<br />
<br />
; ^u(n) : excitation vector with emphasised pitch vector contribution<br />
; β : ^g_p bounded by [0.0, 0.8] or [0.0, 1.0] depending on mode<br />
<br />
<br />
=== Apply adaptive gain control (AGC) through gain scaling ===<br />
<br />
[[Image: Amrnb_gainfac.gif]]<br />
<br />
; η : gain scaling factor for emphasised excitation<br />
<br />
[[Image: Amrnb_gainscalexc.gif]]<br />
<br />
; ^u'(n) : gain-scaled emphasised excitation<br />
<br />
<br />
=== Calculate reconstructed speech samples ===<br />
<br />
[[Image: Amrnb_synthfilter.gif]]<br />
<br />
; ^s(n) : reconstructed speech samples<br />
; ^a_i : LP filter coefficients<br />
<br />
Note: for n-i < 0, ^s(n-i) should be take from previous speech samples if existing, else we will consider them 0 as this behaviour is undefined the specification.<br />
<br />
== Additional instability protection ==<br />
<br />
If an overflow occurs during synthesis, the pitch vector, v(n), is scaled down by a factor of 4 and synthesis is conducted again bypassing emphasising the pitch vector contribution and adaptive gain control.<br />
<br />
Q: What classifies an overflow?<br />
A: s(n) < -32768 or s(n) > 32767 (i.e. 16-bit signed int)<br />
<br />
== Post-processing ==<br />
<br />
=== Adaptive post-filtering ===<br />
<br />
(c.f. §6.2.1)<br />
<br />
==== IIR filtering ====<br />
<br />
The speech samples, ^s(n), are filtered through a formant filter and a tilt compensation filter.<br />
<br />
[[Image: Amrnb_pfformant.gif]]<br />
<br />
; γ_n, γ_d : control the amount of formant post-filtering<br />
<br />
The speech samples, ^s(n), are filtered through ^A(z/γ_n) to produce the residual signal ^r(n).<br />
<br />
[[Image: Amrnb_residual.gif]]<br />
<br />
^r(n) is filtered through 1/^A(z/γ_d).<br />
<br />
[[Image: Amrnb_fmntfilt.gif]]<br />
<br />
The output is filtered through H_t(z) (the tilt compensation filter) resulting in the post-filtered speech signal, ^s_f(n).<br />
<br />
[[Image: Amrnb_pftilt.gif]]<br />
<br />
[[Image: Amrnb_mu.gif]]<br />
<br />
[[Image: Amrnb_reffac.gif]]<br />
<br />
[[Image: Amrnb_r_h.gif]]<br />
<br />
; L_h : 22 - the truncation of the impulse response<br />
; h_f : impulse response of the formant filter H_f<br />
<br />
So, in summation notation:<br />
<br />
[[Image: Amrnb_pftiltsum.gif]]<br />
<br />
===== 12.2 and 10.2 kbps modes =====<br />
<br />
γ_n = 0.7<br />
<br />
γ_d = 0.75<br />
<br />
γ_t = 0.8 if k_1' > 0; 0 otherwise<br />
<br />
===== Other modes =====<br />
<br />
γ_n = 0.55<br />
<br />
γ_d = 0.7<br />
<br />
γ_t = 0.8<br />
<br />
==== Adaptive gain control ====<br />
<br />
Adaptive gain control is used to compensate for the gain difference between the filtered and synthesised speech signals.<br />
<br />
[[Image: Amrnb_gainscalfac.gif]]<br />
<br />
[[Image: Amrnb_gainscalfacwtd.gif]]<br />
<br />
[[Image: Amrnb_gainscalspch.gif]]<br />
<br />
; \alpha : adaptive gain control factor equal to 0.9<br />
<br />
=== High-pass filtering and upscaling ===<br />
<br />
(c.f. §6.2.2)<br />
<br />
[[Image: Amrnb_highpass.gif]]<br />
<br />
After completing all filtering, the samples are scaled up by a factor of 2.<br />
<br />
<br />
<br />
<br />
[[Category:Vocoders]]<br />
[[Category:Audio Codecs]]</div>Adam Quantrillhttps://wiki.multimedia.cx/index.php?title=AMR-NB&diff=14379AMR-NB2013-03-05T17:54:40Z<p>Adam Quantrill: /* Bit stream frame format */</p>
<hr />
<div>* samples: http://samples.mplayerhq.hu/A-codecs/amr/<br />
* specification: http://www.3gpp.org/ftp/Specs/html-info/26-series.htm<br />
* Wikipedia article: http://en.wikipedia.org/wiki/Adaptive_Multi-Rate<br />
<br />
AMR-NB (Adaptive Multi-Rate Narrowband) is a [[vocoder]] employed in low-bitrate applications like mobile phones. It is a form of ACELP where A stands for algebraic.<br />
<br />
----<br />
<br />
The following text aims to be a simpler and more explicit document of the AMR narrow band decoding processes to aid in development of a decoder. Reference to sections of the specification will be made in the following format: (c.f. §5.2.5). Happy reading.<br />
<br />
<br />
<br />
== Nomenclature weirdness ==<br />
<br />
Throughout the specification, a number of references are made to the same (or very similar) items with fairly confusing variation. They are listed below to aid understanding of the following text but efforts will be made to consistently use one item name throughout or to use both with the lesser used name in parenthesis.<br />
<br />
* Pitch / Adaptive codebook<br />
* Fixed / Innovative (also algebraic when referring to the codebook)<br />
* Quantified means quantized<br />
<br />
<br />
== Summary ==<br />
<br />
* Mode dependent bitstream parsing<br />
* Indices parsed from bitstream<br />
* Indices decoded to give LSF vectors, fractional pitch lags, innovative code vectors and the pitch and innovative gains<br />
* LSF vectors converted to LP filter coefficients at each subframe<br />
* Subframe decoding<br />
** Excitation vector = adaptive code vector * adaptive (pitch) gain + innovative code vector * innovative gain<br />
** Excitation vector filtered through an LP synthesis filter to reconstruct speech<br />
** Speech signal filtered with adaptive postfilter<br />
<br />
<br />
<br />
== Bit stream frame format ==<br />
Specification ([http://www.3gpp.org/ftp/Specs/html-info/26101.htm 26.101]) describes two possible frame types - interface formats 1 and 2 (often abbreviated IF1 and IF2). IF2 is byte-aligned. The following tables of data are taken from this specification unless otherwise stated. AMR-NB over GSM uses a third slightly different format whose bit-packing is tightly defined by the GSM specs.<br />
<br />
=== IF1 format ===<br />
<br />
{| border=1<br />
! bits !! low level meaning || high level meaning<br />
|-<br />
| 4<br />
| Frame type<br />
| rowspan=2 | AMR header<br />
|-<br />
| 1<br />
| Frame quality indicator (0 bad/1 good)<br />
|-<br />
| 3<br />
| Mode indication<br />
| rowspan=3 | AMR auxiliary information<br />
|-<br />
| 3<br />
| Mode request<br />
|-<br />
| 8<br />
| CRC<br />
|-<br />
| &nbsp;<br />
| Class A bits<br />
| rowspan=3 | AMR core frame<br />
|-<br />
| &nbsp;<br />
| Class B bits<br />
|-<br />
| &nbsp;<br />
| Class C bits<br />
|}<br />
<br />
=== IF2 format ===<br />
<br />
{| border=1<br />
! bits !! low level meaning || high level meaning<br />
|-<br />
| 4<br />
| Frame type<br />
| AMR header<br />
|-<br />
| &nbsp;<br />
| Class A bits<br />
| rowspan=3 | AMR core frame<br />
|-<br />
| &nbsp;<br />
| Class B bits<br />
|-<br />
| &nbsp;<br />
| Class C bits<br />
|-<br />
| &nbsp;<br />
| Padding (called "Bit stuffing" in the specification)<br />
| &nbsp;<br />
|}<br />
<br />
=== Field meaning ===<br />
<br />
{| border=1<br />
! Frame type !! Frame content<br />
|-<br />
| 0 || AMR 4.75kbps<br />
|-<br />
| 1 || AMR 5.15kbps<br />
|-<br />
| 2 || AMR 5.90kbps<br />
|-<br />
| 3 || AMR 6.70kbps (PDC-EFR)<br />
|-<br />
| 4 || AMR 7.40kbps (TDMA-EFR)<br />
|-<br />
| 5 || AMR 7.95kbps<br />
|-<br />
| 6 || AMR 10.2kbps<br />
|-<br />
| 7 || AMR 12.2kbps (GSM-EFR)<br />
|-<br />
| 8 || AMR SID<br />
|-<br />
| 9 || GSM-EFR SID<br />
|-<br />
| 10 || TDMA-EFR SID<br />
|-<br />
| 11 || PDC-EFR SID<br />
|-<br />
| 12-14 || Reserved for future use<br />
|-<br />
| 15 || No data (no transmission/no reception)<br />
|}<br />
<br />
=== Classes ===<br />
<br />
{| border=1<br />
! Class !! Importance explanation<br />
|-<br />
| A || Data that is most sensitive to error. Any error in these bits leads to a corrupted speech frame that should not be decoded without appropriate error concealment. This class of bits is protected by an 8-bit CRC.<br />
|-<br />
| B || Less sensitive data that are present in all speech frames.<br />
|-<br />
| C || Least sensitive data present only in higher bit rate frames.<br />
|}<br />
<br />
* Class A is protected by an 8-bit CRC with polynomial x^8+x^6+x^5+x^4+1 computed over the Class A bits.<br />
* However for AMR-NB over GSM the CRC is a 6-bit with polynomial x^6+x^5+x^3+x^2+x+1 computed over the Class A bits, and it immediately follows the Class A bits, rather than preceding them. That is, it's between the Class A and Class B bits. [see 3GPP TS 05.03 sec 3.9.4.3]<br />
* There is no significant step-wise change in subjective importance at class boundaries.<br />
* The distribution of bits is ordered from most to least subjective importance at both the class level and within the classes.<br />
<br />
{| border=1<br />
! Frame type !! Total bits !! Class A bits !! Class B bits !! Class C bits<br />
|-<br />
| 0 || 95 || 42 || 53 || 0<br />
|-<br />
| 1 || 103 || 49 || 54 || 0<br />
|-<br />
| 2 || 118 || 55 || 63 || 0<br />
|-<br />
| 3 || 134 || 58 || 76 || 0<br />
|-<br />
| 4 || 148 || 61 || 87 || 0<br />
|-<br />
| 5 || 159 || 75 || 84 || 0<br />
|-<br />
| 6 || 204 || 65 || 99 || 40<br />
|-<br />
| 7 || 244 || 81 || 103 || 60<br />
|-<br />
| 8 || 39 || 39 || 0 || 0<br />
|-<br />
| 9 || 43 || 43 || 0 || 0<br />
|-<br />
| 10 || 38 || 38 || 0 || 0<br />
|-<br />
| 11 || 37 || 37 || 0 || 0<br />
|}<br />
<br />
For the specifics of the bit stream layout (i.e. for the bits to parameter mappings) see [http://www.3gpp.org/ftp/Specs/html-info/26101.htm 26.101 AMR speech codec frame structure] and [http://www.3gpp.org/ftp/Specs/html-info/26090.htm 26.090 AMR speech codec transcoding functions].<br />
<br />
== Decoding of LP filter parameters ==<br />
<br />
The received indices of LSP quantization are used to reconstruct the quantified<br />
LSP vectors. (c.f. §5.2.5)<br />
<br />
<br />
=== 12.2kbps mode summary ===<br />
<br />
* indices into code books are parsed from the bit stream<br />
* indices give elements of split matrix quantised (SMQ) residual LSF vectors from the relevant code books<br />
* prediction from the previous frame is added to obtain the mean-removed LSF vectors<br />
* the mean is added<br />
* the LSF vectors are converted to cosine domain LSP vectors<br />
<br />
==== Decoding SMQ residual LSF vectors ====<br />
<br />
The elements of the SMQ vectors are stored at an index into a code book that<br />
varies according to the mode. There are 5 code books for the 12.2kbps mode<br />
corresponding to the 5 indices. These tables will be referred to as:<br />
<br />
<code>lsf_m_n</code><br />
<br />
; m : the number of indices parsed according to the mode<br />
; n : the index 'position' i.e. 1 for the first index, etc<br />
<br />
The 5 indices are stored using 7, 8, 8 + sign bit, 8, 6 bits respectively.<br />
The four elements of a 'split quantized sub-matrix' are stored at the index<br />
position in the appropriate code book are reassigned as follows:<br />
<br />
; lsf_5_1[index1] : r1_1, r1_2, r2_1, r2_2<br />
; lsf_5_2[index2] : r1_3, r1_4, r2_3, r2_4<br />
; lsf_5_3[index3] : r1_5, r1_6, r2_5, r2_6<br />
; lsf_5_4[index4] : r1_7, r1_8, r2_7, r2_8<br />
; lsf_5_5[index5] : r1_9, r1_10, r2_9, r2_10<br />
<br />
With rj_i :<br />
; j : the first or second residual lsf vector<br />
; i : the coefficient of a residual lsf vector ( i = 1, ..., 10 )<br />
<br />
; rj_i : residual line spectral frequencies (LSFs) in Hz<br />
<br />
==== Mean-removed LSF vector prediction ====<br />
<br />
[[Image: Amrnb_lsfmeanrem.gif]]<br />
<br />
; z_j(n) : the mean-removed LSF vector at the jth subframe<br />
; r_j(n) : prediction residual vector of frame n at the jth subframe<br />
; ^r_2(n-1) : the quantified residual vector from the previous frame at the 2nd subframe<br />
<br />
==== The mean is added ====<br />
<br />
[[Image: Amrnb_lsfvec.gif]]<br />
<br />
; lsf_mean_m : a table of the means of the LSF coefficients<br />
; m : the number of indices parsed according to the mode<br />
; fj : the LSF vectors<br />
<br />
==== LSF to LSP vector conversion ====<br />
<br />
[[Image: Amrnb_lspvec.gif]]<br />
<br />
; q_k[i] : the ith coefficient of the kth line spectral pair (LSP) in the cosine domain<br />
; k : the two lsf vectors give the LSP vectors q2, q4 at the 2nd and 4th subframes; k = 2*j<br />
; f_j[i] : ith coefficient of the jth LSF vector; [0,4000] Hz<br />
; f_s : sampling frequency in Hz (8kHz)<br />
<br />
=== Other active modes summary ===<br />
<br />
The process for the other modes is similar to that for the 12.2kbps mode.<br />
<br />
* indices into code books are parsed from the bit stream<br />
* indices give elements of a split matrix quantised (SMQ) residual LSF vector from the relevant code books<br />
* prediction from the previous frame is added to obtain the mean-removed LSF vector<br />
* the mean is added<br />
* the LSF vector is converted to a cosine domain LSP vector<br />
<br />
==== Decoding the SMQ residual LSF vector ====<br />
<br />
The 3 indices are stored with the following numbers of bits:<br />
<br />
{| border=1<br />
! Mode (kbps) !! 1st index (bits) !! 2nd index (bits) !! 3rd index (bits)<br />
|-<br />
| 10.2 || 8 || 9 || 9<br />
|-<br />
| 7.95 || 9 || 9 || 9<br />
|-<br />
| 7.40 || 8 || 9 || 9<br />
|-<br />
| 6.70 || 8 || 9 || 9<br />
|-<br />
| 5.90 || 8 || 9 || 9<br />
|-<br />
| 5.15 || 8 || 8 || 7<br />
|-<br />
| 4.75 || 8 || 8 || 7<br />
|}<br />
<br />
The four elements of a 'split quantized sub-matrix' are stored at the index<br />
position in the appropriate code book are:<br />
<br />
; 1st index in 1st code book : r_1, r_2, r_3<br />
; 2nd index in 2nd code book : r_4, r_5, r_6<br />
; 3rd index in 3rd code book : r_7, r_8, r_9, r_10<br />
<br />
; r_i : residual LSF vector (Hz)<br />
; i : the coefficient of vector ( i = 1, ..., 10 )<br />
<br />
==== Mean-removed LSF vector prediction ====<br />
<br />
[[Image: Amrnb_lsfmeanrem2.gif]]<br />
<br />
; z_j(n)[i] : the ith coefficient of the mean-removed LSF vector at the jth subframe<br />
; r_j(n)[i] : the ith coefficient of the prediction residual vector of frame n at the jth subframe<br />
; pred_fac[i] : the ith coefficient of the prediction factor<br />
; ^r_j(n-1)[i] : the ith coefficient of the the quantified residual vector of the previous frame at the jth subframe<br />
<br />
These processes give the LSP vector at the 4th subframe (q4)<br />
<br />
=== LSP vector interpolation (c.f. §5.2.6) ===<br />
<br />
==== 12.2 kbps mode ====<br />
<br />
[[Image: Amrnb_lspinterpq1-1.gif]]<br />
<br />
[[Image: Amrnb_lspinterpq3-1.gif]]<br />
<br />
==== Other modes ====<br />
<br />
[[Image: Amrnb_lspinterpq1-2.gif]]<br />
<br />
[[Image: Amrnb_lspinterpq2.gif]]<br />
<br />
[[Image: Amrnb_lspinterpq3-2.gif]]<br />
<br />
=== LSP vector to LP filter coefficient conversion (c.f. §5.2.4) ===<br />
<br />
for i=1..5<br />
f1[i] = 2*f1[i-2] - 2*q[2i-1]*f1[i-1]<br />
for j=i-1..1<br />
f1[j] += f1[j-2] - 2*q[2i-1]*f1[j-1]<br />
end<br />
end<br />
<br />
f1[-1] = 0; f1[0] = 0;<br />
<br />
Same for f2[i] with q[2i] instead of q[2i-1]<br />
<br />
for i=1..5<br />
f'1[i] = f1[i] + f1[i-1]<br />
f'2[i] = f2[i] - f2[i-1]<br />
end<br />
<br />
[[Image: Amrnb_lpccoeff.gif]]<br />
<br />
; a_i : the LP filter coefficients<br />
<br />
== Decoding of the pitch (adaptive codebook) vector ==<br />
<br />
* indices parsed from bitstream<br />
* indices give integer and fractional parts of the pitch lag<br />
* pitch vector v(n) is found by interpolating the past excitation u(n) at the pitch lag using an FIR filter. (c.f. §5.6)<br />
<br />
<br />
=== Decode pitch lag ===<br />
<br />
'''Note: division in this section is integer division!'''<br />
<br />
==== 12.2kbps mode - 1/6 resolution pitch lag ====<br />
<br />
===== First and third subframes =====<br />
<br />
In the first and third subframes, a fractional pitch lag is used with resolutions:<br />
<br />
* 1/6 in the range [17 3/6, 94 3/6]<br />
* 1 in the range [95, 143]<br />
<br />
...encoded using 9 bits.<br />
<br />
For [17 3/6, 94 3/6] the pitch index is encoded as:<br />
<br />
pitch_index = (pitch_lag_int - 17)*6 + pitch_lag_frac - 3;<br />
<br />
; pitch_lag_int : integer part of the pitch lag in the range [17, 94]<br />
; pitch_lag_frac : fractional part of the pitch lag in 1/6 units in the range [-2, 3]<br />
<br />
so...<br />
<br />
if(pitch_index < (94 4/6 - 17 3/6)*6)<br />
// fractional part is encoded in range [17 3/6, 94 3/6]<br />
pitch_lag_int = (pitch_index + 5)/6 + 17;<br />
pitch_lag_frac = pitch_index - pitch_lag_int*6 + (17 3/6)*6;<br />
<br />
And for [95, 143] the pitch index is encoded as:<br />
<br />
pitch_index = (pitch_lag_int - 95) + (94 4/6 - 17 3/6)*6;<br />
<br />
; pitch_lag_int : integer pitch lag in the range [95, 143]<br />
<br />
so...<br />
<br />
else<br />
// only integer part encoded in range [95, 143], no fractional part<br />
pitch_lag_int = pitch_index - (94 4/6 - 17 3/6)*6 + 95;<br />
pitch_lag_frac = 0;<br />
<br />
===== Second and fourth subframes =====<br />
<br />
In the second and fourth subframes, a pitch lag resolution of 1/6 is always used in the range [T1 - 5 3/6, T1 + 4 3/6], where T1 is nearest integer to the fractional pitch lag of the previous (1st or 3rd) subframe. The search range is bounded by [18, 143]. In this case the pitch delay is encoded using 6 bits and is therefore in the range [0,63].<br />
<br />
So the search range for the pitch lag is:<br />
<br />
search_range_min = max(pitch_lag_int_prev - 5, 18);<br />
search_range_max = search_range_min + 9;<br />
if(search_range_max > 143) {<br />
search_range_max = 143;<br />
search_range_min = search_range_max - 9;<br />
}<br />
<br />
; pitch_lag_int_prev : the integer part of the pitch lag from the previous sub frame<br />
<br />
The pitch index is encoded as:<br />
<br />
pitch_index = (pitch_lag_int - (search_range_min - 1))*6 + pitch_lag_frac - 3;<br />
<br />
; pitch_lag_int : the integer part of the pitch lag in the range [search_range_min - 1, search_range_max]<br />
; pitch_lag_frac : the fractional part of the pitch lag in the range [-2, 3]<br />
<br />
The formula for the pitch_index has been chosen to map pitch_lag_int [search_range_min - 1, search_range_max] and pitch_lag_frac [-2, 3] to [0,60]. (pitch_index = [0, 10]*6 + [-2, 3] - 3 = [0, 6, ..., 60] + [-5, 0] = [0,60])<br />
<br />
So the pitch lag is calculated through:<br />
<br />
// integer part of pitch lag = position in range [search_range_min - 1, search_range_max] + lower bound of range<br />
pitch_lag_int = (pitch_index + 5)/6 + search_range_min - 1;<br />
// fractional part of pitch lag = pitch index - (integer part without offset)*6 - 1 3/6 offset to bring the values to the correct range<br />
pitch_lag_frac = pitch_index - ((pitch_index + 5)/6)*6 - 9;<br />
<br />
Note that when using integers and integer division to conduct (pitch_index + 5)/6 the result is similar to taking the ceiling of pitch_index/6.0.<br />
<br />
==== Others modes - 1/3 resolution pitch lag ====<br />
<br />
===== First and third subframes =====<br />
<br />
In the first and third subframes, a fractional pitch lag is used with resolutions:<br />
<br />
* 1/3 in the range [19 1/3, 84 2/3]<br />
* 1 in the range [85, 143]<br />
<br />
...encoded using 8 bits.<br />
<br />
For [19 1/3, 84 2/3] the pitch lag is encoded as:<br />
<br />
pitch_index = pitch_lag_int*3 + pitch_lag_frac - (19 1/3)*3;<br />
<br />
; pitch_lag_int : integer part of the pitch lag in the range [19, 84]<br />
; pitch_lag_frac : fractional part of the pitch lag in 1/3 units in the range [0, 2]<br />
<br />
so...<br />
<br />
if(pitch_index < (85 - 19 1/3)*3)<br />
// fractional part is encoded in range [19 1/3, 84 2/3]<br />
pitch_lag_int = (pitch_index + 2)/3 + 19;<br />
pitch_lag_frac = pitch_index - pitch_lag_int*3 + (19 1/3)*3;<br />
<br />
And for [85, 143] the pitch index is encoded as:<br />
<br />
pitch_index = pitch_lag_int - 85 + (85 - 19 1/3)*3;<br />
<br />
; pitch_lag_int : integer pitch lag in the range [85, 143]<br />
<br />
so...<br />
<br />
else<br />
// only integer part encoded in range [85, 143], no fractional part<br />
pitch_lag_int = pitch_index - (85 - 19 1/3)*3 + 85;<br />
pitch_lag_frac = 0;<br />
<br />
===== Second and fourth subframes =====<br />
<br />
In the second and fourth subframes, the pitch lag resolution varies depending on the mode as follows:<br />
<br />
* 7.95 kbps mode<br />
** resolution of 1/3 is always used in the range [T1 - 10 2/3, T1 + 9 2/3]<br />
** encoded using 6 bits => pitch_index is in the range [0, 63]<br />
* 10.2 and 7.40 kbps modes<br />
** resolution of 1/3 is always used in the range [T1 - 5 2/3, T1 + 4 2/3]<br />
** encoded using 5 bits => pitch_index is in the range [0, 31]<br />
* 6.70, 5.90, 5.15 and 4.75 kbps modes<br />
** resolution of 1 is used in the range [T1 - 5, T1 + 4]<br />
** resolution of 1/3 is always used in the range [T1 - 1 2/3, T1 + 2/3]<br />
** encoded using 4 bits => pitch_index is in the range [0, 15]<br />
<br />
Where T1 is nearest integer to the fractional pitch lag of the previous (1st or 3rd) subframe. The search range is bounded by [20, 143].<br />
<br />
So the search range for the pitch lag is:<br />
<br />
lower_bound = 5;<br />
range = 9;<br />
if(mode == 7.95) {<br />
lower_bound = 10;<br />
range = 19;<br />
}<br />
search_range_min = max(pitch_lag_int_prev - lower_bound, 20);<br />
search_range_max = search_range_min + range;<br />
if(search_range_max > 143) {<br />
search_range_max = 143;<br />
search_range_min = search_range_max - range;<br />
}<br />
<br />
; pitch_lag_int_prev : the integer part of the pitch lag from the previous sub frame<br />
<br />
<br />
For modes 7.40, 7.95 and 10.2 the pitch index is encoded as:<br />
<br />
pitch_index = (pitch_lag_int - search_range_min)*3 + pitch_lag_frac + 2;<br />
<br />
; pitch_lag_int : the integer part of the pitch lag in the range [search_range_min, search_range_max]<br />
; pitch_lag_frac : the fractional part of the pitch lag in the range [-1, 1]<br />
<br />
So the pitch lag is calculated through:<br />
<br />
// integer part of pitch lag = position of pitch lag in range [search_range_min, search_range_max] + lower bound of the range<br />
pitch_lag_int = (pitch_index + 2)/3 - 1 + search_range_min;<br />
// fractional part of pitch lag = pitch index - (integer part without offset)*3 - 2/3 to bring the values to the correct range<br />
pitch_lag_frac = pitch_index - ((pitch_index + 2)/3 - 1)*3 - 2;<br />
<br />
<br />
For modes 4.75, 5.15, 5.90 and 6.70:<br />
<br />
t1_temp = max( min(pitch_lag_int_prev, search_range_min + 5), search_range_max - 4 );<br />
<br />
; t1_temp : predicted pitch lag from the previous frame adjusted to fit into the 0 position of the search range<br />
<br />
The pitch index is encoded as:<br />
<br />
// if pitch lag is below T1 - 1 2/3<br />
if( pitch_lag_int*3 + pitch_lag_frac <= (t1_temp - 2)*3 ) {<br />
// encode with resolution 1<br />
index = (pitch_lag_int - t1_temp) + 5;<br />
// else if pitch lag is below T1 + 1<br />
}else if( pitch_lag_int*3 + pitch_lag_frac < (t1_temp + 1)*3 ) {<br />
// encode with resolution 1/3<br />
index = ( pitch_lag_int*3 + pitch_lag_frac - (t1_temp - 2)*3 ) + 3;<br />
// else pitch lag is above T1 + 2/3<br />
}else {<br />
// encode with resolution 1<br />
index = (pitch_lag_int - t1_temp) + 11;<br />
}<br />
<br />
; pitch_lag_int : the integer part of the pitch lag in the range [search_range_min, search_range_max]<br />
; pitch_lag_frac : the fractional part of the pitch lag in the range [-1, 1]<br />
<br />
The possible pitch indices and values are:<br />
<br />
{| border=1<br />
|-<br />
| 0 || 1 || 2 || 3 || 4 || 5 || 6 || 7 || 8 || 9 || 10 || 11 || 12 || 13 || 14 || 15<br />
|-<br />
| -5 || -4 || -3 || -2 || -1 2/3 || -1 1/3 || -1 || -2/3 || -1/3 || 0 || 1/3 || 2/3 || 1 || 2 || 3 || 4<br />
|-<br />
|}<br />
<br />
So the pitch lag is calculated through:<br />
<br />
if(pitch_index < 4) {<br />
// integer part of pitch lag = pitch lag position in range [t1_temp - 5, t1_temp - 2] + lower bound of range<br />
pitch_lag_int = pitch_index + (t1_temp - 5);<br />
// this range is coded with resolution 1 so no fractional part<br />
pitch_lag_frac = 0;<br />
}else if(pitch_index < 12) {<br />
pitch_lag_int = (pitch_index - 2)/3 + (t1_temp - 2);<br />
pitch_lag_frac = (pitch_index - 4) - ((pitch_index - 2)/3)*3 - 11;<br />
}else {<br />
// integer part of pitch lag = pitch lag position in range [t1_temp + 1, t1_temp + 4] + lower bound of range<br />
pitch_lag_int = pitch_index - 12 + t1_temp + 1;<br />
// this range is coded with resolution 1 so no fractional part<br />
pitch_lag_frac = 0;<br />
}<br />
<br />
<br />
=== Calculate pitch vector ===<br />
<br />
[[Image: Amrnb_pitchvector.gif]]<br />
<br />
; k : integer pitch lag<br />
; n : sample position in the vectors 0, ..., 39<br />
; t : 0, ..., 5 corresponding to fractions 0, 1/6, 2/6, 3/6, -2/6, -1/6 respectively<br />
<br />
This equation can be used for both 1/3 and 1/6 resolution simply by multiplying t by 2 in the 1/3 case.<br />
<br />
(Note: the coefficients b60 are in the reference source in an array called inter6)<br />
<br />
<br />
<br />
== Decoding of the fixed (innovative or algebraic) vector ==<br />
<br />
* the excitation pulse positions and signs are parsed from the bit stream<br />
* the pulse positions and signs are encoded differently depending on the mode<br />
* the fixed code book vector, c(n), is then constructed from the pulse positions and signs<br />
* if pitch_lag_int is less than the subframe size (40), the pitch sharpening procedure is applied<br />
<br />
<br />
=== Decoding the pulse positions ===<br />
<br />
==== 12.2 kbps mode ====<br />
<br />
* 10 pulse positions each coded using 3 bit Gray codes<br />
* signs coded using 1 bit each for 5 pulse pairs<br />
<br />
<br />
{| border=1<br />
! Pulse !! Positions<br />
|-<br />
| i0,i5 || 0, 5, 10, 15, 20, 25, 30, 35<br />
|-<br />
| i1,i6 || 1, 6, 11, 16, 21, 26, 31, 36<br />
|-<br />
| i2,i7 || 2, 7, 12, 17, 22, 27, 32, 37<br />
|-<br />
| i3,i8 || 3, 8, 13, 18, 23, 28, 33, 38<br />
|-<br />
| i4,i9 || 4, 9, 14, 19, 24, 29, 34, 39<br />
|-<br />
|}<br />
<br />
<br />
==== 10.2 kbps mode ====<br />
<br />
* 8 pulse positions, 4 pairs, coded as 3 values using 10, 10 and 7 bits<br />
* signs coded using 1 bit each for 4 pulse pairs<br />
<br />
<br />
{| border=1<br />
! Pulse !! Positions<br />
|-<br />
| i0,i4 || 0, 4, 8, 12, 16, 20, 24, 28, 32, 36<br />
|-<br />
| i1,i5 || 1, 5, 9, 13, 17, 21, 25, 29, 33, 37<br />
|-<br />
| i2,i6 || 2, 6, 10, 14, 18, 22, 26, 30, 34, 38<br />
|-<br />
| i3,i7 || 3, 7, 11, 15, 19, 23, 27, 31, 35, 39<br />
|-<br />
|}<br />
<br />
<br />
==== 7.95 and 7.40 kbps modes ====<br />
<br />
* 4 pulse positions Gray coded using 3, 3, 3 and 4 bits<br />
* signs coded using 1 bit for each pulse<br />
<br />
<br />
{| border=1<br />
! Pulse !! Positions<br />
|-<br />
| i0 || 0, 5, 10, 15, 20, 25, 30, 35<br />
|-<br />
| i1 || 1, 6, 11, 16, 21, 26, 31, 36<br />
|-<br />
| i2 || 2, 7, 12, 17, 22, 27, 32, 37<br />
|-<br />
| i3 || 3, 8, 13, 18, 23, 28, 33, 38<br />
4, 9, 14, 19, 24, 29, 34, 39<br />
|-<br />
|}<br />
<br />
==== 6.70 kbps mode ====<br />
<br />
* 3 pulse positions coded using 3, 4 and 4 bits<br />
* signs coded using 1 bit for each pulse<br />
<br />
<br />
{| border=1<br />
! Pulse !! Positions<br />
|-<br />
| i0 || 0, 5, 10, 15, 20, 25, 30, 35<br />
|-<br />
| i1 || 1, 6, 11, 16, 21, 26, 31, 36<br />
3, 8, 13, 18, 23, 28, 33, 38<br />
|-<br />
| i2 || 2, 7, 12, 17, 22, 27, 32, 37<br />
4, 9, 14, 19, 24, 29, 34, 39<br />
|-<br />
|}<br />
<br />
<br />
==== 5.90 kbps mode ====<br />
<br />
* 2 pulse positions coded using 4 and 5 bits<br />
* signs coded using 1 bit for each pulse<br />
<br />
<br />
{| border=1<br />
! Pulse !! Positions<br />
|-<br />
| i0 || 1, 6, 11, 16, 21, 26, 31, 36<br />
3, 8, 13, 18, 23, 28, 33, 38<br />
|-<br />
| i1 || 0, 5, 10, 15, 20, 25, 30, 35<br />
1, 6, 11, 16, 21, 26, 31, 36<br><br />
2, 7, 12, 17, 22, 27, 32, 37<br><br />
4, 9, 14, 19, 24, 29, 34, 39<br />
|-<br />
|}<br />
<br />
==== 5.15 and 4.75 kbps modes ====<br />
<br />
* 2 pulse positions coded using 1 bit for the position subset and 3 bits per pulse<br />
* signs coded using 1 bit for each pulse<br />
<br />
<br />
{| border=1<br />
! Subframe !! Subset !! Pulse !! Positions<br />
|-<br />
| rowspan="4" align="center" | 1<br />
| rowspan="2" align="center" | 1<br />
| i0 || 0, 5, 10, 15, 20, 25, 30, 35<br />
|-<br />
| i1 || 2, 7, 12, 17, 22, 27, 32, 37<br />
|-<br />
| rowspan="2" align="center" | 2<br />
| i0 || 1, 6, 11, 16, 21, 26, 31, 36<br />
|-<br />
| i1 || 3, 8, 13, 18, 23, 28, 33, 38<br />
|-<br />
| rowspan="4" align="center" | 2<br />
| rowspan="2" align="center" | 1<br />
| i0 || 0, 5, 10, 15, 20, 25, 30, 35<br />
|-<br />
| i1 || 3, 8, 13, 18, 23, 28, 33, 38<br />
|-<br />
| rowspan="2" align="center" | 2<br />
| i0 || 2, 7, 12, 17, 22, 27, 32, 37<br />
|-<br />
| i1 || 4, 9, 14, 19, 24, 29, 34, 39<br />
|-<br />
| rowspan="4" align="center" | 3<br />
| rowspan="2" align="center" | 1<br />
| i0 || 0, 5, 10, 15, 20, 25, 30, 35<br />
|-<br />
| i1 || 2, 7, 12, 17, 22, 27, 32, 37<br />
|-<br />
| rowspan="2" align="center" | 2<br />
| i0 || 1, 6, 11, 16, 21, 26, 31, 36<br />
|-<br />
| i1 || 4, 9, 14, 19, 24, 29, 34, 39<br />
|-<br />
| rowspan="4" align="center" | 4<br />
| rowspan="2" align="center" | 1<br />
| i0 || 0, 5, 10, 15, 20, 25, 30, 35<br />
|-<br />
| i1 || 3, 8, 13, 18, 23, 28, 33, 38<br />
|-<br />
| rowspan="2" align="center" | 2<br />
| i0 || 1, 6, 11, 16, 21, 26, 31, 36<br />
|-<br />
| i1 || 4, 9, 14, 19, 24, 29, 34, 39<br />
|-<br />
|}<br />
<br />
<br />
=== Fixed codebook vector construction ===<br />
<br />
All c(n) are zero if there is no pulse at position n. If there is a pulse at position n then it has the corresponding sign as parsed above.<br />
<br />
<br />
=== Pitch sharpening ===<br />
<br />
[[Image: Amrnb fixvecpitshp.gif]]<br />
<br />
; β : the decoded pitch gain, ^g_p, bounded by [0.0,1.0] for 12.2. kbps or [0.0,0.8] for other modes<br />
<br />
Note: Only sharpen n in [pitch_lag_int-1, 39]. The reference source considers previous fixed vector elements (n<0) to be 0. I think this is also the reason pitch sharpening is not conducted for pitch_lag_int > 40 as all the sharpening contributions would be 0.<br />
<br />
== Decoding of the pitch and fixed codebook gains ==<br />
<br />
<br />
=== Fixed gain prediction ===<br />
<br />
A moving average prediction of the innovation (fixed) energy is conducted.<br />
<br />
<br />
[[Image: Amrnb_fixedgainpred.gif]]<br />
<br />
; g_c' : fixed gain prediction<br />
; \tilde{E} : predicted energy [dB]<br />
; \bar{E} : desired mean innovation (fixed) energy [dB]<br />
; E_I : calculated mean innovation (fixed) energy [dB]<br />
<br />
<br />
[[Image: Amrnb_predener.gif]]<br />
<br />
; b : 4-tap MA prediction coefficients [0.68, 0.58, 0.34, 0.19]<br />
; ^R(k) : quantified prediction errors at subframe k 20*log10(^γ_gc(k))<br />
<br />
<br />
Desired mean innovation (fixed) energy:<br />
<br />
{| border=1<br />
! Mode (kbps) !! Mean energy (dB)<br />
|-<br />
| 12.2 || 36<br />
|-<br />
| 10.2 || 33<br />
|-<br />
| 7.95 || 36<br />
|-<br />
| 7.40 || 30<br />
|-<br />
| 6.70 || 28.75<br />
|-<br />
| 5.90 || 33<br />
|-<br />
| 5.15 || 33<br />
|-<br />
| 4.75 || 33<br />
|-<br />
|}<br />
<br />
<br />
[[Image: Amrnb_meaniner.gif]]<br />
<br />
; E_I : calculated mean innovation (fixed) energy [dB]<br />
; N : subframe size 40<br />
; c(n) : fixed codebook vector<br />
<br />
=== Dequantisation of the gains ===<br />
<br />
==== 12.2kbps and 7.95kbps - scalar quantised gains ====<br />
<br />
The received indices are used to find the quantified pitch gain, ^g_p, and the quantified fixed gain correction factor, ^γ_gc.<br />
<br />
===== Pitch gain =====<br />
<br />
The parsed gain index is used to obtain the quantified pitch gain, ^g_p, from the corresponding codebook. (qua_gain_pitch in the reference source.)<br />
<br />
===== Fixed gain correction factor =====<br />
<br />
The parsed gain index is used to obtain the quantified fixed gain, ^g_c, from the corresponding codebook. (qua_gain_code in the reference source.) The table stores ^γ_gc, and the quantised energy error in two forms (these are needed for the moving average calculation of the predicted fixed gain):<br />
<br />
qua_ener_MR122 = log2(^γ_gc)<br />
qua_ener = 20*log10(^γ_gc)<br />
<br />
^γ_gc is stored at Q11 (i.e. it's multiplied by 2^11.) qua_ener_MR122 and qua_ener are stored at Q10.<br />
<br />
==== Other modes - vector quantised gains ====<br />
<br />
The received index gives both the quantified adaptive codebook gain, ^g_p, and the quantified algebraic codebook gain correction factor, ^γ_gc.<br />
<br />
The tables contains the following data:<br />
<br />
^g_p (Q14),<br />
^γ_gc (Q12), (^g_c = g_c'*^γ_gc),<br />
qua_ener_MR122 (Q10), (log2(^γ_gc))<br />
qua_ener (Q10) (20*log10(^γ_gc))<br />
<br />
The log2() and log10() values are calculated on the fixed point value<br />
(g_fac Q12) and not on the original floating point value of g_fac<br />
to make the quantizer/MA predictdor use corresponding values.<br />
<br />
The codebook used depends on the mode:<br />
<br />
6.70, 7.40, 10.2 kbps modes - table_gain_highrates in the reference source<br />
Four consecutive entries give ^g_p, ^γ_gc, qua_ener_MR122 and qua_ener. Apparently the values for qua_ener are the original ones from IS641 to ensure bit-exactness but are not exactly the rounded value of 20log10(^γ_gc).<br />
<br />
5.15, 5.90 kbps modes - table_gain_lowrates<br />
Similar to table_gain_highrates, four consecutive entries give ^g_p, ^γ_gc, qua_ener_MR122 and qua_ener. There are no special notes for this table.<br />
<br />
4.75 kbps mode - table_gain_MR475<br />
Unlike the above mentioned tables, four consecutive values give ^g_p, ^γ_gc for subframes 0,2, and then ^g_p, ^γ_gc for subframes 1,3.<br />
<br />
At the very least I think these tables could be redesigned a little.<br />
<br />
=== Calculation of the quantified fixed gain ===<br />
<br />
[[Image: Amrnb_fixedgain.gif]]<br />
<br />
== Smoothing of the fixed codebook gain ==<br />
<br />
<br />
'''10.2, 6.70, 5.90, 5.15, 4.75 kbit/s modes only'''<br />
<br />
<br />
=== Calculate averaged LSP vector ===<br />
<br />
[[Image: Amrnb_lspsmooth.gif]]<br />
<br />
; \bar{q}(n) : averaged LSP vector at frame n<br />
; ^q_4(n) : quantified LSP vector for the 4th subframe at frame n<br />
<br />
<br />
=== Calculate fixed gain smoothing factor ===<br />
<br />
[[Image: Amrnb_lspdiff.gif]]<br />
<br />
; diff_m : difference measure at subframe m<br />
; j : loops over LSPs<br />
; m : loops over subframes<br />
; ^q_m : quantified LSP vector at subframe m<br />
<br />
Note: I think the sum over the subframes is an error in the specification and they actually mean to use the quantified LSP vector from subframe m for the calculation of diff for subframe m. I think this is what they do in the reference source and the sum over m is an error.<br />
<br />
<br />
[[Image: Amrnb_smoothfac.gif]]<br />
<br />
; k_m : fixed gain smoothing factor<br />
; K_1 : 0.4<br />
; K_2 : 0.25<br />
; diff_m : difference measure at subframe m<br />
<br />
If diff_m has been greater than 0.65 for 10 subframes, k_m is set to 1.0 (i.e. no smoothing) for 40 subframes.<br />
<br />
=== Calculate mean fixed gain ===<br />
<br />
[[Image: Amrnb_meangc.gif]]<br />
<br />
; \bar{g_c}(m) : mean fixed gain at subframe m<br />
; ^g_c(k) : quantified fixed gain at subframe k<br />
<br />
=== Calculate smoothed fixed gain ===<br />
<br />
[[Image: Amrnb_gcsmooth.gif]]<br />
<br />
; ^g_c : quantified fixed gain<br />
; \bar{g_c} : averaged fixed gain<br />
<br />
== Anti-sparseness processing ==<br />
<br />
<br />
'''7.95, 6.70, 5.90, 5.15, 4.75 kbit/s modes only'''<br />
<br />
<br />
=== Evaluate impulse response filter strength ===<br />
<br />
The fixed vector, c(n), has only a few pulses per subframe. In certain conditions, to reduce perceptual artifacts arising from this, the vector is circularly convolved with a predefined impulse response. The selection of the strength of filter is made based on the decoded gains.<br />
<br />
if ^g_p < 0.6<br />
impNr = 0<br />
else if ^g_p < 0.9<br />
impNr = 1<br />
else<br />
impNr = 2<br />
<br />
; impNr = 0 : strong impulse response filter<br />
; impNr = 1 : medium impulse response filter<br />
; impNr = 2 : no filtering<br />
<br />
if ^g_c(k) > 2 * ^g_c(k-1)<br />
impNr = min( impNr + 1, 2 )<br />
else if impNr = 0 AND median of last five ^g_p >= 0.6<br />
impNr = min( impNr(k), min(impNr(k-1) + 1, 2) )<br />
<br />
<br />
=== Circular convolution of fixed vector and impulse response filter ===<br />
<br />
[[Image: Amrnb_circconv.gif]]<br />
<br />
; (c * h) : convolution of vectors c and h, in this case circular convolution<br />
; h[n] : nth coefficient of the impulse response used for filtering<br />
; c[n] : nth coefficient of the fixed vector<br />
<br />
n = 0, ..., 39<br />
<br />
To make the convolution circular, make the impulse response circular by taking h[-m] = h[39-m]<br />
<br />
== Computing the reconstructed speech ==<br />
<br />
<br />
=== Construct excitation ===<br />
<br />
[[Image: Amrnb_excitation.gif]]<br />
<br />
; u(n) : excitation vector<br />
; ^g_p : quantified pitch gain<br />
; v(n) : pitch vector<br />
; ^g_c : quantified fixed gain<br />
; c(n) : fixed vector<br />
<br />
<br />
=== Emphasise pitch vector contribution ===<br />
<br />
This is apparently a post-processing technique.<br />
<br />
[[Image: Amrnb_pitchvecemph.gif]]<br />
<br />
; ^u(n) : excitation vector with emphasised pitch vector contribution<br />
; β : ^g_p bounded by [0.0, 0.8] or [0.0, 1.0] depending on mode<br />
<br />
<br />
=== Apply adaptive gain control (AGC) through gain scaling ===<br />
<br />
[[Image: Amrnb_gainfac.gif]]<br />
<br />
; η : gain scaling factor for emphasised excitation<br />
<br />
[[Image: Amrnb_gainscalexc.gif]]<br />
<br />
; ^u'(n) : gain-scaled emphasised excitation<br />
<br />
<br />
=== Calculate reconstructed speech samples ===<br />
<br />
[[Image: Amrnb_synthfilter.gif]]<br />
<br />
; ^s(n) : reconstructed speech samples<br />
; ^a_i : LP filter coefficients<br />
<br />
Note: for n-i < 0, ^s(n-i) should be take from previous speech samples if existing, else we will consider them 0 as this behaviour is undefined the specification.<br />
<br />
== Additional instability protection ==<br />
<br />
If an overflow occurs during synthesis, the pitch vector, v(n), is scaled down by a factor of 4 and synthesis is conducted again bypassing emphasising the pitch vector contribution and adaptive gain control.<br />
<br />
Q: What classifies an overflow?<br />
A: s(n) < -32768 or s(n) > 32767 (i.e. 16-bit signed int)<br />
<br />
== Post-processing ==<br />
<br />
=== Adaptive post-filtering ===<br />
<br />
(c.f. §6.2.1)<br />
<br />
==== IIR filtering ====<br />
<br />
The speech samples, ^s(n), are filtered through a formant filter and a tilt compensation filter.<br />
<br />
[[Image: Amrnb_pfformant.gif]]<br />
<br />
; γ_n, γ_d : control the amount of formant post-filtering<br />
<br />
The speech samples, ^s(n), are filtered through ^A(z/γ_n) to produce the residual signal ^r(n).<br />
<br />
[[Image: Amrnb_residual.gif]]<br />
<br />
^r(n) is filtered through 1/^A(z/γ_d).<br />
<br />
[[Image: Amrnb_fmntfilt.gif]]<br />
<br />
The output is filtered through H_t(z) (the tilt compensation filter) resulting in the post-filtered speech signal, ^s_f(n).<br />
<br />
[[Image: Amrnb_pftilt.gif]]<br />
<br />
[[Image: Amrnb_mu.gif]]<br />
<br />
[[Image: Amrnb_reffac.gif]]<br />
<br />
[[Image: Amrnb_r_h.gif]]<br />
<br />
; L_h : 22 - the truncation of the impulse response<br />
; h_f : impulse response of the formant filter H_f<br />
<br />
So, in summation notation:<br />
<br />
[[Image: Amrnb_pftiltsum.gif]]<br />
<br />
===== 12.2 and 10.2 kbps modes =====<br />
<br />
γ_n = 0.7<br />
<br />
γ_d = 0.75<br />
<br />
γ_t = 0.8 if k_1' > 0; 0 otherwise<br />
<br />
===== Other modes =====<br />
<br />
γ_n = 0.55<br />
<br />
γ_d = 0.7<br />
<br />
γ_t = 0.8<br />
<br />
==== Adaptive gain control ====<br />
<br />
Adaptive gain control is used to compensate for the gain difference between the filtered and synthesised speech signals.<br />
<br />
[[Image: Amrnb_gainscalfac.gif]]<br />
<br />
[[Image: Amrnb_gainscalfacwtd.gif]]<br />
<br />
[[Image: Amrnb_gainscalspch.gif]]<br />
<br />
; \alpha : adaptive gain control factor equal to 0.9<br />
<br />
=== High-pass filtering and upscaling ===<br />
<br />
(c.f. §6.2.2)<br />
<br />
[[Image: Amrnb_highpass.gif]]<br />
<br />
After completing all filtering, the samples are scaled up by a factor of 2.<br />
<br />
<br />
<br />
<br />
[[Category:Vocoders]]<br />
[[Category:Audio Codecs]]</div>Adam Quantrillhttps://wiki.multimedia.cx/index.php?title=AMR-NB&diff=14378AMR-NB2013-03-05T17:52:09Z<p>Adam Quantrill: /* Classes */</p>
<hr />
<div>* samples: http://samples.mplayerhq.hu/A-codecs/amr/<br />
* specification: http://www.3gpp.org/ftp/Specs/html-info/26-series.htm<br />
* Wikipedia article: http://en.wikipedia.org/wiki/Adaptive_Multi-Rate<br />
<br />
AMR-NB (Adaptive Multi-Rate Narrowband) is a [[vocoder]] employed in low-bitrate applications like mobile phones. It is a form of ACELP where A stands for algebraic.<br />
<br />
----<br />
<br />
The following text aims to be a simpler and more explicit document of the AMR narrow band decoding processes to aid in development of a decoder. Reference to sections of the specification will be made in the following format: (c.f. §5.2.5). Happy reading.<br />
<br />
<br />
<br />
== Nomenclature weirdness ==<br />
<br />
Throughout the specification, a number of references are made to the same (or very similar) items with fairly confusing variation. They are listed below to aid understanding of the following text but efforts will be made to consistently use one item name throughout or to use both with the lesser used name in parenthesis.<br />
<br />
* Pitch / Adaptive codebook<br />
* Fixed / Innovative (also algebraic when referring to the codebook)<br />
* Quantified means quantized<br />
<br />
<br />
== Summary ==<br />
<br />
* Mode dependent bitstream parsing<br />
* Indices parsed from bitstream<br />
* Indices decoded to give LSF vectors, fractional pitch lags, innovative code vectors and the pitch and innovative gains<br />
* LSF vectors converted to LP filter coefficients at each subframe<br />
* Subframe decoding<br />
** Excitation vector = adaptive code vector * adaptive (pitch) gain + innovative code vector * innovative gain<br />
** Excitation vector filtered through an LP synthesis filter to reconstruct speech<br />
** Speech signal filtered with adaptive postfilter<br />
<br />
<br />
<br />
== Bit stream frame format ==<br />
Specification ([http://www.3gpp.org/ftp/Specs/html-info/26101.htm 26.101]) describes two possible frame types - interface formats 1 and 2 (often abbreviated IF1 and IF2). IF2 is byte-aligned. The following tables of data are taken from this specification unless otherwise stated.<br />
<br />
=== IF1 format ===<br />
<br />
{| border=1<br />
! bits !! low level meaning || high level meaning<br />
|-<br />
| 4<br />
| Frame type<br />
| rowspan=2 | AMR header<br />
|-<br />
| 1<br />
| Frame quality indicator (0 bad/1 good)<br />
|-<br />
| 3<br />
| Mode indication<br />
| rowspan=3 | AMR auxiliary information<br />
|-<br />
| 3<br />
| Mode request<br />
|-<br />
| 8<br />
| CRC<br />
|-<br />
| &nbsp;<br />
| Class A bits<br />
| rowspan=3 | AMR core frame<br />
|-<br />
| &nbsp;<br />
| Class B bits<br />
|-<br />
| &nbsp;<br />
| Class C bits<br />
|}<br />
<br />
=== IF2 format ===<br />
<br />
{| border=1<br />
! bits !! low level meaning || high level meaning<br />
|-<br />
| 4<br />
| Frame type<br />
| AMR header<br />
|-<br />
| &nbsp;<br />
| Class A bits<br />
| rowspan=3 | AMR core frame<br />
|-<br />
| &nbsp;<br />
| Class B bits<br />
|-<br />
| &nbsp;<br />
| Class C bits<br />
|-<br />
| &nbsp;<br />
| Padding (called "Bit stuffing" in the specification)<br />
| &nbsp;<br />
|}<br />
<br />
=== Field meaning ===<br />
<br />
{| border=1<br />
! Frame type !! Frame content<br />
|-<br />
| 0 || AMR 4.75kbps<br />
|-<br />
| 1 || AMR 5.15kbps<br />
|-<br />
| 2 || AMR 5.90kbps<br />
|-<br />
| 3 || AMR 6.70kbps (PDC-EFR)<br />
|-<br />
| 4 || AMR 7.40kbps (TDMA-EFR)<br />
|-<br />
| 5 || AMR 7.95kbps<br />
|-<br />
| 6 || AMR 10.2kbps<br />
|-<br />
| 7 || AMR 12.2kbps (GSM-EFR)<br />
|-<br />
| 8 || AMR SID<br />
|-<br />
| 9 || GSM-EFR SID<br />
|-<br />
| 10 || TDMA-EFR SID<br />
|-<br />
| 11 || PDC-EFR SID<br />
|-<br />
| 12-14 || Reserved for future use<br />
|-<br />
| 15 || No data (no transmission/no reception)<br />
|}<br />
<br />
=== Classes ===<br />
<br />
{| border=1<br />
! Class !! Importance explanation<br />
|-<br />
| A || Data that is most sensitive to error. Any error in these bits leads to a corrupted speech frame that should not be decoded without appropriate error concealment. This class of bits is protected by an 8-bit CRC.<br />
|-<br />
| B || Less sensitive data that are present in all speech frames.<br />
|-<br />
| C || Least sensitive data present only in higher bit rate frames.<br />
|}<br />
<br />
* Class A is protected by an 8-bit CRC with polynomial x^8+x^6+x^5+x^4+1 computed over the Class A bits.<br />
* However for AMR-NB over GSM the CRC is a 6-bit with polynomial x^6+x^5+x^3+x^2+x+1 computed over the Class A bits, and it immediately follows the Class A bits, rather than preceding them. That is, it's between the Class A and Class B bits. [see 3GPP TS 05.03 sec 3.9.4.3]<br />
* There is no significant step-wise change in subjective importance at class boundaries.<br />
* The distribution of bits is ordered from most to least subjective importance at both the class level and within the classes.<br />
<br />
{| border=1<br />
! Frame type !! Total bits !! Class A bits !! Class B bits !! Class C bits<br />
|-<br />
| 0 || 95 || 42 || 53 || 0<br />
|-<br />
| 1 || 103 || 49 || 54 || 0<br />
|-<br />
| 2 || 118 || 55 || 63 || 0<br />
|-<br />
| 3 || 134 || 58 || 76 || 0<br />
|-<br />
| 4 || 148 || 61 || 87 || 0<br />
|-<br />
| 5 || 159 || 75 || 84 || 0<br />
|-<br />
| 6 || 204 || 65 || 99 || 40<br />
|-<br />
| 7 || 244 || 81 || 103 || 60<br />
|-<br />
| 8 || 39 || 39 || 0 || 0<br />
|-<br />
| 9 || 43 || 43 || 0 || 0<br />
|-<br />
| 10 || 38 || 38 || 0 || 0<br />
|-<br />
| 11 || 37 || 37 || 0 || 0<br />
|}<br />
<br />
For the specifics of the bit stream layout (i.e. for the bits to parameter mappings) see [http://www.3gpp.org/ftp/Specs/html-info/26101.htm 26.101 AMR speech codec frame structure] and [http://www.3gpp.org/ftp/Specs/html-info/26090.htm 26.090 AMR speech codec transcoding functions].<br />
<br />
== Decoding of LP filter parameters ==<br />
<br />
The received indices of LSP quantization are used to reconstruct the quantified<br />
LSP vectors. (c.f. §5.2.5)<br />
<br />
<br />
=== 12.2kbps mode summary ===<br />
<br />
* indices into code books are parsed from the bit stream<br />
* indices give elements of split matrix quantised (SMQ) residual LSF vectors from the relevant code books<br />
* prediction from the previous frame is added to obtain the mean-removed LSF vectors<br />
* the mean is added<br />
* the LSF vectors are converted to cosine domain LSP vectors<br />
<br />
==== Decoding SMQ residual LSF vectors ====<br />
<br />
The elements of the SMQ vectors are stored at an index into a code book that<br />
varies according to the mode. There are 5 code books for the 12.2kbps mode<br />
corresponding to the 5 indices. These tables will be referred to as:<br />
<br />
<code>lsf_m_n</code><br />
<br />
; m : the number of indices parsed according to the mode<br />
; n : the index 'position' i.e. 1 for the first index, etc<br />
<br />
The 5 indices are stored using 7, 8, 8 + sign bit, 8, 6 bits respectively.<br />
The four elements of a 'split quantized sub-matrix' are stored at the index<br />
position in the appropriate code book are reassigned as follows:<br />
<br />
; lsf_5_1[index1] : r1_1, r1_2, r2_1, r2_2<br />
; lsf_5_2[index2] : r1_3, r1_4, r2_3, r2_4<br />
; lsf_5_3[index3] : r1_5, r1_6, r2_5, r2_6<br />
; lsf_5_4[index4] : r1_7, r1_8, r2_7, r2_8<br />
; lsf_5_5[index5] : r1_9, r1_10, r2_9, r2_10<br />
<br />
With rj_i :<br />
; j : the first or second residual lsf vector<br />
; i : the coefficient of a residual lsf vector ( i = 1, ..., 10 )<br />
<br />
; rj_i : residual line spectral frequencies (LSFs) in Hz<br />
<br />
==== Mean-removed LSF vector prediction ====<br />
<br />
[[Image: Amrnb_lsfmeanrem.gif]]<br />
<br />
; z_j(n) : the mean-removed LSF vector at the jth subframe<br />
; r_j(n) : prediction residual vector of frame n at the jth subframe<br />
; ^r_2(n-1) : the quantified residual vector from the previous frame at the 2nd subframe<br />
<br />
==== The mean is added ====<br />
<br />
[[Image: Amrnb_lsfvec.gif]]<br />
<br />
; lsf_mean_m : a table of the means of the LSF coefficients<br />
; m : the number of indices parsed according to the mode<br />
; fj : the LSF vectors<br />
<br />
==== LSF to LSP vector conversion ====<br />
<br />
[[Image: Amrnb_lspvec.gif]]<br />
<br />
; q_k[i] : the ith coefficient of the kth line spectral pair (LSP) in the cosine domain<br />
; k : the two lsf vectors give the LSP vectors q2, q4 at the 2nd and 4th subframes; k = 2*j<br />
; f_j[i] : ith coefficient of the jth LSF vector; [0,4000] Hz<br />
; f_s : sampling frequency in Hz (8kHz)<br />
<br />
=== Other active modes summary ===<br />
<br />
The process for the other modes is similar to that for the 12.2kbps mode.<br />
<br />
* indices into code books are parsed from the bit stream<br />
* indices give elements of a split matrix quantised (SMQ) residual LSF vector from the relevant code books<br />
* prediction from the previous frame is added to obtain the mean-removed LSF vector<br />
* the mean is added<br />
* the LSF vector is converted to a cosine domain LSP vector<br />
<br />
==== Decoding the SMQ residual LSF vector ====<br />
<br />
The 3 indices are stored with the following numbers of bits:<br />
<br />
{| border=1<br />
! Mode (kbps) !! 1st index (bits) !! 2nd index (bits) !! 3rd index (bits)<br />
|-<br />
| 10.2 || 8 || 9 || 9<br />
|-<br />
| 7.95 || 9 || 9 || 9<br />
|-<br />
| 7.40 || 8 || 9 || 9<br />
|-<br />
| 6.70 || 8 || 9 || 9<br />
|-<br />
| 5.90 || 8 || 9 || 9<br />
|-<br />
| 5.15 || 8 || 8 || 7<br />
|-<br />
| 4.75 || 8 || 8 || 7<br />
|}<br />
<br />
The four elements of a 'split quantized sub-matrix' are stored at the index<br />
position in the appropriate code book are:<br />
<br />
; 1st index in 1st code book : r_1, r_2, r_3<br />
; 2nd index in 2nd code book : r_4, r_5, r_6<br />
; 3rd index in 3rd code book : r_7, r_8, r_9, r_10<br />
<br />
; r_i : residual LSF vector (Hz)<br />
; i : the coefficient of vector ( i = 1, ..., 10 )<br />
<br />
==== Mean-removed LSF vector prediction ====<br />
<br />
[[Image: Amrnb_lsfmeanrem2.gif]]<br />
<br />
; z_j(n)[i] : the ith coefficient of the mean-removed LSF vector at the jth subframe<br />
; r_j(n)[i] : the ith coefficient of the prediction residual vector of frame n at the jth subframe<br />
; pred_fac[i] : the ith coefficient of the prediction factor<br />
; ^r_j(n-1)[i] : the ith coefficient of the the quantified residual vector of the previous frame at the jth subframe<br />
<br />
These processes give the LSP vector at the 4th subframe (q4)<br />
<br />
=== LSP vector interpolation (c.f. §5.2.6) ===<br />
<br />
==== 12.2 kbps mode ====<br />
<br />
[[Image: Amrnb_lspinterpq1-1.gif]]<br />
<br />
[[Image: Amrnb_lspinterpq3-1.gif]]<br />
<br />
==== Other modes ====<br />
<br />
[[Image: Amrnb_lspinterpq1-2.gif]]<br />
<br />
[[Image: Amrnb_lspinterpq2.gif]]<br />
<br />
[[Image: Amrnb_lspinterpq3-2.gif]]<br />
<br />
=== LSP vector to LP filter coefficient conversion (c.f. §5.2.4) ===<br />
<br />
for i=1..5<br />
f1[i] = 2*f1[i-2] - 2*q[2i-1]*f1[i-1]<br />
for j=i-1..1<br />
f1[j] += f1[j-2] - 2*q[2i-1]*f1[j-1]<br />
end<br />
end<br />
<br />
f1[-1] = 0; f1[0] = 0;<br />
<br />
Same for f2[i] with q[2i] instead of q[2i-1]<br />
<br />
for i=1..5<br />
f'1[i] = f1[i] + f1[i-1]<br />
f'2[i] = f2[i] - f2[i-1]<br />
end<br />
<br />
[[Image: Amrnb_lpccoeff.gif]]<br />
<br />
; a_i : the LP filter coefficients<br />
<br />
== Decoding of the pitch (adaptive codebook) vector ==<br />
<br />
* indices parsed from bitstream<br />
* indices give integer and fractional parts of the pitch lag<br />
* pitch vector v(n) is found by interpolating the past excitation u(n) at the pitch lag using an FIR filter. (c.f. §5.6)<br />
<br />
<br />
=== Decode pitch lag ===<br />
<br />
'''Note: division in this section is integer division!'''<br />
<br />
==== 12.2kbps mode - 1/6 resolution pitch lag ====<br />
<br />
===== First and third subframes =====<br />
<br />
In the first and third subframes, a fractional pitch lag is used with resolutions:<br />
<br />
* 1/6 in the range [17 3/6, 94 3/6]<br />
* 1 in the range [95, 143]<br />
<br />
...encoded using 9 bits.<br />
<br />
For [17 3/6, 94 3/6] the pitch index is encoded as:<br />
<br />
pitch_index = (pitch_lag_int - 17)*6 + pitch_lag_frac - 3;<br />
<br />
; pitch_lag_int : integer part of the pitch lag in the range [17, 94]<br />
; pitch_lag_frac : fractional part of the pitch lag in 1/6 units in the range [-2, 3]<br />
<br />
so...<br />
<br />
if(pitch_index < (94 4/6 - 17 3/6)*6)<br />
// fractional part is encoded in range [17 3/6, 94 3/6]<br />
pitch_lag_int = (pitch_index + 5)/6 + 17;<br />
pitch_lag_frac = pitch_index - pitch_lag_int*6 + (17 3/6)*6;<br />
<br />
And for [95, 143] the pitch index is encoded as:<br />
<br />
pitch_index = (pitch_lag_int - 95) + (94 4/6 - 17 3/6)*6;<br />
<br />
; pitch_lag_int : integer pitch lag in the range [95, 143]<br />
<br />
so...<br />
<br />
else<br />
// only integer part encoded in range [95, 143], no fractional part<br />
pitch_lag_int = pitch_index - (94 4/6 - 17 3/6)*6 + 95;<br />
pitch_lag_frac = 0;<br />
<br />
===== Second and fourth subframes =====<br />
<br />
In the second and fourth subframes, a pitch lag resolution of 1/6 is always used in the range [T1 - 5 3/6, T1 + 4 3/6], where T1 is nearest integer to the fractional pitch lag of the previous (1st or 3rd) subframe. The search range is bounded by [18, 143]. In this case the pitch delay is encoded using 6 bits and is therefore in the range [0,63].<br />
<br />
So the search range for the pitch lag is:<br />
<br />
search_range_min = max(pitch_lag_int_prev - 5, 18);<br />
search_range_max = search_range_min + 9;<br />
if(search_range_max > 143) {<br />
search_range_max = 143;<br />
search_range_min = search_range_max - 9;<br />
}<br />
<br />
; pitch_lag_int_prev : the integer part of the pitch lag from the previous sub frame<br />
<br />
The pitch index is encoded as:<br />
<br />
pitch_index = (pitch_lag_int - (search_range_min - 1))*6 + pitch_lag_frac - 3;<br />
<br />
; pitch_lag_int : the integer part of the pitch lag in the range [search_range_min - 1, search_range_max]<br />
; pitch_lag_frac : the fractional part of the pitch lag in the range [-2, 3]<br />
<br />
The formula for the pitch_index has been chosen to map pitch_lag_int [search_range_min - 1, search_range_max] and pitch_lag_frac [-2, 3] to [0,60]. (pitch_index = [0, 10]*6 + [-2, 3] - 3 = [0, 6, ..., 60] + [-5, 0] = [0,60])<br />
<br />
So the pitch lag is calculated through:<br />
<br />
// integer part of pitch lag = position in range [search_range_min - 1, search_range_max] + lower bound of range<br />
pitch_lag_int = (pitch_index + 5)/6 + search_range_min - 1;<br />
// fractional part of pitch lag = pitch index - (integer part without offset)*6 - 1 3/6 offset to bring the values to the correct range<br />
pitch_lag_frac = pitch_index - ((pitch_index + 5)/6)*6 - 9;<br />
<br />
Note that when using integers and integer division to conduct (pitch_index + 5)/6 the result is similar to taking the ceiling of pitch_index/6.0.<br />
<br />
==== Others modes - 1/3 resolution pitch lag ====<br />
<br />
===== First and third subframes =====<br />
<br />
In the first and third subframes, a fractional pitch lag is used with resolutions:<br />
<br />
* 1/3 in the range [19 1/3, 84 2/3]<br />
* 1 in the range [85, 143]<br />
<br />
...encoded using 8 bits.<br />
<br />
For [19 1/3, 84 2/3] the pitch lag is encoded as:<br />
<br />
pitch_index = pitch_lag_int*3 + pitch_lag_frac - (19 1/3)*3;<br />
<br />
; pitch_lag_int : integer part of the pitch lag in the range [19, 84]<br />
; pitch_lag_frac : fractional part of the pitch lag in 1/3 units in the range [0, 2]<br />
<br />
so...<br />
<br />
if(pitch_index < (85 - 19 1/3)*3)<br />
// fractional part is encoded in range [19 1/3, 84 2/3]<br />
pitch_lag_int = (pitch_index + 2)/3 + 19;<br />
pitch_lag_frac = pitch_index - pitch_lag_int*3 + (19 1/3)*3;<br />
<br />
And for [85, 143] the pitch index is encoded as:<br />
<br />
pitch_index = pitch_lag_int - 85 + (85 - 19 1/3)*3;<br />
<br />
; pitch_lag_int : integer pitch lag in the range [85, 143]<br />
<br />
so...<br />
<br />
else<br />
// only integer part encoded in range [85, 143], no fractional part<br />
pitch_lag_int = pitch_index - (85 - 19 1/3)*3 + 85;<br />
pitch_lag_frac = 0;<br />
<br />
===== Second and fourth subframes =====<br />
<br />
In the second and fourth subframes, the pitch lag resolution varies depending on the mode as follows:<br />
<br />
* 7.95 kbps mode<br />
** resolution of 1/3 is always used in the range [T1 - 10 2/3, T1 + 9 2/3]<br />
** encoded using 6 bits => pitch_index is in the range [0, 63]<br />
* 10.2 and 7.40 kbps modes<br />
** resolution of 1/3 is always used in the range [T1 - 5 2/3, T1 + 4 2/3]<br />
** encoded using 5 bits => pitch_index is in the range [0, 31]<br />
* 6.70, 5.90, 5.15 and 4.75 kbps modes<br />
** resolution of 1 is used in the range [T1 - 5, T1 + 4]<br />
** resolution of 1/3 is always used in the range [T1 - 1 2/3, T1 + 2/3]<br />
** encoded using 4 bits => pitch_index is in the range [0, 15]<br />
<br />
Where T1 is nearest integer to the fractional pitch lag of the previous (1st or 3rd) subframe. The search range is bounded by [20, 143].<br />
<br />
So the search range for the pitch lag is:<br />
<br />
lower_bound = 5;<br />
range = 9;<br />
if(mode == 7.95) {<br />
lower_bound = 10;<br />
range = 19;<br />
}<br />
search_range_min = max(pitch_lag_int_prev - lower_bound, 20);<br />
search_range_max = search_range_min + range;<br />
if(search_range_max > 143) {<br />
search_range_max = 143;<br />
search_range_min = search_range_max - range;<br />
}<br />
<br />
; pitch_lag_int_prev : the integer part of the pitch lag from the previous sub frame<br />
<br />
<br />
For modes 7.40, 7.95 and 10.2 the pitch index is encoded as:<br />
<br />
pitch_index = (pitch_lag_int - search_range_min)*3 + pitch_lag_frac + 2;<br />
<br />
; pitch_lag_int : the integer part of the pitch lag in the range [search_range_min, search_range_max]<br />
; pitch_lag_frac : the fractional part of the pitch lag in the range [-1, 1]<br />
<br />
So the pitch lag is calculated through:<br />
<br />
// integer part of pitch lag = position of pitch lag in range [search_range_min, search_range_max] + lower bound of the range<br />
pitch_lag_int = (pitch_index + 2)/3 - 1 + search_range_min;<br />
// fractional part of pitch lag = pitch index - (integer part without offset)*3 - 2/3 to bring the values to the correct range<br />
pitch_lag_frac = pitch_index - ((pitch_index + 2)/3 - 1)*3 - 2;<br />
<br />
<br />
For modes 4.75, 5.15, 5.90 and 6.70:<br />
<br />
t1_temp = max( min(pitch_lag_int_prev, search_range_min + 5), search_range_max - 4 );<br />
<br />
; t1_temp : predicted pitch lag from the previous frame adjusted to fit into the 0 position of the search range<br />
<br />
The pitch index is encoded as:<br />
<br />
// if pitch lag is below T1 - 1 2/3<br />
if( pitch_lag_int*3 + pitch_lag_frac <= (t1_temp - 2)*3 ) {<br />
// encode with resolution 1<br />
index = (pitch_lag_int - t1_temp) + 5;<br />
// else if pitch lag is below T1 + 1<br />
}else if( pitch_lag_int*3 + pitch_lag_frac < (t1_temp + 1)*3 ) {<br />
// encode with resolution 1/3<br />
index = ( pitch_lag_int*3 + pitch_lag_frac - (t1_temp - 2)*3 ) + 3;<br />
// else pitch lag is above T1 + 2/3<br />
}else {<br />
// encode with resolution 1<br />
index = (pitch_lag_int - t1_temp) + 11;<br />
}<br />
<br />
; pitch_lag_int : the integer part of the pitch lag in the range [search_range_min, search_range_max]<br />
; pitch_lag_frac : the fractional part of the pitch lag in the range [-1, 1]<br />
<br />
The possible pitch indices and values are:<br />
<br />
{| border=1<br />
|-<br />
| 0 || 1 || 2 || 3 || 4 || 5 || 6 || 7 || 8 || 9 || 10 || 11 || 12 || 13 || 14 || 15<br />
|-<br />
| -5 || -4 || -3 || -2 || -1 2/3 || -1 1/3 || -1 || -2/3 || -1/3 || 0 || 1/3 || 2/3 || 1 || 2 || 3 || 4<br />
|-<br />
|}<br />
<br />
So the pitch lag is calculated through:<br />
<br />
if(pitch_index < 4) {<br />
// integer part of pitch lag = pitch lag position in range [t1_temp - 5, t1_temp - 2] + lower bound of range<br />
pitch_lag_int = pitch_index + (t1_temp - 5);<br />
// this range is coded with resolution 1 so no fractional part<br />
pitch_lag_frac = 0;<br />
}else if(pitch_index < 12) {<br />
pitch_lag_int = (pitch_index - 2)/3 + (t1_temp - 2);<br />
pitch_lag_frac = (pitch_index - 4) - ((pitch_index - 2)/3)*3 - 11;<br />
}else {<br />
// integer part of pitch lag = pitch lag position in range [t1_temp + 1, t1_temp + 4] + lower bound of range<br />
pitch_lag_int = pitch_index - 12 + t1_temp + 1;<br />
// this range is coded with resolution 1 so no fractional part<br />
pitch_lag_frac = 0;<br />
}<br />
<br />
<br />
=== Calculate pitch vector ===<br />
<br />
[[Image: Amrnb_pitchvector.gif]]<br />
<br />
; k : integer pitch lag<br />
; n : sample position in the vectors 0, ..., 39<br />
; t : 0, ..., 5 corresponding to fractions 0, 1/6, 2/6, 3/6, -2/6, -1/6 respectively<br />
<br />
This equation can be used for both 1/3 and 1/6 resolution simply by multiplying t by 2 in the 1/3 case.<br />
<br />
(Note: the coefficients b60 are in the reference source in an array called inter6)<br />
<br />
<br />
<br />
== Decoding of the fixed (innovative or algebraic) vector ==<br />
<br />
* the excitation pulse positions and signs are parsed from the bit stream<br />
* the pulse positions and signs are encoded differently depending on the mode<br />
* the fixed code book vector, c(n), is then constructed from the pulse positions and signs<br />
* if pitch_lag_int is less than the subframe size (40), the pitch sharpening procedure is applied<br />
<br />
<br />
=== Decoding the pulse positions ===<br />
<br />
==== 12.2 kbps mode ====<br />
<br />
* 10 pulse positions each coded using 3 bit Gray codes<br />
* signs coded using 1 bit each for 5 pulse pairs<br />
<br />
<br />
{| border=1<br />
! Pulse !! Positions<br />
|-<br />
| i0,i5 || 0, 5, 10, 15, 20, 25, 30, 35<br />
|-<br />
| i1,i6 || 1, 6, 11, 16, 21, 26, 31, 36<br />
|-<br />
| i2,i7 || 2, 7, 12, 17, 22, 27, 32, 37<br />
|-<br />
| i3,i8 || 3, 8, 13, 18, 23, 28, 33, 38<br />
|-<br />
| i4,i9 || 4, 9, 14, 19, 24, 29, 34, 39<br />
|-<br />
|}<br />
<br />
<br />
==== 10.2 kbps mode ====<br />
<br />
* 8 pulse positions, 4 pairs, coded as 3 values using 10, 10 and 7 bits<br />
* signs coded using 1 bit each for 4 pulse pairs<br />
<br />
<br />
{| border=1<br />
! Pulse !! Positions<br />
|-<br />
| i0,i4 || 0, 4, 8, 12, 16, 20, 24, 28, 32, 36<br />
|-<br />
| i1,i5 || 1, 5, 9, 13, 17, 21, 25, 29, 33, 37<br />
|-<br />
| i2,i6 || 2, 6, 10, 14, 18, 22, 26, 30, 34, 38<br />
|-<br />
| i3,i7 || 3, 7, 11, 15, 19, 23, 27, 31, 35, 39<br />
|-<br />
|}<br />
<br />
<br />
==== 7.95 and 7.40 kbps modes ====<br />
<br />
* 4 pulse positions Gray coded using 3, 3, 3 and 4 bits<br />
* signs coded using 1 bit for each pulse<br />
<br />
<br />
{| border=1<br />
! Pulse !! Positions<br />
|-<br />
| i0 || 0, 5, 10, 15, 20, 25, 30, 35<br />
|-<br />
| i1 || 1, 6, 11, 16, 21, 26, 31, 36<br />
|-<br />
| i2 || 2, 7, 12, 17, 22, 27, 32, 37<br />
|-<br />
| i3 || 3, 8, 13, 18, 23, 28, 33, 38<br />
4, 9, 14, 19, 24, 29, 34, 39<br />
|-<br />
|}<br />
<br />
==== 6.70 kbps mode ====<br />
<br />
* 3 pulse positions coded using 3, 4 and 4 bits<br />
* signs coded using 1 bit for each pulse<br />
<br />
<br />
{| border=1<br />
! Pulse !! Positions<br />
|-<br />
| i0 || 0, 5, 10, 15, 20, 25, 30, 35<br />
|-<br />
| i1 || 1, 6, 11, 16, 21, 26, 31, 36<br />
3, 8, 13, 18, 23, 28, 33, 38<br />
|-<br />
| i2 || 2, 7, 12, 17, 22, 27, 32, 37<br />
4, 9, 14, 19, 24, 29, 34, 39<br />
|-<br />
|}<br />
<br />
<br />
==== 5.90 kbps mode ====<br />
<br />
* 2 pulse positions coded using 4 and 5 bits<br />
* signs coded using 1 bit for each pulse<br />
<br />
<br />
{| border=1<br />
! Pulse !! Positions<br />
|-<br />
| i0 || 1, 6, 11, 16, 21, 26, 31, 36<br />
3, 8, 13, 18, 23, 28, 33, 38<br />
|-<br />
| i1 || 0, 5, 10, 15, 20, 25, 30, 35<br />
1, 6, 11, 16, 21, 26, 31, 36<br><br />
2, 7, 12, 17, 22, 27, 32, 37<br><br />
4, 9, 14, 19, 24, 29, 34, 39<br />
|-<br />
|}<br />
<br />
==== 5.15 and 4.75 kbps modes ====<br />
<br />
* 2 pulse positions coded using 1 bit for the position subset and 3 bits per pulse<br />
* signs coded using 1 bit for each pulse<br />
<br />
<br />
{| border=1<br />
! Subframe !! Subset !! Pulse !! Positions<br />
|-<br />
| rowspan="4" align="center" | 1<br />
| rowspan="2" align="center" | 1<br />
| i0 || 0, 5, 10, 15, 20, 25, 30, 35<br />
|-<br />
| i1 || 2, 7, 12, 17, 22, 27, 32, 37<br />
|-<br />
| rowspan="2" align="center" | 2<br />
| i0 || 1, 6, 11, 16, 21, 26, 31, 36<br />
|-<br />
| i1 || 3, 8, 13, 18, 23, 28, 33, 38<br />
|-<br />
| rowspan="4" align="center" | 2<br />
| rowspan="2" align="center" | 1<br />
| i0 || 0, 5, 10, 15, 20, 25, 30, 35<br />
|-<br />
| i1 || 3, 8, 13, 18, 23, 28, 33, 38<br />
|-<br />
| rowspan="2" align="center" | 2<br />
| i0 || 2, 7, 12, 17, 22, 27, 32, 37<br />
|-<br />
| i1 || 4, 9, 14, 19, 24, 29, 34, 39<br />
|-<br />
| rowspan="4" align="center" | 3<br />
| rowspan="2" align="center" | 1<br />
| i0 || 0, 5, 10, 15, 20, 25, 30, 35<br />
|-<br />
| i1 || 2, 7, 12, 17, 22, 27, 32, 37<br />
|-<br />
| rowspan="2" align="center" | 2<br />
| i0 || 1, 6, 11, 16, 21, 26, 31, 36<br />
|-<br />
| i1 || 4, 9, 14, 19, 24, 29, 34, 39<br />
|-<br />
| rowspan="4" align="center" | 4<br />
| rowspan="2" align="center" | 1<br />
| i0 || 0, 5, 10, 15, 20, 25, 30, 35<br />
|-<br />
| i1 || 3, 8, 13, 18, 23, 28, 33, 38<br />
|-<br />
| rowspan="2" align="center" | 2<br />
| i0 || 1, 6, 11, 16, 21, 26, 31, 36<br />
|-<br />
| i1 || 4, 9, 14, 19, 24, 29, 34, 39<br />
|-<br />
|}<br />
<br />
<br />
=== Fixed codebook vector construction ===<br />
<br />
All c(n) are zero if there is no pulse at position n. If there is a pulse at position n then it has the corresponding sign as parsed above.<br />
<br />
<br />
=== Pitch sharpening ===<br />
<br />
[[Image: Amrnb fixvecpitshp.gif]]<br />
<br />
; β : the decoded pitch gain, ^g_p, bounded by [0.0,1.0] for 12.2. kbps or [0.0,0.8] for other modes<br />
<br />
Note: Only sharpen n in [pitch_lag_int-1, 39]. The reference source considers previous fixed vector elements (n<0) to be 0. I think this is also the reason pitch sharpening is not conducted for pitch_lag_int > 40 as all the sharpening contributions would be 0.<br />
<br />
== Decoding of the pitch and fixed codebook gains ==<br />
<br />
<br />
=== Fixed gain prediction ===<br />
<br />
A moving average prediction of the innovation (fixed) energy is conducted.<br />
<br />
<br />
[[Image: Amrnb_fixedgainpred.gif]]<br />
<br />
; g_c' : fixed gain prediction<br />
; \tilde{E} : predicted energy [dB]<br />
; \bar{E} : desired mean innovation (fixed) energy [dB]<br />
; E_I : calculated mean innovation (fixed) energy [dB]<br />
<br />
<br />
[[Image: Amrnb_predener.gif]]<br />
<br />
; b : 4-tap MA prediction coefficients [0.68, 0.58, 0.34, 0.19]<br />
; ^R(k) : quantified prediction errors at subframe k 20*log10(^γ_gc(k))<br />
<br />
<br />
Desired mean innovation (fixed) energy:<br />
<br />
{| border=1<br />
! Mode (kbps) !! Mean energy (dB)<br />
|-<br />
| 12.2 || 36<br />
|-<br />
| 10.2 || 33<br />
|-<br />
| 7.95 || 36<br />
|-<br />
| 7.40 || 30<br />
|-<br />
| 6.70 || 28.75<br />
|-<br />
| 5.90 || 33<br />
|-<br />
| 5.15 || 33<br />
|-<br />
| 4.75 || 33<br />
|-<br />
|}<br />
<br />
<br />
[[Image: Amrnb_meaniner.gif]]<br />
<br />
; E_I : calculated mean innovation (fixed) energy [dB]<br />
; N : subframe size 40<br />
; c(n) : fixed codebook vector<br />
<br />
=== Dequantisation of the gains ===<br />
<br />
==== 12.2kbps and 7.95kbps - scalar quantised gains ====<br />
<br />
The received indices are used to find the quantified pitch gain, ^g_p, and the quantified fixed gain correction factor, ^γ_gc.<br />
<br />
===== Pitch gain =====<br />
<br />
The parsed gain index is used to obtain the quantified pitch gain, ^g_p, from the corresponding codebook. (qua_gain_pitch in the reference source.)<br />
<br />
===== Fixed gain correction factor =====<br />
<br />
The parsed gain index is used to obtain the quantified fixed gain, ^g_c, from the corresponding codebook. (qua_gain_code in the reference source.) The table stores ^γ_gc, and the quantised energy error in two forms (these are needed for the moving average calculation of the predicted fixed gain):<br />
<br />
qua_ener_MR122 = log2(^γ_gc)<br />
qua_ener = 20*log10(^γ_gc)<br />
<br />
^γ_gc is stored at Q11 (i.e. it's multiplied by 2^11.) qua_ener_MR122 and qua_ener are stored at Q10.<br />
<br />
==== Other modes - vector quantised gains ====<br />
<br />
The received index gives both the quantified adaptive codebook gain, ^g_p, and the quantified algebraic codebook gain correction factor, ^γ_gc.<br />
<br />
The tables contains the following data:<br />
<br />
^g_p (Q14),<br />
^γ_gc (Q12), (^g_c = g_c'*^γ_gc),<br />
qua_ener_MR122 (Q10), (log2(^γ_gc))<br />
qua_ener (Q10) (20*log10(^γ_gc))<br />
<br />
The log2() and log10() values are calculated on the fixed point value<br />
(g_fac Q12) and not on the original floating point value of g_fac<br />
to make the quantizer/MA predictdor use corresponding values.<br />
<br />
The codebook used depends on the mode:<br />
<br />
6.70, 7.40, 10.2 kbps modes - table_gain_highrates in the reference source<br />
Four consecutive entries give ^g_p, ^γ_gc, qua_ener_MR122 and qua_ener. Apparently the values for qua_ener are the original ones from IS641 to ensure bit-exactness but are not exactly the rounded value of 20log10(^γ_gc).<br />
<br />
5.15, 5.90 kbps modes - table_gain_lowrates<br />
Similar to table_gain_highrates, four consecutive entries give ^g_p, ^γ_gc, qua_ener_MR122 and qua_ener. There are no special notes for this table.<br />
<br />
4.75 kbps mode - table_gain_MR475<br />
Unlike the above mentioned tables, four consecutive values give ^g_p, ^γ_gc for subframes 0,2, and then ^g_p, ^γ_gc for subframes 1,3.<br />
<br />
At the very least I think these tables could be redesigned a little.<br />
<br />
=== Calculation of the quantified fixed gain ===<br />
<br />
[[Image: Amrnb_fixedgain.gif]]<br />
<br />
== Smoothing of the fixed codebook gain ==<br />
<br />
<br />
'''10.2, 6.70, 5.90, 5.15, 4.75 kbit/s modes only'''<br />
<br />
<br />
=== Calculate averaged LSP vector ===<br />
<br />
[[Image: Amrnb_lspsmooth.gif]]<br />
<br />
; \bar{q}(n) : averaged LSP vector at frame n<br />
; ^q_4(n) : quantified LSP vector for the 4th subframe at frame n<br />
<br />
<br />
=== Calculate fixed gain smoothing factor ===<br />
<br />
[[Image: Amrnb_lspdiff.gif]]<br />
<br />
; diff_m : difference measure at subframe m<br />
; j : loops over LSPs<br />
; m : loops over subframes<br />
; ^q_m : quantified LSP vector at subframe m<br />
<br />
Note: I think the sum over the subframes is an error in the specification and they actually mean to use the quantified LSP vector from subframe m for the calculation of diff for subframe m. I think this is what they do in the reference source and the sum over m is an error.<br />
<br />
<br />
[[Image: Amrnb_smoothfac.gif]]<br />
<br />
; k_m : fixed gain smoothing factor<br />
; K_1 : 0.4<br />
; K_2 : 0.25<br />
; diff_m : difference measure at subframe m<br />
<br />
If diff_m has been greater than 0.65 for 10 subframes, k_m is set to 1.0 (i.e. no smoothing) for 40 subframes.<br />
<br />
=== Calculate mean fixed gain ===<br />
<br />
[[Image: Amrnb_meangc.gif]]<br />
<br />
; \bar{g_c}(m) : mean fixed gain at subframe m<br />
; ^g_c(k) : quantified fixed gain at subframe k<br />
<br />
=== Calculate smoothed fixed gain ===<br />
<br />
[[Image: Amrnb_gcsmooth.gif]]<br />
<br />
; ^g_c : quantified fixed gain<br />
; \bar{g_c} : averaged fixed gain<br />
<br />
== Anti-sparseness processing ==<br />
<br />
<br />
'''7.95, 6.70, 5.90, 5.15, 4.75 kbit/s modes only'''<br />
<br />
<br />
=== Evaluate impulse response filter strength ===<br />
<br />
The fixed vector, c(n), has only a few pulses per subframe. In certain conditions, to reduce perceptual artifacts arising from this, the vector is circularly convolved with a predefined impulse response. The selection of the strength of filter is made based on the decoded gains.<br />
<br />
if ^g_p < 0.6<br />
impNr = 0<br />
else if ^g_p < 0.9<br />
impNr = 1<br />
else<br />
impNr = 2<br />
<br />
; impNr = 0 : strong impulse response filter<br />
; impNr = 1 : medium impulse response filter<br />
; impNr = 2 : no filtering<br />
<br />
if ^g_c(k) > 2 * ^g_c(k-1)<br />
impNr = min( impNr + 1, 2 )<br />
else if impNr = 0 AND median of last five ^g_p >= 0.6<br />
impNr = min( impNr(k), min(impNr(k-1) + 1, 2) )<br />
<br />
<br />
=== Circular convolution of fixed vector and impulse response filter ===<br />
<br />
[[Image: Amrnb_circconv.gif]]<br />
<br />
; (c * h) : convolution of vectors c and h, in this case circular convolution<br />
; h[n] : nth coefficient of the impulse response used for filtering<br />
; c[n] : nth coefficient of the fixed vector<br />
<br />
n = 0, ..., 39<br />
<br />
To make the convolution circular, make the impulse response circular by taking h[-m] = h[39-m]<br />
<br />
== Computing the reconstructed speech ==<br />
<br />
<br />
=== Construct excitation ===<br />
<br />
[[Image: Amrnb_excitation.gif]]<br />
<br />
; u(n) : excitation vector<br />
; ^g_p : quantified pitch gain<br />
; v(n) : pitch vector<br />
; ^g_c : quantified fixed gain<br />
; c(n) : fixed vector<br />
<br />
<br />
=== Emphasise pitch vector contribution ===<br />
<br />
This is apparently a post-processing technique.<br />
<br />
[[Image: Amrnb_pitchvecemph.gif]]<br />
<br />
; ^u(n) : excitation vector with emphasised pitch vector contribution<br />
; β : ^g_p bounded by [0.0, 0.8] or [0.0, 1.0] depending on mode<br />
<br />
<br />
=== Apply adaptive gain control (AGC) through gain scaling ===<br />
<br />
[[Image: Amrnb_gainfac.gif]]<br />
<br />
; η : gain scaling factor for emphasised excitation<br />
<br />
[[Image: Amrnb_gainscalexc.gif]]<br />
<br />
; ^u'(n) : gain-scaled emphasised excitation<br />
<br />
<br />
=== Calculate reconstructed speech samples ===<br />
<br />
[[Image: Amrnb_synthfilter.gif]]<br />
<br />
; ^s(n) : reconstructed speech samples<br />
; ^a_i : LP filter coefficients<br />
<br />
Note: for n-i < 0, ^s(n-i) should be take from previous speech samples if existing, else we will consider them 0 as this behaviour is undefined the specification.<br />
<br />
== Additional instability protection ==<br />
<br />
If an overflow occurs during synthesis, the pitch vector, v(n), is scaled down by a factor of 4 and synthesis is conducted again bypassing emphasising the pitch vector contribution and adaptive gain control.<br />
<br />
Q: What classifies an overflow?<br />
A: s(n) < -32768 or s(n) > 32767 (i.e. 16-bit signed int)<br />
<br />
== Post-processing ==<br />
<br />
=== Adaptive post-filtering ===<br />
<br />
(c.f. §6.2.1)<br />
<br />
==== IIR filtering ====<br />
<br />
The speech samples, ^s(n), are filtered through a formant filter and a tilt compensation filter.<br />
<br />
[[Image: Amrnb_pfformant.gif]]<br />
<br />
; γ_n, γ_d : control the amount of formant post-filtering<br />
<br />
The speech samples, ^s(n), are filtered through ^A(z/γ_n) to produce the residual signal ^r(n).<br />
<br />
[[Image: Amrnb_residual.gif]]<br />
<br />
^r(n) is filtered through 1/^A(z/γ_d).<br />
<br />
[[Image: Amrnb_fmntfilt.gif]]<br />
<br />
The output is filtered through H_t(z) (the tilt compensation filter) resulting in the post-filtered speech signal, ^s_f(n).<br />
<br />
[[Image: Amrnb_pftilt.gif]]<br />
<br />
[[Image: Amrnb_mu.gif]]<br />
<br />
[[Image: Amrnb_reffac.gif]]<br />
<br />
[[Image: Amrnb_r_h.gif]]<br />
<br />
; L_h : 22 - the truncation of the impulse response<br />
; h_f : impulse response of the formant filter H_f<br />
<br />
So, in summation notation:<br />
<br />
[[Image: Amrnb_pftiltsum.gif]]<br />
<br />
===== 12.2 and 10.2 kbps modes =====<br />
<br />
γ_n = 0.7<br />
<br />
γ_d = 0.75<br />
<br />
γ_t = 0.8 if k_1' > 0; 0 otherwise<br />
<br />
===== Other modes =====<br />
<br />
γ_n = 0.55<br />
<br />
γ_d = 0.7<br />
<br />
γ_t = 0.8<br />
<br />
==== Adaptive gain control ====<br />
<br />
Adaptive gain control is used to compensate for the gain difference between the filtered and synthesised speech signals.<br />
<br />
[[Image: Amrnb_gainscalfac.gif]]<br />
<br />
[[Image: Amrnb_gainscalfacwtd.gif]]<br />
<br />
[[Image: Amrnb_gainscalspch.gif]]<br />
<br />
; \alpha : adaptive gain control factor equal to 0.9<br />
<br />
=== High-pass filtering and upscaling ===<br />
<br />
(c.f. §6.2.2)<br />
<br />
[[Image: Amrnb_highpass.gif]]<br />
<br />
After completing all filtering, the samples are scaled up by a factor of 2.<br />
<br />
<br />
<br />
<br />
[[Category:Vocoders]]<br />
[[Category:Audio Codecs]]</div>Adam Quantrill