Skip to content
CosineVariableFrequencyAndAmplitude.md

CosineVariableFrequencyAndAmplitude

Cosine voltage source with variable frequency and amplitude

Variable cosine generator with externally controlled amplitude and frequency inputs.

The component generates a cosine waveform based on integration of the frequency input:

| +––––––+ | | amplitude | | +––-+–––+ | | | v | +––––––+ | | | | | Cosine +––> y | | Generator | | | | | +––––––+ | ^ | | | +––-+–––+ | | frequency | | +––––––+

dϕdt=2πfrequencyy=offset+amplitudecos(ϕ)

Usage

CosineVariableFrequencyAndAmplitude(phi0=0, offset=0)

Parameters:

NameDescriptionUnitsDefault value
phi0Initial phase of the cosine wave0
offsetOffset of the cosine wave0

Connectors

  • amplitude - This connector represents a real signal as an input to a component (RealInput)

  • frequency - This connector represents a real signal as an input to a component (RealInput)

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

Variables

NameDescriptionUnits
phiPhase of the cosine waverad

Behavior

dphi(t)dt=6.2832frequency(t)y(t)=offset+cos(phi(t))amplitude(t)

Source

dyad
# Cosine voltage source with variable frequency and amplitude
#
# Variable cosine generator with externally controlled amplitude and frequency inputs.
#
# The component generates a cosine waveform based on integration of the frequency input:
#
# |    +------------+
# |    | amplitude  |
# |    +-----+------+
# |          |
# |          v
# |    +------------+
# |    |            |
# |    |  Cosine    +----> y
# |    | Generator  |
# |    |            |
# |    +------------+
# |          ^
# |          |
# |    +-----+------+
# |    | frequency  |
# |    +------------+
#
# ```math
# \frac{d\phi}{dt} = 2\pi \cdot frequency
# ```
#
# ```math
# y = offset + amplitude \cdot \cos(\phi)
# ```
component CosineVariableFrequencyAndAmplitude
  # Input port for the amplitude of the cosine wave
  amplitude = RealInput() [{"Dyad": {"placement": {"icon": {"x1": -50, "y1": 250, "x2": 50, "y2": 350}}}}]
  # Input port for the frequency of the cosine wave in Hz
  frequency = RealInput() [{"Dyad": {"placement": {"icon": {"x1": -50, "y1": 650, "x2": 50, "y2": 750}}}}]
  # Output port for the resulting cosine waveform
  y = RealOutput() [{"Dyad": {"placement": {"icon": {"x1": 950, "y1": 450, "x2": 750, "y2": 550}}}}]
  # Initial phase of the cosine wave
  parameter phi0::Real = 0
  # Offset of the cosine wave
  parameter offset::Real = 0
  # Phase of the cosine wave
  variable phi::Angle
relations
  initial phi = phi0
  der(phi) = 2*π*frequency
  y = offset+amplitude*cos(phi)
end
Flattened Source
dyad
# Cosine voltage source with variable frequency and amplitude
#
# Variable cosine generator with externally controlled amplitude and frequency inputs.
#
# The component generates a cosine waveform based on integration of the frequency input:
#
# |    +------------+
# |    | amplitude  |
# |    +-----+------+
# |          |
# |          v
# |    +------------+
# |    |            |
# |    |  Cosine    +----> y
# |    | Generator  |
# |    |            |
# |    +------------+
# |          ^
# |          |
# |    +-----+------+
# |    | frequency  |
# |    +------------+
#
# ```math
# \frac{d\phi}{dt} = 2\pi \cdot frequency
# ```
#
# ```math
# y = offset + amplitude \cdot \cos(\phi)
# ```
component CosineVariableFrequencyAndAmplitude
  # Input port for the amplitude of the cosine wave
  amplitude = RealInput() [{"Dyad": {"placement": {"icon": {"x1": -50, "y1": 250, "x2": 50, "y2": 350}}}}]
  # Input port for the frequency of the cosine wave in Hz
  frequency = RealInput() [{"Dyad": {"placement": {"icon": {"x1": -50, "y1": 650, "x2": 50, "y2": 750}}}}]
  # Output port for the resulting cosine waveform
  y = RealOutput() [{"Dyad": {"placement": {"icon": {"x1": 950, "y1": 450, "x2": 750, "y2": 550}}}}]
  # Initial phase of the cosine wave
  parameter phi0::Real = 0
  # Offset of the cosine wave
  parameter offset::Real = 0
  # Phase of the cosine wave
  variable phi::Angle
relations
  initial phi = phi0
  der(phi) = 2*π*frequency
  y = offset+amplitude*cos(phi)
metadata {}
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"