Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Online ml River Misc SDFT

From Leeroopedia


Knowledge Sources
Domains Online_Learning, Signal_Processing, Time_Series
Last Updated 2026-02-08 16:00 GMT

Overview

Sliding Discrete Fourier Transform for online frequency analysis with fixed-size windows.

Description

Implements SDFT which efficiently updates Fourier coefficients as new values arrive without recomputing the entire FFT. After filling a window, it updates coefficients in O(k) time where k is the number of frequency bins, much faster than O(n log n) for full FFT. Useful for online spectral analysis and frequency monitoring.

Usage

Use for real-time frequency analysis, online feature extraction from periodic signals, or monitoring frequency components in streaming time series. Essential for audio processing, vibration analysis, or any application requiring continuous spectral monitoring.

Code Reference

Source Location

Signature

class SDFT(base.Base):
    def __init__(self, window_size: int):
        ...

    def update(self, x: float) -> None:
        ...

    @property
    def window_size(self) -> int:
        ...

Import

from river import misc

Usage Examples

import numpy as np
from river import misc

X = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
window_size = 5
sdft = misc.SDFT(window_size)

for i, x in enumerate(X):
    sdft.update(x)

    # After window fills, coefficients match numpy FFT
    if i + 1 >= window_size:
        expected = np.fft.fft(X[i+1 - window_size:i+1])
        actual = sdft.coefficients

        print(f"Window: {X[i+1-window_size:i+1]}")
        print(f"Coefficients match: {np.allclose(actual, expected)}")

# Example with periodic signal
t = np.linspace(0, 1, 100)
signal = np.sin(2 * np.pi * 5 * t) + 0.5 * np.sin(2 * np.pi * 10 * t)

sdft2 = misc.SDFT(window_size=20)
for val in signal:
    sdft2.update(val)

# Access Fourier coefficients
print(f"\nNumber of coefficients: {len(sdft2.coefficients)}")
print(f"Coefficient magnitudes: {[abs(c) for c in sdft2.coefficients][:5]}")

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment