%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{Residual Encoding}
\label{flac:write_residual_block}
{\relsize{-1}
  \input{flac/algorithms/encode_residual}
}
\begin{figure}[h]
\includegraphics{flac/figures/residual.pdf}
\end{figure}

\clearpage

\subsubsection{Calculating Residual Partitions}
\label{flac:calculate_residual_partitions}
{\relsize{-1}
  \input{flac/algorithms/calculate_residual_partitions}
}

\clearpage

\subsubsection{Residual Encoding Example}
Given a set of residuals \texttt{[2, 6, -2, 0, -1, -2, 3, -1, -3]},
block size of 10 and predictor order of 1:
{\relsize{-1}
  \begin{align*}
  \intertext{$\text{partition order}~o = 0$:}
  \textsf{partition start}_{0~0} &\leftarrow 0 \\
  \textsf{partition samples}_{0~0} &\leftarrow 10 \div 2 ^ 0 - 1 = 9 \\
  \textsf{partition}_{0~0} &\leftarrow \texttt{[2, 6, -2, 0, -1, -2, 3, -1, -3]} \\
  \textsf{partition sum}_{0~0} &\leftarrow 2 + 6 + 2 + 0 + 1 + 2 + 3 + 1 + 3 = 20 \\
  \textsf{Rice}_{0~0} &\leftarrow \lfloor\log_2 (20 \div 9) \rfloor = 1 \\
  \textsf{partition size}_{0~0} &\leftarrow 4 + ((1 + 1) \times 9) + \left\lfloor\frac{20}{2 ^ {1 - 1}}\right\rfloor - \left\lfloor\frac{9}{2}\right\rfloor = \textbf{38} \\
  \intertext{$\text{partition order}~o = 1$:}
  \textsf{partition start}_{1~0} &\leftarrow 0 \\
  \textsf{partition samples}_{1~0} &\leftarrow 10 \div 2 ^ 1 - 1 = 4 \\
  \textsf{partition}_{1~0} &\leftarrow \texttt{[2, 6, -2, 0]} \\
  \textsf{partition sum}_{1~0} &\leftarrow 2 + 6 + 2 + 0 = 10 \\
  \textsf{Rice}_{1~0} &\leftarrow \lfloor\log_2 (10 \div 4) \rfloor = 1 \\
  \textsf{partition size}_{1~0} &\leftarrow 4 + ((1 + 1) \times 4) + \left\lfloor\frac{10}{2 ^ {1 - 1}}\right\rfloor - \left\lfloor\frac{4}{2}\right\rfloor = \textbf{20} \\
  \textsf{partition start}_{1~1} &\leftarrow (1 \times 10 \div 2 ^ {1}) - 1 = 4 \\
  \textsf{partition samples}_{1~1} &\leftarrow 10 \div 2 ^ 1 = 5 \\
  \textsf{partition}_{1~1} &\leftarrow \texttt{[-1, -2, 3, -1, -3]} \\
  \textsf{partition sum}_{1~1} &\leftarrow 1 + 2 + 3 + 1 + 3 = 10 \\
  \textsf{Rice}_{1~1} &\leftarrow \lfloor\log_2 (10 \div 5) \rfloor = 1 \\
  \textsf{partition size}_{1~1} &\leftarrow 4 + ((1 + 1) \times 5) + \left\lfloor\frac{10}{2 ^ {1 - 1}}\right\rfloor - \left\lfloor\frac{5}{2}\right\rfloor = \textbf{22} \\
\end{align*}
\par
\noindent
Since block size of $10 \bmod 2 ^ 2 \neq 0$, we stop at partition order 1
because the list of residuals can't be divided equally into more partitions.
And because $\textsf{total partitions size}_0$ of 38 is smaller than
$\textsf{total partitions size}_1$ of 42 (20 + 22), we use partition order 0
to encode our residuals into a single partition with 9 residuals.
}
\begin{figure}[h]
  \includegraphics{flac/figures/residuals-enc-example.pdf}
\end{figure}
