---
url: /dyad/stable/stdlib/DiscreteComponents/components/SampleWithADEffects.md
description: >-
  Dyad SampleWithADEffects docs.  A sampler with additional effects that appear
  in practical systems, such as measurement noise and quantization.
---

# `SampleWithADEffects` {#SampleWithADEffects}

A sampler with additional effects that appear in practical systems, such as measurement noise and quantization.

The internal `Sampler` is driven by the clock inferred from the discrete context it is connected to (e.g. a `PeriodicClock` connected in the enclosing model), so there is no internal sample-interval parameter.

The operations occur in the order:

1. Sampling

2. Noise addition (additive Gaussian noise), applied only when `noisy = true`

3. Quantization

# Structural parameters: {#Structural-parameters:}

* `noisy`: If true (default), the output is corrupted by additive Gaussian noise; if false the noise block is bypassed.

* `midrise`: If true (default), the quantizer is a midrise quantizer, otherwise midtread.

# Parameters: {#Parameters:}

* `y_min`: Lower limit of output (only used when `quantized = true`)

* `y_max`: Upper limit of output (only used when `quantized = true`)

* `bits`: Number of bits of quantization (only used when `quantized = true`)

* `quantized`: If true, the output is quantized

* `sigma`: Standard deviation of the additive noise (only used when `noisy = true`)

* `seed`: RNG seed for reproducibility

# Connectors: {#Connectors:}

* `u`: Continuous-time input signal

* `y`: Discrete-time output signal with AD effects applied

## Usage {#Usage}

`DiscreteComponents.SampleWithADEffects(quantized=true, y_min=-1, y_max=1, bits=8, sigma=0.1, seed=1)`

## Parameters: {#Parameters:-2}

|        Name |                                                                                        Description | Units | Default value |
| -----------:| --------------------------------------------------------------------------------------------------:| -----:| -------------:|
|     `noisy` | If true, the output is corrupted by additive Gaussian noise, otherwise the noise block is bypassed |     – |          true |
|   `midrise` |                                  If true, the quantizer is a midrise quantizer, otherwise midtread |     – |          true |
| `quantized` |                                               Whether or not quantization is applied to the output |     – |          true |
|     `y_min` |                                                                              Lower limit of output |     – |            -1 |
|     `y_max` |                                                                              Upper limit of output |     – |             1 |
|      `bits` |                                                                     Number of bits of quantization |     – |             8 |
|     `sigma` |                                                           Standard deviation of the additive noise |     – |           0.1 |
|      `seed` |                                                                                           RNG seed |     – |             1 |

## Connectors {#Connectors}

