/// <summary> /// Called when the calibration points are changed. /// </summary> /// <param name="calibrationPoints">The new calibration points.</param> protected virtual void OnCalibrationPointsChanged(CalibrationPoint[] calibrationPoints) { }
/// <summary> /// Creates an instance of Windows.Devices.Sensors.Calibration /// with the specified point count, minimum and maximum /// reading values and calibration points. /// </summary> /// <param name="pointCount">The required number of calibration points.</param> /// <param name="minimum">The minimum adjusted reading value allowed.</param> /// <param name="maximum">The maximum adjusted reading value allowed.</param> /// <param name="calibrationPoints">The calibrations points used to adjust the reading.</param> public Calibration(int pointCount, float minimum, float maximum, CalibrationPoint[] calibrationPoints) { this.CalibrationPointCount = pointCount; this.Minimum = minimum; this.Maximum = maximum; this.CalibrationPoints = calibrationPoints; }
/// <summary> /// Creates an instance of Windows.Devices.Sensors.NonLinearCalibration /// with the specified point count, minimum and maximum /// reading values and calibration points. /// </summary> /// <param name="minimum">The minimum adjusted reading value allowed.</param> /// <param name="maximum">The maximum adjusted reading value allowed.</param> /// <param name="calibrationPoints">The calibrations points used to adjust the reading.</param> public LinearCalibration(float minimum, float maximum, CalibrationPoint[] calibrationPoints) : base(_calibrationPointCount, minimum, maximum, calibrationPoints) { }
protected override void OnCalibrationPointsChanged(CalibrationPoint[] calibrationPoints) { this.CalculateFormulaVariables(calibrationPoints, out _m, out _b); }
/// <summary> /// Calculates the values a, b and c in the formula y = mx + b /// </summary> public virtual void CalculateFormulaVariables(CalibrationPoint[] calibrationPoints, out float m, out float b) { if (calibrationPoints.Length == this.CalibrationPointCount) { // *** // *** These value are cast into variables here to hopefully // *** makes this more readable // *** float x1 = calibrationPoints[0].X; float x2 = calibrationPoints[1].X; float y1 = calibrationPoints[0].Y; float y2 = calibrationPoints[1].Y; m = (y2 - y1) / (x2 - x1); float b1 = m * x1; float b2 = m * x2; b = (b1 + b2) / 2; } else if (calibrationPoints.Length < this.CalibrationPointCount) { // *** // *** Throw a specific exception letting the caller // *** know there are less than the three required points. // *** throw new ArgumentOutOfRangeException(string.Format("There are too few points defined. {0} must be an array of three points.", nameof(calibrationPoints))); } else { // *** // *** Throw a specific exception letting the caller // *** know there are more than the three required points. // *** throw new ArgumentOutOfRangeException(string.Format("There are too many points defined. {0} must be an array of three points.", nameof(calibrationPoints))); } }
/// <summary> /// Calculates the values a, b and c in the formula y = ax² + bx + c /// </summary> public virtual void CalculateFormulaVariables(CalibrationPoint[] calibrationPoints, out float a, out float b, out float c) { if (calibrationPoints.Length == this.CalibrationPointCount) { // *** // *** These value are cast into variables here to hopefully // *** makes this more readable // *** float x1 = calibrationPoints[0].X; float x2 = calibrationPoints[1].X; float x3 = calibrationPoints[2].X; float y1 = calibrationPoints[0].Y; float y2 = calibrationPoints[1].Y; float y3 = calibrationPoints[2].Y; float denom = (x1 - x2) * (x1 - x3) * (x2 - x3); a = (x3 * (y2 - y1) + x2 * (y1 - y3) + x1 * (y3 - y2)) / denom; b = (x3 * x3 * (y1 - y2) + x2 * x2 * (y3 - y1) + x1 * x1 * (y2 - y3)) / denom; c = (x2 * x3 * (x2 - x3) * y1 + x3 * x1 * (x3 - x1) * y2 + x1 * x2 * (x1 - x2) * y3) / denom; } else if (calibrationPoints.Length < this.CalibrationPointCount) { // *** // *** Throw a specific exception letting the caller // *** know there are less than the three required points. // *** throw new ArgumentOutOfRangeException(string.Format("There are too few points defined. {0} must be an array of three points.", nameof(calibrationPoints))); } else { // *** // *** Throw a specific exception letting the caller // *** know there are more than the three required points. // *** throw new ArgumentOutOfRangeException(string.Format("There are too many points defined. {0} must be an array of three points.", nameof(calibrationPoints))); } }
/// <summary> /// Creates an instance of Windows.Devices.Sensors.AdjustedNonLinearCalibration /// with the specified point count, maximum /// reading values and calibration points. /// </summary> /// <param name="maximum">The maximum adjusted reading value allowed.</param> /// <param name="calibrationPoints">The calibrations points used to adjust the reading.</param> public AdjustedNonLinearCalibration(float maximum, CalibrationPoint[] calibrationPoints) : base(_calibrationPointCount, maximum, calibrationPoints) { }