using System.Collections.Generic; using System.Linq; namespace DTS.Common.Calculations { public static class Resultant { /// /// generates a resultant channel given input vectors /// Will throw an assertion if lengths of data don't match, or if /// units don't match /// /// List of channels to combine via /// sum of squares /// /// /// resultant vector from sum of squares of inputs /// public static ChannelData GenerateResultantChannel(List 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(); //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; } } }