* `u` - This connector represents a real signal as an input to a component ([`RealInput`](/stdlib/Dyad/connectors/RealInput#RealInput))

* `y` - This connector represents a real signal as an output from a component ([`RealOutput`](/stdlib/Dyad/connectors/RealOutput#RealOutput))

## Behavior {#Behavior}

$$ \begin{align}
0 &= \mathrm{connect}\left( quantization\_{+}y, y \right) \\
0 &= \mathrm{connect}\left( u, sampler\_{+}u \right) \\
0 &= \mathrm{connect}\left( normalnoise\_{+}y, add\_{+}u1 \right) \\
0 &= \mathrm{connect}\left( sampler\_{+}y, add\_{+}u2 \right) \\
0 &= \mathrm{connect}\left( add\_{+}y, quantization\_{+}u \right) \\
\mathtt{normalnoise.td}\left( t \right) &= \mathtt{normalnoise.sampler.y}\left( t \right) \\
\mathtt{normalnoise.y}\left( t \right) &= \mathtt{normalnoise.mu} + \mathtt{normalnoise.sigma} ~ seeded_randn\left( \mathtt{normalnoise.seed}, \mathtt{normalnoise.td}\left( t \right) \right) \\
0 &= \mathrm{connect}\left( continuousclock\_{+}y, sampler\_{+}u \right) \\
\mathtt{normalnoise.sampler.y}\left( t \right) &= Sample(InferredClock.InferredDiscrete(0))\left( \mathtt{normalnoise.sampler.u}\left( t \right) \right) \\
\mathtt{normalnoise.continuousclock.y}\left( t \right) &= ifelse\left( \mathtt{normalnoise.continuousclock.start_time} < t, \mathtt{normalnoise.continuousclock.offset} - \mathtt{normalnoise.continuousclock.start_time} + t, \mathtt{normalnoise.continuousclock.offset} \right) \\
\mathtt{quantization.y}\left( t \right) &= ifelse\left( \mathtt{quantization.quantized}, quantize\left( \mathtt{quantization.u}\left( t \right), \mathtt{quantization.bits}, \mathtt{quantization.y_min}, \mathtt{quantization.y_max}, true \right), \mathtt{quantization.u}\left( t \right) \right) \\
\mathtt{add.y}\left( t \right) &= \mathtt{add.k1} ~ \mathtt{add.u1}\left( t \right) + \mathtt{add.k2} ~ \mathtt{add.u2}\left( t \right) \\
\mathtt{sampler.y}\left( t \right) &= Sample(InferredClock.InferredDiscrete(0))\left( \mathtt{sampler.u}\left( t \right) \right)
\end{align}
$$

## Source {#Source}

```dyad
"""
A sampler with additional effects that appear in practical systems, such as measurement noise and quantization.

The internal `Sampler` is driven by the clock inferred from the discrete context it is connected to (e.g. a `PeriodicClock` connected in the enclosing model), so there is no internal sample-interval parameter.

The operations occur in the order:
1. Sampling
2. Noise addition (additive Gaussian noise), applied only when `noisy = true`
3. Quantization

# Structural parameters:
- `noisy`: If true (default), the output is corrupted by additive Gaussian noise; if false the noise block is bypassed.
- `midrise`: If true (default), the quantizer is a midrise quantizer, otherwise midtread.

# Parameters:
- `y_min`: Lower limit of output (only used when `quantized = true`)
- `y_max`: Upper limit of output (only used when `quantized = true`)
- `bits`: Number of bits of quantization (only used when `quantized = true`)
- `quantized`: If true, the output is quantized
- `sigma`: Standard deviation of the additive noise (only used when `noisy = true`)
- `seed`: RNG seed for reproducibility

# Connectors:
- `u`: Continuous-time input signal
- `y`: Discrete-time output signal with AD effects applied
"""
component SampleWithADEffects
  "Input signal"
  u = RealInput() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": -100, "y1": 450, "x2": 0, "y2": 550, "rot": 0}
      },
      "tags": []
    }
  }
  "Output signal"
  y = RealOutput() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 1000, "y1": 450, "x2": 1100, "y2": 550, "rot": 0}
      },
      "tags": []
    }
  }
  normalnoise = DiscreteComponents.NormalNoise(sigma = sigma, mu = 0, seed = seed) if noisy {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 110, "y1": 110, "x2": 310, "y2": 310, "rot": 0}
      },
      "tags": []
    }
  }
  quantization = DiscreteComponents.Quantization(midrise = midrise, quantized = quantized, bits = bits, y_min = y_min, y_max = y_max) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 730, "y1": 400, "x2": 930, "y2": 600, "rot": 0}
      },
      "tags": []
    }
  }
  add = BlockComponents.Math.Add() if noisy {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 480, "y1": 450, "x2": 580, "y2": 550, "rot": 0}
      },
      "tags": []
    }
  }
  sampler = DiscreteComponents.Sampler() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 160, "y1": 480, "x2": 260, "y2": 580, "rot": 0}
      },
      "tags": []
    }
  }
  "Whether or not quantization is applied to the output"
  parameter quantized::Boolean = true
  "If true, the output is corrupted by additive Gaussian noise, otherwise the noise block is bypassed"
  structural parameter noisy::Boolean = true
  "If true, the quantizer is a midrise quantizer, otherwise midtread"
  structural parameter midrise::Boolean = true
  "Lower limit of output"
  parameter y_min::Real = -1
  "Upper limit of output"
  parameter y_max::Real = 1
  "Number of bits of quantization"
  parameter bits::Integer = 8
  "Standard deviation of the additive noise"
  parameter sigma::Real = 0.1
  "RNG seed"
  parameter seed::Integer = 1
relations
  connect(quantization.y, y) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  connect(u, sampler.u) {
    "Dyad": {
      "renderStyle": "standard",
      "edges": [{"S": 1, "M": [{"x": -50, "y": 530}], "E": 2}]
    }
  }
  if noisy
    connect(normalnoise.y, add.u1) {
      "Dyad": {
        "edges": [{"S": 1, "M": [{"x": 397.5, "y": 210}, {"x": 397.5, "y": 470}], "E": 2}],
        "renderStyle": "standard"
      }
    }
    connect(sampler.y, add.u2) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
    connect(add.y, quantization.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  else
    connect(sampler.y, quantization.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  end
metadata {
  "Dyad": {"icons": {"default": "dyad://DiscreteComponents/SampleWithADEffects.svg"}}
}
end
```

```dyad
"""
A sampler with additional effects that appear in practical systems, such as measurement noise and quantization.

The internal `Sampler` is driven by the clock inferred from the discrete context it is connected to (e.g. a `PeriodicClock` connected in the enclosing model), so there is no internal sample-interval parameter.

The operations occur in the order:
1. Sampling
2. Noise addition (additive Gaussian noise), applied only when `noisy = true`
3. Quantization

# Structural parameters:
- `noisy`: If true (default), the output is corrupted by additive Gaussian noise; if false the noise block is bypassed.
- `midrise`: If true (default), the quantizer is a midrise quantizer, otherwise midtread.

# Parameters:
- `y_min`: Lower limit of output (only used when `quantized = true`)
- `y_max`: Upper limit of output (only used when `quantized = true`)
- `bits`: Number of bits of quantization (only used when `quantized = true`)
- `quantized`: If true, the output is quantized
- `sigma`: Standard deviation of the additive noise (only used when `noisy = true`)
- `seed`: RNG seed for reproducibility

# Connectors:
- `u`: Continuous-time input signal
- `y`: Discrete-time output signal with AD effects applied
"""
component SampleWithADEffects
  "Input signal"
  u = RealInput() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": -100, "y1": 450, "x2": 0, "y2": 550, "rot": 0}
      },
      "tags": []
    }
  }
  "Output signal"
  y = RealOutput() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 1000, "y1": 450, "x2": 1100, "y2": 550, "rot": 0}
      },
      "tags": []
    }
  }
  normalnoise = DiscreteComponents.NormalNoise(sigma = sigma, mu = 0, seed = seed) if noisy {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 110, "y1": 110, "x2": 310, "y2": 310, "rot": 0}
      },
      "tags": []
    }
  }
  quantization = DiscreteComponents.Quantization(midrise = midrise, quantized = quantized, bits = bits, y_min = y_min, y_max = y_max) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 730, "y1": 400, "x2": 930, "y2": 600, "rot": 0}
      },
      "tags": []
    }
  }
  add = BlockComponents.Math.Add() if noisy {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 480, "y1": 450, "x2": 580, "y2": 550, "rot": 0}
      },
      "tags": []
    }
  }
  sampler = DiscreteComponents.Sampler() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 160, "y1": 480, "x2": 260, "y2": 580, "rot": 0}
      },
      "tags": []
    }
  }
  "Whether or not quantization is applied to the output"
  parameter quantized::Boolean = true
  "If true, the output is corrupted by additive Gaussian noise, otherwise the noise block is bypassed"
  structural parameter noisy::Boolean = true
  "If true, the quantizer is a midrise quantizer, otherwise midtread"
  structural parameter midrise::Boolean = true
  "Lower limit of output"
  parameter y_min::Real = -1
  "Upper limit of output"
  parameter y_max::Real = 1
  "Number of bits of quantization"
  parameter bits::Integer = 8
  "Standard deviation of the additive noise"
  parameter sigma::Real = 0.1
  "RNG seed"
  parameter seed::Integer = 1
relations
  connect(quantization.y, y) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  connect(u, sampler.u) {
    "Dyad": {
      "renderStyle": "standard",
      "edges": [{"S": 1, "M": [{"x": -50, "y": 530}], "E": 2}]
    }
  }
  if noisy
    connect(normalnoise.y, add.u1) {
      "Dyad": {
        "edges": [{"S": 1, "M": [{"x": 397.5, "y": 210}, {"x": 397.5, "y": 470}], "E": 2}],
        "renderStyle": "standard"
      }
    }
    connect(sampler.y, add.u2) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
    connect(add.y, quantization.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  else
    connect(sampler.y, quantization.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  end
metadata {
  "Dyad": {"icons": {"default": "dyad://DiscreteComponents/SampleWithADEffects.svg"}}
}
end
```

## Test Cases {#Test-Cases}

No test cases defined.

## Related {#Related}

* Examples

* Experiments

* Analyses

* Tests
  * [`TestSampleWithADEffects`](SampleWithADEffects.md)
