%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.

\subsection{Channel Decorrelation}
\label{wavpack:decorrelate_channels}
\input{wavpack/algorithms/decorrelate_channels}

\clearpage

\subsubsection{Visualizing Decorrelation Passes}

This is an example of a relatively small set of residuals
being transformed back into a set of samples
over the course of 5 decorrelation passes.
What's important to note is that each pass
adjusts the residual values' overall range.
The number of these passes and their decorrelation terms
are what separate high levels of WavPack compression from low levels.

\begin{figure}[h]
  \subfloat{
    \includegraphics{wavpack/figures/decorrelation0.pdf}
  }
  \subfloat{
    \includegraphics{wavpack/figures/decorrelation1.pdf}
  }
  \newline
  \subfloat{
    \includegraphics{wavpack/figures/decorrelation2.pdf}
  }
  \subfloat{
    \includegraphics{wavpack/figures/decorrelation3.pdf}
  }
  \newline
  \subfloat{
    \includegraphics{wavpack/figures/decorrelation4.pdf}
  }
  \subfloat{
    \includegraphics{wavpack/figures/decorrelation5.pdf}
  }
\end{figure}


\clearpage

\subsubsection{1 Channel Decorrelation Pass}
\label{wavpack:decorr_pass_1ch}
{\relsize{-1}
  \input{wavpack/algorithms/decorrelate_1ch}
}

\clearpage

\subsubsection{2 Channel Decorrelation Pass}
\label{wavpack:decorr_pass_2ch}
{\relsize{-1}
  \input{wavpack/algorithms/decorrelate_2ch}
}

\clearpage

\subsubsection{2 Channel Decorrelation Pass, Term -1}
\label{wavpack:decorr_pass_2ch_1}
{\relsize{-1}
  \input{wavpack/algorithms/decorrelate_2ch_1}
}

\subsubsection{2 Channel Decorrelation Pass, Term -2}
\label{wavpack:decorr_pass_2ch_2}
{\relsize{-1}
  \input{wavpack/algorithms/decorrelate_2ch_2}
}

\clearpage

\subsubsection{2 Channel Decorrelation Pass, Term -3}
\label{wavpack:decorr_pass_2ch_3}
{\relsize{-1}
  \input{wavpack/algorithms/decorrelate_2ch_3}
}

\subsubsection{Update Weight}
\label{wavpack:update_weight}
{\relsize{-1}
  \input{wavpack/algorithms/update_weight}
}

\clearpage

