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.
- Apply the discrete Fourier transform to the window x to obtain the amplitude spectrum A(f) and the phase spectrum P(f).
- Compute the log-amplitude spectrum L(f) = log A(f).
- Smooth L(f) with an average filter of size q, often q = 3, to obtain AL(f).
- Take the spectral residual R(f) = L(f) minus AL(f), which keeps only the irregular part of the spectrum.
- 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.
| Parameter | Typical value | Effect when increased |
|---|---|---|
| Window length | Hundreds to a few thousand points | More context, slower reaction to drift |
| Average filter size q | 3 | Removes broader structure from the spectrum |
| Local average length z | About 21 points | Smoother baseline, fewer isolated alerts |
| Extension points | About 5 points | Better handling of the newest observation |
| Score threshold tau | Around 3, tuned per metric | Fewer alerts and lower recall |
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.
| Variant | Decision step | Labels required |
|---|---|---|
| Image saliency SR | Thresholded saliency on 2D images | None |
| Time series SR | Relative score against a local mean | None |
| SR-CNN | 1D CNN trained on the saliency map | None, uses injected anomalies |
| SR with supervised head | Classifier on SR features plus others | Some labeled anomalies |
How spectral residual compares with related detectors
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.
| Method | Core idea | Main strength |
|---|---|---|
| Spectral residual | Irregular part of the log spectrum | Fast, training-free spike detection |
| Seasonal hybrid ESD | Decomposition plus robust statistical test | Explicit seasonality handling |
| Forecasting-based detection | Residual from a predicted value | Captures complex temporal patterns |
| Matrix profile | Distance to nearest subsequence | Finds discords of fixed length |
| Change point detection | Shifts in distribution parameters | Detects lasting regime changes |
| Isolation forest on windows | Isolation depth of feature vectors | Handles many engineered features |
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.
| Symptom | Likely cause | Remedy |
|---|---|---|
| Slow drift goes unnoticed | Smooth changes stay in the averaged spectrum | Add change point detection |
| Alerts at every seasonal peak | Window shorter than the seasonal cycle | Lengthen the window |
| Newest point scored poorly | Edge effect of the Fourier transform | Use extension points |
| Too many alerts on noisy metrics | Threshold too low for the noise level | Raise tau or add absolute limits |
| Missed anomalies after gaps | Missing values break the periodic assumption | Impute gaps before scoring |
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.