19
19
using System . Collections ;
20
20
using System . Collections . Generic ;
21
21
using System . Linq ;
22
- using System . Text ;
23
22
24
23
namespace ORTS . Common
25
24
{
@@ -48,6 +47,12 @@ namespace ORTS.Common
48
47
/// </summary>
49
48
public class IIRFilter
50
49
{
50
+ int NCoef ;
51
+ List < float > ACoef ;
52
+ List < float > BCoef ;
53
+ List < float > y ;
54
+ List < float > x ;
55
+
51
56
public IIRFilter ( )
52
57
{
53
58
/**************************************************************
@@ -73,25 +78,24 @@ Z domain Poles
73
78
z = 0.599839 + j -0.394883
74
79
z = 0.599839 + j 0.394883
75
80
***************************************************************/
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
+ } ;
80
87
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
+ } ;
85
94
86
95
NCoef = A . Count - 1 ;
87
96
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 ( ) ;
95
99
96
100
FilterType = FilterTypes . Bessel ;
97
101
}
@@ -102,19 +106,14 @@ Z domain Poles
102
106
/// <param name="a">A coefficients of the filter</param>
103
107
/// <param name="b">B coefficients of the filter</param>
104
108
/// <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 )
106
110
{
107
111
FilterType = type ;
108
112
NCoef = a . Count - 1 ;
109
113
ACoef = a ;
110
114
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 ( ) ;
118
117
}
119
118
120
119
/// <summary>
@@ -126,11 +125,10 @@ public IIRFilter(ArrayList a, ArrayList b, FilterTypes type)
126
125
/// <param name="samplingPeriod">Filter sampling period</param>
127
126
public IIRFilter ( FilterTypes type , int order , float cutoffFrequency , float samplingPeriod )
128
127
{
129
- NCoef = order ;
130
- A = new ArrayList ( ) ;
131
- B = new ArrayList ( ) ;
132
-
133
128
FilterType = type ;
129
+ NCoef = order ;
130
+ A = new List < float > ( ) ;
131
+ B = new List < float > ( ) ;
134
132
135
133
switch ( type )
136
134
{
@@ -147,39 +145,25 @@ public IIRFilter(FilterTypes type, int order, float cutoffFrequency, float sampl
147
145
NCoef = A . Count - 1 ;
148
146
ACoef = A ;
149
147
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 ( ) ;
157
150
}
158
151
159
- int NCoef ;
160
- ArrayList ACoef ;
161
- ArrayList BCoef ;
162
-
163
152
/// <summary>
164
153
/// A coefficients of the filter
165
154
/// </summary>
166
- public ArrayList A
155
+ public List < float > A
167
156
{
168
157
set
169
158
{
170
- if ( NCoef <= 0 )
159
+ if ( NCoef <= 0 )
171
160
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 ( ) ;
179
163
if ( ACoef == null )
180
- ACoef = new ArrayList ( ) ;
164
+ ACoef = new List < float > ( ) ;
181
165
ACoef . Clear ( ) ;
182
- foreach ( object obj in value )
166
+ foreach ( var obj in value )
183
167
{
184
168
ACoef . Add ( obj ) ;
185
169
}
@@ -193,23 +177,18 @@ public ArrayList A
193
177
/// <summary>
194
178
/// B coefficients of the filter
195
179
/// </summary>
196
- public ArrayList B
180
+ public List < float > B
197
181
{
198
182
set
199
183
{
200
- if ( NCoef <= 0 )
184
+ if ( NCoef <= 0 )
201
185
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 ( ) ;
209
188
if ( BCoef == null )
210
- BCoef = new ArrayList ( ) ;
189
+ BCoef = new List < float > ( ) ;
211
190
BCoef . Clear ( ) ;
212
- foreach ( object obj in value )
191
+ foreach ( var obj in value )
213
192
{
214
193
BCoef . Add ( obj ) ;
215
194
}
@@ -220,9 +199,6 @@ public ArrayList B
220
199
}
221
200
}
222
201
223
- ArrayList y ;
224
- ArrayList x ;
225
-
226
202
private float cuttoffFreqRadpS ;
227
203
/// <summary>
228
204
/// Filter Cut off frequency in Radians
@@ -281,19 +257,13 @@ public enum FilterTypes
281
257
/// <returns>Filtered value</returns>
282
258
public float Filter ( float NewSample )
283
259
{
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
- }
290
260
//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 ] ) ;
293
263
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 ] ;
295
265
296
- return ( float ) y [ 0 ] ;
266
+ return y [ 0 ] ;
297
267
}
298
268
299
269
/// <summary>
@@ -321,21 +291,12 @@ public float Filter(float NewSample, float samplingPeriod)
321
291
default :
322
292
throw new NotImplementedException ( "Other filter types are not implemented yet. Try to use constant sampling period and Filter(float NewSample) version of this method." ) ;
323
293
}
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 ] ) ;
333
296
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 ] ;
337
298
338
- return ( float ) y [ 0 ] ;
299
+ return y [ 0 ] ;
339
300
}
340
301
341
302
/// <summary>
@@ -345,8 +306,8 @@ public void Reset()
345
306
{
346
307
for ( int i = 0 ; i < x . Count ; i ++ )
347
308
{
348
- x [ i ] = 0.0 ;
349
- y [ i ] = 0.0 ;
309
+ x [ i ] = 0.0f ;
310
+ y [ i ] = 0.0f ;
350
311
}
351
312
}
352
313
/// <summary>
0 commit comments