\subsubsection{Channel Decorrelation Example}
Given the values from the \VAR{Decorrelation Terms},
\VAR{Decorrelation Weights} and \VAR{Decorrelation Samples}
sub blocks:
\begin{figure}[h]
{\relsize{-1}
  \subfloat{
    \begin{tabular}{|r|r|r|}
      \multicolumn{3}{c}{Decorrelation Terms} \\
      \hline
      $p$ & $\textsf{term}_p$ & $\textsf{delta}_p$ \\
      \hline
      4 & 18 & 2 \\
      3 & 18 & 2 \\
      2 & 2 & 2 \\
      1 & 17 & 2 \\
      0 & 3 & 2 \\
      \hline
    \end{tabular}
  }
  \subfloat{
    \begin{tabular}{|r|r|r|}
      \multicolumn{3}{c}{Decorrelation Weights} \\
      \hline
      $p$ & $\textsf{weight}_{p~0}$ & $\textsf{weight}_{p~1}$ \\
      \hline
      4 & 48 & 48 \\
      3 & 48 & 48 \\
      2 & 32 & 32 \\
      1 & 48 & 48 \\
      0 & 16 & 24 \\
      \hline
    \end{tabular}
  }
  \subfloat{
    \begin{tabular}{|r|r|r|}
      \multicolumn{3}{c}{Decorrelation Samples} \\
      \hline
      $p$ & $\textsf{sample}_{p~0~s}$ & $\textsf{sample}_{p~1~s}$ \\
      \hline
      4 & \texttt{[-73, -78]} & \texttt{[28, 26]} \\
      3 & \texttt{[0, 0]} & \texttt{[0, 0]} \\
      2 & \texttt{[0, 0]} & \texttt{[0, 0]} \\
      1 & \texttt{[0, 0]} & \texttt{[0, 0]} \\
      0 & \texttt{[0, 0, 0]} & \texttt{[0, 0, 0]} \\
      \hline
    \end{tabular}
  }
}
\end{figure}
\par
\noindent
we combine them into a single set of arguments for each decorrelation pass:
\begin{table}[h]
{\relsize{-1}
  \begin{tabular}{|r|r|r|r|r|r|}
    \hline
    & $\textbf{pass}_0$ & $\textbf{pass}_1$ & $\textbf{pass}_2$ &
    $\textbf{pass}_3$ & $\textbf{pass}_4$ \\
    \hline
    $\textsf{term}_p$ & 3 & 17 & 2 & 18 & 18 \\
    $\textsf{delta}_p$ & 2 & 2 & 2 & 2 & 2 \\
    $\textsf{weight}_{p~0}$ & 16 & 48 & 32 & 48 & 48 \\
    $\textsf{sample}_{p~0~s}$ & \texttt{[0, 0, 0]} & \texttt{[0, 0]} &
    \texttt{[0, 0]} & \texttt{[0, 0]} & \texttt{[-73, -78]} \\
    $\textsf{weight}_{p~1}$ & 24 & 48 & 32 & 48 & 48 \\
    $\textsf{sample}_{p~1~s}$ & \texttt{[0, 0, 0]} & \texttt{[0, 0]} &
    \texttt{[0, 0]} & \texttt{[0, 0]} & \texttt{[28, 26]} \\
    \hline
  \end{tabular}
}
\end{table}
\par
\noindent
which we apply to the residuals from the bitstream sub-block:
\par
\noindent
{\relsize{-1}
  \begin{tabular}{|r|r|r|r|r|r|}
    \hline
    $\textsf{residual}_{0~i}$ &
    after $\textbf{pass}_0$ &
    after $\textbf{pass}_1$ &
    after $\textbf{pass}_2$ &
    after $\textbf{pass}_3$ &
    after $\textbf{pass}_4$ \\
    \hline
    -61 & -61 & -61 & -61 & -61 & -64 \\
    -33 & -33 & -39 & -39 & -43 & -46 \\
    -18 & -18 & -19 & -21 & -23 & -25 \\
    1 & 0 & 0 & -1 & -2 & -3 \\
    20 & 20 & 21 & 20 & 20 & 20 \\
    35 & 35 & 37 & 37 & 39 & 41 \\
    50 & 50 & 53 & 54 & 57 & 60 \\
    62 & 62 & 66 & 67 & 71 & 75 \\
    68 & 68 & 73 & 75 & 80 & 85 \\
    71 & 72 & 77 & 79 & 84 & 90 \\
    \hline
    \hline
    $\textsf{residual}_{1~i}$ &
    after $\textbf{pass}_0$ &
    after $\textbf{pass}_1$ &
    after $\textbf{pass}_2$ &
    after $\textbf{pass}_3$ &
    after $\textbf{pass}_4$ \\
    \hline
    31 & 31 & 31 & 31 & 31 & 32 \\
    32 & 32 & 35 & 35 & 37 & 39 \\
    36 & 36 & 38 & 39 & 41 & 43 \\
    37 & 38 & 40 & 41 & 43 & 45 \\
    35 & 36 & 38 & 39 & 41 & 44 \\
    31 & 32 & 34 & 36 & 38 & 40 \\
    25 & 26 & 28 & 30 & 32 & 34 \\
    18 & 19 & 20 & 21 & 23 & 25 \\
    10 & 11 & 12 & 13 & 14 & 15 \\
    0 & 1 & 1 & 2 & 3 & 4 \\
    \hline
  \end{tabular}
}
\par
\noindent
Resulting in final decorrelated samples:
\newline
\begin{tabular}{rr}
$\textsf{channel}_0$ : & \texttt{[-64,~-46,~-25,~-3,~20,~41,~60,~75,~85,~90]} \\
$\textsf{channel}_1$ : & \texttt{[~32,~~39,~~43,~45,~44,~40,~34,~25,~15,~~4]} \\
\end{tabular}

\clearpage

