JuliaDynamics / TransitionsInTimeseries.jl

Transition Indicators / Early Warning Signals / Regime Shifts / Change Point Detection
MIT License
18 stars 5 forks source link

Improve the performance of `PrecomputedLowfreqPowerSpectrum` #20

Open JanJereczek opened 1 year ago

JanJereczek commented 1 year ago

Indicator summary

Compute ratio between low-frequency power spectrum and total power spectrum. What is "low-frequency"? Defined by user but standard is something like 10 lowest percent of the total power spectrum. Performs well wherever CSD is expected (conversely bad wherever CSD might be "hidden", e.g. by high signal-to-noise ratio).

Reference

Bury et al. (2020), Detecting and distinguishing tipping points using spectral early warning signals.

Codebase

Rely on FFTW for FFT

Implementation plan

struct LowfreqPowerSpectrum <: Function
    plan::Any
    i_lofreq::Int
end

function LowfreqPowerSpectrum(x::AbstractVector, width::Int; q_lofreq = 0.1)
    p = plan_rfft(x[1:width+1])
    fft_x = p*x
    i_lofreq = Int(round(length(fft_x) * q_lofreq))
    return LowfreqPowerSpectrum(p, i_lofreq)
end

function (lfps::LowfreqPowerSpectrum)(x::AbstractVector)
    ps = abs.(lfps.plan * x)
    return sum(ps[1:lfps.i_lofreq])
end
JanJereczek commented 6 months ago

Is now part of the code but needs to be made more performant. In particular, the FFTs have to be performed in place.