%Copyright (C) 2007-2014  Brian Langenberger
%This work is licensed under the
%Creative Commons Attribution-Share Alike 3.0 United States License.
%To view a copy of this license, visit
%http://creativecommons.org/licenses/by-sa/3.0/us/ or send a letter to
%Creative Commons,
%171 Second Street, Suite 300,
%San Francisco, California, 94105, USA.

\subsection{Encoding LPC Subframe}
\label{flac:encode_lpc_subframe}
{\relsize{-1}
  \input{flac/algorithms/encode_lpc_subframe}
}

\clearpage

\subsubsection{Calculating QLP Precision}
\label{flac:calculate_qlp_precision}
{\relsize{-2}
  \input{flac/algorithms/calculate_qlp_precision}
}

\subsubsection{Windowing the Input Samples}
\label{flac:window}
{\relsize{-1}
  \input{flac/algorithms/encode_window_samples}
}

\par
For example, given the input samples: \texttt{[18, 20, 26, 24, 24, 23, 21, 24, 23, 20]}
\begin{wrapfigure}[5]{r}{3in}
\includegraphics{flac/figures/tukey.pdf}
\end{wrapfigure}
\begin{table}[h]
\begin{tabular}{r|r|r|r}
$i$ & $\textsf{sample}_i$ & $\textsf{Tukey}_i$ & $\textsf{windowed}_i$ \\
\hline
0 & 18 & 0.00 & 0.0 \\
1 & 20 & 0.41 & 8.2 \\
2 & 26 & 0.97 & 25.2 \\
3 & 24 & 1.00 & 24.0 \\
4 & 24 & 1.00 & 24.0 \\
5 & 23 & 1.00 & 23.0 \\
6 & 21 & 1.00 & 21.0 \\
7 & 24 & 0.97 & 23.3 \\
8 & 23 & 0.41 & 9.4 \\
9 & 20 & 0.00 & 0.0 \\
\end{tabular}
\end{table}

\clearpage

\subsubsection{Performing Autocorrelation}
\label{flac:autocorrelate}
{\relsize{-1}
  \input{flac/algorithms/encode_autocorrelate}
}
For example, given the windowed samples:
\texttt{[0.0, 8.2, 25.2, 24.0, 24.0, 23.0, 21.0, 23.3, 9.4, 0.0]}
and a maximum LPC order of 3:
\begin{figure}[h]
\subfloat{
  {\relsize{-2}
    \begin{tabular}{rrrrr}
      \texttt{0.0} & $\times$ & \texttt{0.0} & $=$ & \texttt{0.00} \\
      \texttt{8.2} & $\times$ & \texttt{8.2} & $=$ & \texttt{67.24} \\
      \texttt{25.2} & $\times$ & \texttt{25.2} & $=$ & \texttt{635.04} \\
      \texttt{24.0} & $\times$ & \texttt{24.0} & $=$ & \texttt{576.00} \\
      \texttt{24.0} & $\times$ & \texttt{24.0} & $=$ & \texttt{576.00} \\
      \texttt{23.0} & $\times$ & \texttt{23.0} & $=$ & \texttt{529.00} \\
      \texttt{21.0} & $\times$ & \texttt{21.0} & $=$ & \texttt{441.00} \\
      \texttt{23.3} & $\times$ & \texttt{23.3} & $=$ & \texttt{542.89} \\
      \texttt{9.4} & $\times$ & \texttt{9.4} & $=$ & \texttt{88.36} \\
      \texttt{0.0} & $\times$ & \texttt{0.0} & $=$ & \texttt{0.00} \\
      \hline
      \multicolumn{3}{r}{$\textsf{autocorrelated}_0$} & $=$ & \texttt{3455.53} \\
    \end{tabular}
  }
}
\includegraphics{flac/figures/lag0.pdf}

\subfloat{
  {\relsize{-2}
    \begin{tabular}{rrrrr}
      \texttt{0.0} & $\times$ & \texttt{8.2} & $=$ & \texttt{0.00} \\
      \texttt{8.2} & $\times$ & \texttt{25.2} & $=$ & \texttt{206.64} \\
      \texttt{25.2} & $\times$ & \texttt{24.0} & $=$ & \texttt{604.80} \\
      \texttt{24.0} & $\times$ & \texttt{24.0} & $=$ & \texttt{576.00} \\
      \texttt{24.0} & $\times$ & \texttt{23.0} & $=$ & \texttt{552.00} \\
      \texttt{23.0} & $\times$ & \texttt{21.0} & $=$ & \texttt{483.00} \\
      \texttt{21.0} & $\times$ & \texttt{23.3} & $=$ & \texttt{489.30} \\
      \texttt{23.3} & $\times$ & \texttt{9.4} & $=$ & \texttt{219.02} \\
      \texttt{9.4} & $\times$ & \texttt{0.0} & $=$ & \texttt{0.00} \\
      \hline
      \multicolumn{3}{r}{$\textsf{autocorrelated}_1$} & $=$ & \texttt{3130.76} \\
    \end{tabular}
  }
}
\includegraphics{flac/figures/lag1.pdf}

\subfloat{
  {\relsize{-2}
    \begin{tabular}{rrrrr}
      \texttt{0.0} & $\times$ & \texttt{25.2} & $=$ & \texttt{0.00} \\
      \texttt{8.2} & $\times$ & \texttt{24.0} & $=$ & \texttt{196.80} \\
      \texttt{25.2} & $\times$ & \texttt{24.0} & $=$ & \texttt{604.80} \\
      \texttt{24.0} & $\times$ & \texttt{23.0} & $=$ & \texttt{552.00} \\
      \texttt{24.0} & $\times$ & \texttt{21.0} & $=$ & \texttt{504.00} \\
      \texttt{23.0} & $\times$ & \texttt{23.3} & $=$ & \texttt{535.90} \\
      \texttt{21.0} & $\times$ & \texttt{9.4} & $=$ & \texttt{197.40} \\
      \texttt{23.3} & $\times$ & \texttt{0.0} & $=$ & \texttt{0.00} \\
      \hline
      \multicolumn{3}{r}{$\textsf{autocorrelated}_2$} & $=$ & \texttt{2590.90} \\
    \end{tabular}
  }
}
\includegraphics{flac/figures/lag2.pdf}

\subfloat{
  {\relsize{-2}
    \begin{tabular}{rrrrr}
      \texttt{0.0} & $\times$ & \texttt{24.0} & $=$ & \texttt{0.00} \\
      \texttt{8.2} & $\times$ & \texttt{24.0} & $=$ & \texttt{196.80} \\
      \texttt{25.2} & $\times$ & \texttt{23.0} & $=$ & \texttt{579.60} \\
      \texttt{24.0} & $\times$ & \texttt{21.0} & $=$ & \texttt{504.00} \\
      \texttt{24.0} & $\times$ & \texttt{23.3} & $=$ & \texttt{559.20} \\
      \texttt{23.0} & $\times$ & \texttt{9.4} & $=$ & \texttt{216.20} \\
      \texttt{21.0} & $\times$ & \texttt{0.0} & $=$ & \texttt{0.00} \\
      \hline
      \multicolumn{3}{r}{$\textsf{autocorrelated}_3$} & $=$ & \texttt{2055.80} \\
    \end{tabular}
  }
}
\includegraphics{flac/figures/lag3.pdf}
\end{figure}
\par
\noindent
Note that the total number of autocorrelation values equals
the maximum LPC order + 1.

\clearpage

\subsubsection{LP Coefficient Calculation}
\label{flac:compute_lp_coeffs}
\input{flac/algorithms/encode_lp_coeffs}

\clearpage

