-
Notifications
You must be signed in to change notification settings - Fork 0
/
parameters.h
174 lines (145 loc) · 3.87 KB
/
parameters.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
* This file is part of libsidplayfp, a SID player engine.
*
* Copyright 2023 Leandro Nini <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef PARAMETERS_H
#define PARAMETERS_H
#include <cmath>
#include <vector>
#include <string>
#include <sstream>
#include <iomanip>
#include <limits>
// Model parameters
enum class Param_t
{
Q,
B,
V
};
// define the postfix increment operator to allow looping over enum
inline Param_t& operator++(Param_t& x, int)
{
return x = static_cast<Param_t>(static_cast<std::underlying_type<Param_t>::type>(x) + 1);
}
typedef struct
{
double Vin;
double Vout;
} data_t;
typedef std::vector<data_t> ref_vector_t;
struct score_t
{
double error;
score_t() :
error(0.)
{}
bool isBetter(const score_t& newScore) const
{
return newScore.error < error;
}
};
std::ostream & operator<<(std::ostream & os, const score_t & foo)
{
os.precision(std::numeric_limits<double>::max_digits10);
os << foo.error;
return os;
}
class Parameters
{
public:
double q, b, v;
public:
Parameters() { reset(); }
void reset()
{
q = 1.;
b = 1.;
v = 1.;
}
double GetValue(Param_t i)
{
switch (i)
{
case Param_t::Q: return q;
case Param_t::B: return b;
case Param_t::V: return v;
default: return 0.; // Just to silence warning
}
}
void SetValue(Param_t i, double d)
{
switch (i)
{
case Param_t::Q: q = d; break;
case Param_t::B: b = d; break;
case Param_t::V: v = d; break;
}
}
std::string toString()
{
std::ostringstream ss;
ss.precision(std::numeric_limits<double>::max_digits10);
ss << "q = " << q << std::endl;
ss << "b = " << b << std::endl;
ss << "v = " << v << std::endl;
return ss.str();
}
private:
double GetValue(double Vin) const
{
// https://en.wikipedia.org/wiki/Generalised_logistic_function
// y = min + (max-min)/(1+Q*e^-B*x)^(1/v)
return std::pow(1. + q*std::exp(b*Vin), 1./v);
}
double GetScore(double Vout, double Vref) const
{
double diff = (Vout - Vref)/Vref;
return diff * diff;
}
public:
score_t Score(const ref_vector_t &reference, bool print, unsigned int bestscore)
{
score_t score;
const double Vmin = reference[0].Vin;
const double Vmax = reference[0].Vout;
double error = 0.;
for (data_t data: reference)
{
// Calculate score
const double simval = Vmin + (Vmax-Vmin)/GetValue(data.Vin-Vmin);
const double err = GetScore(simval, data.Vout);
error += err;
if (print)
{
std::cout
<< simval << " "
<< data.Vout << " ("
<< err << ")"
<< std::endl;
}
}
score.error = std::sqrt(error);
if (print)
{
std::cout << "Error: " << score.error << std::endl;
}
return score;
}
};
#endif