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

Har*_*ime 6 python spectrogram

我不知道怎么做这个,我得到了一个例子,谱图,但这是2D.

我这里有代码生成混合频率,我可以在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()
Run Code Online (Sandbox Code Playgroud)

输出文件

tom*_*m10 8

你所拥有的在技术上是正确的,但你只需要看一个有趣的频谱图信号.为此,您需要频率随时间变化.(要做到这一点,你需要很多振荡,因为它需要一些振荡来建立一个频率,然后你需要其中许多振荡频率随着时间的变化而变得有趣.)

下面我已经尽可能少地修改了你的代码,以获得一个有趣fscale的频率(只是随着时间推移频率).我发布所有代码以使其工作,但我只改变前四行中的三个.

在此输入图像描述

# 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()
Run Code Online (Sandbox Code Playgroud)

此外,请注意,只有频谱图才有用.如果你能看到一个好的波形或光谱,光谱图可能不会很有趣:1)如果波形是清晰的,你可能没有足够的数据和时间来定义频率,并且变化足以引起兴趣; 2)如果全光谱是清晰的,那么光谱图的频率可能没有足够的变化,因为光谱基本上只是光谱图中随时间变化的平均值.

如果您真的想要查看原始信号的频谱图,您只需要在y轴上放大以查看您期望的峰值(请注意,频谱图y轴为2.5e8,必须大于频谱中的峰值): 在此输入图像描述