---
url: /dyad/dev/stdlib/DiscreteComponents/components/Quantization.md
description: >-
  Dyad Quantization docs.  A quantization block that quantizes the input signal
  to a specified number of bits.
---

# `Quantization` {#Quantization}

A quantization block that quantizes the input signal to a specified number of bits.

The output is clamped to the range `[y_min, y_max]` and quantized to discrete levels. A midrise quantizer does not have a zero output level, while a midtread quantizer does.

This block is not differentiable; the derivative is zero everywhere except at level transitions where it is ill defined. To use in a differentiable context, set `quantized = false` which turns this block into the identity function.

# Parameters: {#Parameters:}

* `y_max`: Upper limit of output

* `y_min`: Lower limit of output

* `bits`: Number of bits of quantization

* `quantized`: If quantization effects shall be computed. If false, the output is equal to the input.

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

# Connectors: {#Connectors:}

* `u`: Input signal

* `y`: Output signal

## Usage {#Usage}

`DiscreteComponents.Quantization(y_max=1, y_min=-1, bits=8, quantized=true)`

## Parameters: {#Parameters:-2}

|        Name |                                                       Description | Units | Default value |
| -----------:| -----------------------------------------------------------------:| -----:| -------------:|
|   `midrise` | If true, the quantizer is a midrise quantizer, otherwise midtread |     – |          true |
|     `y_max` |                                             Upper limit of output |     – |             1 |
|     `y_min` |                                             Lower limit of output |     – |            -1 |
|      `bits` |                                    Number of bits of quantization |     – |             8 |
| `quantized` |                         If quantization effects shall be computed |     – |          true |

## 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}
y\left( t \right) &= ifelse\left( \mathtt{quantized}, quantize\left( u\left( t \right), \mathtt{bits}, \mathtt{y_min}, \mathtt{y_max}, true \right), u\left( t \right) \right)
\end{align}
$$

## Source {#Source}

```dyad
"""
A quantization block that quantizes the input signal to a specified number of bits.

The output is clamped to the range `[y_min, y_max]` and quantized to discrete levels. A midrise quantizer does not have a zero output level, while a midtread quantizer does.

This block is not differentiable; the derivative is zero everywhere except at level transitions where it is ill defined. To use in a differentiable context, set `quantized = false` which turns this block into the identity function.

# Parameters:
- `y_max`: Upper limit of output
- `y_min`: Lower limit of output
- `bits`: Number of bits of quantization
- `quantized`: If quantization effects shall be computed. If false, the output is equal to the input.
- `midrise`: (structural) If true (default), the quantizer is a midrise quantizer, otherwise it is a midtread quantizer.

# Connectors:
- `u`: Input signal
- `y`: Output signal
"""
component Quantization@[input clk extends Discrete]
  "Input signal"
  u = RealInput@[clk]() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": -100, "y1": 450, "x2": 0, "y2": 550, "rot": 0}
      },
      "tags": []
    }
  }
  "Output signal"
  y = RealOutput@[clk]() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 1000, "y1": 450, "x2": 1100, "y2": 550, "rot": 0}
      },
      "tags": []
    }
  }
  "If true, the quantizer is a midrise quantizer, otherwise midtread"
  structural parameter midrise::Boolean = true
  "Upper limit of output"
  parameter y_max::Real = 1
  "Lower limit of output"
  parameter y_min::Real = -1
  "Number of bits of quantization"
  parameter bits::Integer = 8
  "If quantization effects shall be computed"
  parameter quantized::Boolean = true
relations
  y = ifelse(quantized, quantize(u, bits, y_min, y_max, midrise), u)
metadata {"Dyad": {"icons": {"default": "dyad://DiscreteComponents/Quantization.svg"}}}
end
```

```dyad
"""
A quantization block that quantizes the input signal to a specified number of bits.

The output is clamped to the range `[y_min, y_max]` and quantized to discrete levels. A midrise quantizer does not have a zero output level, while a midtread quantizer does.

This block is not differentiable; the derivative is zero everywhere except at level transitions where it is ill defined. To use in a differentiable context, set `quantized = false` which turns this block into the identity function.

# Parameters:
- `y_max`: Upper limit of output
- `y_min`: Lower limit of output
- `bits`: Number of bits of quantization
- `quantized`: If quantization effects shall be computed. If false, the output is equal to the input.
- `midrise`: (structural) If true (default), the quantizer is a midrise quantizer, otherwise it is a midtread quantizer.

# Connectors:
- `u`: Input signal
- `y`: Output signal
"""
component Quantization
  "Input signal"
  u = RealInput@[clk]() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": -100, "y1": 450, "x2": 0, "y2": 550, "rot": 0}
      },
      "tags": []
    }
  }
  "Output signal"
  y = RealOutput@[clk]() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 1000, "y1": 450, "x2": 1100, "y2": 550, "rot": 0}
      },
      "tags": []
    }
  }
  "If true, the quantizer is a midrise quantizer, otherwise midtread"
  structural parameter midrise::Boolean = true
  "Upper limit of output"
  parameter y_max::Real = 1
  "Lower limit of output"
  parameter y_min::Real = -1
  "Number of bits of quantization"
  parameter bits::Integer = 8
  "If quantization effects shall be computed"
  parameter quantized::Boolean = true
relations
  y = ifelse(quantized, quantize(u, bits, y_min, y_max, midrise), u)
metadata {"Dyad": {"icons": {"default": "dyad://DiscreteComponents/Quantization.svg"}}}
end
```

## Test Cases {#Test-Cases}

No test cases defined.

## Related {#Related}

* Examples

* Experiments

* Analyses