\subsubsection{LP Coefficient Calculation Example}
Given a maximum LPC order of 3 and 4 autocorrelation values:
{\relsize{-1}
  \begin{align*}
    \kappa_0 &\leftarrow \textsf{autocorrelation}_1 \div \textsf{autocorrelation}_0 \\
    \textsf{LP coefficient}_{0~0} &\leftarrow \kappa_0 \\
    \textsf{error}_0 &\leftarrow \textsf{autocorrelation}_0 \times (1 - {\kappa_0} ^ 2) \\
    i &= 1 \\
    q_1 &\leftarrow \textsf{autocorrelation}_2 - (\textsf{LP coefficient}_{0~0} \times \textsf{autocorrelation}_{1}) \\
    \kappa_1 &\leftarrow q_1 \div error_0 \\
    \textsf{LP coefficient}_{1~0} &\leftarrow \textsf{LP coefficient}_{0~0} - (\kappa_1 \times \textsf{LP coefficient}_{0~0}) \\
    \textsf{LP coefficient}_{1~1} &\leftarrow \kappa_1 \\
    \textsf{error}_1 &\leftarrow \textsf{error}_0 \times (1 - {\kappa_1} ^ 2) \\
    i &= 2 \\
    q_2 &\leftarrow \textsf{autocorrelation}_3 - (\textsf{LP coefficient}_{1~0} \times \textsf{autocorrelation}_{2} + \textsf{LP coefficient}_{1~1} \times \textsf{autocorrelation}_{1}) \\
    \kappa_2 &\leftarrow q_2 \div \textsf{error}_1 \\
    \textsf{LP coefficient}_{2~0} &\leftarrow \textsf{LP coefficient}_{1~0} - (\kappa_2 \times \textsf{LP coefficient}_{1~1}) \\
    \textsf{LP coefficient}_{2~1} &\leftarrow \textsf{LP coefficient}_{1~1} - (\kappa_2 \times \textsf{LP coefficient}_{1~0}) \\
    \textsf{LP coefficient}_{2~2} &\leftarrow \kappa_2 \\
    \textsf{error}_2 &\leftarrow \textsf{error}_1 \times (1 - {\kappa_2} ^ 2) \\
\end{align*}
}
\par
\noindent
With \textsf{autocorrelation} values: \texttt{[3455.53, 3130.76, 2590.90, 2055.80]}
{\relsize{-1}
  \begin{align*}
    \kappa_0 &\leftarrow 3130.76 \div 3455.53 = 0.906 \\
    \textsf{LP coefficient}_{0~0} &\leftarrow \textbf{0.906} \\
    \textsf{error}_0 &\leftarrow 3455.53 \times (1 - {0.906} ^ 2) = \textbf{619.107} \\
    i &= 1 \\
    q_1 &\leftarrow 2590.90 - (0.906 \times 3130.76) = -245.569 \\
    \kappa_1 &\leftarrow -245.569 \div 619.107 = -0.397 \\
    \textsf{LP coefficient}_{1~0} &\leftarrow 0.906 - (-0.397 \times 0.906) = \textbf{1.266} \\
    \textsf{LP coefficient}_{1~1} &\leftarrow \textbf{-0.397} \\
    \textsf{error}_1 &\leftarrow 619.107 \times (1 - {-0.397} ^ 2) = \textbf{521.530} \\
    i &= 2 \\
    q_2 &\leftarrow 2055.80 - (1.266 \times 2590.90 + -0.397 \times 3130.76) = 18.632 \\
    \kappa_2 &\leftarrow 18.632 \div 521.53 = 0.036 \\
    \textsf{LP coefficient}_{2~0} &\leftarrow 1.266 - (0.036 \times -0.397) = \textbf{1.28} \\
    \textsf{LP coefficient}_{2~1} &\leftarrow -0.397 - (0.036 \times 1.266) = \textbf{-0.443} \\
    \textsf{LP coefficient}_{2~2} &\leftarrow \textbf{0.036} \\
    \textsf{error}_2 &\leftarrow 521.53 \times (1 - {0.036} ^ 2) = \textbf{520.854} \\
  \end{align*}
}

\clearpage

\subsubsection{Estimating Best Order}
\label{flac:estimate_best_order}
\input{flac/algorithms/encode_best_order}

\clearpage

\subsubsection{Estimating Best Order Example}

Given the error values \texttt{[619.107, 521.530, 520.854]},
a block size of 10, 16 bits per sample, a QLP precision of 12 and maximum LPC order of 3:
\begin{align*}
  \textsf{error scale} &\leftarrow \frac{({\log_e 2}) ^ 2}{10 \times 2} = 0.024 \\
  i &\leftarrow 0 \\
  o &\leftarrow 0 + 1 = 1 \\
  \textsf{header bits}_1 &\leftarrow 1 \times (16 + 12) = 28 \\
  \textsf{bits per residual}_1 &\leftarrow \frac{\log_e(619.107 \times 0.024)}{({\log_e 2}) \times 2} = 1.947 \\
  \textsf{subframe bits}_1 &\leftarrow 28 + 1.947 \times (10 - 1) = \textbf{45.523} \\
  i &\leftarrow 1 \\
  o &\leftarrow 1 + 1 = 2 \\
  \textsf{header bits}_2 &\leftarrow 2 \times (16 + 12) = 56 \\
  \textsf{bits per residual}_2 &\leftarrow \frac{\log_e(521.530 \times 0.024)}{({\log_e 2}) \times 2} = 1.823 \\
  \textsf{subframe bits}_2 &\leftarrow 56 + 1.823 \times (10 - 2) = \textbf{70.584} \\
  i &\leftarrow 2 \\
  o &\leftarrow 2 + 1 = 3 \\
  \textsf{header bits}_3 &\leftarrow 3 \times (16 + 12) = 84 \\
  \textsf{bits per residual}_3 &\leftarrow \frac{\log_e(520.854 \times 0.024)}{({\log_e 2}) \times 2} = 1.822 \\
  \textsf{subframe bits}_3 &\leftarrow 84 + 1.822 \times (10 - 3) = \textbf{96.754} \\
\end{align*}
\par
\noindent
Since the $\textsf{subframe bits}_1$ value of 45.523 is the smallest,
the best LPC order to use is 1.

\clearpage

\subsubsection{Quantizing LP Coefficients}
\label{flac:quantize_lp_coeffs}
{\relsize{-1}
  \input{flac/algorithms/encode_quantize_coeffs}
}

\clearpage

