---
url: /dyad/stable/stdlib/BlockComponents/tests/Sources.Tests.Square.md
description: >-
  Dyad Sources.Tests.Square docs.  Connects a square wave generator to an
  integrator to test integration of a periodic signal.
---

# `Sources.Tests.Square` {#Sources.Tests.Square}

Connects a square wave generator to an integrator to test integration of a periodic signal.

This component demonstrates the integration of a square wave over time. It connects a Square signal generator (with amplitude 1, frequency 2 Hz, start time 0.5 seconds, and offset 0.7) to an Integrator block. The square wave alternates between values of 0.7 and 1.7, and when integrated, should reach approximately 3.537 at t=5 seconds.

## Usage {#Usage}

`BlockComponents.Sources.Tests.Square()`

## Behavior {#Behavior}

$$ \begin{align}
0 &= \mathrm{connect}\left( signal\_{+}y, integrator\_{+}u \right) \\
\frac{\mathrm{d}\mathtt{integrator.x}\left( t \right)}{\mathrm{d}t} &= \mathtt{integrator.k} ~ \mathtt{integrator.u}\left( t \right) \\
\mathtt{integrator.y}\left( t \right) &= \mathtt{integrator.x}\left( t \right) \\
\mathtt{signal.y}\left( t \right) &= ifelse\left( \mathtt{signal.start_time} < t, \mathtt{signal.offset} + \left( 1 - 2 ~ \left\lfloor 2 ~ \mathtt{signal.frequency} ~ \left(  - \mathtt{signal.start_time} + t \right)\right\rfloor  + 4 ~ \left\lfloor \mathtt{signal.frequency} ~ \left(  - \mathtt{signal.start_time} + t \right)\right\rfloor  \right) ~ \mathtt{signal.amplitude}, \mathtt{signal.offset} \right)
\end{align}
$$

## Source {#Source}

```dyad
"""
Connects a square wave generator to an integrator to test integration of a periodic signal.

This component demonstrates the integration of a square wave over time. It connects a Square
signal generator (with amplitude 1, frequency 2 Hz, start time 0.5 seconds, and offset 0.7)
to an Integrator block. The square wave alternates between values of 0.7 and 1.7, and when
integrated, should reach approximately 3.537 at t=5 seconds.
"""
test component Square
  "Integrator block that accumulates the square wave input"
  integrator = BlockComponents.Continuous.Integrator() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 160, "y1": 20, "x2": 260, "y2": 120, "rot": 0}
      }
    }
  }
  "Square wave generator with specified amplitude, frequency, start time and offset"
  signal = BlockComponents.Sources.Square(amplitude = 1, frequency = 2, start_time = 0.5, offset = 0.7) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 20, "y1": 20, "x2": 120, "y2": 120, "rot": 0}
      }
    }
  }
relations
  connect(signal.y, integrator.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
metadata {
  "Dyad": {
    "icons": {"default": "dyad://BlockComponents/Example.svg"},
    "tests": {
      "case1": {
        "stop": 5,
        "atol": {"integrator.x": 0.001},
        "expect": {
          "initial": {"signal.y": 0.7},
          "signals": ["signal.y", "integrator.x"],
          "final": {"signal.y": 1.7, "integrator.x": 3.5}
        }
      }
    }
  }
}
end
```

```dyad
"""
Connects a square wave generator to an integrator to test integration of a periodic signal.

This component demonstrates the integration of a square wave over time. It connects a Square
signal generator (with amplitude 1, frequency 2 Hz, start time 0.5 seconds, and offset 0.7)
to an Integrator block. The square wave alternates between values of 0.7 and 1.7, and when
integrated, should reach approximately 3.537 at t=5 seconds.
"""
test component Square
  "Integrator block that accumulates the square wave input"
  integrator = BlockComponents.Continuous.Integrator() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 160, "y1": 20, "x2": 260, "y2": 120, "rot": 0}
      }
    }
  }
  "Square wave generator with specified amplitude, frequency, start time and offset"
  signal = BlockComponents.Sources.Square(amplitude = 1, frequency = 2, start_time = 0.5, offset = 0.7) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 20, "y1": 20, "x2": 120, "y2": 120, "rot": 0}
      }
    }
  }
relations
  connect(signal.y, integrator.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
metadata {
  "Dyad": {
    "icons": {"default": "dyad://BlockComponents/Example.svg"},
    "tests": {
      "case1": {
        "stop": 5,
        "atol": {"integrator.x": 0.001},
        "expect": {
          "initial": {"signal.y": 0.7},
          "signals": ["signal.y", "integrator.x"],
          "final": {"signal.y": 1.7, "integrator.x": 3.5}
        }
      }
    }
  }
}
end
```

## Test Cases {#Test-Cases}

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

```julia
plt
```

![](alqtxth.svg)

```julia
plt
```

![](iucvfya.svg)

## Related {#Related}

* Examples

* Experiments

* Analyses

* Tests
  * [`Integrator`](Sources.Tests.Square.md)

  * [`Square`](Sources.Tests.Square.md)

  * [`Square`](Sources.Tests.Square.md)
