Python SciPy equivalent of Matlab's plomb

The Matlab function plomb computes the Lomb-Scargle periodogram. In SciPy the equivalent function is called lombscargle. However, if you want to translate a call to plomp in Matlab to the equivalent Python call, you will find that the function arguments are not the same.

First problem is that the argument order is different, which is easily solved.

Second problem is that the SciPy function requires you to supply the angular frequency vector, which is generated automatically by Matlab.

The following Python function lomb bridges the gap.

import numpy as np
from scipy.signal import lombscargle

def lomb(t, y):
    """Compute the Lomb-Scargle periodogram

        t : sample times
        y : measurement values

        f   : frequency vector
        pxx : power spectral density (PSD) estimate

    Inputs are assumed to be vectors of type numpy.ndarray.
    n = t.size
    ofac = 4  # Oversampling factor
    hifac = 1
    T = t[-1] - t[0]
    Ts = T / (n - 1)
    nout = np.round(0.5 * ofac * hifac * n) 
    f = np.arange(1, nout+1) / (n * Ts * ofac)
    f_ang = f * 2 * np.pi
    pxx = lombscargle(t, y, f_ang, precenter=True)
    pxx = pxx * 2 / n
    return f, pxx

With this function the Python call:

[f, pxx] = lomb(t, y)

provides the same output as the Matlab call:

[pxx, f] = plomb(y, t, 'power');