【发布时间】:2022-01-20 15:33:46
【问题描述】:
import numpy as np
from numpy import asarray
from matplotlib import pyplot as plt
import torch
# generate a signal
fs = 50 # sampling freq
ts = np.arange(0, 10, 1/fs) # times at which signal is sampled
s1 = np.sin(2 * np.pi * 2 * ts) # 2 hz
s2 = np.sin(2 * np.pi * 3 * ts) # 3 hz
s3 = np.sin(2 * np.pi * 6 * ts) # 6 hz
s = s1 + s2 + s3 # aggregate signal
# generate specgram
spectrum, freqs, t, im = plt.specgram(s, Fs=fs, xextent=((0, len(s)/fs)))
# convert matplotlib image to torch tensor
# bypassing the numpy part would be even better!
torch_tensor = torch.from_numpy(asarray(im, np.float32))
print(torch_tensor)
>>> TypeError: float() argument must be a string or a number, not 'AxesImage'
我应该补充一点,'spectrum' 变量是我正在寻找的东西,除了我对它有点困惑,因为它只有两列时间,而且我认为 specgram 图像有不止两列时间步长。如果有办法使用光谱变量将整个图像表示为火炬张量,那么这对我也有用。
【问题讨论】:
标签: python numpy matplotlib pytorch spectrogram