This repository has been archived by the owner on May 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
PulseGenerator.cs
135 lines (126 loc) · 3.92 KB
/
PulseGenerator.cs
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
using RCNet.Extensions;
using System;
namespace RCNet.Neural.Data.Generators
{
/// <summary>
/// Implements the constant pulse generator.
/// </summary>
[Serializable]
public class PulseGenerator : IGenerator
{
//Enums
/// <summary>
/// The pulse timing mode.
/// </summary>
public enum TimingMode
{
/// <summary>
/// The period of the pulses is constant.
/// </summary>
Constant,
/// <summary>
/// The period of the pulses follows the Uniform distribution.
/// </summary>
Uniform,
/// <summary>
/// The period of the pulses follows the Gaussian distribution.
/// </summary>
Gaussian,
/// <summary>
/// The period of the pulses follows the Poisson (Exponential) distribution.
/// </summary>
Poisson
}
//Attributes
private readonly double _signal;
private readonly double _avgPeriod;
private readonly TimingMode _mode;
private Random _rand;
private int _t;
private int _nextPulseTime;
//Constructors
/// <summary>
/// Creates an initialized instance.
/// </summary>
/// <param name="signal">The pulse signal value.</param>
/// <param name="avgPeriod">The pulse average leak.</param>
/// <param name="mode">The pulse timing mode.</param>
public PulseGenerator(double signal, double avgPeriod, TimingMode mode)
{
_signal = signal;
_avgPeriod = Math.Abs(avgPeriod);
_mode = mode;
Reset();
return;
}
/// <summary>
/// Creates an initialized instance.
/// </summary>
/// <param name="cfg">The configuration</param>
public PulseGenerator(PulseGeneratorSettings cfg)
{
_signal = cfg.Signal;
_avgPeriod = cfg.AvgPeriod;
_mode = cfg.Mode;
Reset();
return;
}
//Methods
/// <summary>
/// Schedules the next pulse.
/// </summary>
private void ScheduleNextPulse()
{
double minPeriod = 1d;
double maxPeriod = 1d + 2d * (_avgPeriod - 1d);
double spanPeriod = maxPeriod - minPeriod;
int timeIncrement;
switch (_mode)
{
case TimingMode.Constant:
timeIncrement = (_t == 0) ? 1 : (int)Math.Round(_avgPeriod);
break;
case TimingMode.Uniform:
timeIncrement = (int)Math.Round(_rand.NextRangedUniformDouble(minPeriod, maxPeriod));
break;
case TimingMode.Gaussian:
timeIncrement = (int)Math.Round(_rand.NextRangedGaussianDouble(_avgPeriod, spanPeriod / 6d, minPeriod, maxPeriod));
break;
case TimingMode.Poisson:
timeIncrement = (int)Math.Round(_rand.NextExponentialDouble(_avgPeriod));
break;
default:
timeIncrement = 0;
break;
}
if (timeIncrement <= 0)
{
timeIncrement = 1;
}
_nextPulseTime = _t + timeIncrement;
return;
}
/// <inheritdoc />
public void Reset()
{
_rand = new Random(0);
_t = 0;
ScheduleNextPulse();
return;
}
/// <inheritdoc />
public double Next()
{
++_t;
if (_t == _nextPulseTime)
{
ScheduleNextPulse();
return _signal;
}
else
{
return 0;
}
}
}//PulseGenerator
}//Namespace