init
This commit is contained in:
@@ -0,0 +1,146 @@
|
||||
/*
|
||||
* 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
|
||||
{
|
||||
/// <summary>
|
||||
/// A class to represent the average value of a set of shorts over time.
|
||||
/// </summary>
|
||||
public class AverageShortValueOverTime
|
||||
: AverageValueOverTime<short>
|
||||
{
|
||||
/// <summary>
|
||||
/// Initialize an instance of the AverageShortValueOverTime class, assuming that no
|
||||
/// averaging has been done yet.
|
||||
/// </summary>
|
||||
/// <param name="initialTime">
|
||||
/// The <see cref="double"/> begin time of the average (using the time units specified
|
||||
/// by the "TimeUnitSymbol" property.
|
||||
/// </param>
|
||||
///
|
||||
/// <param name="sampleRate">
|
||||
/// The <see cref="double"/> sample rate of the items being averaged (using the time units
|
||||
/// specifed by the "TimeUnitSymbol" property.
|
||||
/// </param>
|
||||
///
|
||||
/// <param name="timeUnitSymbol">
|
||||
/// The <see cref="string"/> time unit description for begin/end times and the sample rate.
|
||||
/// </param>
|
||||
///
|
||||
public AverageShortValueOverTime(double initialTime,
|
||||
double sampleRate,
|
||||
string timeUnitSymbol)
|
||||
: base(initialTime,
|
||||
sampleRate,
|
||||
0,
|
||||
timeUnitSymbol)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initialize an instance of the AverageShortValueOverTime class.
|
||||
/// </summary>
|
||||
///
|
||||
/// <param name="beginTime">
|
||||
/// The <see cref="double"/> begin time of the average (using the time units specified
|
||||
/// by the "TimeUnitSymbol" property.
|
||||
/// </param>
|
||||
///
|
||||
/// <param name="endTime">
|
||||
/// The <see cref="double"/> end time of the average (using the time units specified
|
||||
/// by the "TimeUnitSymbol" property.
|
||||
/// </param>
|
||||
///
|
||||
/// <param name="sampleRate">
|
||||
/// The <see cref="double"/> sample rate of the items being averaged (using the time units
|
||||
/// specifed by the "TimeUnitSymbol" property.
|
||||
/// </param>
|
||||
///
|
||||
/// <param name="initialAverage">
|
||||
/// The <see cref="short"/> pre-existing average to be appended to.
|
||||
/// </param>
|
||||
///
|
||||
/// <param name="timeUnitSymbol">
|
||||
/// The <see cref="string"/> time unit description for begin/end times and the sample rate.
|
||||
/// </param>
|
||||
///
|
||||
public AverageShortValueOverTime(double beginTime,
|
||||
double endTime,
|
||||
double sampleRate,
|
||||
short initialAverage,
|
||||
string timeUnitSymbol)
|
||||
: base(beginTime,
|
||||
endTime,
|
||||
sampleRate,
|
||||
initialAverage,
|
||||
timeUnitSymbol)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Append the specified <see cref="T:List"/> of values to the running average.
|
||||
/// </summary>
|
||||
///
|
||||
/// <param name="values">
|
||||
/// The <see cref="T:List"/> of <see cref="short"/>s to be appended to the running average.
|
||||
/// </param>
|
||||
///
|
||||
public override void AppendValues(List<short> values)
|
||||
{
|
||||
AppendValues(values.ToArray());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Append the specified array of values to the running average.
|
||||
/// </summary>
|
||||
///
|
||||
/// <param name="values">
|
||||
/// The array of <see cref="short"/>s to be appended to the running average.
|
||||
/// </param>
|
||||
///
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user