Skip to content
Triangular.md

Triangular

Triangular waveform generator with configurable amplitude and frequency.

Produces a continuous triangular wave signal that oscillates between -amplitude and +amplitude values. The triangular wave is generated using modulo arithmetic on the time variable, creating a periodic signal with the specified frequency. The component begins generating the waveform only after the start_time is reached; before that point, it outputs the offset value. The period of the wave is automatically calculated as the inverse of the frequency.

This component extends from Signal

Usage

Triangular(start_time=0.0, offset=0.0, amplitude, frequency, period=1/frequency)

Parameters:

NameDescriptionUnitsDefault value
start_timeTime at which the signal starts changing from its offset values0
offsetConstant value added to the signal output0
amplitudeMaximum amplitude of the triangular wave
frequencyFrequency of the triangular waveHz

Connectors

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

Behavior

y(t)=ifelse(start_time<t,amplitude+4amplitudefrequency|12period+|mod(14periodstart_time+t,period)||,offset)

Source

dyad
# Triangular waveform generator with configurable amplitude and frequency.
#
# Produces a continuous triangular wave signal that oscillates between -amplitude and
# +amplitude values. The triangular wave is generated using modulo arithmetic on the
# time variable, creating a periodic signal with the specified frequency. The component
# begins generating the waveform only after the start_time is reached; before that point,
# it outputs the offset value. The period of the wave is automatically calculated as the
# inverse of the frequency.
component Triangular
  extends Signal
  # Maximum amplitude of the triangular wave
  parameter amplitude::Real
  # Frequency of the triangular wave
  parameter frequency::Frequency
  # Period of the triangular wave, calculated as inverse of frequency
  final parameter period::Period = 1/frequency
relations
  y = if start_time<time then 4*amplitude*frequency*abs(abs(mod(time-period/4-start_time, period))-period/2)-amplitude else offset
metadata {"Dyad": {"icons": {"default": "dyad://BlockComponents/Triangular.svg"}}}
end
Flattened Source
dyad
# Triangular waveform generator with configurable amplitude and frequency.
#
# Produces a continuous triangular wave signal that oscillates between -amplitude and
# +amplitude values. The triangular wave is generated using modulo arithmetic on the
# time variable, creating a periodic signal with the specified frequency. The component
# begins generating the waveform only after the start_time is reached; before that point,
# it outputs the offset value. The period of the wave is automatically calculated as the
# inverse of the frequency.
component Triangular
  # 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 amplitude of the triangular wave
  parameter amplitude::Real
  # Frequency of the triangular wave
  parameter frequency::Frequency
  # Period of the triangular wave, calculated as inverse of frequency
  final parameter period::Period = 1/frequency
relations
  y = if start_time<time then 4*amplitude*frequency*abs(abs(mod(time-period/4-start_time, period))-period/2)-amplitude else offset
metadata {"Dyad": {"icons": {"default": "dyad://BlockComponents/Triangular.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"