# Phase Detector (`phaseDetector`)

> Compares the phase of two signals and outputs the difference.

![phaseDetector](https://voltecx-web.nguyenvanqui291.workers.dev/docs/reference/img/phaseDetector.svg)

[Component reference](https://voltecx-web.nguyenvanqui291.workers.dev/docs/reference/) · [繁體中文](https://voltecx-web.nguyenvanqui291.workers.dev/zh/docs/reference/control/phaseDetector/)

**Group:** Control · **Kind:** `{"block":"phaseDetector","kind":"control"}`

## When to use it

Use it to build a phase-locked loop from separate blocks. Use Phase-Locked Loop (single-phase) when a ready-made loop is enough.

## Pins

| Pin | Domain | Meaning |
|---|---|---|
| `a` | signal | Reference signal |
| `b` | signal | Signal to compare, usually the oscillator output |
| `out` | signal | Detector output, to the loop filter |
| `up` | signal | 1 while a leads, 0 in multiplier mode |
| `down` | signal | 1 while b leads, 0 in multiplier mode |

## Common parameters

- `type`: How the two inputs are compared: `multiplier`, `xor` or `pfd` (Default: multiplier)
- `gain`: Scale factor of the detector output (Default: 1)
- `threshold`: Level above which an input is high (Default: 0)

## Parameters

| Key | Unit | Default | Typical range | Description |
|---|---|---|---|---|
| `type` | — | multiplier |  | How the two inputs are compared: `multiplier`, `xor` or `pfd` |
| `gain` | — | 1 |  | Scale factor of the detector output |
| `threshold` | — | 0 |  | Level above which an input is high |

_Parameter descriptions are in English in both languages._

## Model

Multiplier gives out = gain*a*b. For two unit sines the mean is gain*cos(phase difference)/2, so a loop locks with the two signals 90 degrees apart, and the product also carries a ripple at twice the input frequency. XOR gives out = gain while exactly one input is above threshold. For two square waves the mean is gain*|phase difference|/pi. PFD uses two flip-flops. A rising edge of a sets up, a rising edge of b sets down, and when both are set they both clear, and out = gain*(up - down). Its mean follows the phase difference over plus or minus 2*pi and keeps its sign when the frequencies differ, so it can pull a loop in from far away. Edges are seen at solver steps.

## Common mistakes

- The multiplier output has a large part at twice the input frequency. Put a Second-Order Low-Pass well below that frequency and well above the loop bandwidth, or the oscillator is modulated and the loop may never settle.
- XOR and PFD read a level, not a wave, and an input counts as high above threshold. A 0 to 1 logic signal needs a threshold of 0.5, or every sample counts as high and no edge is ever seen.
- The multiplier locks with b 90 degrees away from a, not in phase. Use Phase-Locked Loop (single-phase), or shift the signal by 90 degrees, when the phase itself matters.

## Datasheet and sources

The phase comparators of a PLL IC such as the 4046 match the XOR type, the PFD type and an edge-triggered latch. For the XOR type set gain to the supply voltage, which gives a detector gain of about the supply voltage divided by pi in volts per radian, and a charge-pump PFD has the supply voltage divided by four pi.

## Example

Open the example with the command `file.openExample` and the name `demo:phaseDetector`.

---
Generated by `cargo xtask reference` from the usage texts and parameter specs. Do not edit by hand.
