001/* An FFT.
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.domains.sdf.lib;
029
030import ptolemy.data.ComplexToken;
031import ptolemy.data.IntToken;
032import ptolemy.data.ScalarToken;
033import ptolemy.data.Token;
034import ptolemy.data.expr.Parameter;
035import ptolemy.data.type.BaseType;
036import ptolemy.kernel.CompositeEntity;
037import ptolemy.kernel.util.Attribute;
038import ptolemy.kernel.util.IllegalActionException;
039import ptolemy.kernel.util.NameDuplicationException;
040import ptolemy.math.Complex;
041import ptolemy.math.SignalProcessing;
042
043///////////////////////////////////////////////////////////////////
044//// FFT
045
046/**
047 This actor calculates the Fast Fourier Transform of a sequence of
048 complex inputs.  The order of the FFT determines the number of tokens
049 that will be consumed and produced on each firing. The order is the
050 base-2 logarithm of the size. The default order is 8, which means that
051 2<sup>8</sup> = 256 tokens are read and 2<sup>8</sup> = 256 tokens are
052 produced.  The result of the FFT is a new array of Complex tokens.
053
054 @author Bart Kienhuis, Steve Neuendorffer
055 @version $Id$
056 @since Ptolemy II 0.4
057 @Pt.ProposedRating Green (neuendor)
058 @Pt.AcceptedRating Yellow (neuendor)
059 @see ptolemy.math.SignalProcessing#FFTComplexOut
060 */
061public class FFT extends SDFTransformer {
062    /** Construct an actor with the given container and name.
063     *  @param container The container.
064     *  @param name The name of this actor.
065     *  @exception IllegalActionException If the actor cannot be contained
066     *   by the proposed container.
067     *  @exception NameDuplicationException If the container already has an
068     *   actor with this name.
069     */
070    public FFT(CompositeEntity container, String name)
071            throws NameDuplicationException, IllegalActionException {
072        super(container, name);
073
074        input.setTypeEquals(BaseType.COMPLEX);
075        output.setTypeEquals(BaseType.COMPLEX);
076
077        order = new Parameter(this, "order");
078        order.setExpression("8");
079        order.setTypeEquals(BaseType.INT);
080
081        input_tokenConsumptionRate.setExpression("2^order");
082        output_tokenProductionRate.setExpression("2^order");
083    }
084
085    ///////////////////////////////////////////////////////////////////
086    ////                         public variables                  ////
087
088    /** The order of the FFT.  The type is IntToken, and the value should
089     *  be greater than zero.  The default value is an IntToken with value 8.
090     */
091    public Parameter order;
092
093    ///////////////////////////////////////////////////////////////////
094    ////                         public methods                    ////
095
096    /** Ensure that the order parameter is positive and recompute the
097     *  size of internal buffers.
098     *  @param attribute The attribute that has changed.
099     *  @exception IllegalActionException If the parameters are out of range.
100     */
101    @Override
102    public void attributeChanged(Attribute attribute)
103            throws IllegalActionException {
104        if (attribute == order) {
105            // Get the size of the FFT transform
106            _orderValue = ((IntToken) order.getToken()).intValue();
107
108            if (_orderValue <= 0) {
109                throw new IllegalActionException(this, "Order was "
110                        + _orderValue + " but must be greater than zero.");
111            }
112
113            _transformSize = (int) Math.pow(2, _orderValue);
114
115            _inComplexArray = new Complex[_transformSize];
116            _outTokenArray = new ComplexToken[_transformSize];
117        } else {
118            super.attributeChanged(attribute);
119        }
120    }
121
122    /** Consume the inputs and produce the outputs of the FFT filter.
123     *  @exception IllegalActionException If a runtime type error occurs.
124     */
125    @Override
126    public void fire() throws IllegalActionException {
127        super.fire();
128
129        Token[] inTokenArray = input.get(0, _transformSize);
130
131        for (int i = 0; i < _transformSize; i++) {
132            _inComplexArray[i] = ((ScalarToken) inTokenArray[i]).complexValue();
133        }
134
135        Complex[] outComplexArray = SignalProcessing
136                .FFTComplexOut(_inComplexArray, _orderValue);
137
138        for (int i = 0; i < _transformSize; i++) {
139            _outTokenArray[i] = new ComplexToken(outComplexArray[i]);
140        }
141
142        output.send(0, _outTokenArray, _transformSize);
143    }
144
145    ///////////////////////////////////////////////////////////////////
146    ////                         private variables                 ////
147    private int _transformSize;
148
149    private int _orderValue;
150
151    private Complex[] _inComplexArray;
152
153    private ComplexToken[] _outTokenArray;
154}