@@ -499,6 +499,83 @@ public double GetOversampledFactor(out int keyIndex)
499
499
}
500
500
}
501
501
502
+ // If both flatlines and oversamples are present, disregard the flatline data, keeping in mind that flatlines can 'amputate' part of an oversampled group of points.
503
+ int flatlinepoints = 0 ;
504
+ int flatlines = 0 ;
505
+ for ( int i = 2 ; i < consecutiveOversamplesList . Count ( ) - flatlines ; i ++ )
506
+ {
507
+ if ( ( consecutiveOversamplesList [ i ] > consecutiveOversamplesList [ i - 1 ] + 2 ) & ( consecutiveOversamplesList [ i ] > consecutiveOversamplesList [ i - 2 ] + 2 ) )
508
+ {
509
+ flatlinepoints += consecutiveOversamplesList [ i ] ;
510
+ flatlines ++ ;
511
+ consecutiveOversamplesList . RemoveAt ( i ) ;
512
+ consecutiveLargeIndexDiffsStartIndexList . RemoveAt ( i ) ;
513
+ }
514
+ }
515
+ if ( consecutiveOversamplesList . Count ( ) > 3 )
516
+ {
517
+ if ( ( consecutiveOversamplesList [ 0 ] > consecutiveOversamplesList [ 1 ] + 2 ) & ( consecutiveOversamplesList [ 0 ] > consecutiveOversamplesList [ 2 ] + 2 ) )
518
+ {
519
+ flatlinepoints += consecutiveOversamplesList [ 0 ] ;
520
+ flatlines ++ ;
521
+ consecutiveOversamplesList . RemoveAt ( 0 ) ;
522
+ consecutiveLargeIndexDiffsStartIndexList . RemoveAt ( 0 ) ;
523
+ if ( consecutiveOversamplesList [ 0 ] > consecutiveOversamplesList [ 1 ] + 2 )
524
+ {
525
+ flatlinepoints += consecutiveOversamplesList [ 0 ] ;
526
+ flatlines ++ ;
527
+ consecutiveOversamplesList . RemoveAt ( 0 ) ;
528
+ consecutiveLargeIndexDiffsStartIndexList . RemoveAt ( 0 ) ;
529
+ }
530
+ }
531
+ else if ( ( consecutiveOversamplesList [ 1 ] > consecutiveOversamplesList [ 0 ] + 2 ) & ( consecutiveOversamplesList [ 1 ] > consecutiveOversamplesList [ 2 ] + 2 ) )
532
+ {
533
+ flatlinepoints += consecutiveOversamplesList [ 1 ] ;
534
+ flatlines ++ ;
535
+ consecutiveOversamplesList . RemoveAt ( 1 ) ;
536
+ consecutiveLargeIndexDiffsStartIndexList . RemoveAt ( 1 ) ;
537
+ }
538
+ }
539
+
540
+ // Revise the oversampled factor calculation if necessary
541
+ if ( flatlines > 0 )
542
+ {
543
+ // Flatlines will cause some oversampled points to be mislabeled as flatlinepoints on each end.
544
+ // These should be taken into account for the calculation. Half an oversample on each end of the flatline is an average value.
545
+ oversampledFactor = ( double ) ( N - flatlinepoints ) / ( double ) ( N - indOversampled . Count ( ) ) ;
546
+ oversampledFactor = ( double ) ( N - ( flatlinepoints - ( flatlines * oversampledFactor ) ) ) / ( double ) ( N - indOversampled . Count ( ) ) ;
547
+ if ( oversampledFactor <= 1 )
548
+ {
549
+ keyIndex = 0 ;
550
+ return 1 ;
551
+ }
552
+ else if ( ( consecutiveOversamplesList . Max ( ) <= ( int ) Math . Ceiling ( oversampledFactor ) + 1 ) & ( consecutiveOversamplesList . Max ( ) >= ( int ) Math . Floor ( oversampledFactor ) - 1 ) )
553
+ {
554
+ keyIndex = 0 ;
555
+ int numConsecutiveSmallOversamples = 0 ;
556
+ int mostConsecutiveSmallOversamples = 0 ;
557
+ int smallestOversample = ( int ) Math . Floor ( oversampledFactor ) ;
558
+ for ( int i = 0 ; i < consecutiveOversamplesList . Count ( ) ; i ++ )
559
+ {
560
+ if ( consecutiveOversamplesList [ i ] == smallestOversample )
561
+ {
562
+ numConsecutiveSmallOversamples ++ ;
563
+ if ( numConsecutiveSmallOversamples > mostConsecutiveSmallOversamples )
564
+ {
565
+ mostConsecutiveSmallOversamples = numConsecutiveSmallOversamples ;
566
+ keyIndex = Math . Max ( consecutiveLargeIndexDiffsStartIndexList [ i - mostConsecutiveSmallOversamples + 1 ] - 1 , 0 ) ;
567
+ }
568
+ }
569
+ }
570
+ return oversampledFactor ;
571
+ }
572
+ else
573
+ {
574
+ keyIndex = 0 ;
575
+ return 1 ;
576
+ }
577
+ }
578
+
502
579
// Return the oversampled factor, or 1 if no evenly spread oversampling is found.
503
580
if ( ( mostConsecutiveOversamples + 1 == ( int ) Math . Ceiling ( oversampledFactor ) ) &
504
581
( largestIndexDiff - secondLargestIndexDiff < 3 ) )
0 commit comments