如何将频谱图转换为3d图.Python [英] How to convert a spectrogram to 3d plot. Python

查看:55
本文介绍了如何将频谱图转换为3d图.Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现wav文件的瀑布图.在我的尝试中,我注意到这基本上是 3d 中的光谱图(或接近我需要的).我正在尝试使用 numpy 和 matplotlib 在 Python 中执行此操作.

我的主要问题是我不知道如何将光谱图从 matplotlib 更改为 3d 绘图.

我的代码"示例:

sample ,data = wavfile.read('file.wav')F =图()a = F.add_subplot(111,projection ='3d')规格, t, 频率, im = a.specgram(data,Fs=2)

我已经走了这么远,不知道下一步该怎么做.我想将现有的情节更改为 3d.由于缺乏知识,我没有将其更改为 3d 的代码.

是否可以将2d图转换为3d?如果可以,怎么办?我最好用光谱图的数据构建一个新图吗?

期望的结果类似于以下内容:感谢您的回复.

解决方案

以下是3D&您可以在

from matplotlib import mlab导入matplotlib.pyplot作为plt将numpy导入为np#修复随机状态以提高可重复性np.random.seed(666)title = ('2 Vrms 正弦波,调制频率约为 3kHz,'被呈指数下降的白噪声破坏"以10 kHz采样的幅度.")fs = 10e3N = 1e5放大器= 2 * np.sqrt(2)noise_power = 0.01 * fs/2t = np.arange(N)/float(fs)mod = 500 * np.cos(2 * np.pi * 0.25 * t)载波 = amp * np.sin(2*np.pi*3e3*t + mod)噪声= np.random.normal(scale = np.sqrt(noise_power),size = t.shape)噪音* = np.exp(-t/5)y =载波+噪声def specgram3d(y, srate=44100, ax=None, title=None):如果不是斧头:ax = plt.axes(projection='3d')ax.set_title(title, loc='center', wrap=True)规格,频率,t = mlab.specgram(y,Fs = srate)X, Y, Z = t[None, :], freqs[:, None], 20.0 * np.log10(spec)ax.plot_surface(X, Y, Z, cmap='viridis')ax.set_xlabel('time(s)')ax.set_ylabel('频率(Hz)')ax.set_zlabel('振幅(dB)')ax.set_zlim(-140,0)返回 X、Y、Zdef specgram2d(y,srate = 44100,ax = None,title = None):如果不是斧头:斧= plt.axes()ax.set_title(title, loc='center', wrap=True)规格,频率,t,im = ax.specgram(y,fs=fs,scale='dB',vmax=0)ax.set_xlabel('时间(s)')ax.set_ylabel('频率(Hz)')cbar = plt.colorbar(im, ax=ax)cbar.set_label('振幅(dB)')cbar.minorticks_on()返回规格,频率,t,即时fig1,ax1 = plt.subplots()specgram2d(y,srate = fs,title = title,ax = ax1)fig2,ax2 = plt.subplots(subplot_kw = {'projection':'3d'})specgram3d(y,srate = fs,title = title,ax = ax2)plt.show()

奖励:

您可以通过使用scipy创建一个wav文件来收听信号:

来自scipy.io的

 导入wavfilewavfile.write('sig.wav',int(fs),y)

I am trying to achieve waterfall graph of wav file. In my attempts I noticed that this is basically a spectogram in 3d (or as close to what I need). I am trying to do this in Python with numpy and matplotlib.

My main problem is I don't know how to change the plot of specgram from matplotlib into a 3d plot.

Sample of my "code":

sample ,data = wavfile.read('file.wav')
F = Figure()
a = F.add_subplot(111,projection='3d') 
Spec, t, freq, im = a.specgram(data,Fs=2)

I've got this far and have no clue what to do next. I wanna change already existing plot into 3d. I have no code of changing it to 3d, due to lack of my knowledge.

Is it possible to convert 2d plot to 3d ? If so how ? Am i better off constructing a new plot with data from specgram?

The desired outcome would be something like the following: Thanks for any replies.

解决方案

Here are 3D & 2D spectrogram plots of an example signal from scipy that you can find at the end of this page.

from matplotlib import mlab
import matplotlib.pyplot as plt
import numpy as np

# Fixing random state for reproducibility
np.random.seed(666)

title = ('2 Vrms sine wave with modulated frequency around 3kHz, '
         'corrupted by white noise of exponentially decreasing '
         'magnitude sampled at 10 kHz.')

fs = 10e3
N = 1e5
amp = 2 * np.sqrt(2)
noise_power = 0.01 * fs / 2
t = np.arange(N) / float(fs)
mod = 500*np.cos(2*np.pi*0.25*t)
carrier = amp * np.sin(2*np.pi*3e3*t + mod)
noise = np.random.normal(scale=np.sqrt(noise_power), size=t.shape)
noise *= np.exp(-t/5)
y = carrier + noise

def specgram3d(y, srate=44100, ax=None, title=None):
  if not ax:
    ax = plt.axes(projection='3d')
  ax.set_title(title, loc='center', wrap=True)
  spec, freqs, t = mlab.specgram(y, Fs=srate)
  X, Y, Z = t[None, :], freqs[:, None],  20.0 * np.log10(spec)
  ax.plot_surface(X, Y, Z, cmap='viridis')
  ax.set_xlabel('time (s)')
  ax.set_ylabel('frequencies (Hz)')
  ax.set_zlabel('amplitude (dB)')
  ax.set_zlim(-140, 0)
  return X, Y, Z

def specgram2d(y, srate=44100, ax=None, title=None):
  if not ax:
    ax = plt.axes()
  ax.set_title(title, loc='center', wrap=True)
  spec, freqs, t, im = ax.specgram(y, Fs=fs, scale='dB', vmax=0)
  ax.set_xlabel('time (s)')
  ax.set_ylabel('frequencies (Hz)')
  cbar = plt.colorbar(im, ax=ax)
  cbar.set_label('Amplitude (dB)')
  cbar.minorticks_on()
  return spec, freqs, t, im

fig1, ax1 = plt.subplots()
specgram2d(y, srate=fs, title=title, ax=ax1)

fig2, ax2 = plt.subplots(subplot_kw={'projection': '3d'})
specgram3d(y, srate=fs, title=title, ax=ax2)
  
plt.show()

BONUS:

You can listen to the signal by creating a wav file using scipy:

from scipy.io import wavfile
wavfile.write('sig.wav', int(fs), y)

这篇关于如何将频谱图转换为3d图.Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