@@ -230,7 +230,7 @@ private static void AddOrInsert(ref Dictionary<uint, BitArray> table, int channe
230230 }
231231 }
232232
233- internal static Rhs2116StepSize GetStepSizeWithMinError ( IEnumerable < Rhs2116StepSize > stepSizes , Rhs2116Stimulus [ ] stimuli , Rhs2116StepSize currentStepSize )
233+ internal static Rhs2116StepSize GetStepSizeWithMinError ( IEnumerable < Rhs2116StepSize > stepSizes , Rhs2116Stimulus [ ] stimuli , double requestedAmplitude , Rhs2116StepSize currentStepSize )
234234 {
235235 var numberOfStepSizes = stepSizes . Count ( ) ;
236236 var maxError = new List < double > ( numberOfStepSizes ) ;
@@ -239,7 +239,9 @@ internal static Rhs2116StepSize GetStepSizeWithMinError(IEnumerable<Rhs2116StepS
239239
240240 static double CalculateError ( double amplitude , double stepSizeuA )
241241 {
242- return Math . Abs ( ( amplitude - ( stepSizeuA * Math . Round ( amplitude / stepSizeuA ) ) ) / amplitude ) ;
242+ return Math . Round ( amplitude / stepSizeuA ) > 0 && Math . Round ( amplitude / stepSizeuA ) <= 255
243+ ? Math . Abs ( ( amplitude - ( stepSizeuA * Math . Round ( amplitude / stepSizeuA ) ) ) / amplitude )
244+ : double . PositiveInfinity ;
243245 }
244246
245247 for ( int s = 0 ; s < numberOfStepSizes ; s ++ )
@@ -253,16 +255,15 @@ static double CalculateError(double amplitude, double stepSizeuA)
253255 var anodicAmp = stimuli [ c ] . AnodicAmplitudeSteps * currentStepSizeuA ;
254256 var cathodicAmp = stimuli [ c ] . CathodicAmplitudeSteps * currentStepSizeuA ;
255257
256- var anodicError = anodicAmp < stepSizesuA [ s ] || anodicAmp > stepSizesuA [ s ] * 255 ?
257- double . PositiveInfinity :
258- CalculateError ( anodicAmp , stepSizesuA [ s ] ) ;
259-
260- var cathodicError = cathodicAmp < stepSizesuA [ s ] || cathodicAmp > stepSizesuA [ s ] * 255 ?
261- double . PositiveInfinity :
262- CalculateError ( cathodicAmp , stepSizesuA [ s ] ) ;
258+ var anodicError = CalculateError ( anodicAmp , stepSizesuA [ s ] ) ;
259+ var cathodicError = CalculateError ( cathodicAmp , stepSizesuA [ s ] ) ;
263260
264261 maxError [ s ] = Math . Max ( maxError [ s ] , Math . Max ( anodicError , cathodicError ) ) ;
265262 }
263+
264+ var requestedError = CalculateError ( requestedAmplitude , stepSizesuA [ s ] ) ;
265+
266+ maxError [ s ] = Math . Max ( maxError [ s ] , requestedError ) ;
266267 }
267268
268269 if ( maxError . Distinct ( ) . Count ( ) == 1 )
@@ -271,7 +272,20 @@ static double CalculateError(double amplitude, double stepSizeuA)
271272 return stepSizes . OrderBy ( s => Math . Abs ( GetStepSizeuA ( s ) - currentStepSizeuA ) ) . First ( ) ;
272273 }
273274
274- var optimalStepIndex = maxError . IndexOf ( maxError . Min ( ) ) ;
275+ var minimumError = maxError . Min ( ) ;
276+ var optimalStepIndex = maxError
277+ . Select ( ( e , ind ) =>
278+ {
279+ if ( e == minimumError )
280+ {
281+ return ( Min : true , Ind : ind ) ;
282+ }
283+ return ( Min : false , Ind : - 1 ) ;
284+ } )
285+ . Where ( e => e . Min )
286+ . OrderBy ( e => Math . Abs ( GetStepSizeuA ( stepSizes . ElementAt ( e . Ind ) ) - currentStepSizeuA ) )
287+ . Select ( e => e . Ind )
288+ . First ( ) ;
275289
276290 return stepSizes . ElementAt ( optimalStepIndex ) ;
277291 }
0 commit comments