---
url: /dyad/dev/stdlib/BlockComponents/examples/Examples.Modulation.md
description: >-
  Dyad Examples.Modulation docs.  Demonstrates amplitude modulation (AM) and
  frequency modulation (FM) using the variable-frequency/amplitude sine and
  cosine sources.
---

# `Examples.Modulation` {#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 {#Usage}

`BlockComponents.Examples.Modulation()`

## Behavior {#Behavior}

$$ \begin{align}
0 &= \mathrm{connect}\left( am\_{amplitude\_{+}y}, sinAM\_{+}amplitude \right) \\
0 &= \mathrm{connect}\left( am\_{amplitude\_{+}y}, cosAM\_{+}amplitude \right) \\
0 &= \mathrm{connect}\left( carrier\_{frequency\_{+}y}, sinAM\_{+}frequency \right) \\
0 &= \mathrm{connect}\left( carrier\_{frequency\_{+}y}, cosAM\_{+}frequency \right) \\
0 &= \mathrm{connect}\left( fm\_{frequency\_{+}y}, sinFM\_{+}frequency \right) \\
0 &= \mathrm{connect}\left( fm\_{frequency\_{+}y}, cosFM\_{+}frequency \right) \\
0 &= \mathrm{connect}\left( carrier\_{amplitude\_{+}y}, sinFM\_{+}amplitude \right) \\
0 &= \mathrm{connect}\left( carrier\_{amplitude\_{+}y}, cosFM\_{+}amplitude \right) \\
\mathtt{sine.y}\left( t \right) &= ifelse\left( \mathtt{sine.continuous}, ifelse\left( t \geq \mathtt{sine.start_time}, \sin\left( \mathtt{sine.phase} + 6.2832 ~ \mathtt{sine.frequency} ~ \left(  - \mathtt{sine.shift_time} + t \right) \right), \sin\left( \mathtt{sine.phase} + 6.2832 ~ \mathtt{sine.frequency} ~ \left(  - \mathtt{sine.shift_time} + \mathtt{sine.start_time} \right) \right) \right) ~ \mathtt{sine.amplitude}, ifelse\left( t \geq \mathtt{sine.start_time}, \sin\left( \mathtt{sine.phase} + 6.2832 ~ \mathtt{sine.frequency} ~ \left(  - \mathtt{sine.shift_time} + t \right) \right) ~ \mathtt{sine.amplitude}, 0 \right) \right) + \mathtt{sine.offset} \\
\mathtt{am_amplitude.y}\left( t \right) &= \mathtt{am_amplitude.offset} + ifelse\left( \mathtt{am_amplitude.continuous}, \mathtt{am_amplitude.amplitude} ~ ifelse\left( t \geq \mathtt{am_amplitude.start_time}, \sin\left( \mathtt{am_amplitude.phase} + 6.2832 ~ \mathtt{am_amplitude.frequency} ~ \left(  - \mathtt{am_amplitude.shift_time} + t \right) \right), \sin\left( \mathtt{am_amplitude.phase} + 6.2832 ~ \mathtt{am_amplitude.frequency} ~ \left(  - \mathtt{am_amplitude.shift_time} + \mathtt{am_amplitude.start_time} \right) \right) \right), ifelse\left( t \geq \mathtt{am_amplitude.start_time}, \mathtt{am_amplitude.amplitude} ~ \sin\left( \mathtt{am_amplitude.phase} + 6.2832 ~ \mathtt{am_amplitude.frequency} ~ \left(  - \mathtt{am_amplitude.shift_time} + t \right) \right), 0 \right) \right) \\
\mathtt{carrier_frequency.y}\left( t \right) &= \mathtt{carrier_frequency.k} \\
\mathtt{carrier_amplitude.y}\left( t \right) &= \mathtt{carrier_amplitude.k} \\
\mathtt{fm_frequency.y}\left( t \right) &= \mathtt{fm_frequency.offset} + ifelse\left( \mathtt{fm_frequency.continuous}, \mathtt{fm_frequency.amplitude} ~ ifelse\left( t \geq \mathtt{fm_frequency.start_time}, \sin\left( \mathtt{fm_frequency.phase} + 6.2832 ~ \mathtt{fm_frequency.frequency} ~ \left(  - \mathtt{fm_frequency.shift_time} + t \right) \right), \sin\left( \mathtt{fm_frequency.phase} + 6.2832 ~ \mathtt{fm_frequency.frequency} ~ \left(  - \mathtt{fm_frequency.shift_time} + \mathtt{fm_frequency.start_time} \right) \right) \right), ifelse\left( t \geq \mathtt{fm_frequency.start_time}, \mathtt{fm_frequency.amplitude} ~ \sin\left( \mathtt{fm_frequency.phase} + 6.2832 ~ \mathtt{fm_frequency.frequency} ~ \left(  - \mathtt{fm_frequency.shift_time} + t \right) \right), 0 \right) \right) \\
\frac{\mathrm{d}\mathtt{sinAM.phi}\left( t \right)}{\mathrm{d}t} &= 6.2832 ~ \mathtt{sinAM.frequency}\left( t \right) \\
\mathtt{sinAM.y}\left( t \right) &= \mathtt{sinAM.offset} + \sin\left( \mathtt{sinAM.phi}\left( t \right) \right) ~ \mathtt{sinAM.amplitude}\left( t \right) \\
\frac{\mathrm{d}\mathtt{cosAM.phi}\left( t \right)}{\mathrm{d}t} &= 6.2832 ~ \mathtt{cosAM.frequency}\left( t \right) \\
\mathtt{cosAM.y}\left( t \right) &= \mathtt{cosAM.offset} + \cos\left( \mathtt{cosAM.phi}\left( t \right) \right) ~ \mathtt{cosAM.amplitude}\left( t \right) \\
\frac{\mathrm{d}\mathtt{sinFM.phi}\left( t \right)}{\mathrm{d}t} &= 6.2832 ~ \mathtt{sinFM.frequency}\left( t \right) \\
\mathtt{sinFM.y}\left( t \right) &= \mathtt{sinFM.offset} + \sin\left( \mathtt{sinFM.phi}\left( t \right) \right) ~ \mathtt{sinFM.amplitude}\left( t \right) \\
\frac{\mathrm{d}\mathtt{cosFM.phi}\left( t \right)}{\mathrm{d}t} &= 6.2832 ~ \mathtt{cosFM.frequency}\left( t \right) \\
\mathtt{cosFM.y}\left( t \right) &= \mathtt{cosFM.offset} + \cos\left( \mathtt{cosFM.phi}\left( t \right) \right) ~ \mathtt{cosFM.amplitude}\left( t \right)
\end{align}
$$

## Source {#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
```

```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 {#Test-Cases}

### Test Case `case1` {#Test-Case-case1}

## Related {#Related}

* Examples

* Experiments

* Analyses
