Spectral residual · anomaly detection

On this page 8

Spectral residual (SR) in anomaly detection is an unsupervised, frequency-domain method that flags unusual points in a time series by computing the difference between the log-amplitude spectrum and its smoothed version, then transforming that residual back into a saliency map whose peaks mark anomalies. Originally proposed for visual saliency detection in images, it was adapted to time series and became widely known through the SR-CNN approach used for large-scale online metric monitoring.

The method is attractive because it needs no labels, no model training and only a few parameters, and it runs in O(n log n) time per window thanks to the fast Fourier transform. It works best for sudden spikes, dips and short level shifts in metrics such as traffic, latency or sensor readings.

How the spectral residual algorithm works

The core idea is that the regular part of a signal produces a smooth log-amplitude spectrum, while rare, abrupt events add irregular components that stand out after the smooth trend is removed. The algorithm processes a sliding window of the series in five steps.

  1. Apply the discrete Fourier transform to the window x to obtain the amplitude spectrum A(f) and the phase spectrum P(f).
  2. Compute the log-amplitude spectrum L(f) = log A(f).
  3. Smooth L(f) with an average filter of size q, often q = 3, to obtain AL(f).
  4. Take the spectral residual R(f) = L(f) minus AL(f), which keeps only the irregular part of the spectrum.
  5. Apply the inverse Fourier transform to exp(R(f) + iP(f)) and take the magnitude to get the saliency map S(x).

The original phase is kept unchanged, so the saliency map stays aligned in time with the input. Points where S(x) is far above its local average are candidates for anomalies.

From saliency map to anomaly score

The saliency map alone is not a decision, so SR converts it into a relative anomaly score. A common rule compares each saliency value with the local mean of the preceding z saliency values and reports an anomaly when (S(x) minus local mean) divided by the local mean exceeds a threshold tau.

A practical issue arises at the right edge of the window, which is exactly where the newest point sits in streaming monitoring. The Fourier transform treats the window as periodic, so the last point is distorted. SR solves this by appending a few estimated points after the window, extrapolated from the average gradient of recent values, so that the newest observation moves toward the center of the analyzed segment.

The table below lists the main parameters and their effect on detection.

ParameterTypical valueEffect when increased
Window lengthHundreds to a few thousand pointsMore context, slower reaction to drift
Average filter size q3Removes broader structure from the spectrum
Local average length zAbout 21 pointsSmoother baseline, fewer isolated alerts
Extension pointsAbout 5 pointsBetter handling of the newest observation
Score threshold tauAround 3, tuned per metricFewer alerts and lower recall
Spectral residual parameters

SR-CNN and other variants

Plain SR is a fixed, rule-based detector, which makes it robust but limits accuracy on complex metrics. The SR-CNN variant feeds the saliency map into a small one-dimensional convolutional network that learns the decision boundary. The network is trained on synthetic anomalies injected into normal data, so it still needs no human labels.

The following table compares the main forms of the method.

VariantDecision stepLabels required
Image saliency SRThresholded saliency on 2D imagesNone
Time series SRRelative score against a local meanNone
SR-CNN1D CNN trained on the saliency mapNone, uses injected anomalies
SR with supervised headClassifier on SR features plus othersSome labeled anomalies
Variants of the spectral residual approach

SR belongs to time series anomaly detection and is best understood against neighbors that make different assumptions. It detects point anomalies and short collective anomalies well, but it does not model seasonality explicitly and does not track long-term change.

The table below positions it against other common methods from the same field.

MethodCore ideaMain strength
Spectral residualIrregular part of the log spectrumFast, training-free spike detection
Seasonal hybrid ESDDecomposition plus robust statistical testExplicit seasonality handling
Forecasting-based detectionResidual from a predicted valueCaptures complex temporal patterns
Matrix profileDistance to nearest subsequenceFinds discords of fixed length
Change point detectionShifts in distribution parametersDetects lasting regime changes
Isolation forest on windowsIsolation depth of feature vectorsHandles many engineered features
Spectral residual compared with neighboring methods

Practical use and configuration

SR is used in metric monitoring platforms where thousands or millions of series must be screened continuously with little per-series tuning. It underpinned the univariate detection in Azure Anomaly Detector and is available in open-source libraries, including an implementation in ML.NET.

Good deployments follow a few rules. Normalize or detrend strongly trending series before scoring, choose a window that covers several seasonal cycles, and tune tau per metric family using historical incidents. Combining SR with dynamic thresholding and simple business rules, such as minimum absolute deviation, reduces alert fatigue considerably.

Limitations and common pitfalls

The main weakness of SR is that it responds to abrupt, short-lived deviations rather than gradual or contextual ones. The table below lists typical symptoms, causes and remedies.

SymptomLikely causeRemedy
Slow drift goes unnoticedSmooth changes stay in the averaged spectrumAdd change point detection
Alerts at every seasonal peakWindow shorter than the seasonal cycleLengthen the window
Newest point scored poorlyEdge effect of the Fourier transformUse extension points
Too many alerts on noisy metricsThreshold too low for the noise levelRaise tau or add absolute limits
Missed anomalies after gapsMissing values break the periodic assumptionImpute gaps before scoring
Common problems with spectral residual

Put simply

Think of a busy street that hums with a steady, familiar noise. A sudden car horn stands out at once. You notice it because it does not fit the usual background sound.

Spectral residual does the same with numbers over time. It first learns the usual background pattern of a chart. It then removes that pattern and looks at what is left. Anything that sticks out sharply is marked as unusual. It needs no examples of past problems, so it can watch many charts at once.

In practice

An online retailer monitors about 40,000 metrics, such as checkout requests per minute, payment errors and page latency for each region. The operations team cannot label incidents for every metric, so it runs spectral residual on a sliding window of 1,440 one-minute points, which covers a full day.

One evening, payment errors in a single region jump from roughly 5 to 60 per minute within three minutes. The saliency score rises far above the threshold of 3, and an alert reaches the on-call engineer within two minutes. A faulty configuration update is rolled back before most customers notice. Over the following weeks, the team adds a minimum absolute change rule, which cuts false alerts on low-traffic metrics by about half.