nnfw/src/libperiodicfunctions.cpp

00001 /********************************************************************************
00002  *  Neural Network Framework.                                                   *
00003  *  Copyright (C) 2005-2011 Gianluca Massera <emmegian@yahoo.it>                *
00004  *                                                                              *
00005  *  This program is free software; you can redistribute it and/or modify        *
00006  *  it under the terms of the GNU General Public License as published by        *
00007  *  the Free Software Foundation; either version 2 of the License, or           *
00008  *  (at your option) any later version.                                         *
00009  *                                                                              *
00010  *  This program is distributed in the hope that it will be useful,             *
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of              *
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               *
00013  *  GNU General Public License for more details.                                *
00014  *                                                                              *
00015  *  You should have received a copy of the GNU General Public License           *
00016  *  along with this program; if not, write to the Free Software                 *
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA  *
00018  ********************************************************************************/
00019 
00020 #include "libperiodicfunctions.h"
00021 #include <cmath>
00022 
00023 namespace farsa {
00024 
00025 PeriodicFunction::PeriodicFunction( double phase, double span, double amplitude )
00026     : OutputFunction() {
00027     this->phase = phase;
00028     this->span = span;
00029     this->amplitude = amplitude;
00030 }
00031 
00032 void PeriodicFunction::configure(ConfigurationParameters& params, QString prefix)
00033 {
00034     phase = 0.0;
00035     QString str = params.getValue(prefix + "phase");
00036     if (!str.isEmpty()) {
00037         bool ok;
00038         phase = str.toDouble(&ok);
00039         if (!ok) {
00040             phase = 0.0;
00041         }
00042     }
00043 
00044     span = 1.0;
00045     str = params.getValue(prefix + "span");
00046     if (!str.isEmpty()) {
00047         bool ok;
00048         span = str.toDouble(&ok);
00049         if (!ok) {
00050             span = 1.0;
00051         }
00052     }
00053 
00054     amplitude = 1.0;
00055     str = params.getValue(prefix + "amplitude");
00056     if (!str.isEmpty()) {
00057         bool ok;
00058         amplitude = str.toDouble(&ok);
00059         if (!ok) {
00060             amplitude = 1.0;
00061         }
00062     }
00063 }
00064 
00065 void PeriodicFunction::save(ConfigurationParameters& params, QString prefix)
00066 {
00067     // Here we call startObjectParameters even if this is an abstract class
00068     // because it creates the group
00069     params.startObjectParameters(prefix, "PeriodicFunction", this);
00070     params.createParameter(prefix, "phase", QString::number(phase));
00071     params.createParameter(prefix, "span", QString::number(span));
00072     params.createParameter(prefix, "amplitude", QString::number(amplitude));
00073 }
00074 
00075 SawtoothFunction::SawtoothFunction( double phase, double span, double amplitude )
00076     : PeriodicFunction(phase,span,amplitude) {
00077 }
00078 
00079 void SawtoothFunction::apply( DoubleVector& inputs, DoubleVector& outputs ) {
00080     // --- out <- 2.0*( (x-c)/a-floor((x-c)/a+0.5) )
00081     for( int i=0; i<(int)inputs.size(); i++ ) {
00082         outputs[i] = amplitude*( (inputs[i]-phase)/span-floor((inputs[i]-phase)/span+0.5) );
00083     }
00084 }
00085 
00086 void SawtoothFunction::configure(ConfigurationParameters& params, QString prefix)
00087 {
00088     // Calling parent function
00089     PeriodicFunction::configure(params, prefix);
00090 }
00091 
00092 void SawtoothFunction::save(ConfigurationParameters& params, QString prefix)
00093 {
00094     // Calling parent function
00095     PeriodicFunction::save(params, prefix);
00096     // Now saving our parameters
00097     params.startObjectParameters(prefix, "SawtoothFunction", this);
00098 }
00099 
00100 TriangleFunction::TriangleFunction( double phase, double span, double amplitude )
00101     : PeriodicFunction(phase,span,amplitude) {
00102 }
00103 
00104 void TriangleFunction::apply( DoubleVector& inputs, DoubleVector& outputs ) {
00105     // --- out <- 2.0*( (x-c)/a-floor((x-c)/a+0.5) )
00106     for( unsigned int i=0; i<inputs.size(); i++ ) {
00107         double sawtooth = (inputs[i]-phase)/span-floor((inputs[i]-phase)/span+0.5);
00108         outputs[i] = amplitude*( 1.0 - fabs( sawtooth ) );
00109     }
00110 }
00111 
00112 void TriangleFunction::configure(ConfigurationParameters& params, QString prefix)
00113 {
00114     // Calling parent function
00115     PeriodicFunction::configure(params, prefix);
00116 }
00117 
00118 void TriangleFunction::save(ConfigurationParameters& params, QString prefix)
00119 {
00120     // Calling parent function
00121     PeriodicFunction::save(params, prefix);
00122     // Now saving our parameters
00123     params.startObjectParameters(prefix, "TriangleFunction", this);
00124 }
00125 
00126 SinFunction::SinFunction( double phase, double span, double amplitude )
00127     : PeriodicFunction(phase,span,amplitude) {
00128 }
00129 
00130 double SinFunction::frequency() {
00131     return 2.0*PI_GRECO/span;
00132 }
00133 
00134 void SinFunction::apply( DoubleVector& inputs, DoubleVector& outputs ) {
00135     for( int i=0; i<(int)inputs.size(); i++ ) {
00136         outputs[i] = amplitude*sin(2.0*PI_GRECO*(inputs[i]/span)-PI_GRECO*phase);
00137     }
00138 }
00139 
00140 void SinFunction::configure(ConfigurationParameters& params, QString prefix)
00141 {
00142     // Calling parent function
00143     PeriodicFunction::configure(params, prefix);
00144 }
00145 
00146 void SinFunction::save(ConfigurationParameters& params, QString prefix)
00147 {
00148     // Now calling parent function
00149     PeriodicFunction::save(params, prefix);
00150     // Now saving our parameters
00151     params.startObjectParameters(prefix, "SinFunction", this);
00152 }
00153 
00154 PseudoGaussFunction::PseudoGaussFunction( double phase, double span, double amplitude )
00155     : PeriodicFunction(phase,span,amplitude) {
00156 }
00157 
00158 void PseudoGaussFunction::apply( DoubleVector& inputs, DoubleVector& outputs ) {
00159     for( unsigned int i=0; i<inputs.size(); i++ ) {
00160         outputs[i] = 0.5*amplitude*( sin( 2.0*PI_GRECO*((inputs[i]-phase)/span+0.25) ) + 1.0 );
00161     }
00162 }
00163 
00164 void PseudoGaussFunction::configure(ConfigurationParameters& params, QString prefix)
00165 {
00166     // Calling parent function
00167     PeriodicFunction::configure(params, prefix);
00168 }
00169 
00170 void PseudoGaussFunction::save(ConfigurationParameters& params, QString prefix)
00171 {
00172     // Now calling parent function
00173     PeriodicFunction::save(params, prefix);
00174     // Now saving our parameters
00175     params.startObjectParameters(prefix, "PseudoGaussFunction", this);
00176 }
00177 
00178 }
00179