From 54d4ce37fb3bffdb3063413c61848c74e1c4cc50 Mon Sep 17 00:00:00 2001 From: Stefan Schreistetter Date: Mon, 7 Oct 2019 11:11:56 +0200 Subject: [PATCH] Tried to fix performance issue with fixed length decimator. Decimator still does not work properly, likely because processing is still slower than 1,7 ms (1/600). --- GazeWebSocketServer/Program.cs | 4 ++-- TrackerBridge/DSP/GazeDecimator.cs | 87 --------------------------------------------------------------------------------------- TrackerBridge/DSP/GazeDecimatorFixedLength.cs | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ TrackerBridge/DSP/IGazeFilter.cs | 1 + TrackerBridge/GazeDataProcessor.cs | 7 ++++--- TrackerBridge/TrackerBridge.csproj | 2 +- 6 files changed, 101 insertions(+), 93 deletions(-) delete mode 100644 TrackerBridge/DSP/GazeDecimator.cs create mode 100644 TrackerBridge/DSP/GazeDecimatorFixedLength.cs diff --git a/GazeWebSocketServer/Program.cs b/GazeWebSocketServer/Program.cs index be567ae..51f01a3 100644 --- a/GazeWebSocketServer/Program.cs +++ b/GazeWebSocketServer/Program.cs @@ -11,7 +11,7 @@ namespace GazeWebSocketServer public class Program { private static GazeServer gazeServer; - private static GazeDecimator decimator = new GazeDecimator(1, 600); + private static GazeDecimatorFixedLength decimator = new GazeDecimatorFixedLength(1, 600); public static void Main(string[] args) { @@ -20,7 +20,7 @@ namespace GazeWebSocketServer if (args.Length > 0 && args[0] == "-d") { - Task.Run(() => SimulateGazeDataHz(10)); + Task.Run(() => SimulateGazeDataHz(600)); } else { diff --git a/TrackerBridge/DSP/GazeDecimator.cs b/TrackerBridge/DSP/GazeDecimator.cs deleted file mode 100644 index 39eca61..0000000 --- a/TrackerBridge/DSP/GazeDecimator.cs +++ /dev/null @@ -1,87 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace TrackerBridge.DSP -{ - public class GazeDecimator : IGazeFilter - { - private GazeData[] buffer; - private Int32 currentValueIndex; - private Single inputFrequency; - private GazeData? output; - private Single outputFrequency; - - private Object outputLock = new Object(); - private Object inputLock = new Object(); - - public Single InputFrequency { - get { return inputFrequency; } - set { - inputFrequency = value; - Reset(); - } - } - public GazeData? Output { - get { - lock (outputLock) - { - GazeData? temp = null; - if (output.HasValue) - { - temp = output.Value; - output = null; - - } - return temp; - } - } - } - public Single OutputFrequency { - get { return outputFrequency; } - set { - outputFrequency = value; - Reset(); - } - } - - public GazeDecimator(Single outputFrequency, Single inputFrequency) - { - this.outputFrequency = outputFrequency; - this.inputFrequency = inputFrequency; - Reset(); - } - - public void Input(GazeData input) - { - lock (inputLock) - { - buffer[currentValueIndex] = input; - currentValueIndex = (currentValueIndex + 1) % buffer.Length; - - if (currentValueIndex == 0) - { - GazeData sum = buffer[0]; - for (Int32 index = 1; index < buffer.Length; index++) - { - sum += buffer[index]; - } - lock (outputLock) - { - output = sum / buffer.Length; - } - } - } - } - - public void Reset() - { - Int32 bufferLength = Convert.ToInt32(InputFrequency / OutputFrequency); - buffer = new GazeData[bufferLength]; - currentValueIndex = 0; - output = null; - } - } -} diff --git a/TrackerBridge/DSP/GazeDecimatorFixedLength.cs b/TrackerBridge/DSP/GazeDecimatorFixedLength.cs new file mode 100644 index 0000000..6c42673 --- /dev/null +++ b/TrackerBridge/DSP/GazeDecimatorFixedLength.cs @@ -0,0 +1,93 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TrackerBridge.DSP +{ + public class GazeDecimatorFixedLength : IGazeFilter + { + private GazeData[] buffer; + private Int32 currentValueIndex; + private Single inputFrequency; + private GazeData? output; + private Single outputFrequency; + + private Object outputLock = new Object(); + private Object inputLock = new Object(); + + public event EventHandler OutputAvailable; + + public Single InputFrequency { + get { return inputFrequency; } + set { + inputFrequency = value; + Reset(); + } + } + public GazeData? Output { + get { + lock (outputLock) + { + GazeData? temp = null; + if (output.HasValue) + { + temp = output.Value; + output = null; + + } + return temp; + } + } + } + + public Single OutputFrequency { + get { return outputFrequency; } + set { + outputFrequency = value; + Reset(); + } + } + + public GazeDecimatorFixedLength(Single outputFrequency, Single inputFrequency) + { + this.outputFrequency = outputFrequency; + this.inputFrequency = inputFrequency; + Reset(); + } + + public void Input(GazeData input) + { + lock (inputLock) + { + currentValueIndex = (currentValueIndex + 1) % buffer.Length; + } + buffer[currentValueIndex] = input; + + if (currentValueIndex == buffer.Length-1) + { + GazeData sum = buffer[0]; + for (Int32 index = 1; index < buffer.Length; index++) + { + sum += buffer[index]; + } + sum /= buffer.Length; + + lock (outputLock) + { + output = sum; + } + } + + } + + public void Reset() + { + Int32 bufferLength = Convert.ToInt32(InputFrequency / OutputFrequency); + buffer = new GazeData[bufferLength]; + currentValueIndex = -1; + output = null; + } + } +} diff --git a/TrackerBridge/DSP/IGazeFilter.cs b/TrackerBridge/DSP/IGazeFilter.cs index c383b77..31e1fe0 100644 --- a/TrackerBridge/DSP/IGazeFilter.cs +++ b/TrackerBridge/DSP/IGazeFilter.cs @@ -8,6 +8,7 @@ namespace TrackerBridge.DSP { interface IGazeFilter { + event EventHandler OutputAvailable; GazeData? Output { get; } void Input(GazeData input); void Reset(); diff --git a/TrackerBridge/GazeDataProcessor.cs b/TrackerBridge/GazeDataProcessor.cs index b67088f..19fa3b3 100644 --- a/TrackerBridge/GazeDataProcessor.cs +++ b/TrackerBridge/GazeDataProcessor.cs @@ -26,13 +26,14 @@ namespace TrackerBridge } } + public GazeDataProcessor(IBridgeTracker tracker) { tracker.TrackingFrequencyChanged += OnTrackingFrequencyChanged; screenHeight = Convert.ToSingle(SystemParameters.PrimaryScreenHeight); screenWidth = Convert.ToSingle(SystemParameters.PrimaryScreenWidth); gazeFilters = new IGazeFilter[1]; - gazeFilters[0] = new GazeDecimator(outputFrequency, tracker.GetTrackingFrequency()); + gazeFilters[0] = new GazeDecimatorFixedLength(outputFrequency, tracker.GetTrackingFrequency()); } public void Initialize() { @@ -66,9 +67,9 @@ namespace TrackerBridge { foreach(IGazeFilter filter in gazeFilters) { - if(filter.GetType() == typeof(GazeDecimator)) + if(filter.GetType() == typeof(GazeDecimatorFixedLength)) { - GazeDecimator decimator = (GazeDecimator)filter; + GazeDecimatorFixedLength decimator = (GazeDecimatorFixedLength)filter; decimator.InputFrequency = frequency; } } diff --git a/TrackerBridge/TrackerBridge.csproj b/TrackerBridge/TrackerBridge.csproj index 53502a0..c0c5052 100644 --- a/TrackerBridge/TrackerBridge.csproj +++ b/TrackerBridge/TrackerBridge.csproj @@ -48,7 +48,7 @@ - + -- libgit2 0.26.0