Jon B.M 4 лет назад
Родитель
Сommit
0c96f5da95
1 измененных файлов с 9 добавлено и 2 удалено
  1. 9 2
      FreeAPS/Sources/APS/CGM/Calibrations/CalibrationService.swift

+ 9 - 2
FreeAPS/Sources/APS/CGM/Calibrations/CalibrationService.swift

@@ -23,6 +23,13 @@ protocol CalibrationService {
 }
 }
 
 
 final class BaseCalibrationService: CalibrationService, Injectable {
 final class BaseCalibrationService: CalibrationService, Injectable {
+    private enum Config {
+        static let minSlope = 0.66
+        static let maxSlope = 1.5
+        static let minIntercept = -100.0
+        static let maxIntercept = 100.0
+    }
+
     @Injected() var storage: FileStorage!
     @Injected() var storage: FileStorage!
 
 
     private(set) var calibrations: [Calibration] = [] {
     private(set) var calibrations: [Calibration] = [] {
@@ -47,7 +54,7 @@ final class BaseCalibrationService: CalibrationService, Injectable {
         let sum2 = average(multiply(xs, xs)) - pow(average(xs), 2)
         let sum2 = average(multiply(xs, xs)) - pow(average(xs), 2)
         let slope = sum1 / sum2
         let slope = sum1 / sum2
 
 
-        return slope
+        return min(max(slope, Config.minSlope), Config.maxSlope)
     }
     }
 
 
     var intercept: Double {
     var intercept: Double {
@@ -59,7 +66,7 @@ final class BaseCalibrationService: CalibrationService, Injectable {
 
 
         let intercept = average(ys) - slope * average(xs)
         let intercept = average(ys) - slope * average(xs)
 
 
-        return intercept
+        return min(max(intercept, Config.minIntercept), Config.maxIntercept)
     }
     }
 
 
     func calibrate(value: Double) -> Double {
     func calibrate(value: Double) -> Double {