从scipy频谱图中读取振幅

2024-05-19 02:53:06 发布

您现在位置:Python中文网/ 问答频道 /正文

在我看来,振幅的计算信号光谱图不正确(在我的情况下大约打八折)。有没有可能改进这个?考虑一下这个例子

import matplotlib.pyplot as plt
import numpy as np
from numpy import pi as π

f_s = 200              # Sampling rate = number of measurements per second in [Hz]
t   = np.arange(-10,10, 1 / f_s)
N = len(t)
T1  = np.tanh(t)/2  + 1.0 # Period in [s]
T2  = 0.125               # Period in [s]
f1  = 1 / T1            # Frequency in [Hz]
f2  = 1 / T2            # Frequency in [Hz] 

# Signal
x = 13*np.sin(2 * π * f1 * t) + 42*np.sin(2 * π * f2 * t)

# Spectrogram with good time window
Δt = 4 # window length in [s]
Nw = np.int(2**np.round(np.log2(Δt * f_s)))
print(f"Effective window length is {Nw/f_s:.1f}s")
f, t_, Sxx = signal.spectrogram(x, f_s, window='hanning', nperseg=Nw, noverlap = Nw - 100, detrend=False, scaling='spectrum')
Δf  =  f[1] - f[0]
Δt_ = t_[1] - t_[0]

fig, ax = plt.subplots(figsize = (15,5))
im = ax.pcolormesh(t_ + t[0] - Δt_, f - Δf/2, np.sqrt(Sxx), cmap = "inferno_r")
plt.colorbar(im)
ax.grid(True)
ax.set_ylabel("Frequency in [Hz]")
ax.set_xlabel("t")
ax.set_ylim(0,10)
plt.show()

很明显,这些线没有显示之前定义的振幅(即13和42)。你知道吗


Tags: inimportnumpyasnppltaxwindow

热门问题