%Copyright (C) 2007-2015  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.

\section{Shorten Encoding}
As with decoding, one needs \texttt{unsigned}, \texttt{signed} and \texttt{long}
functions:

\subsubsection{Writing \texttt{unsigned}}
\label{shorten:write_unsigned}
{\relsize{-1}
  \input{shorten/algorithms/write_unsigned}
}


\subsubsection{Writing \texttt{signed}}
\label{shorten:write_signed}
{\relsize{-1}
  \input{shorten/algorithms/write_signed}
}


\subsubsection{Writing \texttt{long}}
\label{shorten:write_long}
{\relsize{-1}
  \input{shorten/algorithms/write_long}
}


\clearpage

\subsection{Encoding Shorten File}
{\relsize{-1}
  \input{shorten/algorithms/encode_shorten}
}

\clearpage

\subsection{Writing Shorten Header}
\label{shorten:write_header}
\input{shorten/algorithms/write_shorten_header}

\clearpage

\subsection{Encoding Audio Commands}
\label{shorten:encode_audio}
{\relsize{-1}
  \input{shorten/algorithms/encode_audio}
}


\clearpage

\subsection{Calculating Wasted Bits per Sample}
\label{shorten:calculate_wasted_bps}
\input{shorten/algorithms/calculate_wasted_bps}
where the \texttt{wasted} function is defined as:
\begin{equation*}
  \texttt{wasted}(x) =
  \begin{cases}
    \infty & \text{if } x = 0 \\
    0 & \text{if } x \bmod 2 = 1 \\
    1 + \texttt{wasted}(x \div 2) & \text{if } x \bmod 2 = 0 \\
  \end{cases}
\end{equation*}

\clearpage

\subsection{Computing Best \texttt{DIFF} Command, Energy and Residuals}
\label{shorten:compute_best_diff}
\input{shorten/algorithms/compute_diff}
\par
\noindent
Negative sample values are taken from the channel's previous samples,
or 0 if there are none.
Although negative delta values are needed for determining the next delta,
only the non-negative deltas are used for calculating the sums
and as returned residuals.

\clearpage

\subsubsection{Computing Best \texttt{DIFF} Command Example}
{\relsize{-1}
  \begin{tabular}{r|r|rrr}
    $i$ & $\textsf{sample}_i$ & $\textsf{delta}_{1~i}$ & $\textsf{delta}_{2~i}$ & $\textsf{delta}_{3~i}$ \\
    \hline
    \hline
    -3 & 0 & & & \\
    -2 & 0 & 0 & & \\
    -1 & 0 & 0 & 0 & \\
    \hline
    0 & 0 & 0 & 0 & 0 \\
    1 & 16 & 16 & 16 & 16 \\
    2 & 31 & 15 & -1 & -17 \\
    3 & 44 & 13 & -2 & -1 \\
    4 & 54 & 10 & -3 & -1 \\
    5 & 61 & 7 & -3 & 0 \\
    6 & 64 & 3 & -4 & -1 \\
    7 & 63 & -1 & -4 & 0 \\
    8 & 58 & -5 & -4 & 0 \\
    9 & 49 & -9 & -4 & 0 \\
    10 & 38 & -11 & -2 & 2 \\
    11 & 24 & -14 & -3 & -1 \\
    12 & 8 & -16 & -2 & 1 \\
    13 & -8 & -16 & 0 & 2 \\
    14 & -24 & -16 & 0 & 0 \\
    \hline
    \hline
    \multicolumn{2}{r}{$\textsf{sum}_d$} & 152 & 48 & 42 \\
  \end{tabular}
\vskip 1em
\par
\noindent
Since the $\textsf{sum}_3$ value of 42 is the smallest,
we'll use a \texttt{DIFF3} command.
The loop for calculating the energy value is:
\begin{align*}
\text{(block size) } 15 \times 2 ^ 0 &< 42 \text{ ($\textsf{sum}_3$)} \\
15 \times 2 ^ 1 &< 42 \\
15 \times 2 ^ 2 &> 42 \\
\end{align*}
Which means the best energy value to use is 1, the residuals are:
\newline
\texttt{[0, 16, -17, -1, -1, 0, -1, 0, 0, 0, 2, -1, 1, 2, 0]}
\newline
and the entire \texttt{DIFF3} command is encoded as:
}
\begin{figure}[h]
\includegraphics{shorten/figures/block1.pdf}
\end{figure}
