#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 channels,block_size,uncorrelated;
OUTPUT correlated;
VAR channels "channels";
VAR block_size "block size";
VAR uncorrelated "uncorrelated";
VAR correlated "correlated";
if channels == 1 {
    for i <- 0 to block_size {
        correlated[0][i] <- uncorrelated[0][i];
    }
} else {
    for c <- 0 to (channels - 1) {
        for i <- 0 to block_size {
            correlated[c][i] <- uncorrelated[(c + 1)][i] - uncorrelated[c][i];
        }
    }
    c <- channels - 1;
    for i <- 0 to block_size {
        if correlated[(c - 1)][i] >= 0 /*round toward zero*/ {
            correlated[c][i] <-
                uncorrelated[c][i] - floor(correlated[(c - 1)][i] / 2);
        } else {
            correlated[c][i] <-
                uncorrelated[c][i] - ceil(correlated[(c - 1)][i] / 2);
        }
    }
}
return correlated;
