Spectrum Analysis Tools - contains tools to estimate Power Spectral Densities using methods based on Fourier transform, Parametric methods or eigenvalues analysis
—
Auto-regressive (AR) parameter estimation and power spectral density computation using various algorithms. These methods model the data as the output of an all-pole filter driven by white noise, providing high-resolution spectral estimates particularly effective for short data records.
Estimates AR parameters using the Yule-Walker equations based on the autocorrelation function. Provides a stable solution but may have lower resolution compared to other methods.
def aryule(X, order, norm='biased', allow_singularity=True):
"""
AR parameter estimation using Yule-Walker equations.
Parameters:
- X: array-like, input time series data
- order: int, AR model order
- norm: str, normalization type ('biased', 'unbiased')
- allow_singularity: bool, allow singular autocorrelation matrix
Returns:
tuple: (AR coefficients array, noise variance float, reflection coefficients array)
"""
class pyule(data, order, NFFT=None, sampling=1., scale_by_freq=False):
"""
PSD estimation using Yule-Walker method.
Parameters:
- data: array-like, input time series
- order: int, AR model order
- NFFT: int, FFT length for PSD computation
- sampling: float, sampling frequency
- scale_by_freq: bool, scale PSD by frequency
"""Maximum entropy method that estimates AR parameters by minimizing forward and backward prediction errors. Provides excellent frequency resolution and stability.
def arburg(X, order, criteria=None):
"""
AR parameter estimation using Burg algorithm.
Parameters:
- X: array-like, input time series data
- order: int, AR model order (or maximum order if criteria used)
- criteria: str, automatic order selection criterion
Returns:
tuple: (AR coefficients array, prediction error variance float, reflection coefficients array)
"""
class pburg(data, order, criteria=None, NFFT=None, sampling=1., scale_by_freq=False):
"""
PSD estimation using Burg method.
Parameters:
- data: array-like, input time series
- order: int, AR model order
- criteria: str, order selection criteria ('AIC', 'MDL', etc.)
- NFFT: int, FFT length for PSD computation
- sampling: float, sampling frequency
- scale_by_freq: bool, scale PSD by frequency
"""Estimates AR parameters using the covariance method, which minimizes the forward prediction error. Provides unbiased estimates but may be less stable than other methods.
def arcovar(x, order):
"""
AR parameter estimation using covariance method.
Parameters:
- x: array-like, input time series data
- order: int, AR model order
Returns:
tuple: (AR coefficients array, noise variance float, reflection coefficients array)
"""
def arcovar_marple(x, order):
"""
Marple's implementation of covariance method.
Parameters:
- x: array-like, input time series data
- order: int, AR model order (works for order <= 4)
Returns:
tuple: (AR coefficients array, noise variance float, reflection coefficients array)
"""
class pcovar(data, order, NFFT=None, sampling=1., scale_by_freq=False):
"""
PSD estimation using covariance method.
Parameters:
- data: array-like, input time series
- order: int, AR model order
- NFFT: int, FFT length for PSD computation
- sampling: float, sampling frequency
- scale_by_freq: bool, scale PSD by frequency
"""Combines forward and backward prediction errors to provide improved parameter estimates. Generally more stable than the standard covariance method.
def modcovar(x, order):
"""
AR parameter estimation using modified covariance method.
Parameters:
- x: array-like, input time series data
- order: int, AR model order
Returns:
tuple: (AR coefficients array, noise variance float, reflection coefficients array)
"""
def modcovar_marple(X, IP):
"""
Marple's implementation of modified covariance method.
Parameters:
- X: array-like, input time series data
- IP: int, AR model order
Returns:
tuple: (AR coefficients array, noise variance float, reflection coefficients array)
"""
class pmodcovar(data, order, NFFT=None, sampling=1., scale_by_freq=False):
"""
PSD estimation using modified covariance method.
Parameters:
- data: array-like, input time series
- order: int, AR model order
- NFFT: int, FFT length for PSD computation
- sampling: float, sampling frequency
- scale_by_freq: bool, scale PSD by frequency
"""import spectrum
import numpy as np
# Generate AR(2) test signal
N = 256
noise = 0.1 * np.random.randn(N)
# AR coefficients for two-pole system
ar_true = [1.0, -1.6180, 0.8607]
signal = spectrum.lfilter([1], ar_true, noise)
# Estimate AR parameters using different methods
ar_yule, var_yule, rc_yule = spectrum.aryule(signal, order=2)
ar_burg, var_burg, rc_burg = spectrum.arburg(signal, order=2)
ar_cov, var_cov, rc_cov = spectrum.arcovar(signal, order=2)
print(f"True AR coefficients: {ar_true[1:]}")
print(f"Yule-Walker: {ar_yule}")
print(f"Burg: {ar_burg}")
print(f"Covariance: {ar_cov}")import spectrum
import numpy as np
# Generate noisy sinusoidal signal
t = np.arange(256)
signal = np.sin(0.2*t) + np.sin(0.3*t) + 0.2*np.random.randn(256)
# PSD estimation with automatic order selection using AIC
p = spectrum.pburg(signal, order=20, criteria='AIC')
p.plot()
# Compare different methods
p_yule = spectrum.pyule(signal, order=15)
p_cov = spectrum.pcovar(signal, order=15)
p_mod = spectrum.pmodcovar(signal, order=15)
# Plot all methods for comparison
import matplotlib.pyplot as plt
f = np.linspace(0, 0.5, len(p.psd))
plt.figure(figsize=(10, 6))
plt.semilogy(f, p.psd, label='Burg')
plt.semilogy(f, p_yule.psd, label='Yule-Walker')
plt.semilogy(f, p_cov.psd, label='Covariance')
plt.semilogy(f, p_mod.psd, label='Modified Covariance')
plt.xlabel('Normalized Frequency')
plt.ylabel('PSD')
plt.legend()
plt.grid(True)
plt.show()Install with Tessl CLI
npx tessl i tessl/pypi-spectrum