gpt4 book ai didi

3D 中的 Python 频谱图(类似于 matlab 的频谱图函数)

转载 作者:太空宇宙 更新时间:2023-11-03 23:58:07 25 4
gpt4 key购买 nike

我的问题如下:

我拥有频谱图所需的所有值 (scipy.fftpack.fft)。我想在 python 中创建一个 3D 频谱图。

在 MATLAB 中这是一个非常简单的任务,而在 Python 中它似乎要复杂得多。我尝试了 mayavi,3D 绘图 matplotlib,但我没有设法做到这一点。

谢谢


我的代码:

import numpy as np
import pandas as pd
import numpy as np
from scipy import signal
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.collections import PolyCollection

fs = 11240.
t = 10
time = np.arange(fs*t) / fs
frequency = 1000.
mysignal = np.sin(2.0 * np.pi * frequency * time)

nperseg = 2**14
noverlap = 2**13
f, t, Sxx = signal.spectrogram(mysignal, fs, nperseg=nperseg,noverlap=noverlap)

myfilter = (f>800) & (f<1200)

fig,ax = plt.subplots()

plt.pcolormesh(t, f[myfilter], 10*np.log10(Sxx[myfilter, :]), cmap='jet')
plt.show()

fig = plt.figure()
ax = fig.gca(projection='3d')
x = []
y = []

for counter,i in enumerate(f):
x.append(np.array([i for k in t]))
y.append(t)

ax.plot_surface(np.array(x), np.array(y), 10.0*np.log10(Sxx), cmap=cm.coolwarm)
plt.show()


类似的未回答问题:How to convert a spectrogram to 3d plot. Python

像 Matlab 的图一样用 python 绘制的所需图(此处最后一个图:https://www.mathworks.com/help/signal/ref/spectrogram.html)

enter image description here

最佳答案

您只需要让您的阵列具有正确的形状:

fs = 11240.
t = 10
time = np.arange(fs*t) / fs
frequency = 1000.
mysignal = np.sin(2.0 * np.pi * frequency * time)

nperseg = 2**14
noverlap = 2**13
f, t, Sxx = signal.spectrogram(mysignal, fs, nperseg=nperseg,noverlap=noverlap)

myfilter = (f>800) & (f<1200)

f = f[myfilter]
Sxx = Sxx[myfilter, ...]

fig = plt.figure()
ax = fig.gca(projection='3d')

ax.plot_surface(f[:, None], t[None, :], 10.0*np.log10(Sxx), cmap=cm.coolwarm)
plt.show()

关于3D 中的 Python 频谱图(类似于 matlab 的频谱图函数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56788798/

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