Skip to content
SecondOrder.md

SecondOrder

Second-order filter with configurable gain, bandwidth, and damping ratio.

A filter that implements a second-order transfer function with configurable gain k, bandwidth w, and relative damping d. The filter can be configured to achieve different response characteristics by adjusting the damping ratio: critically damped (d=1), under-damped (d<1), or over-damped (d>1). A damping value of d=1/√2 creates a Butterworth filter with maximally flat frequency response. The filter is characterized by the transfer function

Y(s)/U(s)=kw2s2+2dws+w2

This component extends from SISO

Usage

SecondOrder(k=1.0, w=1.0, d=1.0)

Parameters:

NameDescriptionUnitsDefault value
kGain1
wBandwidth (angular frequency)1
dRelative damping1

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
xState of SecondOrder filter
xdDerivative state of SecondOrder filter

Behavior

dx(t)dt=xd(t)dxd(t)dt=(2dxd(t)+(x(t)+ku(t))w)wy(t)=x(t)

Source

dyad
# Second-order filter with configurable gain, bandwidth, and damping ratio.
#
# A filter that implements a second-order transfer function with configurable gain k,
# bandwidth w, and relative damping d. The filter can be configured to achieve different
# response characteristics by adjusting the damping ratio: critically damped (d=1),
# under-damped (d<1), or over-damped (d>1). A damping value of d=1/√2 creates a Butterworth
# filter with maximally flat frequency response. The filter is characterized by the transfer
# function
#
# ```math
# Y(s)/U(s) = \frac{k \cdot w^2}{s^2 + 2d \cdot w \cdot s + w^2}
# ```
component SecondOrder
  extends SISO
  # State of SecondOrder filter
  variable x::Real
  # Derivative state of SecondOrder filter
  variable xd::Real
  # Gain
  parameter k::Real = 1.0
  # Bandwidth (angular frequency)
  parameter w::Real = 1.0
  # Relative damping
  parameter d::Real = 1.0
relations
  der(x) = xd
  der(xd) = w*(w*(k*u-x)-2*d*xd)
  y = x
metadata {"Dyad": {"icons": {"default": "dyad://BlockComponents/SecondOrder.svg"}}}
end
Flattened Source
dyad
# Second-order filter with configurable gain, bandwidth, and damping ratio.
#
# A filter that implements a second-order transfer function with configurable gain k,
# bandwidth w, and relative damping d. The filter can be configured to achieve different
# response characteristics by adjusting the damping ratio: critically damped (d=1),
# under-damped (d<1), or over-damped (d>1). A damping value of d=1/√2 creates a Butterworth
# filter with maximally flat frequency response. The filter is characterized by the transfer
# function
#
# ```math
# Y(s)/U(s) = \frac{k \cdot w^2}{s^2 + 2d \cdot w \cdot s + w^2}
# ```
component SecondOrder
  # 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}}
    }
  }]
  # State of SecondOrder filter
  variable x::Real
  # Derivative state of SecondOrder filter
  variable xd::Real
  # Gain
  parameter k::Real = 1.0
  # Bandwidth (angular frequency)
  parameter w::Real = 1.0
  # Relative damping
  parameter d::Real = 1.0
relations
  der(x) = xd
  der(xd) = w*(w*(k*u-x)-2*d*xd)
  y = x
metadata {"Dyad": {"icons": {"default": "dyad://BlockComponents/SecondOrder.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"