/* * DataWindowAverager.cs * * Copyright © 2009 * Diversified Technical Systems, Inc. * All Rights Reserved */ using System; using System.Linq; using DTS.Common.Utilities.DotNetProgrammingConstructs; namespace DTS.Common.Utilities { /// /// Time averaging data over a specific window /// there can be pretrigger time and begin time can be used to compute the start /// of the averaging window /// public partial class DataWindowAverager : Exceptional { /// /// /// Initialize an instance of this data window averager class. /// /// /// /// Get the time value (sec) of the start of the averaging window. /// /// /// /// Get the time value (sec) of the end of the averaging window. /// /// /// /// Get the pre-trigger time (sec). /// /// /// /// Get the sample rate (Hz). /// /// /// /// Get the default ADC value to be returned if the specified window /// is invalid. /// /// public DataWindowAverager( double beginTimeSec, double endTimeSec, double preTriggerTimeSec, double sampleRateHz, short defaultValue) { try { BeginTimeSec = beginTimeSec; EndTimeSec = endTimeSec; PreTriggerTimeSec = preTriggerTimeSec; SampleRateHz = sampleRateHz; DefaultValue = defaultValue; } catch (System.Exception ex) { throw new Exception("encountered problem constructing " + GetType().FullName, ex); } } /// /// Get the time value (sec) of the start of the averaging window. /// public double BeginTimeSec { get => _beginTimeSec.Value; private set => _beginTimeSec.Value = value; } private readonly Property _beginTimeSec = new Property( typeof(DataWindowAverager).FullName + ".BeginTimeSec", 0.0, false ); /// /// Get the time value (sec) of the end of the averaging window. /// public double EndTimeSec { get => _endTimeSec.Value; private set => _endTimeSec.Value = value; } private readonly Property _endTimeSec = new Property( typeof(DataWindowAverager).FullName + ".EndTimeSec", 0.0, false ); /// /// Get the pre-trigger time (sec). /// public double PreTriggerTimeSec { get => _preTriggerTimeSec.Value; private set => _preTriggerTimeSec.Value = value; } private readonly Property _preTriggerTimeSec = new Property( typeof(DataWindowAverager).FullName + ".PreTriggerTimeSec", 0.0, false ); /// /// Get the sample rate (Hz). /// public double SampleRateHz { get => _sampleRateHz.Value; private set => _sampleRateHz.Value = value; } private readonly Property _sampleRateHz = new Property( typeof(DataWindowAverager).FullName + ".SampleRateHz", 0.0, false ); /// /// Get the default ADC value to be returned if the specified window /// is invalid. /// public short DefaultValue { get => _defaultValue.Value; private set => _defaultValue.Value = value; } private readonly Property _defaultValue = new Property( typeof(DataWindowAverager).FullName + ".DefaultValue", 0, false ); /// /// Compute the average of the window represented by the current state /// of this object over the specified window values. /// /// /// /// An array of values to be window-averaged. /// /// /// /// The average of the specified value falling within this object's window. /// /// public short DetermineWindowAverage(short[] windowValues) { try { var numSamples = (ulong)((EndTimeSec - BeginTimeSec) * SampleRateHz) + 1; var windowSamples = new double[numSamples]; var startingIndex = ((long)((PreTriggerTimeSec + BeginTimeSec) * SampleRateHz)); if (0 > startingIndex) throw new WindowDoesNotExistException( "the specified averaging window (" + BeginTimeSec + "s to " + EndTimeSec + "s) does not exist in the specified data set" ); try { for (ulong i = 0; i < numSamples; i++) windowSamples[i] = windowValues[i + (ulong)startingIndex]; return ((short)windowSamples.Average()); } catch (IndexOutOfRangeException) { throw new WindowDoesNotExistException("the specified averaging window lies outside of the supplied data"); } } catch (System.Exception ex) { throw new Exception("encountered problem determining window average", ex); } } /// /// Compute the average of the window represented by the current state /// of this object over the specified window values. /// /// /// /// An array of values to be window-averaged. /// /// /// /// The average of the specified value falling within this object's window. /// /// public short DetermineWindowAverage(double[] windowValues) { try { var numSamples = (ulong)((EndTimeSec - BeginTimeSec) * SampleRateHz) + 1; var windowSamples = new double[numSamples]; var startingIndex = ((long)((PreTriggerTimeSec + BeginTimeSec) * SampleRateHz)); if (0 > startingIndex) throw new WindowDoesNotExistException( "the specified averaging window (" + BeginTimeSec + "s to " + EndTimeSec + "s) does not exist in the given data set" ); try { for (ulong i = 0; i < numSamples; i++) windowSamples[i] = windowValues[i + (ulong)startingIndex]; var ave = (short)windowSamples.Average(); return (ave); } catch (IndexOutOfRangeException) { throw new WindowDoesNotExistException("the specified averaging window lies outside of the supplied data"); } } catch (System.Exception ex) { throw new Exception("encountered problem determining window average", ex); } } } }