001/* An IFFT.
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.Token;
033import ptolemy.data.expr.Parameter;
034import ptolemy.data.type.BaseType;
035import ptolemy.kernel.CompositeEntity;
036import ptolemy.kernel.util.Attribute;
037import ptolemy.kernel.util.IllegalActionException;
038import ptolemy.kernel.util.NameDuplicationException;
039import ptolemy.math.Complex;
040import ptolemy.math.SignalProcessing;
041
042///////////////////////////////////////////////////////////////////
043//// IFFT
044
045/**
046 This actor calculates the inverse FFT of a complex input array.
047 The order of the IFFT determines the number of tokens that
048 will be consumed and produced on each firing. The order is
049 the base-2 logarithm of the size. The default order is 8,
050 which means that 2<sup>8</sup> = 256 tokens are read and 2<sup>8</sup>
051 = 256 tokens are produced.
052 The result of the IFFT is a new array of Complex tokens.
053
054 @author Bart Kienhuis, Steve Neuendorffer
055 @version $Id$
056 @since Ptolemy II 1.0
057 @Pt.ProposedRating Yellow (neuendor)
058 @Pt.AcceptedRating Yellow (eal)
059 @see ptolemy.math.SignalProcessing#IFFTComplexOut
060 */
061public class IFFT 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 IFFT(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 IFFT.  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 IFFT 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] = ((ComplexToken) inTokenArray[i])
133                    .complexValue();
134        }
135
136        Complex[] outComplexArray = SignalProcessing
137                .IFFTComplexOut(_inComplexArray, _orderValue);
138
139        for (int i = 0; i < _transformSize; i++) {
140            _outTokenArray[i] = new ComplexToken(outComplexArray[i]);
141        }
142
143        output.send(0, _outTokenArray, _transformSize);
144    }
145
146    ///////////////////////////////////////////////////////////////////
147    ////                         private variables                 ////
148    private int _transformSize;
149
150    private int _orderValue;
151
152    private ComplexToken[] _outTokenArray;
153
154    private Complex[] _inComplexArray;
155}