Cypress  1.0
C++ Spiking Neural Network Simulation Framework
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
delta_sigma.hpp
Go to the documentation of this file.
1 /*
2  * Cypress -- C++ Spiking Neural Network Simulation Framework
3  * Copyright (C) 2016 Andreas Stöckel
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 3 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, see <http://www.gnu.org/licenses/>.
17  */
18 
28 #ifndef CYPRESS_NEF_DELTA_SIGMA
29 #define CYPRESS_NEF_DELTA_SIGMA
30 
31 #include <cmath>
32 #include <cstddef>
33 #include <vector>
34 
35 #include <cypress/core/types.hpp>
36 
37 namespace cypress {
38 namespace nef {
39 
44 class DeltaSigma {
45 public:
46  static constexpr Real DEFAULT_RESPONSE_TIME = 50e-3;
47  static constexpr Real DEFAULT_STEP = 1e-4;
48  static constexpr Real DEFAULT_EPS = 1e-6;
49  static constexpr Real DEFAULT_MIN_SPIKE_INTERVAL = 1e-3;
50 
57  struct GaussWindow {
66  static Real value(Real x) { return std::exp(-x * x); }
67 
73  static Real limit(Real eps = 1e-6f)
74  {
75  return std::sqrt(-std::log(eps));
76  }
77  };
78 
91  static Real value(Real x)
92  {
93  return x < 0.0f ? 0.0f : std::exp(-x);
94  }
95 
101  static Real limit(Real eps = 1e-6f) { return -std::log(eps); }
102  };
103 
108  private:
109  Real m_alpha;
110  Real m_sigma;
111  Real m_step;
112  Real m_integral;
113  Real m_integral_to_zero;
114  std::vector<Real> m_values;
115 
116  DiscreteWindow(Real alpha, Real sigma, Real step, Real integral,
117  Real integral_to_zero, std::vector<Real> &&values)
118  : m_alpha(alpha),
119  m_sigma(sigma),
120  m_step(step),
121  m_integral(integral),
122  m_integral_to_zero(integral_to_zero),
123  m_values(values)
124  {
125  }
126 
127  template <typename Window>
128  static std::pair<Real, Real> calculate_alpha_and_response_time(
129  Real spike_interval, Real sigma, Real step, Real eps,
130  Real p = 0.05f);
131 
136  template <typename Window>
137  static std::pair<Real, Real> choose_params(
138  Real min_spike_interval = DEFAULT_MIN_SPIKE_INTERVAL,
139  Real response_time = DEFAULT_RESPONSE_TIME,
140  Real step = DEFAULT_STEP, Real eps = DEFAULT_EPS);
141 
142  public:
154  template <typename Window>
155  static DiscreteWindow create_manual(Real alpha, Real sigma,
156  Real step = DEFAULT_STEP,
157  Real eps = DEFAULT_EPS);
158 
173  template <typename Window>
174  static DiscreteWindow create(
175  Real min_spike_interval = DEFAULT_MIN_SPIKE_INTERVAL,
176  Real response_time = DEFAULT_RESPONSE_TIME,
177  Real step = DEFAULT_STEP, Real eps = DEFAULT_EPS);
178 
182  Real alpha() const { return m_alpha; }
183 
187  Real sigma() const { return m_sigma; }
188 
192  Real step() const { return m_step; }
193 
201  Real integral() const { return m_integral; }
202 
207  Real integral_to_zero() const { return m_integral_to_zero; }
208 
214  Real limit(Real eps = DEFAULT_EPS) const
215  {
216  return std::sqrt(-std::log(eps));
217  }
218 
222  size_t size() const { return m_values.size(); }
223 
227  Real operator[](size_t i) const { return m_values[i]; }
228 
232  auto begin() const { return m_values.begin(); }
233 
237  auto end() const { return m_values.end(); }
238  };
239 
257  static std::vector<Real> encode(
258  const std::vector<Real> &values, const DiscreteWindow &window, Real t0,
259  Real min_val = -1.0, Real max_val = 1.0,
260  Real min_spike_interval = DEFAULT_MIN_SPIKE_INTERVAL);
261 
278  template <typename Fun>
279  static std::vector<Real> encode(
280  const Fun &f, const DiscreteWindow &window, Real t0, Real t1,
281  Real min_val = -1.0, Real max_val = 1.0,
282  Real min_spike_interval = DEFAULT_MIN_SPIKE_INTERVAL)
283  {
284  const size_t n_samples = std::ceil(t1 - t0) / window.step();
285  std::vector<Real> values(n_samples);
286  for (size_t i = 0; i < n_samples; i++) {
287  values[i] = f(t0 + i * window.step());
288  }
289  return encode(values, window, t0, min_val, max_val, min_spike_interval);
290  }
291 
305  static std::vector<Real> decode(const std::vector<Real> &spikes,
306  const DiscreteWindow &window, Real t0,
307  Real t1, Real min_val = -1.0,
308  Real max_val = 1.0);
309 };
310 }
311 }
312 
313 #endif /* CYPRESS_NEF_DELTA_SIGMA */
314 
auto begin() const
Definition: delta_sigma.hpp:232
static constexpr Real DEFAULT_STEP
Definition: delta_sigma.hpp:47
Real step() const
Definition: delta_sigma.hpp:192
static std::vector< Real > decode(const std::vector< Real > &spikes, const DiscreteWindow &window, Real t0, Real t1, Real min_val=-1.0, Real max_val=1.0)
static constexpr Real DEFAULT_MIN_SPIKE_INTERVAL
Definition: delta_sigma.hpp:49
auto end() const
Definition: delta_sigma.hpp:237
static constexpr Real DEFAULT_EPS
Definition: delta_sigma.hpp:48
Definition: delta_sigma.hpp:57
Definition: delta_sigma.hpp:82
static Real limit(Real eps=1e-6f)
Definition: delta_sigma.hpp:73
double Real
Definition: types.hpp:56
static Real limit(Real eps=1e-6f)
Definition: delta_sigma.hpp:101
Real sigma() const
Definition: delta_sigma.hpp:187
size_t size() const
Definition: delta_sigma.hpp:222
Real limit(Real eps=DEFAULT_EPS) const
Definition: delta_sigma.hpp:214
static Real value(Real x)
Definition: delta_sigma.hpp:91
Real integral_to_zero() const
Definition: delta_sigma.hpp:207
Real integral() const
Definition: delta_sigma.hpp:201
Definition: brainscales_lib.hpp:39
static std::vector< Real > encode(const std::vector< Real > &values, const DiscreteWindow &window, Real t0, Real min_val=-1.0, Real max_val=1.0, Real min_spike_interval=DEFAULT_MIN_SPIKE_INTERVAL)
Definition: delta_sigma.hpp:44
Definition: delta_sigma.hpp:107
Real alpha() const
Definition: delta_sigma.hpp:182
static constexpr Real DEFAULT_RESPONSE_TIME
Definition: delta_sigma.hpp:46
Real operator[](size_t i) const
Definition: delta_sigma.hpp:227
static std::vector< Real > encode(const Fun &f, const DiscreteWindow &window, Real t0, Real t1, Real min_val=-1.0, Real max_val=1.0, Real min_spike_interval=DEFAULT_MIN_SPIKE_INTERVAL)
Definition: delta_sigma.hpp:279
static Real value(Real x)
Definition: delta_sigma.hpp:66