如何在python中生成1D信号的谱图?

2024-09-28 05:24:58 发布

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

我不知道怎么做,我得到了一个例子,spectrogram e.g.但这是在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()

output file


Tags: thefftlennpdtpirangecos
3条回答

为了得到你想要的:

1)高频采样1d波形(至少为其最高频率分量频率的5倍)

2)使用样本块(2的幂,如102416384等)计算FFT

3)对于每个频谱,绘制一条像素垂直线,其颜色表示每个频率的振幅。

4)对每一块样品重复步骤2和3。

在你的情况下,情节有一个完整的彩虹颜色,不应该只有两个非常不同的频率出现。你的光谱图在峰值周围有相当宽的波段,但这可能是由于低采样率和平滑绘制。

我刚刚开始使用Python3.6 感谢您提供的光谱图样本代码!

然而,在Python3.6中,我有点费劲地使这个示例光谱图代码工作(函数调用和浮点除法 我已经编辑了代码,所以现在它可以在python 3.6上为我的python新手朋友工作。

享受

'''
Original Script for Python 2.7
https://stackoverflow.com/questions/19052324/how-do-i-generate-a-spectrogram-of-a-1d-signal-in-python
Modified in August 2017 for Python 3.6
Python 2.7 two integers / Division generate Integer
Python 3.6 two integers / Division generate Float
Python 3.6 two integers // Division generate integer
'''


import numpy as np
from scipy import fftpack
import matplotlib.pyplot as plt


dt = 40e-9
t = np.arange(0, 1000e-6, dt)
fscale = t/max(t)
y = np.cos(2 * np.pi * 1e6 * t*fscale) + (np.cos(2 * np.pi * 2e6 *t*fscale) * np.cos(2 * np.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 = fftpack.fft(yy) / n  # fft computing and normalization
Y = Y[range(n // 2)] / max(Y[range(n // 2)])

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

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

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

你所得到的技术上是正确的,但是你只需要用一个有趣的光谱图来观察一个信号。为此,您需要频率随时间变化。(要做到这一点,你需要很多振荡,因为建立一个频率需要一些振荡,然后你需要其中的许多振荡才能使频率以有趣的方式随时间变化。)

下面我将尽可能少地修改您的代码,以获得一个可以做一些有趣事情的频率(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

相关问题 更多 >

    热门问题