001/* Find the Signal to Noise ratio between two signals
002 @Copyright (c) 1998-2014 The Regents of the University of California.
003 All rights reserved.
004
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
008 above copyright notice and the following two paragraphs appear in all
009 copies 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 */
027package ptolemy.domains.sdf.lib.vq;
028
029import ptolemy.actor.TypedAtomicActor;
030import ptolemy.actor.TypedIOPort;
031import ptolemy.data.DoubleToken;
032import ptolemy.data.IntMatrixToken;
033import ptolemy.data.type.BaseType;
034import ptolemy.kernel.CompositeEntity;
035import ptolemy.kernel.util.IllegalActionException;
036import ptolemy.kernel.util.NameDuplicationException;
037import ptolemy.math.ExtendedMath;
038
039///////////////////////////////////////////////////////////////////
040//// PSNR
041
042/**
043 This actor consumes an IntMatrixToken from each input port, and calculates the
044 Power Signal to Noise Ratio (PSNR) between them.  The PSNR is output on the
045 output port as a DoubleToken.
046
047 @author Michael Leung, Steve Neuendorffer
048 @version $Id$
049 @since Ptolemy II 0.4
050 @Pt.ProposedRating Green (neuendor)
051 @Pt.AcceptedRating Yellow (neuendor)
052 */
053public class PSNR extends TypedAtomicActor {
054    /** Construct an actor in the specified container with the specified
055     *  name.
056     *  @param container The container.
057     *  @param name The name of this adder within the container.
058     *  @exception IllegalActionException If the actor cannot be contained
059     *   by the proposed container.
060     *  @exception NameDuplicationException If the name coincides with
061     *   an actor already in the container.
062     */
063    public PSNR(CompositeEntity container, String name)
064            throws IllegalActionException, NameDuplicationException {
065        super(container, name);
066
067        output = new TypedIOPort(this, "output", false, true);
068        output.setTypeEquals(BaseType.DOUBLE);
069
070        signal = new TypedIOPort(this, "signal", true, false);
071        signal.setTypeEquals(BaseType.INT_MATRIX);
072
073        distortedSignal = new TypedIOPort(this, "distortedSignal", true, false);
074        distortedSignal.setTypeEquals(BaseType.INT_MATRIX);
075    }
076
077    ///////////////////////////////////////////////////////////////////
078    ////                      ports and parameters                 ////
079
080    /** The input signal. */
081    public TypedIOPort signal;
082
083    /** A distorted version of the input signal. */
084    public TypedIOPort distortedSignal;
085
086    /** The calculated PSNR between the two inputs. */
087    public TypedIOPort output;
088
089    /** Fire the actor.
090     *  Consume one image on each of the input ports.   Calculate the
091     *  PSNR as follows:
092     *  noise = signal-distortedSignal;
093     *  signalPower = Power(signal);
094     *  noisePower = Power(noise);
095     *  PSNR = 10 * log10(signalPower/noisePower);
096     *  @exception IllegalActionException If the dimensions of the
097     *  input tokens do not match.
098     */
099    @Override
100    public void fire() throws IllegalActionException {
101        super.fire();
102        int i;
103        int j;
104        int signalPower = 0;
105        int noisePower = 0;
106        int element1;
107        int element2;
108
109        double PSNRValue;
110
111        IntMatrixToken signalToken = (IntMatrixToken) signal.get(0);
112        IntMatrixToken distortedSignalToken = (IntMatrixToken) distortedSignal
113                .get(0);
114        int columns = signalToken.getColumnCount();
115        int rows = signalToken.getRowCount();
116
117        if (distortedSignalToken.getColumnCount() != columns
118                || distortedSignalToken.getRowCount() != rows) {
119            throw new IllegalActionException(
120                    "Input token dimensions " + "must match!");
121        }
122
123        for (j = 0; j < rows; j++) {
124            for (i = 0; i < columns; i++) {
125                element1 = signalToken.getElementAt(j, i);
126                element2 = distortedSignalToken.getElementAt(j, i);
127
128                signalPower = signalPower + element1 * element1;
129                noisePower = noisePower
130                        + (element1 - element2) * (element1 - element2);
131            }
132        }
133
134        PSNRValue = 10 * ExtendedMath.log10((double) signalPower / noisePower);
135
136        DoubleToken message = new DoubleToken(PSNRValue);
137        output.send(0, message);
138    }
139}