1#This work is licensed under the
2#Creative Commons Attribution-Share Alike 3.0 United States License.
3#To view a copy of this license, visit
4#http://creativecommons.org/licenses/by-sa/3.0/us/ or send a letter to
5#Creative Commons,
6#171 Second Street, Suite 300,
7#San Francisco, California, 94105, USA.
8
9INPUT block_samples,channel,decorrelation_delta,weight,decorrelation_samples;
10OUTPUT correlated,weight,decorrelation_samples;
11VAR channel "channel";
12VAR block_samples "block samples";
13VAR correlated "correlated";
14VAR weight "weights";
15VAR decorrelation_delta "delta";
16VAR decorrelation_samples "samples";
17FUNC update_weight "update weight" "wavpack:update_weight";
18channel[0][(-1)] <- decorrelation_samples[1][0];
19channel[1][(-1)] <- decorrelation_samples[0][0];
20for i <- 0 to block_samples {
21    correlated[0][i] <-
22        channel[0][i] -
23        floor((weight[0] * channel[1][i] + 2 ^ 9) / 2 ^ 10);
24    correlated[1][i] <-
25        channel[1][i] -
26        floor((weight[1] * channel[0][(i - 1)] + 2 ^ 9) / 2 ^ 10);
27    weight[0] <- update_weight(channel[1][i],
28                               correlated[0][i],
29                               weight[0],
30                               decorrelation_delta);
31    weight[1] <- update_weight(channel[0][(i - 1)],
32                               correlated[1][i],
33                               weight[1],
34                               decorrelation_delta);
35}
36decorrelation_samples[0][0] <-
37    channel[1][(block_samples - 1)];
38decorrelation_samples[1][0] <-
39    channel[0][(block_samples - 1)];
40return correlated,
41       weight,
42       decorrelation_samples;
43