A sensor trace mixes slow changes with quick fluctuations, and the pattern you care about can be hard to see in the raw signal. Looking at its frequencies gives you another view of those changes. The Fourier transform separates contributions at different frequencies, helping us examine repeating structure and the effects of filtering before trying to forecast the series.
Separating frequencies helps fitting, but forecast errors accumulate. Per-band networks exceed R² = 0.999 in sample, yet drift over 80 steps. We recover a pulse with a Gaussian filter, build a Gabor spectrogram and train the networks to check that short-horizon limit.
Decomposing a Signal
Take a composite signal made of three sine waves at 5, 23, and 50 Hz. In the time domain, the waveform looks complex. But the FFT instantly reveals the three constituent frequencies as sharp spikes in the frequency spectrum:

Let's Build It
Click the badge to run this yourself:
import numpy as np
import matplotlib.pyplot as plt
# Composite signal: 3 sinusoids
n = 512
t = np.linspace(0, 1, n, endpoint=False)
fs = n # sampling frequency (512 Hz)
signal = (2*np.sin(2*np.pi*5*t) +
1.5*np.sin(2*np.pi*23*t) +
0.8*np.sin(2*np.pi*50*t))
# FFT: time domain → frequency domain
F = np.fft.fft(signal)
freqs = np.fft.fftfreq(n, d=1/fs)
# One-sided magnitude spectrum
magnitude = 2 * np.abs(F[:n//2]) / n
Three peaks at exactly 5, 23, and 50 Hz
with magnitudes 2.0, 1.5, and 0.8 — matching the input amplitudes.
The FFT takes time-domain samples and returns complex coefficients. Each coefficient encodes the amplitude and phase of a sine wave at a specific frequency. The magnitude tells you "how much" of that frequency is present; the phase tells you "where" the sine wave starts.
What Just Happened?
The DFT Formula
The Discrete Fourier Transform converts a sequence into frequency coefficients:
Each is a complex number. Its magnitude gives the amplitude of the frequency component at Hz. The inverse transform reconstructs the original signal exactly:
This perfect reconstruction is the key property we exploit throughout this post: anything we do in the frequency domain can be undone by the inverse FFT.
Band-Pass Filtering: Splitting the Signal
The Fourier transform is linear. If a signal is the sum of several components, its FFT is the sum of their individual FFTs. This means we can decompose a signal into frequency bands, process each band independently, and sum the results to recover the original.
def bandpass(F, freqs, f_low, f_high):
"""Keep only frequencies in [f_low, f_high)."""
mask = (np.abs(freqs) >= f_low) & (np.abs(freqs) < f_high)
F_band = np.zeros_like(F)
F_band[mask] = F[mask]
return np.real(np.fft.ifft(F_band))
# Decompose into three bands
low = bandpass(F, freqs, 0, 15) # catches the 5 Hz component
mid = bandpass(F, freqs, 15, 35) # catches the 23 Hz component
high = bandpass(F, freqs, 35, 70) # catches the 50 Hz component
# Verify: sum of bands = original (to machine precision)
error = np.max(np.abs(signal - (low + mid + high)))
# error ≈ 3.3e-14

The low band isolates the slow 5 Hz wave, the mid band extracts the 23 Hz oscillation, and the high band captures the fast 50 Hz component. Summing them recovers the original to machine precision (). This is the same linearity property the original R code exploits: "summed up, the sub-signals make up the original signal since FFT is a linear operation."
Going Deeper: Filtering Noise
Gaussian Filter in the Frequency Domain
Suppose a radar transmits a pulse, , but the received signal is corrupted by white noise. The noise affects all frequencies equally, so the time-domain signal looks like random static. Can we recover the original pulse?
The Gaussian filter multiplies the frequency-domain signal by a bell curve centred at the known signal frequency :
Frequencies near pass through; distant frequencies (mostly noise) are suppressed.
def gaussian_filter(F, k, k0, bandwidth):
return F * np.exp(-bandwidth * (k - k0)**2)
# Filter the noisy signal (k0=0 is where sech(t) lives)
F_filtered = gaussian_filter(F_noisy, k, k0=0, bandwidth=0.2)
recovered = np.fft.ifft(F_filtered)

The filter succeeds because we know where in the frequency domain the signal lives. If we centred the filter at instead (where there is no signal), the filter would find only noise. This is the fundamental trade-off: a frequency-domain filter requires prior knowledge about which frequencies matter.
Signal Averaging: When You Have Repetitions
What if you do not know the signal frequency, but you can observe the signal multiple times? White noise has zero mean, so averaging multiple noisy observations causes the noise to cancel while the signal accumulates:

After 100 repetitions, the pulse emerges cleanly from the noise. The noise standard deviation decreases as , so 100 averages reduce it tenfold. This works because the signal is stationary: it stays at the same position in every measurement. For non-stationary signals (a moving target, a chirp, a time series with evolving frequency content), simple averaging fails. That is where the Gabor transform comes in.
The Gabor Transform: Time Meets Frequency
The standard FFT tells you which frequencies are present, but not when they occur. For a non-stationary signal whose frequency content changes over time, we need both. The Gabor transform (also called the Short-Time Fourier Transform) slides a Gaussian window across the signal and takes the FFT at each position:
The window localises the signal around time . At each position, the FFT reveals the local frequency content.
Watch the Gabor window slide across a complex non-stationary signal. At each position, the frequency spectrum changes:

The resulting spectrogram shows how frequency content evolves over time:
# Gabor spectrogram
spectrogram = np.zeros((n//2, len(window_centers)))
for j, tau in enumerate(window_centers):
window = np.exp(-bandwidth * (t - tau)**2)
windowed = signal * window
F_windowed = np.fft.fft(windowed)
spectrogram[:, j] = np.abs(F_windowed[:n//2])

The spectrogram reveals structure that is invisible in both the time domain (too complex) and the standard frequency domain (loses all temporal information). The chirp component appears as a V-shaped ridge: its instantaneous frequency decreases from ~6 Hz at down to zero at , then increases again. The two stationary sinusoidal components ( at ~0.3 Hz and at ~0.8 Hz) appear as horizontal bands.
There is a fundamental limit to this approach. A narrow window gives good time resolution but poor frequency resolution; a wide window gives the opposite. This is the Heisenberg uncertainty principle for time-frequency analysis: you cannot simultaneously know exactly when and at what frequency a signal component occurs. The Gabor transform with a Gaussian window achieves the theoretical optimum for this trade-off.
Forecasting with FFT + Neural Networks
The original R code's most ambitious idea combines everything above into a forecasting pipeline:
- Decompose the signal into frequency bands via band-pass filtering
- Train one neural network per band (each band is simpler than the original)
- Forecast each band auto-regressively (predict one step, feed back, repeat)
- Sum the forecasts (exploiting linearity)
from sklearn.neural_network import MLPRegressor
# Train one MLP per frequency band
models = []
for sub in sub_signals:
X_train, y_train = make_sliding_windows(sub, window=4)
mlp = MLPRegressor(hidden_layer_sizes=(4,), max_iter=3000,
activation='tanh', solver='lbfgs')
mlp.fit(X_train, y_train)
models.append(mlp)
# Forecast: predict one step, feed back, repeat
for step in range(n_forecast):
pred = model.predict(current_window)
current_window = np.roll(current_window, 1)
current_window[0, 0] = pred

The in-sample fits are excellent (R^2 > 0.999 for all bands). But the auto-regressive forecasts diverge because each prediction error feeds into the next step, compounding over time:

The R code forecast only 14 steps ahead and used a window of 4 observations, matching the signal's periodicity. The key insight is that this approach works best for short-horizon forecasts of highly periodic signals. The more periodic the signal (fewer dominant frequencies, higher SNR), the better the per-band MLPs can extrapolate. For financial time series, where the signal-to-noise ratio is low and non-stationarity is the norm, this approach faces the same challenges as any frequency-domain method: the frequencies you discover in historical data may not persist into the future.
Where This Comes From
The Fourier transform traces back to Joseph Fourier's 1807 memoir on heat conduction, where he showed that any periodic function can be represented as a sum of sines and cosines. The discrete version became computationally practical with the Cooley-Tukey algorithm in 1965, which reduced the complexity from to .
"An arbitrary function, continuous or with discontinuities, defined in a finite interval by an arbitrarily capricious graph can always be expressed as a sum of sinusoids."
Fourier, J. (1807). Mémoire sur la propagation de la chaleur.
The Cooley-Tukey FFT is the algorithm that numpy.fft.fft actually runs. It works by recursively splitting the DFT of size into two DFTs of size , exploiting the symmetry of the complex exponentials. This is why FFT performance is best when is a power of 2.
The Gabor transform was introduced by Dennis Gabor in his 1946 paper "Theory of communication," where he proposed using Gaussian-windowed sinusoids as the elementary signals for communication. He showed that these "logons" (Gaussian-enveloped sinusoids) achieve the minimum time-frequency uncertainty, analogous to the Heisenberg uncertainty principle in quantum mechanics. Gabor received the 1971 Nobel Prize in Physics for the invention of holography, a related application of wave interference.
The idea of training separate neural networks per frequency band is not from any single paper but reflects a practical engineering insight: simpler signals are easier to learn. The original R code (2015) applied this to order volume forecasting, using R's nnet package (single-layer MLPs) on band-pass filtered components of weekly business data. The linearity of the FFT guarantees that summing the per-band predictions exactly reconstructs the full signal prediction.
Further Reading
- The FFT algorithm: Cooley, J.W. & Tukey, J.W. (1965). An algorithm for the machine calculation of complex Fourier series. Mathematics of Computation, 19(90), 297-301.
- Gabor transform: Gabor, D. (1946). Theory of communication. Journal of the IEE, 93(26), 429-457.
- Signal processing textbook: Oppenheim, A.V. & Willsky, A.S. (1997). Signals and Systems (2nd ed.). Prentice Hall.
- Next algorithm to learn: For detecting when a time series changes regime (rather than decomposing its frequencies), see our post on Changepoint Detection.
Try It Yourself
- Add noise to the 3-component signal. Add Gaussian noise and see how the FFT peaks broaden. At what noise level do the peaks become undetectable?
- Build a spectrogram for audio. Load a
.wavfile and compute its Gabor spectrogram. Can you identify the vowels in a spoken word from their frequency signatures? - Try a financial time series. Apply band-pass decomposition to SPY daily returns. Do any frequency bands have predictive power, or does the efficient market hypothesis hold?
- Compare window functions. Replace the Gaussian window in the Gabor transform with a rectangular window and a Hann window. How does the spectrogram change?
Related Posts
- Changepoint Detection: Finding Regime Shifts in Financial Data: Changepoint methods detect when a time series switches regime; FFT band-pass filtering assumes the frequency structure is stable.
- Cointegration Test for Pairs Trading in Python: Engle-Granger and Johansen: Another frequency-domain perspective: cointegrated series share a common stochastic trend, which band-pass filtering can isolate.
- Hidden Markov Models: When Clusters Have Memory: HMMs model regime-switching dynamics that the Gabor spectrogram can visualise but not capture parametrically.
- Candlestick Pattern Discovery with K-Means: Our candlestick post concluded that past price patterns do not predict future returns, consistent with what the FFT forecasting approach finds for noisy financial data.
- Backpropagation: Neural Nets from First Principles: The per-band MLPs use backpropagation to learn the mapping from sliding windows to predictions.
- Modern Portfolio Theory from Scratch: The Efficient Frontier: Another quantitative tool for decomposing and forecasting asset returns.
- Hamiltonian Neural Networks: When the signal is a physical system, learning its Hamiltonian captures dynamics that a fixed frequency basis assumes are stationary.
- Time Series as Dynamical Systems: Delay Embedding: The state-space counterpart to the frequency view: reconstruct the attractor from one series instead of decomposing it into sinusoids.








