private static int quantizeCoefficients ( double [ ] coefficients , int [ ] dest , int order , int precision ) { assert ( precision >= 2 && precision <= 15 ) ; assert ( coefficients . length >= order + 1 ) ; assert ( dest . length >= order + 1 ) ; if ( precision < 2 || precision > 15 ) throw new IllegalArgumentException ( "Error! precision must be between 2 and 15, inclusive." ) ; int shiftApplied = 0 ; int maxValAllowed = ( 1 << ( precision - 2 ) ) - 1 ; //minus an extra bit for sign. int minValAllowed = - 1 * maxValAllowed - 1 ; double maxVal = 0 ; for ( int i = 1 ; i <= order ; i ++ ) { double temp = coefficients [ i ] ; if ( temp < 0 ) temp *= - 1 ; if ( temp > maxVal ) maxVal = temp ; } //find shift to use(by max value) for ( shiftApplied = 15 ; shiftApplied > 0 ; shiftApplied -- ) { int temp = ( int ) ( maxVal * ( 1 << shiftApplied ) ) ; if ( temp <= maxValAllowed ) break ; } if ( maxVal > maxValAllowed ) { //no shift should have been applied //ensure max value is not too large, cap all necessary // for ( int i = 1 ; i <= order ; i ++ ) { double temp = coefficients [ i ] ; if ( temp < 0 ) temp = temp * - 1 ; if ( temp > maxValAllowed ) { if ( coefficients [ i ] < 0 ) dest [ i ] = minValAllowed ; else dest [ i ] = maxValAllowed ; } else dest [ i ] = ( int ) coefficients [ i ] ; } } else { //shift and quantize all values by found shift for ( int i = 1 ; i <= order ; i ++ ) { double temp = coefficients [ i ] * ( 1 << shiftApplied ) ; temp = ( temp > 0 ) ? temp + 0.5 : temp - 0.5 ; dest [ i ] = ( int ) temp ; } } return shiftApplied ; }
Quantize coefficients to integer values of the given precision and calculate the shift needed .