---
url: /dyad/stable/stdlib/DiscreteComponents/tests/Examples.TestNDelay.md
description: >-
  Dyad Examples.TestNDelay docs.  Test component that validates the NDelay
  component.
---

# `Examples.TestNDelay` {#Examples.TestNDelay}

Test component that validates the NDelay component.

This component validates that a single `NDelay` block is equivalent to a chain of `UnitDelay` blocks.

## Usage {#Usage}

`DiscreteComponents.Examples.TestNDelay()`

## Variables {#Variables}

|           Name | Description | Units |
| --------------:| -----------:| -----:|
|  `ud1equalsnd` |             |     – |
| `ud2equalsnd1` |             |     – |

## Behavior {#Behavior}

$$ \begin{align}
\mathtt{ud1equalsnd}\left( t \right) &= \mathtt{unitdelay.y}\left( t \right) = \mathtt{ndelay.y}\left( t \right) \\
\mathtt{ud2equalsnd1}\left( t \right) &= \mathtt{unitdelay1.y}\left( t \right) = \mathtt{ndelay1.y}\left( t \right) \\
0 &= \mathrm{connect}\left( ramp\_{+}y, sampler\_{+}u \right) \\
0 &= \mathrm{connect}\left( sampler\_{+}y, ndelay\_{+}u, ndelay1\_{+}u, unitdelay\_{+}u, periodicclock\_{+}y \right) \\
0 &= \mathrm{connect}\left( unitdelay\_{+}y, unitdelay1\_{+}u \right) \\
0 &= \mathrm{connect}\left( unitdelay1\_{+}y, unitdelay2\_{+}u \right) \\
\mathtt{ramp.y}\left( t \right) &= ifelse\left( \mathtt{ramp.start_time} < t, ifelse\left( t < \mathtt{ramp.duration} + \mathtt{ramp.start_time}, \mathtt{ramp.offset} + \frac{\mathtt{ramp.height} ~ \left(  - \mathtt{ramp.start_time} + t \right)}{\mathtt{ramp.duration}}, \mathtt{ramp.height} + \mathtt{ramp.offset} \right), \mathtt{ramp.offset} \right) \\
\mathtt{sampler.y}\left( t \right) &= Sample(InferredClock.InferredDiscrete(0))\left( \mathtt{sampler.u}\left( t \right) \right) \\
\mathtt{unitdelay.y}\left( t \right) &= Shift(t, -1)\left( \mathtt{unitdelay.u}\left( t \right) \right) \\
\mathtt{unitdelay1.y}\left( t \right) &= Shift(t, -1)\left( \mathtt{unitdelay1.u}\left( t \right) \right) \\
\mathtt{unitdelay2.y}\left( t \right) &= Shift(t, -1)\left( \mathtt{unitdelay2.u}\left( t \right) \right) \\
\mathtt{ndelay.y}\left( t \right) &= Shift(t, -2)\left( \mathtt{ndelay.u}\left( t \right) \right) \\
\mathtt{ndelay1.y}\left( t \right) &= Shift(t, -3)\left( \mathtt{ndelay1.u}\left( t \right) \right)
\end{align}
$$

## Source {#Source}

