#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 block_samples,correlated,decorrelation_delta,
decorrelation_weights,decorrelation_samples;
OUTPUT decorrelated;
VAR block_samples "block samples";
VAR correlated "correlated";
VAR decorrelated "decorrelated";
VAR decorrelation_delta "decorrelation delta";
VAR decorrelation_weights "decorrelation weights";
VAR decorrelation_samples "decorrelation samples";
FUNC update_weight "update weight" "wavpack:update_weight";
decorrelated[0][(-1)] <- decorrelation_samples[1][0] /*not returned*/;
decorrelated[1][(-1)] <- decorrelation_samples[0][0] /*not returned*/;
for i <- 0 to block_samples {
    decorrelated[0][i] <- floor((decorrelation_weights[0] *
                                 decorrelated[1][(i - 1)] + 2 ^ 9) / 2 ^ 10) +
                                 correlated[0][i];
    decorrelated[1][i] <- floor((decorrelation_weights[1] *
                                 decorrelated[0][(i - 1)] + 2 ^ 9) / 2 ^ 10) +
                                 correlated[1][i];
    decorrelation_weights[0] <- update_weight(
        decorrelated[1][(i - 1)],
        correlated[0][i],
        decorrelation_weights[0],
        decorrelation_delta);
    decorrelation_weights[1] <- update_weight(
        decorrelated[0][(i - 1)],
        correlated[1][i],
        decorrelation_weights[1],
        decorrelation_delta);
}
return decorrelated;
