PERIODOGRAM

This function computes a periodogram to estimate how signal power is distributed across frequencies. It returns a two-column array containing frequency and estimated spectral power.

The periodogram is based on the squared magnitude of the discrete Fourier transform (DFT):

P(f) \propto |X(f)|^2

where X(f) is the DFT of the input signal. Depending on scaling, the output represents either power spectral density or power spectrum.

Excel Usage

=PERIODOGRAM(data, fs, window, nfft, detrend, return_onesided, scaling)
  • data (list[list], required): 2D range of time-series samples.
  • fs (float, optional, default: 1): Sampling frequency in hertz (Hz).
  • window (str, optional, default: “boxcar”): Window name applied before FFT.
  • nfft (int, optional, default: null): FFT length; null uses input length.
  • detrend (str, optional, default: “constant”): Detrending method for the signal.
  • return_onesided (bool, optional, default: true): Return one-sided spectrum for real-valued input.
  • scaling (str, optional, default: “density”): Spectral scaling mode.

Returns (list[list]): 2D array with columns [frequency, power].

Example 1: Basic periodogram with defaults

Inputs:

data fs window nfft detrend return_onesided scaling
0 1 0 -1 0 1 0 -1 0 1 0 -1 0 1 0 -1 8 boxcar constant true density

Excel formula:

=PERIODOGRAM({0,1,0,-1,0,1,0,-1,0,1,0,-1,0,1,0,-1}, 8, "boxcar", , "constant", TRUE, "density")

Expected output:

Result
0 0
0.5 0
1 0
1.5 0
2 1
2.5 0
3 0
3.5 0
4 0
Example 2: Periodogram with linear detrending

Inputs:

data fs window nfft detrend return_onesided scaling
0 0.8 0.1 -0.9 -0.2 1.1 0.2 -1 -0.1 0.9 0 -1.1 0.1 1 -0.2 -0.8 8 hann 32 linear true density

Excel formula:

=PERIODOGRAM({0,0.8,0.1,-0.9,-0.2,1.1,0.2,-1,-0.1,0.9,0,-1.1,0.1,1,-0.2,-0.8}, 8, "hann", 32, "linear", TRUE, "density")

Expected output:

Result
0 0.000128004
0.25 0.00167162
0.5 0.000885883
0.75 0.000758796
1 0.00260373
1.25 0.00674368
1.5 0.118549
1.75 0.426249
2 0.671639
2.25 0.545337
2.5 0.220244
2.75 0.0313019
3 0.0000948799
3.25 0.00383545
3.5 0.00216765
3.75 0.000520404
4 0.00010619
Example 3: Periodogram returning power spectrum without detrending

Inputs:

data fs window nfft detrend return_onesided scaling
0 1 0 -1 0 1 0 -1 0 1 0 -1 0 1 0 -1 8 boxcar none true spectrum

Excel formula:

=PERIODOGRAM({0,1,0,-1,0,1,0,-1,0,1,0,-1,0,1,0,-1}, 8, "boxcar", , "none", TRUE, "spectrum")

Expected output:

Result
0 0
0.5 0
1 0
1.5 0
2 0.5
2.5 0
3 0
3.5 0
4 0
Example 4: Two-sided periodogram output

Inputs:

data fs window nfft detrend return_onesided scaling
0 1 0 -1 0 1 0 -1 0 1 0 -1 0 1 0 -1 8 boxcar constant false density

Excel formula:

=PERIODOGRAM({0,1,0,-1,0,1,0,-1,0,1,0,-1,0,1,0,-1}, 8, "boxcar", , "constant", FALSE, "density")

Expected output:

Result
0 0
0.5 0
1 0
1.5 0
2 0.5
2.5 0
3 0
3.5 0
-4 0
-3.5 0
-3 0
-2.5 0
-2 0.5
-1.5 0
-1 0
-0.5 0

Python Code

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

def periodogram(data, fs=1, window='boxcar', nfft=None, detrend='constant', return_onesided=True, scaling='density'):
    """
    Estimate the power spectral density of a time series 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:
        data (list[list]): 2D range of time-series samples.
        fs (float, optional): Sampling frequency in hertz (Hz). Default is 1.
        window (str, optional): Window name applied before FFT. Default is 'boxcar'.
        nfft (int, optional): FFT length; null uses input length. Default is None.
        detrend (str, optional): Detrending method for the signal. Valid options: Constant, Linear, None. Default is 'constant'.
        return_onesided (bool, optional): Return one-sided spectrum for real-valued input. Default is True.
        scaling (str, optional): Spectral scaling mode. Valid options: Density, Spectrum. Default is 'density'.

    Returns:
        list[list]: 2D array with columns [frequency, power].
    """
    try:
        def to2d(x):
            return [[x]] if not isinstance(x, list) else x

        data = to2d(data)

        if fs <= 0:
            return "Error: fs must be greater than 0"
        if detrend not in ("constant", "linear", "none"):
            return "Error: detrend must be 'constant', 'linear', or 'none'"
        if scaling not in ("density", "spectrum"):
            return "Error: scaling must be 'density' or 'spectrum'"
        if not isinstance(data, list) or not all(isinstance(row, list) for row in data):
            return "Error: data must be a 2D list"

        values = []
        for row in data:
            for item in row:
                try:
                    values.append(float(item))
                except (TypeError, ValueError):
                    continue

        if len(values) < 2:
            return "Error: data must contain at least two numeric values"

        nfft_arg = None if nfft is None or nfft <= 0 else nfft
        detrend_arg = False if detrend == "none" else detrend

        freqs, power = scipy_periodogram(
            np.asarray(values, dtype=float),
            fs=fs,
            window=window,
            nfft=nfft_arg,
            detrend=detrend_arg,
            return_onesided=return_onesided,
            scaling=scaling,
        )

        return [[float(freqs[i]), float(power[i])] for i in range(len(freqs))]
    except Exception as e:
        return f"Error: {str(e)}"

Online Calculator

2D range of time-series samples.
Sampling frequency in hertz (Hz).
Window name applied before FFT.
FFT length; null uses input length.
Detrending method for the signal.
Return one-sided spectrum for real-valued input.
Spectral scaling mode.