libperiodicfunctions.cpp
1 /********************************************************************************
2  * Neural Network Framework. *
3  * Copyright (C) 2005-2011 Gianluca Massera <emmegian@yahoo.it> *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the Free Software *
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
18  ********************************************************************************/
19 
20 #include "libperiodicfunctions.h"
21 #include <cmath>
22 
23 namespace farsa {
24 
25 PeriodicFunction::PeriodicFunction( double phase, double span, double amplitude )
26  : OutputFunction() {
27  this->phase = phase;
28  this->span = span;
29  this->amplitude = amplitude;
30 }
31 
33 {
34  phase = 0.0;
35  QString str = params.getValue(prefix + "phase");
36  if (!str.isEmpty()) {
37  bool ok;
38  phase = str.toDouble(&ok);
39  if (!ok) {
40  phase = 0.0;
41  }
42  }
43 
44  span = 1.0;
45  str = params.getValue(prefix + "span");
46  if (!str.isEmpty()) {
47  bool ok;
48  span = str.toDouble(&ok);
49  if (!ok) {
50  span = 1.0;
51  }
52  }
53 
54  amplitude = 1.0;
55  str = params.getValue(prefix + "amplitude");
56  if (!str.isEmpty()) {
57  bool ok;
58  amplitude = str.toDouble(&ok);
59  if (!ok) {
60  amplitude = 1.0;
61  }
62  }
63 }
64 
65 void PeriodicFunction::save(ConfigurationParameters& params, QString prefix)
66 {
67  // Here we call startObjectParameters even if this is an abstract class
68  // because it creates the group
69  params.startObjectParameters(prefix, "PeriodicFunction", this);
70  params.createParameter(prefix, "phase", QString::number(phase));
71  params.createParameter(prefix, "span", QString::number(span));
72  params.createParameter(prefix, "amplitude", QString::number(amplitude));
73 }
74 
75 SawtoothFunction::SawtoothFunction( double phase, double span, double amplitude )
76  : PeriodicFunction(phase,span,amplitude) {
77 }
78 
80  // --- out <- 2.0*( (x-c)/a-floor((x-c)/a+0.5) )
81  for( int i=0; i<(int)inputs.size(); i++ ) {
82  outputs[i] = amplitude*( (inputs[i]-phase)/span-floor((inputs[i]-phase)/span+0.5) );
83  }
84 }
85 
87 {
88  // Calling parent function
89  PeriodicFunction::configure(params, prefix);
90 }
91 
92 void SawtoothFunction::save(ConfigurationParameters& params, QString prefix)
93 {
94  // Calling parent function
95  PeriodicFunction::save(params, prefix);
96  // Now saving our parameters
97  params.startObjectParameters(prefix, "SawtoothFunction", this);
98 }
99 
100 TriangleFunction::TriangleFunction( double phase, double span, double amplitude )
101  : PeriodicFunction(phase,span,amplitude) {
102 }
103 
105  // --- out <- 2.0*( (x-c)/a-floor((x-c)/a+0.5) )
106  for( unsigned int i=0; i<inputs.size(); i++ ) {
107  double sawtooth = (inputs[i]-phase)/span-floor((inputs[i]-phase)/span+0.5);
108  outputs[i] = amplitude*( 1.0 - fabs( sawtooth ) );
109  }
110 }
111 
113 {
114  // Calling parent function
115  PeriodicFunction::configure(params, prefix);
116 }
117 
118 void TriangleFunction::save(ConfigurationParameters& params, QString prefix)
119 {
120  // Calling parent function
121  PeriodicFunction::save(params, prefix);
122  // Now saving our parameters
123  params.startObjectParameters(prefix, "TriangleFunction", this);
124 }
125 
126 SinFunction::SinFunction( double phase, double span, double amplitude )
127  : PeriodicFunction(phase,span,amplitude) {
128 }
129 
131  return 2.0*PI_GRECO/span;
132 }
133 
134 void SinFunction::apply( DoubleVector& inputs, DoubleVector& outputs ) {
135  for( int i=0; i<(int)inputs.size(); i++ ) {
136  outputs[i] = amplitude*sin(2.0*PI_GRECO*(inputs[i]/span)-PI_GRECO*phase);
137  }
138 }
139 
140 void SinFunction::configure(ConfigurationParameters& params, QString prefix)
141 {
142  // Calling parent function
143  PeriodicFunction::configure(params, prefix);
144 }
145 
146 void SinFunction::save(ConfigurationParameters& params, QString prefix)
147 {
148  // Now calling parent function
149  PeriodicFunction::save(params, prefix);
150  // Now saving our parameters
151  params.startObjectParameters(prefix, "SinFunction", this);
152 }
153 
154 PseudoGaussFunction::PseudoGaussFunction( double phase, double span, double amplitude )
155  : PeriodicFunction(phase,span,amplitude) {
156 }
157 
159  for( unsigned int i=0; i<inputs.size(); i++ ) {
160  outputs[i] = 0.5*amplitude*( sin( 2.0*PI_GRECO*((inputs[i]-phase)/span+0.25) ) + 1.0 );
161  }
162 }
163 
165 {
166  // Calling parent function
167  PeriodicFunction::configure(params, prefix);
168 }
169 
171 {
172  // Now calling parent function
173  PeriodicFunction::save(params, prefix);
174  // Now saving our parameters
175  params.startObjectParameters(prefix, "PseudoGaussFunction", this);
176 }
177 
178 }
179