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

INPUT channel[0],channel[1],
sample_count,interlacing_shift,interlacing_leftweight;
OUTPUT "2 correlated channels of samples";
VAR channel "channel";
VAR sample_count "sample count";
VAR interlacing_shift "interlacing shift";
VAR interlacing_leftweight "interlacing leftweight";
VAR correlated "correlated";
for i <- 0 to sample_count {
   if interlacing_leftweight > 0 {
       correlated[1][i] <- channel[0][i] - channel[1][i];
       correlated[0][i] <- channel[1][i] +
          floor(frac(correlated[1][i] * interlacing_leftweight,
                     2 ^ interlacing_shift));

   } else {
       correlated[0][i] <- channel[0][i];
       correlated[1][i] <- channel[1][i];
   }
}
return correlated[0],correlated[1];
