Skip to content
Square.md

Square

Square wave generator that alternates between positive and negative values.

Generates a square wave signal that oscillates between offset+amplitude and offset-amplitude at the specified frequency. The square wave is generated using floor functions to create the periodic pattern. The output is held at the offset value until the start time is reached, after which the oscillation begins. The signal transitions instantaneously between high and low states.

This component extends from Signal

Usage

Square(start_time=0.0, offset=0.0, amplitude, frequency)

Parameters:

NameDescriptionUnitsDefault value
start_timeTime at which the signal starts changing from its offset values0
offsetConstant value added to the signal output0
amplitudeMaximum height of the square wave from zero (or from offset if specified)
frequencyFrequency of the square wave oscillationHz

Connectors

  • y - This connector represents a real signal as an output from a component (RealOutput)

Behavior

y(t)=ifelse(start_time<t,offset+amplitude(122frequency(start_time+t)+4frequency(start_time+t)),offset)

Source

dyad
# Square wave generator that alternates between positive and negative values.
#
# Generates a square wave signal that oscillates between `offset+amplitude` and `offset-amplitude`
# at the specified frequency. The square wave is generated using floor functions to create the
# periodic pattern. The output is held at the offset value until the start time is reached, after
# which the oscillation begins. The signal transitions instantaneously between high and low states.
component Square
  extends Signal
  # Maximum height of the square wave from zero (or from offset if specified)
  parameter amplitude::Real
  # Frequency of the square wave oscillation
  parameter frequency::Frequency
relations
  y = if start_time<time then offset+(amplitude*(4*floor(frequency*(time-start_time))-2*floor(2*(time-start_time)*frequency)+1)) else offset
metadata {"Dyad": {"icons": {"default": "dyad://BlockComponents/Square.svg"}}}
end
Flattened Source
dyad
# Square wave generator that alternates between positive and negative values.
#
# Generates a square wave signal that oscillates between `offset+amplitude` and `offset-amplitude`
# at the specified frequency. The square wave is generated using floor functions to create the
# periodic pattern. The output is held at the offset value until the start time is reached, after
# which the oscillation begins. The signal transitions instantaneously between high and low states.
component Square
  # Real-valued output connector for the component
  y = RealOutput() [{"Dyad": {"placement": {"icon": {"x1": 950, "y1": 450, "x2": 1050, "y2": 550}}}}]
  # Time at which the signal starts changing from its offset value
  parameter start_time::Time = 0.0
  # Constant value added to the signal output
  parameter offset::Real = 0.0
  # Maximum height of the square wave from zero (or from offset if specified)
  parameter amplitude::Real
  # Frequency of the square wave oscillation
  parameter frequency::Frequency
relations
  y = if start_time<time then offset+(amplitude*(4*floor(frequency*(time-start_time))-2*floor(2*(time-start_time)*frequency)+1)) else offset
metadata {"Dyad": {"icons": {"default": "dyad://BlockComponents/Square.svg"}}}
end


Test Cases

This is setup code, that must be run before each test case.

julia
using BlockComponents
using ModelingToolkit, OrdinaryDiffEqDefault
using Plots
using CSV, DataFrames

snapshotsdir = joinpath(dirname(dirname(pathof(BlockComponents))), "test", "snapshots")
"/home/actions-runner-10/.julia/packages/BlockComponents/77kIK/test/snapshots"