#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 samples,block_size;
OUTPUT diff,energy,residual;
VAR samples "samples";
VAR block_size "block size";
VAR diff "diff";
VAR energy "energy";
VAR residual "residual";
VAR delta "delta";
VAR sum_ "sum";
for i <- -2 to block_size {
    /*negative delta indexes aren't returned*/;
    delta[1][i] <- samples[i] - samples[i - 1];
}
for i <- -1 to block_size {
    /*negative delta indexes aren't returned*/;
    delta[2][i] <- delta[1][i] - delta[1][i - 1];
}
for i <- 0 to block_size {
    delta[3][i] <- delta[2][i] - delta[2][i - 1];
}
for d <- 1 to 4 {
    sum_[d] <- sum i <- 0 to block_size - 1 {|delta[d][i]|};
}
if (sum_[1] < sum_[2]) and (sum_[1] < sum_[3]) {
    return 1, floor(log 2 {(sum_[1] / block_size)}) + 1, delta[1];
} elif sum_[2] < sum_[3] {
    return 2, floor(log 2 {(sum_[2] / block_size)}) + 1, delta[2];
} else {
    return 3, floor(log 2 {(sum_[3] / block_size)}) + 1, delta[3];
}