{\relsize{-2}
\begin{tabular}{r||r|>{$}r<{$}|>{$}r<{$}|>{$}r<{$}|>{$}r<{$}}
%% \multicolumn{5}{c}{\textbf{pass 0} - term 3 - delta 2 - weight 16} \\
& $i$ & \textsf{correlated}_i & \textsf{temp}_i & \textsf{decorrelated}_{i + 3} & \textsf{weight}_{i + 1} \\
\hline
\multirow{10}{1em}{\begin{sideways}$\textbf{pass}_0$ - term 3\end{sideways}}
& 0 & -61 & &
\lfloor(16 \times 0 + 2 ^ 9) \div 2 ^ {10}\rfloor - 61 = -61 &
16 + 0 = 16
\\
& 1 & -33 & &
\lfloor(16 \times 0 + 2 ^ 9) \div 2 ^ {10}\rfloor - 33 = -33 &
16 + 0 = 16
\\
& 2 & -18 & &
\lfloor(16 \times 0 + 2 ^ 9) \div 2 ^ {10}\rfloor - 18 = -18 &
16 + 0 = 16
\\
& 3 & 1 & &
\lfloor(16 \times -61 + 2 ^ 9) \div 2 ^ {10}\rfloor + 1 = 0 &
16 - 2 = 14
\\
& 4 & 20 & &
\lfloor(14 \times -33 + 2 ^ 9) \div 2 ^ {10}\rfloor + 20 = 20 &
14 - 2 = 12
\\
& 5 & 35 & &
\lfloor(12 \times -18 + 2 ^ 9) \div 2 ^ {10}\rfloor + 35 = 35 &
12 - 2 = 10
\\
& 6 & 50 & &
\lfloor(10 \times 0 + 2 ^ 9) \div 2 ^ {10}\rfloor + 50 = 50 &
10 + 0 = 10
\\
& 7 & 62 & &
\lfloor(10 \times 20 + 2 ^ 9) \div 2 ^ {10}\rfloor + 62 = 62 &
10 + 2 = 12
\\
& 8 & 68 & &
\lfloor(12 \times 35 + 2 ^ 9) \div 2 ^ {10}\rfloor + 68 = 68 &
12 + 2 = 14
\\
& 9 & 71 & &
\lfloor(14 \times 50 + 2 ^ 9) \div 2 ^ {10}\rfloor + 71 = 72 &
14 + 2 = 16
\\
\hline
\hline
%% \multicolumn{5}{c}{\textbf{pass 1 } - term 17 - delta 2 - weight 48} \\
& $i$ & \textsf{correlated}_i & \textsf{temp}_i & \textsf{decorrelated}_{i + 2} & \textsf{weight}_{i + 1} \\
\hline
\multirow{10}{1em}{\begin{sideways}$\textbf{pass}_1$ - term 17\end{sideways}}
& 0 & -61 &
2 \times 0 - 0 = 0 &
\lfloor(48 \times 0 + 2 ^ 9) \div 2 ^ {10}\rfloor - 61 = -61 &
48 + 0 = 48
\\
& 1 & -33 &
2 \times -61 - 0 = -122 &
\lfloor(48 \times -122 + 2 ^ 9) \div 2 ^ {10}\rfloor - 33 = -39 &
48 + 2 = 50
\\
& 2 & -18 &
2 \times -39 + 61 = -17 &
\lfloor(50 \times -17 + 2 ^ 9) \div 2 ^ {10}\rfloor - 18 = -19 &
50 + 2 = 52
\\
& 3 & 0 &
2 \times -19 + 39 = 1 &
\lfloor(52 \times 1 + 2 ^ 9) \div 2 ^ {10}\rfloor + 0 = 0 &
52 + 0 = 52
\\
& 4 & 20 &
2 \times 0 + 19 = 19 &
\lfloor(52 \times 19 + 2 ^ 9) \div 2 ^ {10}\rfloor + 20 = 21 &
52 + 2 = 54
\\
& 5 & 35 &
2 \times 21 - 0 = 42 &
\lfloor(54 \times 42 + 2 ^ 9) \div 2 ^ {10}\rfloor + 35 = 37 &
54 + 2 = 56
\\
& 6 & 50 &
2 \times 37 - 21 = 53 &
\lfloor(56 \times 53 + 2 ^ 9) \div 2 ^ {10}\rfloor + 50 = 53 &
56 + 2 = 58
\\
& 7 & 62 &
2 \times 53 - 37 = 69 &
\lfloor(58 \times 69 + 2 ^ 9) \div 2 ^ {10}\rfloor + 62 = 66 &
58 + 2 = 60
\\
& 8 & 68 &
2 \times 66 - 53 = 79 &
\lfloor(60 \times 79 + 2 ^ 9) \div 2 ^ {10}\rfloor + 68 = 73 &
60 + 2 = 62
\\
& 9 & 72 &
2 \times 73 - 66 = 80 &
\lfloor(62 \times 80 + 2 ^ 9) \div 2 ^ {10}\rfloor + 72 = 77 &
62 + 2 = 64
\\
\hline
\hline
%% \multicolumn{5}{c}{\textbf{pass 2 } - term 2 - delta 2 - weight 32} \\
& $i$ & \textsf{correlated}_i & \textsf{temp}_i & \textsf{decorrelated}_{i + 2} & \textsf{weight}_{i + 1} \\
\hline
\multirow{10}{1em}{\begin{sideways}$\textbf{pass}_2$ - term 2\end{sideways}}
& 0 & -61 & &
\lfloor(32 \times 0 + 2 ^ 9) \div 2 ^ {10}\rfloor - 61 = -61 &
32 + 0 = 32
\\
& 1 & -39 & &
\lfloor(32 \times 0 + 2 ^ 9) \div 2 ^ {10}\rfloor - 39 = -39 &
32 + 0 = 32
\\
& 2 & -19 & &
\lfloor(32 \times -61 + 2 ^ 9) \div 2 ^ {10}\rfloor - 19 = -21 &
32 + 2 = 34
\\
& 3 & 0 & &
\lfloor(34 \times -39 + 2 ^ 9) \div 2 ^ {10}\rfloor + 0 = -1 &
34 + 0 = 34
\\
& 4 & 21 & &
\lfloor(34 \times -21 + 2 ^ 9) \div 2 ^ {10}\rfloor + 21 = 20 &
34 - 2 = 32
\\
& 5 & 37 & &
\lfloor(32 \times -1 + 2 ^ 9) \div 2 ^ {10}\rfloor + 37 = 37 &
32 - 2 = 30
\\
& 6 & 53 & &
\lfloor(30 \times 20 + 2 ^ 9) \div 2 ^ {10}\rfloor + 53 = 54 &
30 + 2 = 32
\\
& 7 & 66 & &
\lfloor(32 \times 37 + 2 ^ 9) \div 2 ^ {10}\rfloor + 66 = 67 &
32 + 2 = 34
\\
& 8 & 73 & &
\lfloor(34 \times 54 + 2 ^ 9) \div 2 ^ {10}\rfloor + 73 = 75 &
34 + 2 = 36
\\
& 9 & 77 & &
\lfloor(36 \times 67 + 2 ^ 9) \div 2 ^ {10}\rfloor + 77 = 79 &
36 + 2 = 38
\\
\hline
\hline
%% \multicolumn{5}{c}{\textbf{pass 3 } - term 18 - delta 2 - weight 48} \\
& $i$ & \textsf{correlated}_i & \textsf{temp}_i & \textsf{decorrelated}_{i + 2} & \textsf{weight}_{i + 1} \\
\hline
\multirow{10}{1em}{\begin{sideways}$\textbf{pass}_3$ - term 18\end{sideways}}
& 0 & -61 &
\lfloor(3 \times 0 - 0) \div 2\rfloor = 0 &
\lfloor(48 \times 0 + 2 ^ 9) \div 2 ^ {10}\rfloor - 61 = -61 &
48 + 0 = 48
\\
& 1 & -39 &
\lfloor(3 \times -61 - 0) \div 2\rfloor = -92 &
\lfloor(48 \times -92 + 2 ^ 9) \div 2 ^ {10}\rfloor - 39 = -43 &
48 + 2 = 50
\\
& 2 & -21 &
\lfloor(3 \times -43 + 61) \div 2\rfloor = -34 &
\lfloor(50 \times -34 + 2 ^ 9) \div 2 ^ {10}\rfloor - 21 = -23 &
50 + 2 = 52
\\
& 3 & -1 &
\lfloor(3 \times -23 + 43) \div 2\rfloor = -13 &
\lfloor(52 \times -13 + 2 ^ 9) \div 2 ^ {10}\rfloor - 1 = -2 &
52 + 2 = 54
\\
& 4 & 20 &
\lfloor(3 \times -2 + 23) \div 2\rfloor = 8 &
\lfloor(54 \times 8 + 2 ^ 9) \div 2 ^ {10}\rfloor + 20 = 20 &
54 + 2 = 56
\\
& 5 & 37 &
\lfloor(3 \times 20 + 2) \div 2\rfloor = 31 &
\lfloor(56 \times 31 + 2 ^ 9) \div 2 ^ {10}\rfloor + 37 = 39 &
56 + 2 = 58
\\
& 6 & 54 &
\lfloor(3 \times 39 - 20) \div 2\rfloor = 48 &
\lfloor(58 \times 48 + 2 ^ 9) \div 2 ^ {10}\rfloor + 54 = 57 &
58 + 2 = 60
\\
& 7 & 67 &
\lfloor(3 \times 57 - 39) \div 2\rfloor = 66 &
\lfloor(60 \times 66 + 2 ^ 9) \div 2 ^ {10}\rfloor + 67 = 71 &
60 + 2 = 62
\\
& 8 & 75 &
\lfloor(3 \times 71 - 57) \div 2\rfloor = 78 &
\lfloor(62 \times 78 + 2 ^ 9) \div 2 ^ {10}\rfloor + 75 = 80 &
62 + 2 = 64
\\
& 9 & 79 &
\lfloor(3 \times 80 - 71) \div 2\rfloor = 84 &
\lfloor(64 \times 84 + 2 ^ 9) \div 2 ^ {10}\rfloor + 79 = 84 &
64 + 2 = 66
\\
\hline
\hline
%% \multicolumn{5}{c}{\textbf{pass 4 } - term 18 - delta 2 - weight 48} \\
& $i$ & \textsf{correlated}_i & \textsf{temp}_i & \textsf{decorrelated}_{i + 2} & \textsf{weight}_{i + 1} \\
\hline
\multirow{10}{1em}{\begin{sideways}$\textbf{pass}_4$ - term 18\end{sideways}}
& 0 & -61 &
\lfloor(3 \times -73 + 78) \div 2\rfloor = -71 &
\lfloor(48 \times -71 + 2 ^ 9) \div 2 ^ {10}\rfloor - 61 = -64 &
48 + 2 = 50
\\
& 1 & -43 &
\lfloor(3 \times -64 + 73) \div 2\rfloor = -60 &
\lfloor(50 \times -60 + 2 ^ 9) \div 2 ^ {10}\rfloor - 43 = -46 &
50 + 2 = 52
\\
& 2 & -23 &
\lfloor(3 \times -46 + 64) \div 2\rfloor = -37 &
\lfloor(52 \times -37 + 2 ^ 9) \div 2 ^ {10}\rfloor - 23 = -25 &
52 + 2 = 54
\\
& 3 & -2 &
\lfloor(3 \times -25 + 46) \div 2\rfloor = -15 &
\lfloor(54 \times -15 + 2 ^ 9) \div 2 ^ {10}\rfloor - 2 = -3 &
54 + 2 = 56
\\
& 4 & 20 &
\lfloor(3 \times -3 + 25) \div 2\rfloor = 8 &
\lfloor(56 \times 8 + 2 ^ 9) \div 2 ^ {10}\rfloor + 20 = 20 &
56 + 2 = 58
\\
& 5 & 39 &
\lfloor(3 \times 20 + 3) \div 2\rfloor = 31 &
\lfloor(58 \times 31 + 2 ^ 9) \div 2 ^ {10}\rfloor + 39 = 41 &
58 + 2 = 60
\\
& 6 & 57 &
\lfloor(3 \times 41 - 20) \div 2\rfloor = 51 &
\lfloor(60 \times 51 + 2 ^ 9) \div 2 ^ {10}\rfloor + 57 = 60 &
60 + 2 = 62
\\
& 7 & 71 &
\lfloor(3 \times 60 - 41) \div 2\rfloor = 69 &
\lfloor(62 \times 69 + 2 ^ 9) \div 2 ^ {10}\rfloor + 71 = 75 &
62 + 2 = 64
\\
& 8 & 80 &
\lfloor(3 \times 75 - 60) \div 2\rfloor = 82 &
\lfloor(64 \times 82 + 2 ^ 9) \div 2 ^ {10}\rfloor + 80 = 85 &
64 + 2 = 66
\\
& 9 & 84 &
\lfloor(3 \times 85 - 75) \div 2\rfloor = 90 &
\lfloor(66 \times 90 + 2 ^ 9) \div 2 ^ {10}\rfloor + 84 = 90 &
66 + 2 = 68
\\
\end{tabular}
}
\begin{center}
$\text{channel}_0$ decorrelation passes
\end{center}
