使用Python3的pytftb绘制Wigner-Ville分布及频率轴调整咨询
Got it, let's fix that frequency axis issue for your Wigner-Ville Distribution plot in pytftb!
First, a quick reminder: pytftb uses normalized frequency by default (usually ranging from -0.5 to 0.5) because it’s agnostic to your signal’s sampling rate. To convert this to absolute frequency (like Hz), you just need to multiply the normalized values by your signal’s sampling frequency (Fs)—you should have this value from when you acquired or generated your signal (e.g., 1000 Hz for a 1kHz sample rate).
Here are two reliable ways to adjust the plot:
1. Pass absolute frequencies directly to the plot method
If you check pytftb’s docs, the WignerVilleDistribution.plot() method accepts a freqs parameter that lets you override the default normalized frequencies. Here’s how to use it:
import matplotlib.pyplot as plt # Your existing code tfr = WignerVilleDistribution(prepack[0]) tfr.run() # Define your signal's sampling rate (replace with your actual Fs) Fs = 1000 # Example: 1000 Hz # Convert normalized frequencies to absolute frequencies normalized_freqs = tfr.freqs absolute_freqs = normalized_freqs * Fs # Plot with custom frequency axis tfr.plot(show_tf=True, freqs=absolute_freqs) plt.ylabel("Absolute Frequency (Hz)") plt.show()
2. Manually tweak the axis after plotting
If the freqs parameter doesn’t work for your pytftb version, you can grab the matplotlib axis object and update the ticks/labels directly:
import matplotlib.pyplot as plt # Your existing code tfr = WignerVilleDistribution(prepack[0]) tfr.run() # Create a figure and axis to modify fig, ax = plt.subplots() tfr.plot(show_tf=True, ax=ax) # Define your sampling rate Fs = 1000 # Get current normalized frequency ticks (assuming frequency is on the y-axis) normalized_ticks = ax.get_yticks() # Convert to absolute frequency absolute_ticks = normalized_ticks * Fs # Update the axis ticks and labels ax.set_yticks(normalized_ticks) ax.set_yticklabels([f"{tick:.2f}" for tick in absolute_ticks]) ax.set_ylabel("Absolute Frequency (Hz)") plt.show()
A quick note on interpreting the results: Wigner-Ville Distribution shows the signal’s instantaneous power distribution over time and frequency. For noisy signals, you might see cross-term artifacts (since WVD is a quadratic time-frequency distribution)—just keep that in mind when estimating the original signal’s amplitude.
内容的提问来源于stack exchange,提问作者Danf