```dyad
"""
Test component that validates the NDelay component.

This component validates that a single `NDelay` block is equivalent to a chain of `UnitDelay` blocks.
"""
test component TestNDelay
  ramp = BlockComponents.Sources.Ramp(duration = 10, height = 10) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": -50, "y1": 450, "x2": 50, "y2": 550, "rot": 0}
      },
      "tags": []
    }
  }
  sampler = DiscreteComponents.Sampler() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 90, "y1": 450, "x2": 190, "y2": 550, "rot": 0}
      },
      "tags": []
    }
  }
  periodicclock = DiscreteComponents.PeriodicClock(dt = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 90, "y1": 600, "x2": 190, "y2": 700, "rot": 0}
      },
      "tags": []
    }
  }
  unitdelay = DiscreteComponents.UnitDelay() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 250, "y1": 340, "x2": 350, "y2": 440, "rot": 0}
      },
      "tags": []
    }
  }
  unitdelay1 = DiscreteComponents.UnitDelay() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 380, "y1": 340, "x2": 480, "y2": 440, "rot": 0}
      },
      "tags": []
    }
  }
  unitdelay2 = DiscreteComponents.UnitDelay() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 510, "y1": 340, "x2": 610, "y2": 440, "rot": 0}
      },
      "tags": []
    }
  }
  ndelay = DiscreteComponents.NDelay(n = 2) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 250, "y1": 530, "x2": 350, "y2": 630, "rot": 0}
      },
      "tags": []
    }
  }
  ndelay1 = DiscreteComponents.NDelay(n = 3) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 250, "y1": 670, "x2": 350, "y2": 770, "rot": 0}
      },
      "tags": []
    }
  }
  variable ud1equalsnd::Boolean
  variable ud2equalsnd1::Boolean
relations
  ud1equalsnd = unitdelay.y == ndelay.y
  ud2equalsnd1 = unitdelay1.y == ndelay1.y
  connect(ramp.y, sampler.u) {"Dyad": {"renderStyle": "standard", "edges": [{"S": 1, "E": 2, "M": []}]}}
  connect(sampler.y, ndelay.u, ndelay1.u, unitdelay.u, periodicclock.y) {
    "Dyad": {
      "renderStyle": "standard",
      "edges": [{"S": 1, "M": [{"x": 221, "y": 500}, {"x": 221, "y": 580}], "E": 2}]
    }
  }
  connect(unitdelay.y, unitdelay1.u) {"Dyad": {"renderStyle": "standard", "edges": [{"S": 1, "E": 2, "M": []}]}}
  connect(unitdelay1.y, unitdelay2.u) {"Dyad": {"renderStyle": "standard", "edges": [{"S": 1, "E": 2, "M": []}]}}
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": ["ramp.y", "ndelay.y", "ndelay1.y", "unitdelay1.y", "unitdelay2.y"]
        }
      }
    },
    "labels": [
      {
        "label": "This chain of unit delays approximates the n-delay blocks",
        "x": 430,
        "y": 240,
        "rot": 0,
        "layer": "diagram",
        "attrs": {
          "fill": "black",
          "font-size": "20",
          "dominant-baseline": "central",
          "text-anchor": "middle"
        }
      },
      {
        "label": "== ndelay",
        "x": 430,
        "y": 320,
        "rot": 0,
        "layer": "diagram",
        "attrs": {
          "fill": "black",
          "font-size": "20",
          "dominant-baseline": "central",
          "text-anchor": "middle"
        }
      },
      {
        "label": "== ndelay1",
        "x": 560,
        "y": 320,
        "rot": 0,
        "layer": "diagram",
        "attrs": {
          "fill": "black",
          "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 NDelay component.

This component validates that a single `NDelay` block is equivalent to a chain of `UnitDelay` blocks.
"""
test component TestNDelay
  ramp = BlockComponents.Sources.Ramp(duration = 10, height = 10) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": -50, "y1": 450, "x2": 50, "y2": 550, "rot": 0}
      },
      "tags": []
    }
  }
  sampler = DiscreteComponents.Sampler() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 90, "y1": 450, "x2": 190, "y2": 550, "rot": 0}
      },
      "tags": []
    }
  }
  periodicclock = DiscreteComponents.PeriodicClock(dt = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 90, "y1": 600, "x2": 190, "y2": 700, "rot": 0}
      },
      "tags": []
    }
  }
  unitdelay = DiscreteComponents.UnitDelay() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 250, "y1": 340, "x2": 350, "y2": 440, "rot": 0}
      },
      "tags": []
    }
  }
  unitdelay1 = DiscreteComponents.UnitDelay() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 380, "y1": 340, "x2": 480, "y2": 440, "rot": 0}
      },
      "tags": []
    }
  }
  unitdelay2 = DiscreteComponents.UnitDelay() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 510, "y1": 340, "x2": 610, "y2": 440, "rot": 0}
      },
      "tags": []
    }
  }
  ndelay = DiscreteComponents.NDelay(n = 2) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 250, "y1": 530, "x2": 350, "y2": 630, "rot": 0}
      },
      "tags": []
    }
  }
  ndelay1 = DiscreteComponents.NDelay(n = 3) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 250, "y1": 670, "x2": 350, "y2": 770, "rot": 0}
      },
      "tags": []
    }
  }
  variable ud1equalsnd::Boolean
  variable ud2equalsnd1::Boolean
relations
  ud1equalsnd = unitdelay.y == ndelay.y
  ud2equalsnd1 = unitdelay1.y == ndelay1.y
  connect(ramp.y, sampler.u) {"Dyad": {"renderStyle": "standard", "edges": [{"S": 1, "E": 2, "M": []}]}}
  connect(sampler.y, ndelay.u, ndelay1.u, unitdelay.u, periodicclock.y) {
    "Dyad": {
      "renderStyle": "standard",
      "edges": [{"S": 1, "M": [{"x": 221, "y": 500}, {"x": 221, "y": 580}], "E": 2}]
    }
  }
  connect(unitdelay.y, unitdelay1.u) {"Dyad": {"renderStyle": "standard", "edges": [{"S": 1, "E": 2, "M": []}]}}
  connect(unitdelay1.y, unitdelay2.u) {"Dyad": {"renderStyle": "standard", "edges": [{"S": 1, "E": 2, "M": []}]}}
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": ["ramp.y", "ndelay.y", "ndelay1.y", "unitdelay1.y", "unitdelay2.y"]
        }
      }
    },
    "labels": [
      {
        "label": "This chain of unit delays approximates the n-delay blocks",
        "x": 430,
        "y": 240,
        "rot": 0,
        "layer": "diagram",
        "attrs": {
          "fill": "black",
          "font-size": "20",
          "dominant-baseline": "central",
          "text-anchor": "middle"
        }
      },
      {
        "label": "== ndelay",
        "x": 430,
        "y": 320,
        "rot": 0,
        "layer": "diagram",
        "attrs": {
          "fill": "black",
          "font-size": "20",
          "dominant-baseline": "central",
          "text-anchor": "middle"
        }
      },
      {
        "label": "== ndelay1",
        "x": 560,
        "y": 320,
        "rot": 0,
        "layer": "diagram",
        "attrs": {
          "fill": "black",
          "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
```

![](qvrsqff.svg)

```julia
plt
```

![](ypoyick.svg)

```julia
plt
```

![](salvxse.svg)

```julia
plt
```

![](mhdsobr.svg)

```julia
plt
```

![](duqfcef.svg)

## Related {#Related}

* Examples

* Experiments

* Analyses

* Tests
  * [`NDelay`](Examples.TestNDelay.md)

  * [`PeriodicClock`](Examples.TestNDelay.md)

  * [`Ramp`](Examples.TestNDelay.md)

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

  * [`UnitDelay`](Examples.TestNDelay.md)
