---
url: /dyad/stable/stdlib/DiscreteComponents/tests/Examples.TestUnitDifference.md
description: >-
  Dyad Examples.TestUnitDifference docs.  Test component that validates the
  UnitDifference block by computing the discrete difference of a sine wave.
---

# `Examples.TestUnitDifference` {#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 {#Usage}

`DiscreteComponents.Examples.TestUnitDifference()`

## Behavior {#Behavior}

$$ \begin{align}
0 &= \mathrm{connect}\left( unitdifference\_{+}y, terminator\_{+}u \right) \\
0 &= \mathrm{connect}\left( sine\_{+}y, sampler\_{+}u \right) \\
0 &= \mathrm{connect}\left( sampler\_{+}y, unitdifference\_{+}u, periodicclock\_{+}y \right) \\
\mathtt{sine.y}\left( t \right) &= ifelse\left( \mathtt{sine.continuous}, ifelse\left( t \geq \mathtt{sine.start_time}, \sin\left( \mathtt{sine.phase} + 6.2832 ~ \mathtt{sine.frequency} ~ \left(  - \mathtt{sine.shift_time} + t \right) \right), \sin\left( \mathtt{sine.phase} + 6.2832 ~ \mathtt{sine.frequency} ~ \left(  - \mathtt{sine.shift_time} + \mathtt{sine.start_time} \right) \right) \right) ~ \mathtt{sine.amplitude}, ifelse\left( t \geq \mathtt{sine.start_time}, \sin\left( \mathtt{sine.phase} + 6.2832 ~ \mathtt{sine.frequency} ~ \left(  - \mathtt{sine.shift_time} + t \right) \right) ~ \mathtt{sine.amplitude}, 0 \right) \right) + \mathtt{sine.offset} \\
\mathtt{unitdifference.y}\left( t \right) &=  - Shift(t, -1)\left( \mathtt{unitdifference.u}\left( t \right) \right) + \mathtt{unitdifference.u}\left( t \right) \\
\mathtt{sampler.y}\left( t \right) &= Sample(InferredClock.InferredDiscrete(0))\left( \mathtt{sampler.u}\left( t \right) \right)
\end{align}
$$

## Source {#Source}

```dyad
"""
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.Sources.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(initialization = DiscreteComponents.InitialCondition.InitialState(x0 = 0.0)) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 380, "y1": 140, "x2": 480, "y2": 240, "rot": 0}
      },
      "tags": []
    }
  }
  terminator = BlockComponents.Routing.Terminator() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 580, "y1": 140, "x2": 680, "y2": 240, "rot": 0}
      },
      "tags": []
    }
  }
  sampler = DiscreteComponents.Sampler() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 190, "y1": 140, "x2": 290, "y2": 240, "rot": 0}
      },
      "tags": []
    }
  }
  periodicclock = DiscreteComponents.PeriodicClock(dt = 0.1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 190, "y1": 280, "x2": 290, "y2": 380, "rot": 0}
      },
      "tags": []
    }
  }
relations
  connect(unitdifference.y, terminator.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  connect(sine.y, sampler.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  connect(sampler.y, unitdifference.u, periodicclock.y) {"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", "sampler.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
```

```dyad
"""
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.Sources.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(initialization = DiscreteComponents.InitialCondition.InitialState(x0 = 0.0)) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 380, "y1": 140, "x2": 480, "y2": 240, "rot": 0}
      },
      "tags": []
    }
  }
  terminator = BlockComponents.Routing.Terminator() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 580, "y1": 140, "x2": 680, "y2": 240, "rot": 0}
      },
      "tags": []
    }
  }
  sampler = DiscreteComponents.Sampler() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 190, "y1": 140, "x2": 290, "y2": 240, "rot": 0}
      },
      "tags": []
    }
  }
  periodicclock = DiscreteComponents.PeriodicClock(dt = 0.1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 190, "y1": 280, "x2": 290, "y2": 380, "rot": 0}
      },
      "tags": []
    }
  }
relations
  connect(unitdifference.y, terminator.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  connect(sine.y, sampler.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  connect(sampler.y, unitdifference.u, periodicclock.y) {"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", "sampler.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
```

## Test Cases {#Test-Cases}

### Test Case `case1` {#Test-Case-case1}

```julia
plt
```

![](cwatjhl.svg)

```julia
plt
```

![](sprgpte.svg)

```julia
plt
```

![](rbfhlhm.svg)

## Related {#Related}

* Examples

* Experiments

* Analyses

* Tests
  * [`PeriodicClock`](Examples.TestUnitDifference.md)

  * [`Sampler`](Examples.TestUnitDifference.md)

  * [`Sine`](Examples.TestUnitDifference.md)

  * [`Terminator`](Examples.TestUnitDifference.md)

  * [`UnitDifference`](Examples.TestUnitDifference.md)
