Skip to content
Cosine.md

Cosine

Generates a cosine wave with configurable parameters.

A signal generator that produces a cosine wave with the equation

y(t)=offset+amplitudecos(2πfrequency(tstart_time)+phase)

when time exceeds start_time, otherwise outputs the offset value. The component allows setting the wave's amplitude, frequency, phase shift and vertical offset.

This component extends from Signal

Usage

Cosine(start_time=0.0, offset=0.0, amplitude, frequency, phase=0.0)

Parameters:

NameDescriptionUnitsDefault value
start_timeTime at which the signal starts changing from its offset values0
offsetConstant value added to the signal output0
amplitudeThe peak value of the cosine wave
frequencyThe number of cycles per secondHz
phaseThe phase shift of the cosine wave in radiansrad0

Connectors

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

Behavior

y(t)=ifelse(start_time<t,offset+amplitudecos(phase+6.2832frequency(start_time+t)),offset)

Source

dyad
# Generates a cosine wave with configurable parameters.
#
# A signal generator that produces a cosine wave with the equation
#
# ```math
# y(t) = offset + amplitude \cdot \cos(2\pi \cdot frequency \cdot (t-start\_time) + phase)
# ```
#
# when time exceeds start_time, otherwise outputs the offset value. The component allows setting
# the wave's amplitude, frequency, phase shift and vertical offset.
component Cosine
  extends Signal
  # The peak value of the cosine wave
  parameter amplitude::Real
  # The number of cycles per second
  parameter frequency::Frequency
  # The phase shift of the cosine wave in radians
  parameter phase::Angle = 0.0
relations
  y = if start_time<time then offset+amplitude*cos(2*π*frequency*(time-start_time)+phase) else offset
metadata {"Dyad": {"icons": {"default": "dyad://BlockComponents/Cosine.svg"}}}
end
Flattened Source
dyad
# Generates a cosine wave with configurable parameters.
#
# A signal generator that produces a cosine wave with the equation
#
# ```math
# y(t) = offset + amplitude \cdot \cos(2\pi \cdot frequency \cdot (t-start\_time) + phase)
# ```
#
# when time exceeds start_time, otherwise outputs the offset value. The component allows setting
# the wave's amplitude, frequency, phase shift and vertical offset.
component Cosine
  # 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
  # The peak value of the cosine wave
  parameter amplitude::Real
  # The number of cycles per second
  parameter frequency::Frequency
  # The phase shift of the cosine wave in radians
  parameter phase::Angle = 0.0
relations
  y = if start_time<time then offset+amplitude*cos(2*π*frequency*(time-start_time)+phase) else offset
metadata {"Dyad": {"icons": {"default": "dyad://BlockComponents/Cosine.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"