[파이썬] scipy 스펙트럼 추정

In the field of signal processing and spectral analysis, Scipy provides a powerful library for estimating spectra. Spectral estimation is commonly used to analyze the frequency content of a signal or time series data. In this blog post, we will explore the various techniques available in Scipy for spectrum estimation.

1. Power Spectral Density (PSD) Estimation

The Power Spectral Density (PSD) is a measure of the power distribution across different frequencies in a signal. Scipy provides the welch function from the scipy.signal module, which estimates the PSD using the Welch’s method. Here’s an example code for estimating the PSD:

import numpy as np
from scipy import signal
import matplotlib.pyplot as plt

# Generate a test signal
fs = 1000  # Sampling frequency (Hz)
t = np.arange(0, 1, 1/fs)  # Time vector
x = np.sin(2*np.pi*50*t) + np.sin(2*np.pi*120*t)  # Test signal

# Estimate the PSD using Welch's method
f, Pxx = signal.welch(x, fs)

# Plot the PSD
plt.figure()
plt.plot(f, Pxx)
plt.xlabel('Frequency (Hz)')
plt.ylabel('Power Spectral Density')
plt.title('PSD Estimation using Welch\'s method')
plt.show()

2. Autoregressive (AR) Spectral Estimation

The Autoregressive (AR) method is another popular technique for spectral estimation. It models the signal as a linear combination of its past values and the estimation is based on finding the coefficients of an autoregressive model. Scipy provides the arburg function from the scipy.signal module for autoregressive spectral estimation. Here’s an example code:

import numpy as np
from scipy import signal
import matplotlib.pyplot as plt

# Generate a test signal
fs = 1000  # Sampling frequency (Hz)
t = np.arange(0, 1, 1/fs)  # Time vector
x = np.sin(2*np.pi*50*t) + np.sin(2*np.pi*120*t)  # Test signal

# Estimate the autoregressive spectrum
order = 32  # AR model order
freq, response = signal.arburg(x, order)

# Plot the spectrum
plt.figure()
plt.plot(freq, 10 * np.log10(response))
plt.xlabel('Frequency (Hz)')
plt.ylabel('Power Spectral Density (dB/Hz)')
plt.title('Autoregressive Spectrum Estimation')
plt.show()

Conclusion

Scipy provides a variety of methods for spectral estimation, including Welch’s method for Power Spectral Density (PSD) estimation and the Autoregressive (AR) method. These techniques are essential for analyzing the frequency content of signals and time series data. By utilizing the power of Scipy, Python developers can easily perform accurate and efficient spectral estimation for their analysis tasks.

Remember to import the necessary modules and libraries, such as numpy, scipy, and matplotlib, to unleash the full potential of spectral estimation in Scipy. Happy coding!