Skip to content
LIBRARY
Examples.Modulation.md

Examples.Modulation

Demonstrates amplitude modulation (AM) and frequency modulation (FM) using the variable-frequency/amplitude sine and cosine sources.

Mirrors Modelica.Blocks.Examples.Modulation. The carrier frequency is 100 Hz and the message (modulating) frequency is 2 Hz:

  • sine.y is the unmodulated 100 Hz carrier reference, sin(2π·100·t).

  • sinAM.y / cosAM.y are amplitude modulated with a constant 100 Hz carrier and a time-varying amplitude 1 + 0.5·sin(2π·2·t).

  • sinFM.y / cosFM.y are frequency modulated with a constant amplitude of 1 and an instantaneous frequency 100 + 50·sin(2π·2·t).

The SineVariableFrequencyAndAmplitude / CosineVariableFrequencyAndAmplitude blocks always read both amplitude and frequency from input ports, so the constant carrier frequency (AM) and constant carrier amplitude (FM) are supplied by Constant sources.

Usage

BlockComponents.Examples.Modulation()

Behavior

julia
using BlockComponents #hide
using ModelingToolkit #hide
@named sys = BlockComponents.Examples.Modulation() #hide
let eqs = full_equations(sys); Base.length(eqs) > 25 ? nothing : eqs end #hide
<< @example-block not executed in draft mode >>

Source

dyad
"""
Demonstrates amplitude modulation (AM) and frequency modulation (FM) using the
variable-frequency/amplitude sine and cosine sources.

Mirrors `Modelica.Blocks.Examples.Modulation`. The carrier frequency is 100 Hz and
the message (modulating) frequency is 2 Hz:

- `sine.y` is the unmodulated 100 Hz carrier reference,
  `sin(2π·100·t)`.
- `sinAM.y` / `cosAM.y` are amplitude modulated with a constant 100 Hz carrier and a
  time-varying amplitude `1 + 0.5·sin(2π·2·t)`.
- `sinFM.y` / `cosFM.y` are frequency modulated with a constant amplitude of 1 and an
  instantaneous frequency `100 + 50·sin(2π·2·t)`.

The `SineVariableFrequencyAndAmplitude` / `CosineVariableFrequencyAndAmplitude`
blocks always read both `amplitude` and `frequency` from input ports, so the constant
carrier frequency (AM) and constant carrier amplitude (FM) are supplied by `Constant`
sources.
"""
example component Modulation
  "Unmodulated 100 Hz carrier reference"
  sine = BlockComponents.Sources.Sine(amplitude = 1, frequency = 100) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 350, "y1": 30, "x2": 450, "y2": 130, "rot": 0}
      },
      "tags": []
    }
  }
  "AM amplitude signal: 1 + 0.5*sin(2π*2*t)"
  am_amplitude = BlockComponents.Sources.Sine(amplitude = 0.5, frequency = 2, offset = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 390, "y1": 610, "x2": 490, "y2": 710, "rot": 0}
      },
      "tags": []
    }
  }
  "Constant 100 Hz carrier frequency for the AM blocks"
  carrier_frequency = BlockComponents.Sources.Constant(k = 100) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 390, "y1": 820, "x2": 490, "y2": 920, "rot": 0}
      },
      "tags": []
    }
  }
  "Constant unit carrier amplitude for the FM blocks"
  carrier_amplitude = BlockComponents.Sources.Constant(k = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 360, "y1": 210, "x2": 460, "y2": 310, "rot": 0}
      },
      "tags": []
    }
  }
  "FM frequency signal: 100 + 50*sin(2π*2*t)"
  fm_frequency = BlockComponents.Sources.Sine(amplitude = 50, frequency = 2, offset = 100) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 370, "y1": 370, "x2": 470, "y2": 470, "rot": 0}
      },
      "tags": []
    }
  }
  "Amplitude-modulated sine"
  sinAM = BlockComponents.Sources.SineVariableFrequencyAndAmplitude() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 700, "y1": 610, "x2": 800, "y2": 710, "rot": 0}
      },
      "tags": []
    }
  }
  "Amplitude-modulated cosine"
  cosAM = BlockComponents.Sources.CosineVariableFrequencyAndAmplitude() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 700, "y1": 830, "x2": 800, "y2": 930, "rot": 0}
      },
      "tags": []
    }
  }
  "Frequency-modulated sine"
  sinFM = BlockComponents.Sources.SineVariableFrequencyAndAmplitude() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 690, "y1": 360, "x2": 790, "y2": 460, "rot": 0}
      },
      "tags": []
    }
  }
  "Frequency-modulated cosine"
  cosFM = BlockComponents.Sources.CosineVariableFrequencyAndAmplitude() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 680, "y1": 200, "x2": 780, "y2": 300, "rot": 0}
      },
      "tags": []
    }
  }
