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.
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:
Name | Description | Units | Default value |
---|---|---|---|
x0 | Initial value of the derivative-filter state | – | 0 |
T | Time constant of the filter (smaller values give better derivative approximation but more noise sensitivity) | – | |
k | Gain factor applied to the derivative approximation | – | 1 |
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
Name | Description | Units |
---|---|---|
x | Internal state variable of the first-order filter | – |
Behavior
Source
# 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
# 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.
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"
Related
Examples
Experiments
Analyses
Tests