Files
DP44/Common/DTS.Common.Calculations/.svn/pristine/8d/8dc28d306311cd56a44add38f5a086e0d7866451.svn-base
2026-04-17 14:55:32 -04:00

48 lines
1.8 KiB
Plaintext

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