TestUnitDifference Icon
Examples.TestUnitDifference
Test component that validates the UnitDifference block by computing the discrete difference of a sine wave.
This component connects a sine wave generator to a UnitDifference block which computes y[n] = u[n] - u[n-1]. The discrete difference approximates the derivative of the sine wave. With a sample time of 0.1s and sine frequency of 0.1 Hz, the test verifies that the difference operator produces the expected rate of change of the input signal.
Usage
DiscreteComponents.Examples.TestUnitDifference()
Behavior
\[ \begin{equation} \left[ \begin{array}{c} \mathrm{connect}\left( unitdifference_{+}y(t), terminator_{+}u(t) \right) \\ \mathrm{connect}\left( sine_{+}y(t), periodicsampler_{+}u(t) \right) \\ \mathrm{connect}\left( periodicsampler_{+}y(t), unitdifference_{+}u(t) \right) \\ \mathtt{sine.y}\left( t \right) = \mathtt{sine.offset} + \mathtt{sine.amplitude} ifelse\left( t \geq \mathtt{sine.start\_time}, \sin\left( \mathtt{sine.phase} + 6.2832 \mathtt{sine.frequency} \left( - \mathtt{sine.start\_time} + t \right) \right), \sin\left( \mathtt{sine.phase} \right) \right) \\ \mathtt{unitdifference.y}\left( t \right) = - Shift(t, -1)\left( \mathtt{unitdifference.u}\left( t \right) \right) + \mathtt{unitdifference.u}\left( t \right) \\ \frac{\mathrm{d} \mathtt{unitdifference.dummy.dummy}\left( t \right)}{\mathrm{d}t} = 0 \\ \mathrm{connect}\left( u(t), sampler_{+}u(t) \right) \\ \mathrm{connect}\left( sampler_{+}y(t), y(t), clock_{+}y(t) \right) \\ \frac{\mathrm{d} \mathtt{periodicsampler.clock.dummy.dummy}\left( t \right)}{\mathrm{d}t} = 0 \\ \mathtt{periodicsampler.sampler.y}\left( t \right) = Sample(ModelingToolkit.InferredClock.var"typeof(InferredClock)"(ModelingToolkit.InferredClock.var"##Storage#InferredDiscrete"(0)))\left( \mathtt{periodicsampler.sampler.u}\left( t \right) \right) \\ \end{array} \right] \end{equation} \]
Source
"""
Test component that validates the UnitDifference block by computing the discrete difference of a sine wave.
This component connects a sine wave generator to a UnitDifference block which computes y[n] = u[n] - u[n-1].
The discrete difference approximates the derivative of the sine wave. With a sample time of 0.1s and
sine frequency of 0.1 Hz, the test verifies that the difference operator produces the expected rate of
change of the input signal.
"""
test component TestUnitDifference
sine = BlockComponents.Sine(frequency = 0.1, amplitude = 1.0) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 20, "y1": 140, "x2": 120, "y2": 240, "rot": 0}
},
"tags": []
}
}
unitdifference = DiscreteComponents.UnitDifference(initial_condition = 0.0) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 380, "y1": 140, "x2": 480, "y2": 240, "rot": 0}
},
"tags": []
}
}
terminator = BlockComponents.Terminator() {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 580, "y1": 140, "x2": 680, "y2": 240, "rot": 0}
},
"tags": []
}
}
periodicsampler = DiscreteComponents.PeriodicSampler(dt = 0.1) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 190, "y1": 140, "x2": 290, "y2": 240, "rot": 0}
},
"tags": []
}
}
relations
connect(unitdifference.y, terminator.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
connect(sine.y, periodicsampler.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
connect(periodicsampler.y, unitdifference.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
metadata {
"Dyad": {
"experiments": {},
"tests": {
"case1": {
"solver": "auto",
"start": 0,
"stop": 10,
"params": {},
"initial": {},
"atol": {},
"rtol": {},
"abstol": 0.000001,
"reltol": 0.000001,
"expect": {
"initial": {},
"final": {},
"signals": ["sine.y", "periodicsampler.y", "unitdifference.y"]
}
}
},
"labels": [
{
"label": "First, we have to sample the sine function to convert it to a discrete-time signal.",
"x": 170,
"y": 80,
"rot": 0,
"layer": "diagram",
"attrs": {
"fill": "purple",
"font-size": "20",
"dominant-baseline": "central",
"text-anchor": "middle"
}
},
{
"label": "Then, we take the unit difference, which is `sin(t) - sin(t-dt)` in this case",
"x": 480,
"y": 340,
"rot": 0,
"layer": "diagram",
"attrs": {
"fill": "purple",
"font-size": "20",
"dominant-baseline": "central",
"text-anchor": "middle"
}
}
],
"icons": {"default": "dyad://Dyad/Default.svg"},
"path": {},
"doc": {"behavior": true}
}
}
end
Flattened Source
"""
Test component that validates the UnitDifference block by computing the discrete difference of a sine wave.
This component connects a sine wave generator to a UnitDifference block which computes y[n] = u[n] - u[n-1].
The discrete difference approximates the derivative of the sine wave. With a sample time of 0.1s and
sine frequency of 0.1 Hz, the test verifies that the difference operator produces the expected rate of
change of the input signal.
"""
test component TestUnitDifference
sine = BlockComponents.Sine(frequency = 0.1, amplitude = 1.0) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 20, "y1": 140, "x2": 120, "y2": 240, "rot": 0}
},
"tags": []
}
}
unitdifference = DiscreteComponents.UnitDifference(initial_condition = 0.0) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 380, "y1": 140, "x2": 480, "y2": 240, "rot": 0}
},
"tags": []
}
}
terminator = BlockComponents.Terminator() {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 580, "y1": 140, "x2": 680, "y2": 240, "rot": 0}
},
"tags": []
}
}
periodicsampler = DiscreteComponents.PeriodicSampler(dt = 0.1) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 190, "y1": 140, "x2": 290, "y2": 240, "rot": 0}
},
"tags": []
}
}
relations
connect(unitdifference.y, terminator.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
connect(sine.y, periodicsampler.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
connect(periodicsampler.y, unitdifference.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
metadata {
"Dyad": {
"experiments": {},
"tests": {
"case1": {
"solver": "auto",
"start": 0,
"stop": 10,
"params": {},
"initial": {},
"atol": {},
"rtol": {},
"abstol": 0.000001,
"reltol": 0.000001,
"expect": {
"initial": {},
"final": {},
"signals": ["sine.y", "periodicsampler.y", "unitdifference.y"]
}
}
},
"labels": [
{
"label": "First, we have to sample the sine function to convert it to a discrete-time signal.",
"x": 170,
"y": 80,
"rot": 0,
"layer": "diagram",
"attrs": {
"fill": "purple",
"font-size": "20",
"dominant-baseline": "central",
"text-anchor": "middle"
}
},
{
"label": "Then, we take the unit difference, which is `sin(t) - sin(t-dt)` in this case",
"x": 480,
"y": 340,
"rot": 0,
"layer": "diagram",
"attrs": {
"fill": "purple",
"font-size": "20",
"dominant-baseline": "central",
"text-anchor": "middle"
}
}
],
"icons": {"default": "dyad://Dyad/Default.svg"},
"path": {},
"doc": {"behavior": true}
}
}
endTest Cases
Test Case case1
pltpltpltRelated
- Examples
- Experiments
- Analyses
- Tests