如何从Lomb Scargle周期图中提取相位?基于gatspy实现
Extract Phase from LombScargleFast Periodogram in gatspy
Absolutely! You can pull phase values for each frequency from your LombScargleFast model—while it’s not as direct as grabbing phases from a discrete Fourier transform (DFT), gatspy provides tools to get this information using the fitted model parameters.
Here’s a step-by-step implementation with your existing code:
First, after fitting your data, you can use the model_parameters() method of the LombScargleFast class to retrieve the amplitude, phase, and constant offset for any given frequency. This method directly gives you the phase term from the model's cosine-based fit (y(t) = A*cos(2πft + φ) + B, where φ is the phase we want).
import numpy as np from gatspy.periodic import LombScargleFast import matplotlib.pyplot as plt # Replace with your actual t, y data t = np.linspace(0, 10, 100) y = 2 * np.cos(2 * np.pi * 0.5 * t + np.pi/4) + np.random.normal(0, 0.1, len(t)) # Your existing periodogram code model = LombScargleFast().fit(t, y) periods, power = model.periodogram_auto() frequencies = 1 / periods # Extract phase for each frequency phases = [] for freq in frequencies: # Get amplitude, phase, and offset for the current frequency amplitude, phase, offset = model.model_parameters(frequency=freq) phases.append(phase) # Visualize both power and phase fig, (ax_power, ax_phase) = plt.subplots(2, 1, figsize=(10, 8)) ax_power.plot(frequencies, power) ax_power.set_xlabel("Frequency") ax_power.set_ylabel("Power") ax_power.set_title("Lomb-Scargle Periodogram") ax_phase.plot(frequencies, phases) ax_phase.set_xlabel("Frequency") ax_phase.set_ylabel("Phase (radians)") ax_phase.set_title("Phase vs. Frequency") plt.tight_layout() plt.show()
Key Notes:
- The phase returned is relative to a cosine function (gatspy's Lomb-Scargle implementation uses a cosine basis). If you need to align this with DFT's typical sine-based phase, you can adjust it using
φ_dft = φ_lomb + π/2(sincecos(x + φ) = sin(x + φ + π/2)). - This method works because
model_parameters()fits a single-frequency cosine model to your data at the specified frequency, which aligns with what the Lomb-Scargle periodogram optimizes for when calculating power.
内容的提问来源于stack exchange,提问作者Andreas Ioannou
相关产品推荐
相关产品推荐

