ENG_PERIODOGRAM

The periodogram is an estimate of the spectral density of a signal. It is calculated as the squared magnitude of the discrete Fourier transform (DFT) of the signal, normalized by the sampling frequency.

P(f) = \frac{\Delta t}{N} \left| \sum_{n=0}^{N-1} x_n e^{-i 2 \pi f n \Delta t} \right|^2

where x_n are the signal samples and N is the number of samples.

Excel Usage

=ENG_PERIODOGRAM(x, fs, window, nfft, spectral_detrend, return_onesided, spectral_scaling)
  • x (list[list], required): Time series of measurement values (Excel range).
  • fs (float, optional, default: 1): Sampling frequency of the x time series.
  • window (str, optional, default: “boxcar”): Desired window to use.
  • nfft (int, optional, default: null): Length of the FFT used.
  • spectral_detrend (str, optional, default: “constant”): Specifies how to detrend each segment.
  • return_onesided (bool, optional, default: true): If True, return a one-sided spectrum for real data.
  • spectral_scaling (str, optional, default: “density”): Selects between ‘density’ and ‘spectrum’.

Returns (list[list]): A 2D array where the first row is frequencies and the second row is the power spectral density.

Example 1: Basic PSD

Inputs:

x fs
1 2 3 4 5 4 3 2 1 1

Excel formula:

=ENG_PERIODOGRAM({1,2,3,4,5,4,3,2,1}, 1)

Expected output:

Result
0 0.111111 0.222222 0.333333 0.444444
8.76512e-32 15.2752 0.0178125 0.222222 0.0403322
Example 2: Periodogram with linear detrending

Inputs:

x spectral_detrend
1 3 5 7 9 11 13 15 linear

Excel formula:

=ENG_PERIODOGRAM({1,3,5,7,9,11,13,15}, "linear")

Expected output:

Result
0 0.125 0.25 0.375 0.5
0 0 0 0 0
Example 3: Periodogram with spectrum scaling

Inputs:

x spectral_scaling nfft
0 1 0 -1 0 1 0 -1 spectrum 8

Excel formula:

=ENG_PERIODOGRAM({0,1,0,-1,0,1,0,-1}, "spectrum", 8)

Expected output:

Result
0 0.125 0.25 0.375 0.5
0 0 0.5 0 0
Example 4: Periodogram with scalar-compatible input

Inputs:

x
5

Excel formula:

=ENG_PERIODOGRAM(5)

Expected output:

Result
0
0

Python Code

Show Code
import numpy as np
from scipy.signal import periodogram as scipy_periodogram

def eng_periodogram(x, fs=1, window='boxcar', nfft=None, spectral_detrend='constant', return_onesided=True, spectral_scaling='density'):
    """
    Estimate power spectral density using a periodogram.

    See: https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.periodogram.html

    This example function is provided as-is without any representation of accuracy.

    Args:
        x (list[list]): Time series of measurement values (Excel range).
        fs (float, optional): Sampling frequency of the x time series. Default is 1.
        window (str, optional): Desired window to use. Default is 'boxcar'.
        nfft (int, optional): Length of the FFT used. Default is None.
        spectral_detrend (str, optional): Specifies how to detrend each segment. Valid options: Constant, Linear, None. Default is 'constant'.
        return_onesided (bool, optional): If True, return a one-sided spectrum for real data. Default is True.
        spectral_scaling (str, optional): Selects between 'density' and 'spectrum'. Valid options: Density, Spectrum. Default is 'density'.

    Returns:
        list[list]: A 2D array where the first row is frequencies and the second row is the power spectral density.
    """
    try:
        def to_1d(v):
            if isinstance(v, list):
                if all(isinstance(row, list) for row in v):
                    return np.array([float(x) for row in v for x in row], dtype=float)
                return np.array([float(x) for x in v], dtype=float)
            return np.array([float(v)], dtype=float)

        x_arr = to_1d(x)

        detrend = spectral_detrend
        if detrend == "False":
            detrend = False

        f, pxx = scipy_periodogram(
            x_arr, 
            fs=float(fs), 
            window=window, 
            nfft=int(nfft) if nfft is not None else None, 
            detrend=detrend, 
            return_onesided=bool(return_onesided), 
              scaling=spectral_scaling
        )

        return [f.tolist(), pxx.tolist()]
    except Exception as e:
        return f"Error: {str(e)}"

Online Calculator

Time series of measurement values (Excel range).
Sampling frequency of the x time series.
Desired window to use.
Length of the FFT used.
Specifies how to detrend each segment.
If True, return a one-sided spectrum for real data.
Selects between 'density' and 'spectrum'.