# Hilbert Transform

A Statistics concept (Regression & filtering) in the LuxAlgo Library, with 1 indicator implementation.

## What is the Hilbert Transform?

The Hilbert transform is a signal-processing operation that shifts every frequency component of a series by 90 degrees. Pairing the original series with this shifted copy (the quadrature component) forms the analytic signal, from which two things can be read at every bar: instantaneous amplitude, the size of the current cycle, and instantaneous phase, the position within it. The rate at which phase advances is instantaneous frequency, and its inverse is the cycle period.

John Ehlers adapted the transform for trading with short filter approximations, making it the engine of the DSP tradition in charting: [dominant cycle measurement](https://www.luxalgo.com/library/concept/dominant-cycle-measurement/), phase tools like the [sinewave indicator](https://www.luxalgo.com/library/concept/sinewave-indicator/), and adaptive averages such as [MAMA/FAMA](https://www.luxalgo.com/library/concept/mama-fama/) that retune themselves to the measured period. The caveat: the math assumes a reasonably clean oscillation, which raw price is not, so implementations detrend and smooth first and still produce estimates that jitter around regime changes.

Chart implementations cannot compute the textbook transform, which needs an infinite window; they approximate the 90-degree shift with short weighted-difference filters spanning a handful of bars, applied after detrending. Ehlers' published designs, the reference most Library scripts follow, pair that quadrature approximation with a homodyne discriminator, which averages successive phase differences to steady the period estimate, and constrain measurable cycles to roughly 6 to 50 bars: a narrower but faster read than block methods like [FFT/spectral analysis](https://www.luxalgo.com/library/concept/fft-spectral-analysis/).

What practitioners actually consume sits downstream of the math. The MESA adaptive moving average family, represented here by vosechu's Ehlers MESA Adaptive Moving Average and cheatcountry's Mother of Adaptive Moving Averages, retunes its smoothing to the measured phase rate each bar. Sine-wave tools project the dominant cycle as an oscillator with an anticipatory lead line, and jacobnie2008's Hilbert Sine Wave Support and Resistance maps cycle extremes onto price levels. Ehlers also recommends gating everything on signal-to-noise: when measured amplitude is small relative to noise, cycle tools should stand down.

## How to identify Hilbert transform outputs on a chart

You rarely plot the transform itself; you meet its outputs, period, phase and amplitude, inside other tools. Auditing them follows the processing chain.

1. Confirm the input is detrended, typically by differencing or subtracting a smooth, because the analytic-signal math assumes a roughly zero-mean oscillation and trend leaks into phase as a spurious slow cycle.
2. Check the quadrature stage: a short weighted-difference filter approximating the 90-degree shift, usually following Ehlers' published coefficients.
3. Read amplitude and phase from the analytic pair, the price component against the quadrature component, at each bar.
4. Verify the period logic: phase should advance roughly 360 degrees over one measured cycle, with a homodyne or similar averaging stage steadying the estimate.
5. Look for an amplitude or signal-to-noise gate; an implementation that acts on phase while amplitude is negligible is trading noise.

## How it's calculated

A 90-degree phase-shifted copy of the price series that, paired with the original, yields the instantaneous phase, amplitude, and dominant cycle period of the market cycle.

```
x_t = (4 × P_t + 3 × P_(t-1) + 2 × P_(t-2) + P_(t-3)) / 10
k_t = 0.075 × Period_(t-1) + 0.54
Q_t = (0.0962 × x_t + 0.5769 × x_(t-2) - 0.5769 × x_(t-4) - 0.0962 × x_(t-6)) × k_t
I_t = x_(t-3)
z_t = I_t + i × Q_t
Phase_t = arctan(Q_t / I_t)
Amplitude_t = sqrt(I_t^2 + Q_t^2)
Period_t = 360 / abs(Phase_t - Phase_(t-1))

  P_t: source price at bar t, commonly the median price (high + low) / 2
  x_t: smoothed source, a 4-bar weighted average of P
  k_t: amplitude correction factor tied to the previously measured cycle period
  Q_t: quadrature component, the Hilbert Transform of the smoothed price, 90 degrees out of phase
  I_t: in-phase component, the smoothed price delayed 3 bars
  z_t: analytic signal combining the in-phase and quadrature parts
  i: imaginary unit
  arctan(): inverse tangent, read in degrees here
  Phase_t: instantaneous phase at bar t, in degrees
  Amplitude_t: instantaneous cycle amplitude at bar t
  Period_t: dominant cycle period estimate in bars, typically clamped to 6 to 50
  t: bar index
```

The exact Hilbert Transform is an infinite-length convolution that shifts every frequency component by 90 degrees; the 4-coefficient filter above is John Ehlers' finite approximation, tuned for cycle periods of roughly 6 to 50 bars.

In Ehlers' full dominant-cycle measurement the same filter is applied twice (once to detrend, once for quadrature), I and Q are smoothed, and the bar-to-bar change in Period is limited to about 0.67x to 1.5x of the prior value.

TA-Lib's HT_TRENDLINE, HT_DCPERIOD, HT_PHASOR, and HT_SINE are all built on this construction.

## How traders use it

- To measure the dominant cycle period, which then drives adaptive lookbacks for oscillators and moving averages instead of fixed settings.
- To trade phase directly: phase-based tools anticipate cyclic turns as phase approaches its extremes, rather than waiting for a lagging crossover after the turn.
- To classify trend versus cycle mode: when measured phase stops advancing at the expected cycle rate, DSP systems switch from cycle tactics to trend tactics.
- To draw cyclical support and resistance: projecting the measured amplitude around a centerline turns the abstract cycle into price levels that update as the cycle evolves.
- To gate other systems: measured signal-to-noise decides whether cycle tactics are allowed at all, suppressing oscillator entries when amplitude is too small to trade.

## Hilbert transform vs other cycle and estimation tools

- **FFT/Spectral Analysis** (https://www.luxalgo.com/library/concept/fft-spectral-analysis/): The FFT decomposes a whole window into many frequencies at once, assuming the content held still across it. The Hilbert transform gives one instantaneous estimate per bar and tracks change quickly, but only for the dominant cycle; it cannot see several cycles at once.
- **Kalman Filter** (https://www.luxalgo.com/library/concept/kalman-filter/): A Kalman filter estimates hidden state from an explicit model with specified noise, and its quality depends on that model being right. The Hilbert approach is nearly model-free, extracting phase and amplitude directly, which makes it more flexible and less principled.
- **Autocorrelation** (https://www.luxalgo.com/library/concept/autocorrelation/): Autocorrelation finds the period by locating the lag where the series best matches itself, a robust but coarse read. Ehlers himself moved toward autocorrelation periodograms in later work because raw Hilbert period estimates jitter in noise; the transform still wins on per-bar phase.

## FAQ

### What does the Hilbert transform actually give a trader?

Two per-bar readings ordinary indicators do not provide: instantaneous phase, the position within the current cycle, and instantaneous amplitude, the cycle's size. Phase yields the cycle period and an anticipatory read on turns; amplitude indicates whether the cycle is big enough to trade. Both are estimates from a noisy series, so they are smoothed and can still lag or jitter.

### Why do Hilbert-based cycle readings jump around?

The transform assumes the underlying oscillation is fairly stable, but markets shift period and amplitude constantly while adding trend and noise on top. Near regime changes the phase estimate can stall or slip, and period readings swing until the new rhythm settles. Detrending and smoothing reduce the jitter at the cost of lag; no setting removes it entirely.

### Why must price be detrended before the Hilbert transform?

The analytic-signal construction assumes the input oscillates around zero. A trend violates that: the transform reads it as part of an extremely long cycle, contaminating phase and stretching period estimates. Differencing, subtracting a moving average, or band-limiting the series first removes the offending low-frequency content, which is why every serious implementation detrends as step one.

### What is the analytic signal?

The complex pairing of the original series with its 90-degree-shifted copy. Treat the original as the horizontal component and the shifted copy as the vertical: the vector's length is instantaneous amplitude, its angle is instantaneous phase, and the speed at which the angle turns is instantaneous frequency. All Hilbert-based indicator outputs are read off this one geometric object.

### What cycle lengths can chart implementations actually measure?

Ehlers' filter designs, which most scripts inherit, are tuned for roughly 6-to-50-bar cycles. Below that, a cycle is sampled by too few bars to resolve its phase; above it, the short quadrature filters lose accuracy and the estimate drifts. Longer rhythms are usually measured by switching to a higher timeframe rather than stretching the filter, or with [autocorrelation](https://www.luxalgo.com/library/concept/autocorrelation/) methods that tolerate noise better at long lags.

### Is the Hilbert transform predictive?

It measures the present precisely rather than forecasting: amplitude and phase describe the cycle as it stands. The anticipatory feel of tools built on it comes from extrapolation, assuming phase keeps advancing at its current rate, which holds in stable cycle regimes and fails exactly when the market shifts character. Unlike a [Kalman filter](https://www.luxalgo.com/library/concept/kalman-filter/), it carries no explicit forecast model, so treat its lead as conditional, not clairvoyant.

## Implementations in the Library

- Hilbert Transform (LuxAlgo): https://www.luxalgo.com/library/indicator/hilbert-transform/

## Related concepts

- Linear Regression: https://www.luxalgo.com/library/concept/linear-regression/
- Polynomial Regression: https://www.luxalgo.com/library/concept/polynomial-regression/
- Quantile Regression: https://www.luxalgo.com/library/concept/quantile-regression/
- Kalman Filter: https://www.luxalgo.com/library/concept/kalman-filter/
- Hodrick-Prescott Filter: https://www.luxalgo.com/library/concept/hodrick-prescott-filter/
- Wavelet Decomposition: https://www.luxalgo.com/library/concept/wavelet-decomposition/
- FFT/spectral Analysis: https://www.luxalgo.com/library/concept/fft-spectral-analysis/
- Maximum-entropy Spectrum: https://www.luxalgo.com/library/concept/maximum-entropy-spectrum/
- Exponential Smoothing Forecasts: https://www.luxalgo.com/library/concept/exponential-smoothing-forecasts/
- LOESS Smoothing: https://www.luxalgo.com/library/concept/loess-smoothing/

---

Source: https://www.luxalgo.com/library/concept/hilbert-transform/ (LuxAlgo Library, the encyclopedia of trading & technical analysis). Free to use with attribution: https://www.luxalgo.com/library/license/