48 lines
1.8 KiB
C#
48 lines
1.8 KiB
C#
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Linq;
|
|||
|
|
|
|||
|
|
namespace DTS.Common.Calculations
|
|||
|
|
{
|
|||
|
|
public static class Resultant
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// generates a resultant channel given input vectors
|
|||
|
|
/// Will throw an assertion if lengths of data don't match, or if
|
|||
|
|
/// units don't match
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="channels">List of channels to combine via
|
|||
|
|
/// sum of squares
|
|||
|
|
/// </param>
|
|||
|
|
/// <returns>
|
|||
|
|
/// resultant vector from sum of squares of inputs
|
|||
|
|
/// </returns>
|
|||
|
|
public static ChannelData GenerateResultantChannel(List<ChannelData> channels)
|
|||
|
|
{
|
|||
|
|
int length = (from ch in channels select ch.FilteredEU.Length).Max();
|
|||
|
|
foreach (var ch in channels) { System.Diagnostics.Trace.Assert(ch.FilteredEU.Length == length); }
|
|||
|
|
//make sure the channels are all the same units and lengths,
|
|||
|
|
//we assert here since the higher level should be expected to provide clean inputs
|
|||
|
|
var units = (from ch in channels select ch.Units).Distinct();
|
|||
|
|
System.Diagnostics.Trace.Assert(units.Count() == 1);
|
|||
|
|
|
|||
|
|
var values = new List<double>();
|
|||
|
|
|
|||
|
|
//in a future version we can parallize this for efficiency
|
|||
|
|
//values.Add will need to be replaced by an indexer when we do
|
|||
|
|
for (int i = 0; i < length; i++)
|
|||
|
|
{
|
|||
|
|
double d = 0D;
|
|||
|
|
foreach (var ch in channels)
|
|||
|
|
{
|
|||
|
|
d += ch.FilteredEU[i] * ch.FilteredEU[i];
|
|||
|
|
}
|
|||
|
|
values.Add(System.Math.Sqrt(d));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
var cd = new ChannelData(units.First());
|
|||
|
|
cd.FilteredEU = values.ToArray();
|
|||
|
|
return cd;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|