relations
  connect(am_amplitude.y, sinAM.amplitude) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 610, "y": 660}, {"x": 610, "y": 632}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(am_amplitude.y, cosAM.amplitude) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 610, "y": 660}, {"x": 610, "y": 854}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(carrier_frequency.y, sinAM.frequency) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 560, "y": 870}, {"x": 560, "y": 683}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(carrier_frequency.y, cosAM.frequency) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 560, "y": 870}, {"x": 560, "y": 903}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(fm_frequency.y, sinFM.frequency) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 530, "y": 420}, {"x": 530, "y": 433}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(fm_frequency.y, cosFM.frequency) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 530, "y": 420}, {"x": 530, "y": 273}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(sinFM.amplitude, carrier_amplitude.y) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 565, "y": 382}, {"x": 565, "y": 260}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(cosFM.amplitude, carrier_amplitude.y) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 565, "y": 224}, {"x": 565, "y": 260}], "E": 2}],
      "renderStyle": "standard"
    }
  }
metadata {
  "Dyad": {
    "icons": {"default": "dyad://BlockComponents/Example.svg"},
    "tests": {
      "case1": {"stop": 1, "signals": ["sine.y", "sinAM.y", "cosAM.y", "sinFM.y", "cosFM.y"]}
    }
  }
}
end
Flattened Source
dyad
"""
Demonstrates amplitude modulation (AM) and frequency modulation (FM) using the
variable-frequency/amplitude sine and cosine sources.

Mirrors `Modelica.Blocks.Examples.Modulation`. The carrier frequency is 100 Hz and
the message (modulating) frequency is 2 Hz:

- `sine.y` is the unmodulated 100 Hz carrier reference,
  `sin(2π·100·t)`.
- `sinAM.y` / `cosAM.y` are amplitude modulated with a constant 100 Hz carrier and a
  time-varying amplitude `1 + 0.5·sin(2π·2·t)`.
- `sinFM.y` / `cosFM.y` are frequency modulated with a constant amplitude of 1 and an
  instantaneous frequency `100 + 50·sin(2π·2·t)`.

The `SineVariableFrequencyAndAmplitude` / `CosineVariableFrequencyAndAmplitude`
blocks always read both `amplitude` and `frequency` from input ports, so the constant
carrier frequency (AM) and constant carrier amplitude (FM) are supplied by `Constant`
sources.
"""
example component Modulation
  "Unmodulated 100 Hz carrier reference"
  sine = BlockComponents.Sources.Sine(amplitude = 1, frequency = 100) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 350, "y1": 30, "x2": 450, "y2": 130, "rot": 0}
      },
      "tags": []
    }
  }
  "AM amplitude signal: 1 + 0.5*sin(2π*2*t)"
  am_amplitude = BlockComponents.Sources.Sine(amplitude = 0.5, frequency = 2, offset = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 390, "y1": 610, "x2": 490, "y2": 710, "rot": 0}
      },
      "tags": []
    }
  }
  "Constant 100 Hz carrier frequency for the AM blocks"
  carrier_frequency = BlockComponents.Sources.Constant(k = 100) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 390, "y1": 820, "x2": 490, "y2": 920, "rot": 0}
      },
      "tags": []
    }
  }
  "Constant unit carrier amplitude for the FM blocks"
  carrier_amplitude = BlockComponents.Sources.Constant(k = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 360, "y1": 210, "x2": 460, "y2": 310, "rot": 0}
      },
      "tags": []
    }
  }
  "FM frequency signal: 100 + 50*sin(2π*2*t)"
  fm_frequency = BlockComponents.Sources.Sine(amplitude = 50, frequency = 2, offset = 100) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 370, "y1": 370, "x2": 470, "y2": 470, "rot": 0}
      },
      "tags": []
    }
  }
  "Amplitude-modulated sine"
  sinAM = BlockComponents.Sources.SineVariableFrequencyAndAmplitude() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 700, "y1": 610, "x2": 800, "y2": 710, "rot": 0}
      },
      "tags": []
    }
  }
  "Amplitude-modulated cosine"
  cosAM = BlockComponents.Sources.CosineVariableFrequencyAndAmplitude() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 700, "y1": 830, "x2": 800, "y2": 930, "rot": 0}
      },
      "tags": []
    }
  }
  "Frequency-modulated sine"
  sinFM = BlockComponents.Sources.SineVariableFrequencyAndAmplitude() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 690, "y1": 360, "x2": 790, "y2": 460, "rot": 0}
      },
      "tags": []
    }
  }
  "Frequency-modulated cosine"
  cosFM = BlockComponents.Sources.CosineVariableFrequencyAndAmplitude() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 680, "y1": 200, "x2": 780, "y2": 300, "rot": 0}
      },
      "tags": []
    }
  }
