Second-Order Differentiation

Second-Order Differentiation#

The second difference detrending calculates the difference of differences, which is a common method for detrending time series. It works by computing the difference between consecutive first differences, effectively removing both constant offsets and linear trends from the data. Just as acceleration represents the change in velocity, the second difference captures the change in the rate of change of the time series.

from delaynet.detrending_methods import second_difference

# Apply second difference detrending
detrended_ts = second_difference(time_series)

Note that the length of the time series is reduced by 2 after applying this detrending.

Hide code cell source

import delaynet as dn
from numpy.random import default_rng
import matplotlib.pyplot as plt

# Generate fMRI data for a single node
fmri_data = dn.preparation.data_generator.gen_fmri(
    ts_len=1000,               # Length of time series
    downsampling_factor=2,     # Downsampling factor
    time_resolution=0.2,       # Time resolution
    coupling_strength=2.0,     # Coupling strength
    noise_initial_sd=1.0,      # Standard deviation of initial noise
    noise_final_sd=0.1,        # Standard deviation of final noise
    rng=default_rng(25968)     # Random number generator seed
)[:, 0]

plt.figure(figsize=(10, 4), dpi=300)
plt.plot(fmri_data, label='Original FMRI Data')
plt.title('Original and Second Difference Detrended FMRI Data')
plt.plot(dn.detrending_methods.second_difference(fmri_data), label='Second Difference Detrended Data')
plt.legend()
plt.grid()
plt.show()
../../../_images/26cf90a44bf5b3455cdc831eb41425d179e7b205d6ee8e9df92f85a52d153f67.png