/* Math.Iso.Differentiation.cs 24 November 2009 - Adapted from old Dave codebase to generic DTS utility, with appropriate name change. $Log: Math.Iso.ChannelDifferentiation.cs,v $ Revision 1.3 2007/04/20 17:40:42 Paul Hrissikopoulos Fixed bad range checking in sample rate property set accessor. Revision 1.2 2007/02/05 17:17:08 Paul Hrissikopoulos Finished installing generic channel math framework + basic calculus operations. Revision 1.1 2007/02/02 22:34:09 Paul Hrissikopoulos Added framework for DAVE channel math operators. Copyright © 2007 Diversified Technical Systems, Inc. All Rights Reserved */ using System; using System.Collections.Generic; using System.Text; using DTS.Common.Utilities; using DTS.Common.Utilities.DotNetProgrammingConstructs; namespace DTS.Common.Utilities.Math.Iso { //////////////////////////////////////////////////////////////////////////////// /// /// Representation of the ISO-based differentiation /// System.Collections.Generic.IList of /// -based operation. /// public class Differentiation : DoubleListOperation { //////////////////////////////////////////////////////////////////////////////// /// /// Get/set the time resolution of this differentiation. /// Should probably be the length of one "sample" of digital data seeing as /// we're trying to approximate a continuous domain. /// public double SampleRate { get => _SampleRate.Value; set => _SampleRate.Value = value; } private Property _SampleRate = new Property( typeof(Differentiation).FullName + ".SampleRate", 0.0, false ); //////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// /// /// Initialize an instance of the Differentiation class. /// /// /// /// The System.Collection.Generic.IList of s /// domain for this operation. /// /// public Differentiation(IList domain) : base(domain) { } //////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// /// /// Initialize an instance of the Iso.ChannelDifferentiation class. /// /// /// /// The System.Collections.Generic.IList of s /// domain for this operation. /// /// /// /// The sample rate of the data to be differentiated. /// /// public Differentiation(IList domain, double sampleRate) : this(domain) { try { SampleRate = sampleRate; } catch (System.Exception ex) { throw new Exception("encountered problem constructing " + GetType().FullName, ex); } } //////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// /// /// Get the result of the operation on the /// System.Collections.Generic.IList of s /// domain representation. /// public override IList Range { get { try { if (null == Domain) throw new ArgumentNullException("attempted to differentiate null domain reference"); else if (0 >= SampleRate) throw new ArgumentOutOfRangeException("attempted to differentiate with sample rate <= zero"); else if (!(Domain is ICloneable)) throw new InvalidCastException("the domain type for " + typeof(Differentiation).FullName + " must be ICloneable"); else { var IndexOfLastDomainValue = Domain.Count - 1; var range = ((ICloneable)Domain).Clone() as double[]; var deltaT = SampleRate / 12.0; for (var t = 2; t <= IndexOfLastDomainValue - 2; t++) range[t] = ((Domain[t + 1] - Domain[t - 1]) * 8 - Domain[t + 2] + Domain[t - 2]) * deltaT; Domain[IndexOfLastDomainValue - 1] = 0; Domain[IndexOfLastDomainValue] = 0; Domain[0] = 0; Domain[1] = 0; return range; } } catch (System.Exception ex) { throw new Exception("encountered problem doing " + GetType().FullName + " operation", ex); } } } //////////////////////////////////////////////////////////////////////////////// } //////////////////////////////////////////////////////////////////////////////// }