Files
DP44/Common/DTS.Common.Utilities/Math.Iso.Differentiation.cs
2026-04-17 14:55:32 -04:00

147 lines
5.6 KiB
C#

/*
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
{
////////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Representation of the ISO-based differentiation
/// System.Collections.Generic.IList of <see cref="double"/>
/// -based operation.
/// </summary>
public class Differentiation : DoubleListOperation
{
////////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Get/set the <see cref="double"/> 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.
/// </summary>
public double SampleRate
{
get => _SampleRate.Value;
set => _SampleRate.Value = value;
}
private Property<double> _SampleRate
= new Property<double>(
typeof(Differentiation).FullName + ".SampleRate",
0.0,
false
);
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Initialize an instance of the Differentiation class.
/// </summary>
///
/// <param name="domain">
/// The System.Collection.Generic.IList of <see cref="double"/>s
/// domain for this operation.
/// </param>
///
public Differentiation(IList<double> domain)
: base(domain)
{
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Initialize an instance of the Iso.ChannelDifferentiation class.
/// </summary>
///
/// <param name="domain">
/// The System.Collections.Generic.IList of <see cref="double"/>s
/// domain for this operation.
/// </param>
///
/// <param name="sampleRate">
/// The <see cref="double"/> sample rate of the data to be differentiated.
/// </param>
///
public Differentiation(IList<double> domain, double sampleRate)
: this(domain)
{
try { SampleRate = sampleRate; }
catch (System.Exception ex)
{
throw new Exception("encountered problem constructing " + GetType().FullName, ex);
}
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Get the result of the operation on the
/// System.Collections.Generic.IList of <see cref="double"/>s
/// domain representation.
/// </summary>
public override IList<double> 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);
}
}
}
////////////////////////////////////////////////////////////////////////////////
}
////////////////////////////////////////////////////////////////////////////////
}