Skip to content

Commit c31e3c4

Browse files
committed
Automatic merge of T1.5.1-351-gc9abcd22b and 8 pull requests
- Pull request #570 at de7a14f: Experimental glTF 2.0 support with PBR lighting - Pull request #732 at 1edb2e5: Improvements for air brakes - Pull request #751 at 00981a2: Web interface to control cab controls with external hardware - Pull request #767 at 82c5f1c: Refine sunrise and sunset - Pull request #799 at eb92d81: Consolidated wind simulation - Pull request #803 at 7157e08: Various adjustments to steam adhesion - Pull request #809 at f67822a: Some on-screen messages not suppressed, Bug #2008012 - Pull request #813 at 153f923: Refactored garbage generators
10 parents 1045519 + c9abcd2 + de7a14f + 1edb2e5 + 00981a2 + 82c5f1c + eb92d81 + 7157e08 + f67822a + 153f923 commit c31e3c4

File tree

1 file changed

+50
-89
lines changed

1 file changed

+50
-89
lines changed

Source/ORTS.Common/Filter.cs

+50-89
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
using System.Collections;
2020
using System.Collections.Generic;
2121
using System.Linq;
22-
using System.Text;
2322

2423
namespace ORTS.Common
2524
{
@@ -48,6 +47,12 @@ namespace ORTS.Common
4847
/// </summary>
4948
public class IIRFilter
5049
{
50+
int NCoef;
51+
List<float> ACoef;
52+
List<float> BCoef;
53+
List<float> y;
54+
List<float> x;
55+
5156
public IIRFilter()
5257
{
5358
/**************************************************************
@@ -73,25 +78,24 @@ Z domain Poles
7378
z = 0.599839 + j -0.394883
7479
z = 0.599839 + j 0.394883
7580
***************************************************************/
76-
ACoef = new ArrayList();
77-
ACoef.Add(0.00023973435363423468);
78-
ACoef.Add(0.00047946870726846936);
79-
ACoef.Add(0.00023973435363423468);
81+
ACoef = new List<float>
82+
{
83+
0.00023973435363423468f,
84+
0.00047946870726846936f,
85+
0.00023973435363423468f
86+
};
8087

81-
BCoef = new ArrayList();
82-
BCoef.Add(1.00000000000000000000);
83-
BCoef.Add(-1.94607498611971570000);
84-
BCoef.Add(0.94703573071858904000);
88+
BCoef = new List<float>
89+
{
90+
1.00000000000000000000f,
91+
-1.94607498611971570000f,
92+
0.94703573071858904000f
93+
};
8594

8695
NCoef = A.Count - 1;
8796

88-
x = new ArrayList();
89-
y = new ArrayList();
90-
for (int i = 0; i <= NCoef; i++)
91-
{
92-
x.Add(0.0);
93-
y.Add(0.0);
94-
}
97+
x = Enumerable.Repeat(0f, NCoef).ToList();
98+
y = Enumerable.Repeat(0f, NCoef).ToList();
9599

96100
FilterType = FilterTypes.Bessel;
97101
}
@@ -102,19 +106,14 @@ Z domain Poles
102106
/// <param name="a">A coefficients of the filter</param>
103107
/// <param name="b">B coefficients of the filter</param>
104108
/// <param name="type">Filter type</param>
105-
public IIRFilter(ArrayList a, ArrayList b, FilterTypes type)
109+
public IIRFilter(List<float> a, List<float> b, FilterTypes type)
106110
{
107111
FilterType = type;
108112
NCoef = a.Count - 1;
109113
ACoef = a;
110114
BCoef = b;
111-
x = new ArrayList();
112-
y = new ArrayList();
113-
for (int i = 0; i <= NCoef; i++)
114-
{
115-
x.Add(0.0);
116-
y.Add(0.0);
117-
}
115+
x = Enumerable.Repeat(0f, NCoef).ToList();
116+
y = Enumerable.Repeat(0f, NCoef).ToList();
118117
}
119118

120119
/// <summary>
@@ -126,11 +125,10 @@ public IIRFilter(ArrayList a, ArrayList b, FilterTypes type)
126125
/// <param name="samplingPeriod">Filter sampling period</param>
127126
public IIRFilter(FilterTypes type, int order, float cutoffFrequency, float samplingPeriod)
128127
{
129-
NCoef = order;
130-
A = new ArrayList();
131-
B = new ArrayList();
132-
133128
FilterType = type;
129+
NCoef = order;
130+
A = new List<float>();
131+
B = new List<float>();
134132

135133
switch (type)
136134
{
@@ -147,39 +145,25 @@ public IIRFilter(FilterTypes type, int order, float cutoffFrequency, float sampl
147145
NCoef = A.Count - 1;
148146
ACoef = A;
149147
BCoef = B;
150-
x = new ArrayList();
151-
y = new ArrayList();
152-
for (int i = 0; i <= NCoef; i++)
153-
{
154-
x.Add(0.0);
155-
y.Add(0.0);
156-
}
148+
x = Enumerable.Repeat(0f, NCoef).ToList();
149+
y = Enumerable.Repeat(0f, NCoef).ToList();
157150
}
158151

159-
int NCoef;
160-
ArrayList ACoef;
161-
ArrayList BCoef;
162-
163152
/// <summary>
164153
/// A coefficients of the filter
165154
/// </summary>
166-
public ArrayList A
155+
public List<float> A
167156
{
168157
set
169158
{
170-
if(NCoef <= 0)
159+
if (NCoef <= 0)
171160
NCoef = value.Count - 1;
172-
x = new ArrayList();
173-
y = new ArrayList();
174-
for (int i = 0; i <= NCoef; i++)
175-
{
176-
x.Add(0.0);
177-
y.Add(0.0);
178-
}
161+
x = Enumerable.Repeat(0f, NCoef).ToList();
162+
y = Enumerable.Repeat(0f, NCoef).ToList();
179163
if (ACoef == null)
180-
ACoef = new ArrayList();
164+
ACoef = new List<float>();
181165
ACoef.Clear();
182-
foreach (object obj in value)
166+
foreach (var obj in value)
183167
{
184168
ACoef.Add(obj);
185169
}
@@ -193,23 +177,18 @@ public ArrayList A
193177
/// <summary>
194178
/// B coefficients of the filter
195179
/// </summary>
196-
public ArrayList B
180+
public List<float> B
197181
{
198182
set
199183
{
200-
if(NCoef <= 0)
184+
if (NCoef <= 0)
201185
NCoef = value.Count - 1;
202-
x = new ArrayList();
203-
y = new ArrayList();
204-
for (int i = 0; i <= NCoef; i++)
205-
{
206-
x.Add(0.0);
207-
y.Add(0.0);
208-
}
186+
x = Enumerable.Repeat(0f, NCoef).ToList();
187+
y = Enumerable.Repeat(0f, NCoef).ToList();
209188
if (BCoef == null)
210-
BCoef = new ArrayList();
189+
BCoef = new List<float>();
211190
BCoef.Clear();
212-
foreach (object obj in value)
191+
foreach (var obj in value)
213192
{
214193
BCoef.Add(obj);
215194
}
@@ -220,9 +199,6 @@ public ArrayList B
220199
}
221200
}
222201

223-
ArrayList y;
224-
ArrayList x;
225-
226202
private float cuttoffFreqRadpS;
227203
/// <summary>
228204
/// Filter Cut off frequency in Radians
@@ -281,19 +257,13 @@ public enum FilterTypes
281257
/// <returns>Filtered value</returns>
282258
public float Filter(float NewSample)
283259
{
284-
//shift the old samples
285-
for (int n = NCoef; n > 0; n--)
286-
{
287-
x[n] = x[n - 1];
288-
y[n] = y[n - 1];
289-
}
290260
//Calculate the new output
291-
x[0] = NewSample;
292-
y[0] = (float)Convert.ToDouble(ACoef[0]) * (float)Convert.ToDouble(x[0]);
261+
x.Insert(0, NewSample);
262+
y.Insert(0, ACoef[0] * x[0]);
293263
for (int n = 1; n <= NCoef; n++)
294-
y[0] = (float)Convert.ToDouble(y[0]) + (float)Convert.ToDouble(ACoef[n]) * (float)Convert.ToDouble(x[n]) - (float)Convert.ToDouble(BCoef[n]) * (float)Convert.ToDouble(y[n]);
264+
y[0] += ACoef[n] * x[n] - BCoef[n] * y[n];
295265

296-
return (float)y[0];
266+
return y[0];
297267
}
298268

299269
/// <summary>
@@ -321,21 +291,12 @@ public float Filter(float NewSample, float samplingPeriod)
321291
default:
322292
throw new NotImplementedException("Other filter types are not implemented yet. Try to use constant sampling period and Filter(float NewSample) version of this method.");
323293
}
324-
//shift the old samples
325-
for (int n = NCoef; n > 0; n--)
326-
{
327-
x[n] = x[n - 1];
328-
y[n] = y[n - 1];
329-
}
330-
//Calculate the new output
331-
x[0] = NewSample;
332-
y[0] = (float)ACoef[0] * (float)x[0];
294+
x.Insert(0, NewSample);
295+
y.Insert(0, ACoef[0] * x[0]);
333296
for (int n = 1; n <= NCoef; n++)
334-
{
335-
y[0] = (float)Convert.ToDouble(y[0]) + (float)Convert.ToDouble(ACoef[n]) * (float)Convert.ToDouble(x[n]) - (float)Convert.ToDouble(BCoef[n]) * (float)Convert.ToDouble(y[n]);
336-
}
297+
y[0] += ACoef[n] * x[n] - BCoef[n] * y[n];
337298

338-
return (float)y[0];
299+
return y[0];
339300
}
340301

341302
/// <summary>
@@ -345,8 +306,8 @@ public void Reset()
345306
{
346307
for (int i = 0; i < x.Count; i++)
347308
{
348-
x[i] = 0.0;
349-
y[i] = 0.0;
309+
x[i] = 0.0f;
310+
y[i] = 0.0f;
350311
}
351312
}
352313
/// <summary>

0 commit comments

Comments
 (0)