You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Automatic merge of T1.5.1-376-g885592329 and 12 pull requests
- Pull request #570 at de7a14f: Experimental glTF 2.0 support with PBR lighting
- Pull request #732 at 2451991: Improvements for air brakes
- Pull request #751 at 00981a2: Web interface to control cab controls with external hardware
- Pull request #799 at dc03850: Consolidated wind simulation
- Pull request #802 at 4d198e4: Added support for activity location events to the TrackViewer
- Pull request #803 at 7157e08: Various adjustments to steam adhesion
- Pull request #813 at 8223595: Refactored garbage generators
- Pull request #815 at a5cc165: chore: Add GitHub automatic release notes configuration
- Pull request #818 at 73b637f: Allow independent drive axles for locomotives
- Pull request #820 at 6e7a5b6: delay option for masterkey does not delay switch off
- Pull request #821 at e0fa5a8: Adds suppression of safety valves
- Pull request #822 at 82ef736: battery switch two buttons option in combination with a delay
Copy file name to clipboardExpand all lines: Source/ORTS.Common/Filter.cs
+31-21Lines changed: 31 additions & 21 deletions
Original file line number
Diff line number
Diff line change
@@ -50,8 +50,8 @@ public class IIRFilter
50
50
intNCoef;
51
51
List<float>ACoef;
52
52
List<float>BCoef;
53
-
List<float>y;
54
-
List<float>x;
53
+
float[]x;
54
+
float[]y;
55
55
56
56
publicIIRFilter()
57
57
{
@@ -94,8 +94,8 @@ Z domain Poles
94
94
95
95
NCoef=A.Count-1;
96
96
97
-
x=Enumerable.Repeat(0f,NCoef).ToList();
98
-
y=Enumerable.Repeat(0f,NCoef).ToList();
97
+
x=newfloat[NCoef+1];
98
+
y=newfloat[NCoef+1];
99
99
100
100
FilterType=FilterTypes.Bessel;
101
101
}
@@ -112,8 +112,8 @@ public IIRFilter(List<float> a, List<float> b, FilterTypes type)
112
112
NCoef=a.Count-1;
113
113
ACoef=a;
114
114
BCoef=b;
115
-
x=Enumerable.Repeat(0f,NCoef).ToList();
116
-
y=Enumerable.Repeat(0f,NCoef).ToList();
115
+
x=newfloat[NCoef+1];
116
+
y=newfloat[NCoef+1];
117
117
}
118
118
119
119
/// <summary>
@@ -145,8 +145,8 @@ public IIRFilter(FilterTypes type, int order, float cutoffFrequency, float sampl
145
145
NCoef=A.Count-1;
146
146
ACoef=A;
147
147
BCoef=B;
148
-
x=Enumerable.Repeat(0f,NCoef).ToList();
149
-
y=Enumerable.Repeat(0f,NCoef).ToList();
148
+
x=newfloat[NCoef+1];
149
+
y=newfloat[NCoef+1];
150
150
}
151
151
152
152
/// <summary>
@@ -158,8 +158,8 @@ public List<float> A
158
158
{
159
159
if(NCoef<=0)
160
160
NCoef=value.Count-1;
161
-
x=Enumerable.Repeat(0f,NCoef).ToList();
162
-
y=Enumerable.Repeat(0f,NCoef).ToList();
161
+
x=newfloat[NCoef+1];
162
+
y=newfloat[NCoef+1];
163
163
if(ACoef==null)
164
164
ACoef=newList<float>();
165
165
ACoef.Clear();
@@ -183,8 +183,8 @@ public List<float> B
183
183
{
184
184
if(NCoef<=0)
185
185
NCoef=value.Count-1;
186
-
x=Enumerable.Repeat(0f,NCoef).ToList();
187
-
y=Enumerable.Repeat(0f,NCoef).ToList();
186
+
x=newfloat[NCoef+1];
187
+
y=newfloat[NCoef+1];
188
188
if(BCoef==null)
189
189
BCoef=newList<float>();
190
190
BCoef.Clear();
@@ -257,9 +257,15 @@ public enum FilterTypes
257
257
/// <returns>Filtered value</returns>
258
258
publicfloatFilter(floatNewSample)
259
259
{
260
+
//shift the old samples
261
+
for(intn=x.Length-1;n>0;n--)
262
+
{
263
+
x[n]=x[n-1];
264
+
y[n]=y[n-1];
265
+
}
260
266
//Calculate the new output
261
-
x.Insert(0,NewSample);
262
-
y.Insert(0,ACoef[0]*x[0]);
267
+
x[0]=NewSample;
268
+
y[0]=ACoef[0]*x[0];
263
269
for(intn=1;n<=NCoef;n++)
264
270
y[0]+=ACoef[n]*x[n]-BCoef[n]*y[n];
265
271
@@ -291,8 +297,15 @@ public float Filter(float NewSample, float samplingPeriod)
291
297
default:
292
298
thrownewNotImplementedException("Other filter types are not implemented yet. Try to use constant sampling period and Filter(float NewSample) version of this method.");
293
299
}
294
-
x.Insert(0,NewSample);
295
-
y.Insert(0,ACoef[0]*x[0]);
300
+
//shift the old samples
301
+
for(intn=x.Length-1;n>0;n--)
302
+
{
303
+
x[n]=x[n-1];
304
+
y[n]=y[n-1];
305
+
}
306
+
//Calculate the new output
307
+
x[0]=NewSample;
308
+
y[0]=ACoef[0]*x[0];
296
309
for(intn=1;n<=NCoef;n++)
297
310
y[0]+=ACoef[n]*x[n]-BCoef[n]*y[n];
298
311
@@ -304,11 +317,8 @@ public float Filter(float NewSample, float samplingPeriod)
304
317
/// </summary>
305
318
publicvoidReset()
306
319
{
307
-
for(inti=0;i<x.Count;i++)
308
-
{
309
-
x[i]=0.0f;
310
-
y[i]=0.0f;
311
-
}
320
+
Array.Clear(x);
321
+
Array.Clear(y);
312
322
}
313
323
/// <summary>
314
324
/// Resets all buffers of the filter with given initial value
0 commit comments