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:
Name | Description | Units | Default value |
---|---|---|---|
start_time | Time at which the signal starts changing from its offset value | s | 0 |
offset | Constant value added to the signal output | – | 0 |
amplitude | Maximum height of the square wave from zero (or from offset if specified) | – | |
frequency | Frequency of the square wave oscillation | Hz |
Connectors
y
- This connector represents a real signal as an output from a component (RealOutput
)
Behavior
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"
Related
Examples
Experiments
Analyses
Tests