# A High Efficiency Multistage Coder for Lossless Audio Compression using OLS+ and CDCCR Method
## 7 Adaptive Golomb Code and Context Adaptive Binary Arithmetic Coder
### Medium-Term Features of the Probability Distribution  
The values of \(b_{\rm medium}\) and \(b_{\rm ultra}\) presented in Section 7.1 provide information on short-term features of the currently estimated probability distribution. Additionally, in the case of the prediction error stream, the assumption is made about the medium-term stationarity of its distribution, which is usually similar to the geometrical distribution. Data on such features can be coded in a highly efficient manner using the Golomb code family [18]. For each coded \(\vec{e}(n)\) value, an individual \(m\) group number is calculated. For this purpose, the adaptive average cost of coding with the Golomb code use one of the appropriately selected 40 probability distributions. Probability distributions are defined based on the formula \(G(i)=(1-p)\cdot p^{i}\), with individual values \(p_{(i)}\) associated with the \(j\)-th distribution depending on the \(m_{(i)}\) parameter of the Golomb code, which is the \(j\)-th number from the experimentally selected set \(\mathbf{m}=\{1,\,2,\,3,\,4,\,6,\,8,\,12,\,16,\,20,\,24,\,32,\,40,\,48,\,64,\,8,\,96,\,128,\,160,\,192,\,256,\,320,\,384,\,448,\,512,\,576,\,640,\,768,\,896, \,1024,\,1152,\,1280,\,1536,\,1792,\,2048,\,2560,\,3072,\,4096,\,5120,\,6144, \,8192\}\). This increased flexibility gives the Golomb codes an advantage over the Rice code, for which \(m\) values can be only a number 2 raised to an integer value (e.g., \(2^{1}=2,\,2^{2}=4,\,2^{3}=8,\,2^{4}=16,\,\dots\) ). To calculate a current predicted value of \(m\), in [17], the estimate using backward adaptation was proposed, applying a local assumption of stationarity \(S(n)\), being the average value of modified prediction errors. Assuming that the expected value of modified prediction errors is inversely proportional to \(1-p\), we obtain \(p=(S(n)-1)/S(n)\), where the expected value of \(S(n)\) equals  
\[S(n)=\frac{1}{N_{G}}\sum_{i=1}^{N_{G}}\vec{e}(n-i), \tag{28}\]  
where \(N_{G}\) specifies the number of previously coded modified prediction errors. The group number \(m\) can be calculated using formula presented in [41; 42] (at \(\delta=0\)):  
\[m=\left[-\frac{\log_{10}(1+p)}{\log_{10}p}+\delta\right]. \tag{29}\]  
An experimental correction value \(\delta=0.41\)[17] was introduced to Formula (29) which resulted in a slight decrease in the bit average (for the whole test base). At \(\delta=0\), there is a linear relationship between \(m\) and \(S(n)\) values of \(m=(\ln 2)\cdot S(n)\)[43], and after taking into account \(\delta=0.41\), an approximated form of the formula was determined:  
\[m=[0.693147\cdot S+0.563636]. \tag{30}\]  
After simplifying the Formula (28) to the iterative form and using the auxiliary sum \(S_{\gamma}\), which is adapted using the formula \(S_{\gamma}^{(q)}(n)=\vec{e}(n-1)+\gamma_{q}\cdot S_{\gamma}^{(q)}(n)\), we obtain the value of \(S^{(q)}(n)=(1-\gamma_{q})\cdot S_{\gamma}^{(q)}(n)\). The value of \(\gamma_{q}\) is the experimentally determined forgetting factor, in work [17] it was set at a compromise level of 0.952. A noticeable improvement was obtained by using two forgetting factors (\(\gamma_{1}=0.935\) and \(\gamma_{2}=0.992\)) and using them to calculate two sums of \(S^{(1)}(n)\) and \(S^{(2)}(n)\). In addition, we use the weighting effect of these sums, calculating the current average costs \(L_{\rm cost(q)}\) of encoding the input data stream (for \(q=\{1,\,2\}\)) using Golomb codes separately for parameters \(m_{1}\) and \(m_{2}\):  
\[L_{\rm cost(q)}(n)=0.8\cdot L_{\rm cost(q)}(n-1)+length_{(q)}(\vec{e}(n)), \tag{31}\]  
which allows calculating weights \(\gamma_{\rm cost(q)}(n)=0.7^{L_{\rm cost(q)}(n)}\) necessary to calculate the final value of \(m\):  
\[m=\left|0.693147\cdot\frac{\gamma_{\rm cost(1)}(n)\cdot S^{(1)}(n)+\gamma_{ \rm cost(2)}(n)\cdot S^{(2)}(n)}{\gamma_{\rm cost(1)}(n)+\gamma_{\rm cost(2)}(n) }+0.563636\right|. \tag{32}\]which is additionally scaled to best approximate one of the 40 values found in the \(\mathbf{m}\) vector.