-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSineWaveGenerator.cs
More file actions
208 lines (174 loc) · 5.83 KB
/
Copy pathSineWaveGenerator.cs
File metadata and controls
208 lines (174 loc) · 5.83 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
using UnityEngine;
using System.Linq;
[CreateAssetMenu(fileName = "SineWaveGenerator", menuName = "Generate Sine Wave")]
public class SineWaveGenerator : ScriptableObject
{
//public float fps = 60f; // Sampling frequency i.e. frames per second
[Tooltip("In Seconds")]
public float Duration = 5f; // in seconds
[Tooltip("Frequency in Hz")]
public float sinFreq1 = 3f; // sine wave frequency
[Tooltip("Frequency in Hz")]
public float sinFreq2 = 5f; // sine wave frequency
public float fps = 250f;
[Tooltip("Resultant sine wave")]
public float[] sumOfSins; // sin(2*pi*F*t);
private float[] t; // (0:timeStep:Duration)
public float[] sin1; // sin(2*pi*F*t);
public float[] sin2; // sin(2*pi*F*t);
private float timeStep = 0f; // 1/fps = seconds per sample
public int arrayLength;
public float[] Init()
{
timeStep = 1f / fps;
//timeStep = Time.fixedDeltaTime;
arrayLength = Mathf.RoundToInt(Duration / timeStep);
//Debug.Log("Arr length: "+ arrayLength);
t = new float[arrayLength + 1];
sin1 = new float[arrayLength + 1];
sin2 = new float[arrayLength + 1];
sumOfSins = new float[arrayLength + 1];
//Debug.Log("Array length: " + arrayLength.ToString()); // +
int dex = 0;
for (float i = 0; i < Duration; i += timeStep)
{
try
{
t[dex] = i;
dex++;
}
catch
{ }
}
dex = 0;
foreach (float d in t)
{
try
{
sin1[dex] = Mathf.Sin(2 * Mathf.PI * sinFreq1 * t[dex]);
sin2[dex] = Mathf.Sin(2 * Mathf.PI * sinFreq2 * t[dex]);
sumOfSins[dex] = sin1[dex] + sin2[dex];
dex++;
}
catch
{ }
}
return sumOfSins;
}
public float CreateSine(float x, float amp, float flatness, float midline)
{
return amp * Mathf.Sin(x + flatness) + midline;
}
public float CreateCosine(float x, float amp, float flatness, float midline)
{
return amp * Mathf.Cos(x + flatness) + midline;
}
// New approach
//private static Random rnd = new Random();
public float[] RandomTerrarain(int length, int sinuses, int cosinuses, float amplsin, float amplcos, float noise)
{
//if (length <= 0)
// throw new ArgumentOutOfRangeException("length", length, "Length must be greater than zero!");
float[] returnValues = new float[length];
for (int i = 0; i < length; i++)
{
// sin
for (int sin = 1; sin <= sinuses; sin++)
{
returnValues[i] += amplsin * Mathf.Sin((2 * sin * i * Mathf.PI) / (float)length);
}
// cos
for (int cos = 1; cos <= cosinuses; cos++)
{
returnValues[i] += amplcos * Mathf.Cos((2 * cos * i * Mathf.PI) / (float)length);
}
// noise
returnValues[i] += (noise * NexFloat()) - (noise * NexFloat());
}
// give offset so it be higher than 0
float ofs = returnValues.Min();
if (ofs < 0)
{
ofs *= -1;
for (int i = 0; i < length; i++)
{
returnValues[i] += ofs;
}
}
// resize to be fit in 100
float max = returnValues.Max();
if (max >= 100f)
{
float scaler = max / 100.0f;
for (int i = 0; i < length; i++)
{
returnValues[i] /= scaler;
}
}
return returnValues;
}
float NexFloat()
{
return Random.value;
}
public float SampleGaussian(float mean, float stddev)
{
float x1, x2, S;
// The method requires sampling from a uniform random of (0,1]
do
{
x1 = 2f * NexFloat() - 1f;
x2 = 2f * NexFloat() - 1f;
S = x1 * x1 + x2 * x2;
} while (S >= 1.0f);
float fac = Mathf.Sqrt(-2.0f * Mathf.Log(S) / S);
return x1 * fac;
}
float[] NormalizedRandom(float mean, float sigma,float minValue, float maxValue)
{
mean = (minValue + maxValue) / 2f;
sigma = (maxValue - mean) / 3f;
float[] res = new float[2] { mean, sigma };
return res;
}
public float[] Linspace(float StartValue, float EndValue, int numberofpoints)
{
float[] parameterVals = new float[numberofpoints];
float crement = 0f;
if (StartValue < EndValue) // Decrement
{
crement = (EndValue - StartValue) / (numberofpoints - 1f);
}
else // Increment
{
crement = Mathf.Abs(StartValue - EndValue) / (numberofpoints - 1f);
}
int j = 0; //will keep a track of the numbers
float nextValue = StartValue;
for (int i = 0; i < numberofpoints; i++)
{
parameterVals.SetValue(nextValue, j);
j++;
if (j > numberofpoints)
{
throw new System.Exception(); //.IndexOutOfRangeException();
}
if (StartValue < EndValue) // Decrement
{
nextValue = nextValue - crement;
}
else
{
nextValue = nextValue + crement;
}
}
return parameterVals;
}
}
//"Current trial: " + expState.currentTrial.ToSting());
/*
* +
"t: " + t.ToString() +
"sum of sines: " + sumOfSins.ToString() +
"Sine1: " + sin1.ToString());
*/