1919using System . Collections ;
2020using System . Collections . Generic ;
2121using System . Linq ;
22- using System . Text ;
2322
2423namespace 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+ float [ ] x ;
54+ float [ ] y ;
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 = new float [ NCoef + 1 ] ;
98+ y = new float [ NCoef + 1 ] ;
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 = new float [ NCoef + 1 ] ;
116+ y = new float [ NCoef + 1 ] ;
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 = new float [ NCoef + 1 ] ;
149+ y = new float [ NCoef + 1 ] ;
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 = new float [ NCoef + 1 ] ;
162+ y = new float [ NCoef + 1 ] ;
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 = new float [ NCoef + 1 ] ;
187+ y = new float [ NCoef + 1 ] ;
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
@@ -282,18 +258,18 @@ public enum FilterTypes
282258 public float Filter ( float NewSample )
283259 {
284260 //shift the old samples
285- for ( int n = NCoef ; n > 0 ; n -- )
261+ for ( int n = x . Length - 1 ; n > 0 ; n -- )
286262 {
287263 x [ n ] = x [ n - 1 ] ;
288264 y [ n ] = y [ n - 1 ] ;
289265 }
290266 //Calculate the new output
291267 x [ 0 ] = NewSample ;
292- y [ 0 ] = ( float ) Convert . ToDouble ( ACoef [ 0 ] ) * ( float ) Convert . ToDouble ( x [ 0 ] ) ;
268+ y [ 0 ] = ACoef [ 0 ] * x [ 0 ] ;
293269 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 ] ) ;
270+ y [ 0 ] += ACoef [ n ] * x [ n ] - BCoef [ n ] * y [ n ] ;
295271
296- return ( float ) y [ 0 ] ;
272+ return y [ 0 ] ;
297273 }
298274
299275 /// <summary>
@@ -322,31 +298,29 @@ public float Filter(float NewSample, float samplingPeriod)
322298 throw new NotImplementedException ( "Other filter types are not implemented yet. Try to use constant sampling period and Filter(float NewSample) version of this method." ) ;
323299 }
324300 //shift the old samples
325- for ( int n = NCoef ; n > 0 ; n -- )
301+ for ( int n = x . Length - 1 ; n > 0 ; n -- )
326302 {
327303 x [ n ] = x [ n - 1 ] ;
328304 y [ n ] = y [ n - 1 ] ;
329305 }
330306 //Calculate the new output
331307 x [ 0 ] = NewSample ;
332- y [ 0 ] = ( float ) ACoef [ 0 ] * ( float ) x [ 0 ] ;
308+ y [ 0 ] = ACoef [ 0 ] * x [ 0 ] ;
333309 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- }
310+ y [ 0 ] += ACoef [ n ] * x [ n ] - BCoef [ n ] * y [ n ] ;
337311
338- return ( float ) y [ 0 ] ;
312+ return y [ 0 ] ;
339313 }
340314
341315 /// <summary>
342316 /// Resets all buffers of the filter
343317 /// </summary>
344318 public void Reset ( )
345319 {
346- for ( int i = 0 ; i < x . Count ; i ++ )
320+ for ( int i = 0 ; i < x . Length ; i ++ )
347321 {
348- x [ i ] = 0.0 ;
349- y [ i ] = 0.0 ;
322+ x [ i ] = 0.0f ;
323+ y [ i ] = 0.0f ;
350324 }
351325 }
352326 /// <summary>
0 commit comments