\subsubsection{Quantizing LP Coefficients Example}

Given the $\textsf{LP coefficient}_3$ \texttt{[1.280, -0.443, 0.036]},
an \textsf{order} of \texttt{3} and a \textsf{QLP precision} \texttt{12}:
\begin{align*}
l &\leftarrow 1.280 \\
\textsf{QLP shift} &\leftarrow 12 - \lfloor \log_2(1.280) \rfloor - 2 = 10 \\
\textsf{QLP max} &\leftarrow 2 ^ {12 - 1} - 1 = 2047 \\
\textsf{QLP min} &\leftarrow -(2 ^ {12 - 1}) = -2048 \\
\textsf{error} &\leftarrow 0.0 \\
i &= 0 \\
\textsf{error} &\leftarrow 0.0 + 1.280 \times 2 ^ {10} = 1310.72 \\
\textsf{QLP coefficient}_0 &\leftarrow 1311 \\
\textsf{error} &\leftarrow 1310.72 - 1311 = -0.28 \\
i &= 1 \\
\textsf{error} &\leftarrow -0.28 + -0.443 \times 2 ^ {10} = -453.912 \\
\textsf{QLP coefficient}_1 &\leftarrow -454 \\
\textsf{error} &\leftarrow -453.912 - -454 = 0.088 \\
i &= 2 \\
\textsf{error} &\leftarrow 0.088 + 0.036 \times 2 ^ {10} = 36.952 \\
\textsf{QLP coefficient}_2 &\leftarrow 37 \\
\textsf{error} &\leftarrow 36.952 - 37 = -0.048 \\
\end{align*}
\par
\noindent
Resulting in the QLP coefficients \texttt{1311, -454, 37}
and a QLP shift of \texttt{10}.
These values, in addition to QLP precision,
are inserted directly into a desired QLP subframe header
and are also used to calculate its residuals.

\clearpage

\subsection{Writing an LPC Subframe}
\label{flac:write_lpc_subframe}
{\relsize{-1}
  \input{flac/algorithms/write_lpc_subframe}
}
\begin{figure}[h]
\includegraphics{flac/figures/lpc.pdf}
\end{figure}

\clearpage

\subsubsection{LPC Subframe Residuals Calculation Example}
{\relsize{-1}
  \begin{tabular}{rl}
    \textsf{samples} : & \texttt{[18, 20, 26, 24, 24, 23, 21, 24, 23, 20]} \\
    \textsf{block size} : & 10 \\
    \textsf{subframe's bits per sample} : & 16 \\
    \textsf{wasted BPS} : & 0 \\
    \textsf{LPC order} : & \texttt{3} \\
    \textsf{QLP precision} : &\texttt{12} \\
    \textsf{QLP shift needed} : & \texttt{10} \\
    \textsf{QLP coefficients} : & \texttt{[1311, -454, 37]} \\
  \end{tabular}
  \newline
  \begin{align*}
    \textsf{residual}_0 &\leftarrow 24 - \left\lfloor\frac{(1311 \times 26) + (-454 \times 20) + (37 \times 18)}{2 ^ {10}}\right\rfloor = -1 \\
    \textsf{residual}_1 &\leftarrow 24 - \left\lfloor\frac{(1311 \times 24) + (-454 \times 26) + (37 \times 20)}{2 ^ {10}}\right\rfloor = 5 \\
    \textsf{residual}_2 &\leftarrow 23 - \left\lfloor\frac{(1311 \times 24) + (-454 \times 24) + (37 \times 26)}{2 ^ {10}}\right\rfloor = 2 \\
    \textsf{residual}_3 &\leftarrow 21 - \left\lfloor\frac{(1311 \times 23) + (-454 \times 24) + (37 \times 24)}{2 ^ {10}}\right\rfloor = 2 \\
    \textsf{residual}_4 &\leftarrow 24 - \left\lfloor\frac{(1311 \times 21) + (-454 \times 23) + (37 \times 24)}{2 ^ {10}}\right\rfloor = 7 \\
    \textsf{residual}_5 &\leftarrow 23 - \left\lfloor\frac{(1311 \times 24) + (-454 \times 21) + (37 \times 23)}{2 ^ {10}}\right\rfloor = 1 \\
    \textsf{residual}_6 &\leftarrow 20 - \left\lfloor\frac{(1311 \times 23) + (-454 \times 24) + (37 \times 21)}{2 ^ {10}}\right\rfloor = 1
  \end{align*}
  Leading to a final set of 7 residual values: \texttt{[-1, 5, 2, 2, 7, 1, 1]}.
  Encoding them to a residual block, our final LPC subframe is:
}
\begin{figure}[h]
\includegraphics{flac/figures/lpc-parse2.pdf}
\end{figure}
