From 70ccb8b4d805e7a3d61c1c67ca24f8b459057f80 Mon Sep 17 00:00:00 2001 From: Milan Hanajik Date: Fri, 18 Aug 2017 21:37:58 +0200 Subject: [PATCH] Statistics support filtering, skipping first samples, etc., ver. 2.15.642 --- .../BenchControl/Sequences/ProcessData.cs | 14 +-- .../BenchControl/Sequences/Statistics.cs | 119 +++++++++++++++--- TestBenchFramework/Properties/AssemblyInfo.cs | 4 +- 3 files changed, 112 insertions(+), 25 deletions(-) diff --git a/TestBenchFramework/BenchControl/Sequences/ProcessData.cs b/TestBenchFramework/BenchControl/Sequences/ProcessData.cs index 8f8e834f2..1bd9e0ba5 100644 --- a/TestBenchFramework/BenchControl/Sequences/ProcessData.cs +++ b/TestBenchFramework/BenchControl/Sequences/ProcessData.cs @@ -79,13 +79,13 @@ namespace TBF.BenchControl.Sequences public static Statistics AmbTempStat = new Statistics(); public static Statistics AmbPressStat = new Statistics(); public static Statistics AmbHumiStat = new Statistics(); - public static Statistics TempUpStat = new Statistics(); - public static Statistics TempDownStat = new Statistics(); - public static Statistics TempDivStat = new Statistics(); - public static Statistics PressUpStat = new Statistics(); - public static Statistics PressDownStat = new Statistics(); - public static Statistics PressDeltaStat = new Statistics(); - public static Statistics RefFlowStat = new Statistics(); + public static Statistics TempUpStat = new Statistics(5, 5, true); + public static Statistics TempDownStat = new Statistics(5, 5, true); + public static Statistics TempDivStat = new Statistics(5, 5, true); + public static Statistics PressUpStat = new Statistics(5, 5, true); + public static Statistics PressDownStat = new Statistics(5, 5, true); + public static Statistics PressDeltaStat = new Statistics(5, 5, true); + public static Statistics RefFlowStat = new Statistics(5, 5, true); public static Statistics TempRefHiStat = new Statistics(); public static Statistics TempRefLoStat = new Statistics(); diff --git a/TestBenchFramework/BenchControl/Sequences/Statistics.cs b/TestBenchFramework/BenchControl/Sequences/Statistics.cs index 9ae611858..9286926fc 100644 --- a/TestBenchFramework/BenchControl/Sequences/Statistics.cs +++ b/TestBenchFramework/BenchControl/Sequences/Statistics.cs @@ -4,26 +4,48 @@ namespace TBF.BenchControl.Sequences { public class Statistics { + public readonly int SkippedSamplesCount; /// 0 = do not skip any samples + public readonly int FilterSize; /// 0 = no filter + public readonly bool MedianFilter; /// true - median filter instead of average + + + double[] fifo; + double[] sorted; double first; double last; double min; double max; double sum; - UInt32 count; + UInt32 totalCount; /// Number of samples passed to Update() + UInt32 count; /// Number of processed and filtered samples + UInt32 fifoCount; /// Number of samples inserted into FIFO public double First { get { return first; } } public double Last { get { return last; } } public double Min { get { return min; } } public double Max { get { return max; } } - public double Average { get { if (Count > 0) return (sum / (double)count); else return 0; } } + public double Average { get { if (count > 0) return (sum / (double)count); else return 0; } } public UInt32 Count { get { return count; } } public double Sum { get { return sum; } } - public Statistics() + public Statistics(int skippedSamplesCount, int filterSize, bool medianFilter) { + this.SkippedSamplesCount = skippedSamplesCount; + this.FilterSize = filterSize; + this.MedianFilter = medianFilter; + Clear(); + if (FilterSize > 0) + { + fifo = new double[FilterSize]; + sorted = new double[FilterSize]; + } + } + + public Statistics() : this(0, 0, false) + { } /// @@ -36,22 +58,11 @@ namespace TBF.BenchControl.Sequences max = double.MinValue; first = 0; last = 0; + totalCount = 0; count = 0; + fifoCount = 0; } - /// - /// Updates statistics - /// - /// New value - public void Update(double value) - { - if (count == 0) first = value; - last = value; - sum += value; - if (value < min) min = value; - if (value > max) max = value; - count++; - } /// /// Updates statistics @@ -70,5 +81,81 @@ namespace TBF.BenchControl.Sequences { Update(box.Val); } + + /// + /// All samples are passed to this function + /// + /// New value + public void Update(double value) + { + if (totalCount >= SkippedSamplesCount) Process(value); + totalCount++; + } + + + /// + /// Only samples after first 'SkippedSamplesCount' samples are passed to this function. + /// Any filtering is done inside this function. + /// + /// New value + void Process(double value) + { + double filteredValue; + if (ApplyFilter(value, out filteredValue)) + { + if (count == 0) first = value; + last = value; + sum += value; + if (value < min) min = value; + if (value > max) max = value; + count++; + } + } + + /// + /// Inserts values to FIFO and calculates filetered value when FIFO is full. + /// Returns true when FIFO is full and calculated values are valid. + /// + /// Input value + /// Output filtered value + /// true when 'fileterdValue' is valid + bool ApplyFilter(double value, out double filteredValue) + { + filteredValue = value; + + if (FilterSize == 0) return true; /// no filter -> input value is passed to output + + + /// Shift values in FIFO buffer, put the new value into fifo[0] + double sum = 0; + for (int i = FilterSize - 1; i > 0; i--) + { + sorted[i] = fifo[i] = fifo[i - 1]; + sum += fifo[i]; + } + sorted[0] = fifo[0] = value; + sum += value; + fifoCount++; + + + if (fifoCount < FilterSize) + { + return false; + } + else + { + if (MedianFilter) + { + Array.Sort(sorted); + filteredValue = sorted[FilterSize / 2]; + } + else + { + filteredValue = sum / FilterSize; + } + + return true; + } + } } } diff --git a/TestBenchFramework/Properties/AssemblyInfo.cs b/TestBenchFramework/Properties/AssemblyInfo.cs index 8b73a32a1..4c542fea7 100644 --- a/TestBenchFramework/Properties/AssemblyInfo.cs +++ b/TestBenchFramework/Properties/AssemblyInfo.cs @@ -29,5 +29,5 @@ using System.Runtime.InteropServices; // Build Number // Revision // -[assembly: AssemblyVersion("2.15.641.1")] -[assembly: AssemblyFileVersion("2.15.641.1")] +[assembly: AssemblyVersion("2.15.642.1")] +[assembly: AssemblyFileVersion("2.15.642.1")]