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; } } }