braintools.input.Square#

class braintools.input.Square(amplitude, frequency, duration, duty_cycle=0.5, bias=False, t_start=None, t_end=None)#

Composable square wave input generator.

Creates a square wave that alternates between two levels at a specified frequency. This class is composable with other Input objects.

The square wave alternates between +amplitude and -amplitude (or 0 and 2*amplitude with bias).

Parameters:
  • amplitude (float) – Peak amplitude of the square wave.

  • frequency (Quantity) – Frequency of oscillation. Must be in Hz units.

  • duration (Array | ndarray | bool | number | bool | int | float | complex | Quantity) – Total duration of the signal. Supports time units.

  • duty_cycle (float) – Fraction of period spent at high level (0 to 1). Default is 0.5 (symmetric square wave).

  • bias (bool) – If True, adds DC offset equal to amplitude (non-negative output). If False, alternates between +amplitude and -amplitude.

  • t_start (Array | ndarray | bool | number | bool | int | float | complex | Quantity | None) – Time when square wave starts. Before this, output is 0. Default is 0.

  • t_end (Array | ndarray | bool | number | bool | int | float | complex | Quantity | None) – Time when square wave ends. After this, output is 0. Default is duration.

amplitude#

Peak amplitude of the square wave

Type:

float

frequency#

Frequency of oscillation in Hz

Type:

Quantity

duty_cycle#

Duty cycle of the square wave

Type:

float

bias#

Whether DC bias is applied

Type:

bool

t_start#

Start time of the square wave

Type:

float or Quantity

t_end#

End time of the square wave

Type:

float or Quantity

Examples

>>> import brainunit as u
>>> import brainstate
>>> brainstate.environ.set(dt=0.1 * u.ms)

Create symmetric square wave:

>>> square = Square(1.0, 5 * u.Hz, 1000 * u.ms)
>>> signal = square()

Create pulse train with 20% duty cycle:

>>> pulses = Square(
...     amplitude=5.0,
...     frequency=10 * u.Hz,
...     duration=500 * u.ms,
...     duty_cycle=0.2  # 20% high, 80% low
... )

Smooth square wave transitions:

>>> square = Square(2.0, 5 * u.Hz, 800 * u.ms)
>>> smoothed = square.smooth(tau=5 * u.ms)  # Low-pass filter

Create clock signal for synchronization:

>>> clock = Square(
...     amplitude=1.0,
...     frequency=40 * u.Hz,
...     duration=250 * u.ms,
...     duty_cycle=0.5
... )

Combine with DC offset:

>>> from braintools.input import Constant
>>> square = Square(3.0, 2 * u.Hz, 2000 * u.ms)
>>> offset = Constant([(2.0, 2000 * u.ms)])
>>> shifted_square = square + offset

Create gated stimulation:

>>> from braintools.input import Step
>>> square = Square(1.0, 50 * u.Hz, 1000 * u.ms)
>>> gate = Step([0, 1, 0], [0 * u.ms, 200 * u.ms, 800 * u.ms], 1000 * u.ms)
>>> gated = square * gate

Square wave with positive bias:

>>> positive_square = Square(
...     amplitude=4.0,
...     frequency=10 * u.Hz,
...     duration=500 * u.ms,
...     bias=True  # Alternates between 0 and 8
... )

Create PWM-like signal:

>>> pwm = Square(
...     amplitude=5.0,
...     frequency=100 * u.Hz,
...     duration=100 * u.ms,
...     duty_cycle=0.1  # 10% duty cycle
... )

Notes

  • Without bias: alternates between +amplitude and -amplitude

  • With bias: alternates between 0 and 2*amplitude

  • Duty cycle controls fraction of time at high level

  • Transitions are instantaneous (limited by dt resolution)

  • Can be combined with other Input classes using +, -, *, /

See also

Sinusoidal

Sinusoidal wave generator

Triangular

Triangular wave generator

square

Functional API for square wave input

__init__(amplitude, frequency, duration, duty_cycle=0.5, bias=False, t_start=None, t_end=None)[source]#

Initialize the Input base class.

Parameters:

duration (Array | ndarray | bool | number | bool | int | float | complex | Quantity) – The total duration of the input.

Methods

__init__(amplitude, frequency, duration[, ...])

Initialize the Input base class.

apply(func)

Apply a custom function to the input.

clip([min_val, max_val])

Clip the input values to a range.

generate()

Generate the square wave using the functional API.

repeat(n_times)

Repeat the input pattern n times.

scale(factor)

Scale the input by a factor.

shift(time_shift)

Shift the input in time.

smooth(tau)

Apply exponential smoothing to the input.

Attributes

dt

Get the time step from global environment.

n_steps

Get the number of time steps.

shape

Get the shape of the input array.