Mathematical helper functions

Helper functions for mathematical and physical operations.

spectrumkit.lib.math.FT(t: ndarray, x: ndarray, indvar: bool = True) ndarray | tuple[ndarray, ndarray][source]

Discrete Fourier transformation using fast Fourier transformation (FFT).

Parameters:
  • t (numpy.ndarray) – Time values of the time series.

  • x (numpy.ndarray) – Function values corresponding to the time series.

  • indvar (bool) – If True, returns the FFT and frequency values. If False, returns only the FFT.

Returns:

If indvar is True, returns a tuple (k, xf2) where:
  • k (numpy.ndarray): Frequency values corresponding to the FFT.

  • xf2 (numpy.ndarray): FFT of the input function, scaled by the time range and phase shifted.

If indvar is False, returns the FFT (xf2) directly as a numpy.ndarray.

Return type:

tuple(numpy.ndarray, numpy.ndarray) or numpy.ndarray

Raises:

RuntimeError – If the time series is not equally spaced.

Example

>>> t = np.linspace(0, np.pi, 4)
>>> x = np.sin(t)
>>> k, xf2 = FT(t, x)
>>> k
array([-3. , -1.5,  0. ,  1.5])
>>> np.round(xf2, 2)
array([ 0.  +0.j  , -0.68+0.68j,  1.36+0.j  , -0.68-0.68j])

See also

iFT()

For the inverse fourier transform.

spectrumkit.lib.math.correlation_function(timeseries, dt)[source]

Take a timeseries and calculate the autocorrelation function.

The autocorrelation function is defined by \(C(t) = \langle f(0) f(t) \rangle\) as defined in the SI of Carlson20a.

This function returns \(C(t)\).

spectrumkit.lib.math.iFT(k: ndarray, xf: ndarray, indvar: bool = True) ndarray | tuple[ndarray, ndarray][source]

Inverse Fourier transformation using fast Fourier transformation (FFT).

Takes the frequency series and the function as arguments. By default, returns the iFT and the time series. Setting indvar=False means the function returns only the iFT.

Parameters:
  • k (numpy.ndarray) – The frequency series.

  • xf (numpy.ndarray) – The function series in the frequency domain.

  • indvar (bool) – If True, return both the iFT and the time series. If False, return only the iFT.

Returns:

If indvar is True, returns a tuple containing the time series and the iFT. If indvar is False, returns only the iFT.

Return type:

tuple(numpy.ndarray, numpy.ndarray) or numpy.ndarray

Raises:

RuntimeError – If the time series is not equally spaced.

See also

FT()

For the Fourier transform.

spectrumkit.lib.math.kramers_kronig(nu, f)[source]

Implementation of Kramers Kronig using the Hilbert transform.

spectrumkit.lib.math.powerspectrum_from_timeseries(t, timeseries)[source]

Take a timeseries and calculate the power spectrum.

The power spectrum is defined by \(\int_{\infty}^{\infty} dt e^{2 \pi i \nu t} \langle f(0) f(t) \rangle = \frac{1}{L_t} | f(\nu) |^2\) as defined in the SI of Carlson20a.

This function returns \(\frac{1}{L_t} | f(\nu) |^2\)