relations
  connect(am_amplitude.y, sinAM.amplitude) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 610, "y": 660}, {"x": 610, "y": 632}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(am_amplitude.y, cosAM.amplitude) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 610, "y": 660}, {"x": 610, "y": 854}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(carrier_frequency.y, sinAM.frequency) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 560, "y": 870}, {"x": 560, "y": 683}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(carrier_frequency.y, cosAM.frequency) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 560, "y": 870}, {"x": 560, "y": 903}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(fm_frequency.y, sinFM.frequency) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 530, "y": 420}, {"x": 530, "y": 433}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(fm_frequency.y, cosFM.frequency) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 530, "y": 420}, {"x": 530, "y": 273}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(sinFM.amplitude, carrier_amplitude.y) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 565, "y": 382}, {"x": 565, "y": 260}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(cosFM.amplitude, carrier_amplitude.y) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 565, "y": 224}, {"x": 565, "y": 260}], "E": 2}],
      "renderStyle": "standard"
    }
  }
metadata {
  "Dyad": {
    "icons": {"default": "dyad://BlockComponents/Example.svg"},
    "tests": {
      "case1": {"stop": 1, "signals": ["sine.y", "sinAM.y", "cosAM.y", "sinFM.y", "cosFM.y"]}
    }
  }
}
end


Test Cases

julia
using BlockComponents
using DyadInterface: TransientAnalysis, rebuild_sol, ODEAlg
using ModelingToolkit: toggle_namespacing, get_initial_conditions, @named
using CSV, DataFrames, Plots

snapshotsdir = joinpath(dirname(dirname(pathof(BlockComponents))), "test", "snapshots")
<< @setup-block not executed in draft mode >>

Test Case case1

julia
@named model_case1 = BlockComponents.Examples.Modulation()
model_case1 = toggle_namespacing(model_case1, false)

model_case1 = toggle_namespacing(model_case1, true)
result_case1 = TransientAnalysis(; model = model_case1, alg = ODEAlg.Auto(), start = 0e+0, stop = 1e+0, abstol=1e-6, reltol=1e-6)
sol_case1 = rebuild_sol(result_case1)
<< @setup-block not executed in draft mode >>
  • Examples

  • Experiments

  • Analyses

julia
<< @example-block not executed in draft mode >>