/* * AverageShortValueOverTime.cs * * Copyright © 2009 * Diversified Technical Systems, Inc. * All Rights Reserved */ using DTS.Common.Utilities.Logging; using System.Collections.Generic; using System.Linq; namespace DTS.Common.Utilities { /// /// A class to represent the average value of a set of shorts over time. /// public class AverageShortValueOverTime : AverageValueOverTime { /// /// Initialize an instance of the AverageShortValueOverTime class, assuming that no /// averaging has been done yet. /// /// /// The begin time of the average (using the time units specified /// by the "TimeUnitSymbol" property. /// /// /// /// The sample rate of the items being averaged (using the time units /// specifed by the "TimeUnitSymbol" property. /// /// /// /// The time unit description for begin/end times and the sample rate. /// /// public AverageShortValueOverTime(double initialTime, double sampleRate, string timeUnitSymbol) : base(initialTime, sampleRate, 0, timeUnitSymbol) { } /// /// Initialize an instance of the AverageShortValueOverTime class. /// /// /// /// The begin time of the average (using the time units specified /// by the "TimeUnitSymbol" property. /// /// /// /// The end time of the average (using the time units specified /// by the "TimeUnitSymbol" property. /// /// /// /// The sample rate of the items being averaged (using the time units /// specifed by the "TimeUnitSymbol" property. /// /// /// /// The pre-existing average to be appended to. /// /// /// /// The time unit description for begin/end times and the sample rate. /// /// public AverageShortValueOverTime(double beginTime, double endTime, double sampleRate, short initialAverage, string timeUnitSymbol) : base(beginTime, endTime, sampleRate, initialAverage, timeUnitSymbol) { } /// /// Append the specified of values to the running average. /// /// /// /// The of s to be appended to the running average. /// /// public override void AppendValues(List values) { AppendValues(values.ToArray()); } /// /// Append the specified array of values to the running average. /// /// /// /// The array of s to be appended to the running average. /// /// public override void AppendValues(short[] values) { if (0 == values.Length) { APILogger.Log($"AverageShortValueOverTime.AppendValues - no values to append"); return; } // // Get the average of the appended values by themselves. // var numAppendixSamples = values.Length; var appendixSamples = new double[numAppendixSamples]; for (var i = 0; i < numAppendixSamples; i++) appendixSamples[i] = values[i]; var appendixAverage = appendixSamples.Average(); // // Get the previous average and number of samples. // var numPreviousSamples = (ulong)((EndTime - BeginTime) * SampleRate); double previousAverage = CurrentAverage; // // Now combine them with the previous average values (with the // appropriate weighting) to get a new combined average. // var numTotalSamples = numPreviousSamples + (ulong)numAppendixSamples; var combinedAverage = ((previousAverage * numPreviousSamples) + (appendixAverage * numAppendixSamples)) / numTotalSamples; // Save our new average. CurrentAverage = (short)combinedAverage; // Update the end time to take into account the appended data. EndTime += (numAppendixSamples / SampleRate); } } }