Python 中的 Matplotlib.pyplot.specgram()来绘制频谱图
本教程介绍了我们如何使用 matplotlib.pyplot.specgram()
和 scipy.signal.spectrogram()
方法在 Python 中绘制频谱图。
我们可以通过频谱图得到信号强度的详细信息。频谱图中某一点的颜色越深,说明该点的信号越强。
使用 matplotlib.pyplot.specgram()
方法绘制频谱图
matplotlib.pyplot.specgram(x,
NFFT=None,
Fs=None,
Fc=None,
detrend=None,
window=None,
noverlap=None,
cmap=None,
xextent=None,
pad_to=None,
sides=None,
scale_by_freq=None,
mode=None,
scale=None,
vmin=None,
vmax=None, *,
data=None,
**kwargs)
示例: 使用 matplotlib.pyplot.specgram()
方法绘制频谱图
import math
import numpy as np
import matplotlib.pyplot as plt
dt = 0.0001
w = 2
t = np.linspace(0, 5, math.ceil(5 / dt))
A = 20 * (np.sin(3 * np.pi * t))
plt.specgram(A, Fs=1)
plt.title("Spectrogram Using matplotlib.pyplot.specgram() method")
plt.show()
它使用 matplotlib.pyplot.specgram()
方法为函数 A=20sin(3*np.pi*t)
创建一个频谱图。该方法中的参数 fs
代表采样频率。
使用 scipy.signal.spectrogram()
方法绘制频谱图
import math
import numpy as np
import matplotlib.pyplot as plt
from scipy import signal
dt = 0.0001
w = 2
t = np.linspace(0, 5, math.ceil(5 / dt))
A = 2 * (np.sin(1 * np.pi * 300 * t))
f, t, Sxx = signal.spectrogram(A, fs=1, nfft=514)
plt.pcolormesh(t, f, Sxx)
plt.ylabel("Frequency")
plt.xlabel("Time")
plt.title("Spectrogram Using scipy.signal.spectrogram() method")
plt.show()
它使用 scipy.signal.spectrogram()
方法为函数 A=2sin(300*np.pi*t)
创建一个频谱图。该方法中的参数 fs
代表采样频率,ntft
代表所用 FFT
的长度。
该方法返回三个值 f
、t
和 Sxx
。f
代表采样频率数组,t
代表采样时间数组,Sxx
代表 A
的频谱图。
这种方法并不能生成输入信号的频谱图。我们可以使用 matplotlib.pyplot.colormesh()
来生成图形。
相关文章
Pandas DataFrame DataFrame.shift() 函数
发布时间:2024/04/24 浏览次数:133 分类:Python
-
DataFrame.shift() 函数是将 DataFrame 的索引按指定的周期数进行移位。
Python pandas.pivot_table() 函数
发布时间:2024/04/24 浏览次数:82 分类:Python
-
Python Pandas pivot_table()函数通过对数据进行汇总,避免了数据的重复。
Pandas read_csv()函数
发布时间:2024/04/24 浏览次数:254 分类:Python
-
Pandas read_csv()函数将指定的逗号分隔值(csv)文件读取到 DataFrame 中。
Pandas 多列合并
发布时间:2024/04/24 浏览次数:628 分类:Python
-
本教程介绍了如何在 Pandas 中使用 DataFrame.merge()方法合并两个 DataFrames。
Pandas loc vs iloc
发布时间:2024/04/24 浏览次数:837 分类:Python
-
本教程介绍了如何使用 Python 中的 loc 和 iloc 从 Pandas DataFrame 中过滤数据。
在 Python 中将 Pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:Python
-
了解如何在 Python 中将 Pandas 系列日期时间转换为字符串