001/* Class providing static functions for PtolemyII expression language 002 that operate exclusively on the fixed point data type. 003 004 Copyright (c) 1998-2013 The Regents of the University of California. 005 All rights reserved. 006 Permission is hereby granted, without written agreement and without 007 license or royalty fees, to use, copy, modify, and distribute this 008 software and its documentation for any purpose, provided that the above 009 copyright notice and the following two paragraphs appear in all copies 010 of this software. 011 012 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 013 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 014 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 015 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 016 SUCH DAMAGE. 017 018 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 019 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 020 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 021 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 022 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 023 ENHANCEMENTS, OR MODIFICATIONS. 024 025 PT_COPYRIGHT_VERSION_2 026 COPYRIGHTENDKEY 027 028 029 */ 030package ptolemy.data.expr; 031 032import ptolemy.math.FixPoint; 033import ptolemy.math.Precision; 034import ptolemy.math.Quantizer; 035 036/////////////////////////////////////////////////////////////////// 037//// FixPointFunctions 038 039/** 040 This class provides static functions for operating on Fixpoint numbers 041 in the Ptolemy II expression language. The added functionality is 042 <ul> 043 <li> Create a FixPoint for a double value with a particular 044 precision. The result is an instance of <i>FixPoint</i>. 045 For example: 046 <pre> 047 fix(5.34, 10, 4) 048 </pre> 049 creates a fixed point number with a total of 10 bits, 4 of which are 050 integer bits, representing the number 5.34. 051</li> 052 <li> Create a FixPointMatrix with entries that consist of instances of 053 FixPoint. Each entry in the fixed point matrix has 054 the same precision. For example, 055 <pre> 056 fix([ -.040609, -.001628, .17853, .37665, .37665, .17853, 057 -.001628, -.040609 ], 10, 2) 058 </pre> 059 creates a matrix where each entry has 10 bits, two of which are 060 integer bits. 061</li> 062 <li> Create a DoubleToken whose value is the quantized version of the 063 given double value. The value is quantized by converting it into a 064 fixed point value with a particular precision and then back again to 065 a double value. 066 For example, 067 <pre> 068 quantize(5.34, 10, 4) 069 </pre> 070 quantizes the number 5.34 to 10 bits of precision, 4 of which 071 are integer bits. 072 </li> 073 <li> 074 Create a matrix whose entries are the quantized version of the 075 values of the given matrix. The values are quantized by converting 076 them into a fixed point value with a particular precision and then 077 back again into a double value. Each entry is quantized using the same 078 precision. The result is an instance of <i>DoubleMatrixToken</i>. 079 For example: 080 <pre> 081 quantize([ -.040609, -.001628, .17853, .37665, .37665, .17853, 082 -.001628, -.040609 ], 10, 2) 083 </pre> 084 creates a new instance of DoubleMatrixToken containing the specified 085 values with 10 bits of precision, two of which are integer bits. 086 </li> 087 </ul> 088 In all cases, rounding is used when quantization errors occur, 089 and saturation is used when overflow occurs. 090 091 @author Bart Kienhuis, Contributor: Edward A. Lee 092 @version $Id$ 093 @since Ptolemy II 0.4 094 @Pt.ProposedRating Yellow (kienhuis) 095 @Pt.AcceptedRating Red (kienhuis) 096 @see PtParser 097 @see ptolemy.math.FixPoint 098 @see ptolemy.math.Quantizer 099 */ 100public class FixPointFunctions { 101 // The only constructor is private so that this class cannot 102 // be instantiated. 103 private FixPointFunctions() { 104 } 105 106 /////////////////////////////////////////////////////////////////// 107 //// public methods //// 108 109 /** Create a FixPoint representing the specified integer. 110 * For example, 111 * <pre> 112 * fix(5, 10, 4) 113 * </pre> 114 * creates a fixed point representation of the integer 5 with 115 * 10 bits of precision, 4 of which are integer bits. 116 * 117 * @param value The value to represent. 118 * @param numberOfBits The total number of bits. 119 * @param integerBits The number of bits used for the integer part. 120 * @return A fixed point representation of the value. 121 */ 122 public static FixPoint fix(int value, int numberOfBits, int integerBits) { 123 Precision precision = new Precision(numberOfBits, integerBits); 124 return Quantizer.round(value, precision); 125 } 126 127 /** Create a FixPoint representing the specified double. 128 * For example, 129 * <pre> 130 * fix(5.34, 10, 4) 131 * </pre> 132 * creates a fixed point representation of the numer 5.34 with 133 * 10 bits of precision, 4 of which are integer bits. 134 * 135 * @param value The value to represent. 136 * @param numberOfBits The total number of bits. 137 * @param integerBits The number of bits used for the integer part. 138 * @return A fixed point representation of the value. 139 */ 140 public static FixPoint fix(double value, int numberOfBits, 141 int integerBits) { 142 Precision precision = new Precision(numberOfBits, integerBits); 143 return Quantizer.round(value, precision); 144 } 145 146 /** Create a double whose value is the quantized version of 147 * the given double value. The value is quantized by converting 148 * it into a fixed point value with a particular precision and 149 * then back again into a double value. For example, 150 * <pre> 151 * quantize(5.34, 10, 4) 152 * </pre> 153 * yields a double representing 5.34 quantized to 10 bits of 154 * precision, of which 4 bits are used 155 * for the integer part and 6 bits are used for the fractional 156 * part. 157 * 158 * @param value The value to quantize. 159 * @param numberOfBits The total number of bits. 160 * @param integerBits The number of bits used for the integer part. 161 * @return a double with value that is quantized. 162 */ 163 public static double quantize(double value, int numberOfBits, 164 int integerBits) { 165 FixPoint fixValue = Quantizer.round(value, 166 new Precision(numberOfBits, integerBits)); 167 return fixValue.doubleValue(); 168 } 169}