gpt4 book ai didi

python - 如何在 python 中生成一维信号的频谱图?

转载 作者:太空狗 更新时间:2023-10-29 22:19:28 24 4
gpt4 key购买 nike

我不确定该怎么做,有人给我举了一个例子,spectrogram e.g.但这是二维的。

我这里有生成混合频率的代码,我可以在 fft 中挑选出这些,我怎样才能在频谱图中看到这些?我很欣赏我示例中的频率不会随时间变化;那么这是否意味着我会在频谱图中看到一条直线?

我的代码和输出图像:

# create a wave with 1Mhz and 0.5Mhz frequencies
dt = 2e-9
t = np.arange(0, 10e-6, dt)
y = np.cos(2 * pi * 1e6 * t) + (np.cos(2 * pi * 2e6 *t) * np.cos(2 * pi * 2e6 * t))
y *= np.hanning(len(y))
yy = np.concatenate((y, ([0] * 10 * len(y))))

# FFT of this
Fs = 1 / dt # sampling rate, Fs = 500MHz = 1/2ns
n = len(yy) # length of the signal
k = np.arange(n)
T = n / Fs
frq = k / T # two sides frequency range
frq = frq[range(n / 2)] # one side frequency range
Y = fft(yy) / n # fft computing and normalization
Y = Y[range(n / 2)] / max(Y[range(n / 2)])

# plotting the data
subplot(3, 1, 1)
plot(t * 1e3, y, 'r')
xlabel('Time (micro seconds)')
ylabel('Amplitude')
grid()

# plotting the spectrum
subplot(3, 1, 2)
plot(frq[0:600], abs(Y[0:600]), 'k')
xlabel('Freq (Hz)')
ylabel('|Y(freq)|')
grid()

# plotting the specgram
subplot(3, 1, 3)
Pxx, freqs, bins, im = specgram(y, NFFT=512, Fs=Fs, noverlap=10)
show()

output file

最佳答案

您所拥有的在技术上是正确的,但您只需要查看具有有趣频谱图的信号。为此,您需要频率随时间变化。 (要做到这一点,您需要多次振荡,因为需要几次振荡才能建立一个频率,然后您需要多次振荡才能使频率以一种有趣的方式随时间变化。)

下面我已经尽可能少地修改了您的代码,以获得可以做一些有趣的事情的频率(fscale 只是随着时间的推移逐渐增加频率)。我发布了所有代码以使其正常工作,但我只更改了前四行中的三行。

enter image description here

# create a wave with 1Mhz and 0.5Mhz frequencies
dt = 40e-9
t = np.arange(0, 1000e-6, dt)
fscale = t/max(t)
y = np.cos(2 * pi * 1e6 * t*fscale) + (np.cos(2 * pi * 2e6 *t*fscale) * np.cos(2 * pi * 2e6 * t*fscale))
y *= np.hanning(len(y))
yy = np.concatenate((y, ([0] * 10 * len(y))))

# FFT of this
Fs = 1 / dt # sampling rate, Fs = 500MHz = 1/2ns
n = len(yy) # length of the signal
k = np.arange(n)
T = n / Fs
frq = k / T # two sides frequency range
frq = frq[range(n / 2)] # one side frequency range
Y = fft(yy) / n # fft computing and normalization
Y = Y[range(n / 2)] / max(Y[range(n / 2)])

# plotting the data
subplot(3, 1, 1)
plot(t * 1e3, y, 'r')
xlabel('Time (micro seconds)')
ylabel('Amplitude')
grid()

# plotting the spectrum
subplot(3, 1, 2)
plot(frq[0:600], abs(Y[0:600]), 'k')
xlabel('Freq (Hz)')
ylabel('|Y(freq)|')
grid()

# plotting the specgram
subplot(3, 1, 3)
Pxx, freqs, bins, im = specgram(y, NFFT=512, Fs=Fs, noverlap=10)
show()

另外,请注意这里只有频谱图有用。如果您能看到良好的波形或频谱,则频谱图可能不会很有趣:1) 如果波形清晰,您可能没有足够的数据和时间来确定频率的定义和变化足以使它变得有趣; 2) 如果完整的光谱清晰,您可能没有足够的频率变化用于频谱图,因为频谱基本上只是您在频谱图中看到的随时间变化的平均值。

如果您真的想查看原始信号的频谱图,只需放大 y 轴即可查看您期望的峰值(请注意,频谱图 y 轴为 2.5e8,必须大于您的光谱): enter image description here

关于python - 如何在 python 中生成一维信号的频谱图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19052324/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com