Skip to content
Derivative.md

Derivative

Filtered derivative approximation with configurable time constant and gain.

Implements a filtered derivative that approximates the differentiation operation while avoiding noise amplification. The derivative approximation is implemented using a first-order filter with configurable time constant and gain parameters.

kTksT2+T=kssT+1

The smaller the time constant T, the closer the approximation to an ideal derivative, though this increases sensitivity to noise.

This component extends from SISO

Usage

Derivative(x0=0, T, k=1.0)

Parameters:

NameDescriptionUnitsDefault value
x0Initial value of the derivative-filter state0
TTime constant of the filter (smaller values give better derivative approximation but more noise sensitivity)
kGain factor applied to the derivative approximation1

Connectors

  • u - This connector represents a real signal as an input to a component (RealInput)

  • y - This connector represents a real signal as an output from a component (RealOutput)

Variables

NameDescriptionUnits
xInternal state variable of the first-order filter

Behavior

dx(t)dt=u(t)x(t)Ty(t)=k(u(t)x(t))T

Source

dyad
# Filtered derivative approximation with configurable time constant and gain.
#
# Implements a filtered derivative that approximates the differentiation operation while avoiding
# noise amplification. The derivative approximation is implemented using a first-order filter with
# configurable time constant and gain parameters.
#
# ```math
# \frac{k}{T} - \frac{k}{sT^2 + T} = \frac{ks}{sT + 1}
# ```
#
# The smaller the time constant T, the closer the approximation to an ideal derivative, though this
# increases sensitivity to noise.
component Derivative
  extends SISO
  # Internal state variable of the first-order filter
  variable x::Real
  # Initial value of the derivative-filter state
  parameter x0::Real = 0
  # Time constant of the filter (smaller values give better derivative approximation but more noise sensitivity)
  parameter T::Real
  # Gain factor applied to the derivative approximation
  parameter k::Real = 1.0
relations
  initial x = x0
  der(x) = (u-x)/T
  y = (k/T)*(u-x)
metadata {"Dyad": {"icons": {"default": "dyad://BlockComponents/Derivative.svg"}}}
end
Flattened Source
dyad
# Filtered derivative approximation with configurable time constant and gain.
#
# Implements a filtered derivative that approximates the differentiation operation while avoiding
# noise amplification. The derivative approximation is implemented using a first-order filter with
# configurable time constant and gain parameters.
#
# ```math
# \frac{k}{T} - \frac{k}{sT^2 + T} = \frac{ks}{sT + 1}
# ```
#
# The smaller the time constant T, the closer the approximation to an ideal derivative, though this
# increases sensitivity to noise.
component Derivative
  # Input signal port
  u = RealInput() [{
    "Dyad": {
      "placement": {"icon": {"iconName": "input", "x1": -50, "y1": 450, "x2": 50, "y2": 550}}
    }
  }]
  # Output signal port
  y = RealOutput() [{
    "Dyad": {
      "placement": {"icon": {"iconName": "output", "x1": 950, "y1": 450, "x2": 1050, "y2": 550}}
    }
  }]
  # Internal state variable of the first-order filter
  variable x::Real
  # Initial value of the derivative-filter state
  parameter x0::Real = 0
  # Time constant of the filter (smaller values give better derivative approximation but more noise sensitivity)
  parameter T::Real
  # Gain factor applied to the derivative approximation
  parameter k::Real = 1.0
relations
  initial x = x0
  der(x) = (u-x)/T
  y = (k/T)*(u-x)
metadata {"Dyad": {"icons": {"default": "dyad://BlockComponents/Derivative.svg"}}}
end


Test Cases

This is setup code, that must be run before each test case.

julia
using BlockComponents
using ModelingToolkit, OrdinaryDiffEqDefault
using Plots
using CSV, DataFrames

snapshotsdir = joinpath(dirname(dirname(pathof(BlockComponents))), "test", "snapshots")
"/home/actions-runner-10/.julia/packages/BlockComponents/77kIK/test/snapshots"