001/* An actor that outputs a random sequence with a Rician distribution.
002
003 Copyright (c) 1998-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.DoubleToken;
031import ptolemy.data.expr.Parameter;
032import ptolemy.data.type.BaseType;
033import ptolemy.kernel.CompositeEntity;
034import ptolemy.kernel.util.IllegalActionException;
035import ptolemy.kernel.util.NameDuplicationException;
036
037///////////////////////////////////////////////////////////////////
038//// Rician
039
040/**
041 <p>Produce a random sequence with a Rician distribution.
042 A Rician random variable is defined as follows:
043 Let Z = sqrt(X<sup>2</sup> + Y<sup>2</sup>), where X and Y are statistically
044 independent Gaussian random variables with means given by parameters
045 <i>xMean</i> and <i>yMean</i> respectively, and common variance given by
046 parameter <i>standardDeviation</i>.
047 </p><p>
048 The default values of <i>xMean</i> and <i>yMean</i> are both set to be zero,
049 in which the distribution is also called a Rayleigh distribution. Hence,
050 the actor is by default a Rayleigh random generator.
051 </p><p>
052 On each iteration, a new random number is produced. The output port
053 is of type DoubleToken. The values that are generated are independent
054 and identically distributed with the means and the standard deviation
055 given by parameters. In addition, the seed can be specified as a
056 parameter to control the sequence that is generated.</p>
057
058 @author Ye Zhou
059 @version $Id$
060 @since Ptolemy II 3.0
061 @Pt.ProposedRating Green (eal)
062 @Pt.AcceptedRating Green (bilung)
063 @see ptolemy.actor.lib.Bernoulli
064 @see ptolemy.actor.lib.DiscreteRandomSource
065 @see ptolemy.actor.lib.Triangular
066 @see ptolemy.actor.lib.Uniform
067 */
068public class Rician extends RandomSource {
069    /** Construct an actor with the given container and name.
070     *  @param container The container.
071     *  @param name The name of this actor.
072     *  @exception IllegalActionException If the actor cannot be contained
073     *   by the proposed container.
074     *  @exception NameDuplicationException If the container already has an
075     *   actor with this name.
076     */
077    public Rician(CompositeEntity container, String name)
078            throws NameDuplicationException, IllegalActionException {
079        super(container, name);
080
081        output.setTypeEquals(BaseType.DOUBLE);
082
083        xMean = new Parameter(this, "xMean", new DoubleToken(0.0));
084        xMean.setTypeEquals(BaseType.DOUBLE);
085
086        yMean = new Parameter(this, "yMean", new DoubleToken(0.0));
087        yMean.setTypeEquals(BaseType.DOUBLE);
088
089        standardDeviation = new Parameter(this, "standardDeviation",
090                new DoubleToken(1.0));
091        standardDeviation.setTypeEquals(BaseType.DOUBLE);
092    }
093
094    ///////////////////////////////////////////////////////////////////
095    ////                     ports and parameters                  ////
096
097    /** The mean of the random number along the X-axis.
098     *  This parameter contains a DoubleToken, initially with value 0.0.
099     */
100    public Parameter xMean;
101
102    /** The mean of the random number along the Y-axis.
103     *  This parameter contains a DoubleToken, initially with value 0.0.
104     */
105    public Parameter yMean;
106
107    /** The standard deviation of the random number.
108     *  This parameter contains a DoubleToken, initially with value 1.0.
109     */
110    public Parameter standardDeviation;
111
112    ///////////////////////////////////////////////////////////////////
113    ////                         public methods                    ////
114
115    /** Send a random number with a Rician distribution to the output.
116     *  This number is only changed in the prefire() method, so it will
117     *  remain constant throughout an iteration.
118     *  @exception IllegalActionException If there is no director.
119     */
120    @Override
121    public void fire() throws IllegalActionException {
122        super.fire();
123        output.send(0, new DoubleToken(_current));
124    }
125
126    ///////////////////////////////////////////////////////////////////
127    ////                         protected methods                 ////
128
129    /** Generate a new random number.
130     *  @exception IllegalActionException If parameter values are incorrect.
131     */
132    @Override
133    protected void _generateRandomNumber() throws IllegalActionException {
134        double xMeanValue = ((DoubleToken) xMean.getToken()).doubleValue();
135        double yMeanValue = ((DoubleToken) yMean.getToken()).doubleValue();
136        double standardDeviationValue = ((DoubleToken) standardDeviation
137                .getToken()).doubleValue();
138        double xRawNum = _random.nextGaussian();
139        double yRawNum = _random.nextGaussian();
140        _current = java.lang.Math.sqrt(java.lang.Math
141                .pow(xRawNum * standardDeviationValue + xMeanValue, 2)
142                + java.lang.Math
143                        .pow(yRawNum * standardDeviationValue + yMeanValue, 2));
144    }
145
146    ///////////////////////////////////////////////////////////////////
147    ////                         private variables                 ////
148    // The random number for the current iteration.
149    private double _current;
150}