#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 "list of LP coefficient lists, coefficient order";
OUTPUT "list of QLP coefficients as signed integers";
VAR LP_coeff "LP coefficient";
VAR order "coefficient order";
VAR error "error";
VAR QLP_min "QLP minimum";
VAR QLP_max "QLP maximum";
VAR QLP_coeff "QLP coefficient";
QLP_min <- 2 ^ 15 - 1;
QLP_max <- -(2 ^ 15);
error <- 0.0;
for i <- 0 to order {
    error <- error + LP_coeff[(order - 1)][i] * 2 ^ 9;
    c[i] <- round(error);
    if c[i] > QLP_max {
        QLP_coeff[i] <- QLP_max;
    } elif c[i] < QLP_min {
        QLP_coeff[i] <- QLP_min;
    } else {
        QLP_coeff[i] <- c[i];
    }
    error <- error - QLP_coeff[i];
}
return QLP_coeff;
