001/* Convert to dB.
002
003 Copyright (c) 1997-2014 The Regents of the University of California.
004 All rights reserved.
005 Permission is hereby granted, without written agreement and without
006 license or royalty fees, to use, copy, modify, and distribute this
007 software and its documentation for any purpose, provided that the above
008 copyright notice and the following two paragraphs appear in all copies
009 of this software.
010
011 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
012 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
013 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
014 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
015 SUCH DAMAGE.
016
017 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
018 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
019 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
020 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
021 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
022 ENHANCEMENTS, OR MODIFICATIONS.
023
024 PT_COPYRIGHT_VERSION_2
025 COPYRIGHTENDKEY
026
027 */
028package ptolemy.actor.lib;
029
030import ptolemy.data.BooleanToken;
031import ptolemy.data.DoubleToken;
032import ptolemy.data.Token;
033import ptolemy.data.expr.Parameter;
034import ptolemy.data.type.BaseType;
035import ptolemy.kernel.CompositeEntity;
036import ptolemy.kernel.util.IllegalActionException;
037import ptolemy.kernel.util.NameDuplicationException;
038import ptolemy.kernel.util.Workspace;
039
040///////////////////////////////////////////////////////////////////
041//// DB
042
043/**
044 Produce a token that is the value of the input in decibels.
045 That is, if the input is <i>z</i>, then the output is
046 <i>k</i>*log<sub>10</sub>(<em>z</em>).
047 The constant <i>k</i> depends on the value of the <i>inputIsPower</i>
048 parameter.  If that parameter is true, then <i>k</i> = 10.
049 Otherwise (the default) <i>k</i> = 20.
050 Normally, you would set <i>inputIsPower</i> to true if
051 the input is the square of a signal, and to false otherwise.
052 <p>
053 The output is never smaller than the value of the <i>min</i> parameter.
054 This makes it easier to plot by limiting the range of output values.
055 If the input is zero or negative, then the output is the
056 value of the <i>min</i> parameter.
057 <p>
058 The input and output both have type double.
059
060 @author Bart Kienhuis and Edward A. Lee
061 @version $Id$
062 @since Ptolemy II 1.0
063 @Pt.ProposedRating Yellow (eal)
064 @Pt.AcceptedRating Yellow (ssachs)
065 */
066public class DB extends Transformer {
067    /** Construct an actor in the specified container with the specified
068     *  name.
069     *  @param container The container.
070     *  @param name The name of this actor within the container.
071     *  @exception IllegalActionException If the actor cannot be contained
072     *   by the proposed container.
073     *  @exception NameDuplicationException If the name coincides with
074     *   an actor already in the container.
075     */
076    public DB(CompositeEntity container, String name)
077            throws IllegalActionException, NameDuplicationException {
078        super(container, name);
079        input.setTypeEquals(BaseType.DOUBLE);
080        output.setTypeEquals(BaseType.DOUBLE);
081
082        inputIsPower = new Parameter(this, "inputIsPower",
083                new BooleanToken(false));
084        inputIsPower.setTypeEquals(BaseType.BOOLEAN);
085        min = new Parameter(this, "min", new DoubleToken(-100.0));
086        min.setTypeEquals(BaseType.DOUBLE);
087    }
088
089    ///////////////////////////////////////////////////////////////////
090    ////                         ports and parameters              ////
091
092    /** If the input is proportional to power, then set this to true.
093     *  This must be a boolean, and defaults to false.
094     */
095    public Parameter inputIsPower;
096
097    /** The minimum value of the output.  This is a double,
098     *  and defaults to -100.0.
099     */
100    public Parameter min;
101
102    ///////////////////////////////////////////////////////////////////
103    ////                         public methods                    ////
104
105    /** Clone the actor into the specified workspace.
106     *  @param workspace The workspace for the new object.
107     *  @return A new actor.
108     *  @exception CloneNotSupportedException If a derived class contains
109     *   an attribute that cannot be cloned.
110     */
111    @Override
112    public Object clone(Workspace workspace) throws CloneNotSupportedException {
113        DB newObject = (DB) super.clone(workspace);
114
115        newObject._resultArray = new DoubleToken[_resultArray.length];
116        System.arraycopy(_resultArray, 0, newObject._resultArray, 0,
117                _resultArray.length);
118        return newObject;
119    }
120
121    /** Read a token from the input and convert its value into a
122     *  decibel representation. If the input does not contain any tokens,
123     *  do nothing.
124     *  @exception IllegalActionException If there is no director.
125     */
126    @Override
127    public void fire() throws IllegalActionException {
128        super.fire();
129        if (input.hasToken(0)) {
130            DoubleToken in = (DoubleToken) input.get(0);
131            double number = in.doubleValue();
132            double minValue = ((DoubleToken) min.getToken()).doubleValue();
133            output.send(0, _doFunction(number, minValue));
134        }
135    }
136
137    /** Invoke a specified number of iterations of this actor. Each
138     *  iteration converts a single token to decibels. An invocation
139     *  of this method therefore applies the conversion to <i>count</i>
140     *  successive input tokens.
141     *  <p>
142     *  This method should be called instead of the usual prefire(),
143     *  fire(), postfire() methods when this actor is used in a
144     *  domain that supports vectorized actors.  This leads to more
145     *  efficient execution.
146     *  @param count The number of iterations to perform.
147     *  @return COMPLETED if the actor was successfully iterated the
148     *   specified number of times. Otherwise, return NOT_READY, and do
149     *   not consume any input tokens.
150     *  @exception IllegalActionException If iterating cannot be
151     *  performed.
152     */
153    @Override
154    public int iterate(int count) throws IllegalActionException {
155        // Check whether we need to reallocate the output token array.
156        if (count > _resultArray.length) {
157            _resultArray = new DoubleToken[count];
158        }
159
160        if (input.hasToken(0, count)) {
161            double minValue = ((DoubleToken) min.getToken()).doubleValue();
162
163            // NOTE: inArray.length may be > count, in which case
164            // only the first count tokens are valid.
165            Token[] inArray = input.get(0, count);
166
167            for (int i = 0; i < count; i++) {
168                double input = ((DoubleToken) inArray[i]).doubleValue();
169                _resultArray[i] = _doFunction(input, minValue);
170            }
171
172            output.send(0, _resultArray, count);
173            return COMPLETED;
174        } else {
175            return NOT_READY;
176        }
177    }
178
179    ///////////////////////////////////////////////////////////////////
180    ////                         private methods                   ////
181
182    /** Return the specified number in decibels,
183     *  but no less than <i>minValue</i>.
184     */
185    private DoubleToken _doFunction(double number, double minValue)
186            throws IllegalActionException {
187        double outNumber;
188
189        if (number <= 0.0) {
190            outNumber = minValue;
191        } else {
192            outNumber = ptolemy.math.SignalProcessing.toDecibels(number);
193
194            if (((BooleanToken) inputIsPower.getToken()).booleanValue()) {
195                outNumber /= 2.0;
196            }
197
198            if (outNumber < minValue) {
199                outNumber = minValue;
200            }
201        }
202
203        return new DoubleToken(outNumber);
204    }
205
206    ///////////////////////////////////////////////////////////////////
207    ////                         private variables                 ////
208    private DoubleToken[] _resultArray = new DoubleToken[1];
209}