Skip to content

Commit faaa5e5

Browse files
committed
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
14 parents fa4a533 + 8855923 + de7a14f + 2451991 + 00981a2 + dc03850 + 4d198e4 + 7157e08 + 8223595 + a5cc165 + 73b637f + 6e7a5b6 + e0fa5a8 + 82ef736 commit faaa5e5

File tree

1 file changed

+31
-21
lines changed

1 file changed

+31
-21
lines changed

Source/ORTS.Common/Filter.cs

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ public class IIRFilter
5050
int NCoef;
5151
List<float> ACoef;
5252
List<float> BCoef;
53-
List<float> y;
54-
List<float> x;
53+
float[] x;
54+
float[] y;
5555

5656
public IIRFilter()
5757
{
@@ -94,8 +94,8 @@ Z domain Poles
9494

9595
NCoef = A.Count - 1;
9696

97-
x = Enumerable.Repeat(0f, NCoef).ToList();
98-
y = Enumerable.Repeat(0f, NCoef).ToList();
97+
x = new float[NCoef + 1];
98+
y = new float[NCoef + 1];
9999

100100
FilterType = FilterTypes.Bessel;
101101
}
@@ -112,8 +112,8 @@ public IIRFilter(List<float> a, List<float> b, FilterTypes type)
112112
NCoef = a.Count - 1;
113113
ACoef = a;
114114
BCoef = b;
115-
x = Enumerable.Repeat(0f, NCoef).ToList();
116-
y = Enumerable.Repeat(0f, NCoef).ToList();
115+
x = new float[NCoef + 1];
116+
y = new float[NCoef + 1];
117117
}
118118

119119
/// <summary>
@@ -145,8 +145,8 @@ public IIRFilter(FilterTypes type, int order, float cutoffFrequency, float sampl
145145
NCoef = A.Count - 1;
146146
ACoef = A;
147147
BCoef = B;
148-
x = Enumerable.Repeat(0f, NCoef).ToList();
149-
y = Enumerable.Repeat(0f, NCoef).ToList();
148+
x = new float[NCoef + 1];
149+
y = new float[NCoef + 1];
150150
}
151151

152152
/// <summary>
@@ -158,8 +158,8 @@ public List<float> A
158158
{
159159
if (NCoef <= 0)
160160
NCoef = value.Count - 1;
161-
x = Enumerable.Repeat(0f, NCoef).ToList();
162-
y = Enumerable.Repeat(0f, NCoef).ToList();
161+
x = new float[NCoef + 1];
162+
y = new float[NCoef + 1];
163163
if (ACoef == null)
164164
ACoef = new List<float>();
165165
ACoef.Clear();
@@ -183,8 +183,8 @@ public List<float> B
183183
{
184184
if (NCoef <= 0)
185185
NCoef = value.Count - 1;
186-
x = Enumerable.Repeat(0f, NCoef).ToList();
187-
y = Enumerable.Repeat(0f, NCoef).ToList();
186+
x = new float[NCoef + 1];
187+
y = new float[NCoef + 1];
188188
if (BCoef == null)
189189
BCoef = new List<float>();
190190
BCoef.Clear();
@@ -257,9 +257,15 @@ public enum FilterTypes
257257
/// <returns>Filtered value</returns>
258258
public float Filter(float NewSample)
259259
{
260+
//shift the old samples
261+
for (int n = x.Length - 1; n > 0; n--)
262+
{
263+
x[n] = x[n - 1];
264+
y[n] = y[n - 1];
265+
}
260266
//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];
263269
for (int n = 1; n <= NCoef; n++)
264270
y[0] += ACoef[n] * x[n] - BCoef[n] * y[n];
265271

@@ -291,8 +297,15 @@ public float Filter(float NewSample, float samplingPeriod)
291297
default:
292298
throw new NotImplementedException("Other filter types are not implemented yet. Try to use constant sampling period and Filter(float NewSample) version of this method.");
293299
}
294-
x.Insert(0, NewSample);
295-
y.Insert(0, ACoef[0] * x[0]);
300+
//shift the old samples
301+
for (int n = 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];
296309
for (int n = 1; n <= NCoef; n++)
297310
y[0] += ACoef[n] * x[n] - BCoef[n] * y[n];
298311

@@ -304,11 +317,8 @@ public float Filter(float NewSample, float samplingPeriod)
304317
/// </summary>
305318
public void Reset()
306319
{
307-
for (int i = 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);
312322
}
313323
/// <summary>
314324
/// Resets all buffers of the filter with given initial value

0 commit comments

Comments
 (0)