-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqNoise.cpp
152 lines (137 loc) · 5.79 KB
/
qNoise.cpp
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
/*
qNoise: A generator of non-Gaussian colored noise
Copyright © 2021, Juan Ignacio Deza
email: ignacio.deza@uwe.ac.uk
Description
qNoise is a non-gaussian colored random noise generator. It is a handy source of
self-correlated noise for a great variety of applications. It depends on two
parameters only: tau for controlling the autocorrelation, and q for controlling
the statistics. This noise tends smoothly for q = 1 to an Ornstein-Uhlenbeck
(colored gaussian) noise with autocorrelation tau. for q < 1 it is bounded noise
and it is supra-Gaussian for q > 1. The noise is generated via a stochastic
differential equation using the Heun method (a second order Runge-Kutta type
integration scheme) and it is implemented as a stand-alone library in c++. It
Useful as input for numerical simulations, as a source of noise for controlling
experiments using synthetic noise via micro-controllers and for a wide variety
of applications.
Requirements
It is a stand-alone library with no dependencies other than the standard
libraries. Due to it's use of some functions from the <random> library the
library currently works on c++11 or higher only. This should be OK for most Macs
and new Linux systems. In some older systems it is possible that you need to add
`-std=gnu++11` to your compilation flags.
Licence
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 3 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, see <http://www.gnu.org/licenses/>.
*/
#include "qNoise.h"
// #define UNIT_TEST
#ifdef UNIT_TEST
#include "unit_tests.cpp"
#endif
// Manual seeding.
void qNoiseGen::seedManual(unsigned UserSeed) {
seed = UserSeed;
generator.seed(seed);
}
// Timer seeding
// Note: when using this on multiple threads, call seedManual on each thread
// using different values of seed
void qNoiseGen::seedTimer() {
// obtain a seed from the timer
myclock::duration d = myclock::now() - beginning;
seed = d.count();
generator.seed(seed);
}
// Gaussian White noise
double qNoiseGen::gaussWN() { return randNorm(generator); }
// Ornstein-Uhlembeck noise type.
double qNoiseGen::orsUhl(double eta, double tau, double H) {
return eta * exp(-H / tau) +
sqrt((1 - exp(-2 / tau * H)) / 2 / tau) * randNorm(generator);
}
/*
* qNoise Potential derivative. Private Function for use only by qNoise
* The potential is:
* \frac{\text{sigma}^2 \log \left(\frac{\text{eta}^2 (q-1)
* \text{tau}}{\text{sigma}^2}+1\right)}{2 (q-1) \text{tau}} it's derivative is:
* \frac{\text{eta}}{\frac{\text{eta}^2 (q-1) \text{tau}}{\text{sigma}^2}+1}
* The /tau is the tau present in the differential equation.
*/
double qNoiseGen::potQNoisePrime(double eta, double tau, double q) {
return (eta / (1 + (eta * eta * tau * (q - 1)))) / tau;
}
/*qNoise.
* This functions integrates a differential equation using the Heun Method
* for q=1 it behaves like the orstein Uhlembeck noise
* for q<1 it it is defined in a acotated support only (+/- etaCut)
* for q>1 its statistics are more than gaussian tending (supra-gaussian)
*/
double qNoiseGen::qNoise(double eta, double tau, double q, double H,
double sqrt_H = -1) {
double kHeun, lHeun, differential;
bool error = false;
int countError = 0;
// If the square root of H is provided, it will be used, otherwise
// calculate it every time the function is invoked.
if (sqrt_H < 0)
sqrt_H = sqrt(H);
// The cut value.
double etaCut = 1 / sqrt(tau * (1 - q));
while (1) {
kHeun = H * potQNoisePrime(eta, tau, q);
lHeun = sqrt_H * randNorm(generator) / tau;
differential = -H / 2 *
(potQNoisePrime(eta, tau, q) +
potQNoisePrime(eta + kHeun + lHeun, tau, q)) +
lHeun;
/*
* Check if the system is inside the boundary.
* This is only important when q<1.
* If the eta + D_eta are outside the bounds, it retries 10 times.
* If it is still out of bounds, it retries with an OH noise, another 10
* times This very time step will be Gaussian but with the correct tau. If
* it is still out of bounds it hard resets it with gaussian noise. The bool
* error is on place to debug these errors. However it should be a concern
* only for very low values of q and very high values of tau.
*/
if ((fabs(eta + differential) > etaCut) || isnan(eta + differential)) {
countError++;
if (countError > 20) {
if (error)
std::cerr << "Out of bounds phase 3: " << eta << "\t" << differential
<< std::endl;
return eta/fabs(eta) * etaCut * (0.9 + 0.1 * uniform(generator) );
}
if (countError > 10) {
eta = etaCut * orsUhl(eta, tau, H);
if (error)
std::cerr << "Out of bounds phase 2: " << eta << "\t" << differential
<< std::endl;
} else {
if (error)
std::cerr << "Out of bounds: " << eta << "\t" << differential
<< std::endl;
}
} else {
return eta + differential;
}
}
}
/*qNoiseNorm.
* This functions works as qNoise but where both tau and the variance of the
* noise are independent of q to first order. This approximation fails for q ->
* 5/3, where both variables diverge.
*/
double qNoiseGen::qNoiseNorm(double eta, double tau, double q, double H,
double sqrt_H = -1) {
return qNoise(eta, tau * (5 - 3 * q) / 2, q, H, sqrt_H);
}