我计算了uint8 I / Q数据的STFT并将其存储在numpy矩阵中,其中每行存储一个窗口的STFT,如下面的sudo代码所示。
#k= length of window
#fs= Sampling frequency
#n= Number of STFT calculated
#matrix= Initially empty numpy array
for i in range(0,n):
t=data[start:end,:] #start & end calculated with each iteration
t=t.flatten()
t=t-127.5
array = np.empty(t.shape[0]//2, dtype=np.complex128)
array.real = t[::2]
array.imag = t[1::2]
transform=(np.fft.fft(temp_array))
line = 2*abs(transform)/k
#Inserting row into numpy array
if(i==0):
matrix = np.hstack((matrix, line))
else:
matrix = np.vstack((matrix, line))
现在我如何绘制频率与时间频谱图?
答案 0 :(得分:0)
你能做的就是用这个
第一种方法:
import scipy.io.wavfile as wav
import scipy.signal as signal
from matplotlib import pyplot as plt
sample_rate, samples = wav.read(filename)
f, t, Zxx = signal.stft(samples, fs=sample_rate)
plt.pcolormesh(t, f, np.abs(Zxx), cmap=cmap)
或第二种方法:
plt.specgram(samples, cmap=cmap, Fs=sample_rate)
我发现第二个具有更好的可视化效果。 不知道怎么让第一个看起来和第二个看起来一样好。
答案 1 :(得分:0)
import os
import librosa
import librosa.display
import IPython.display as ipd
import numpy as np
import matplotlib.pyplot as plt
filename = '<yourfile name/ location>'
x, sr = librosa.load(filename)
import librosa.display #explicitly import librosa.display
X = librosa.stft(x) #perform short-term fourier transfrom
Xdb = librosa.amplitude_to_db(abs(X)) #convert an amplitude spectrogram to dB-scaled spectrogram.
plt.figure(figsize=(15, 5)) #initialize the fig size
librosa.display.specshow(Xdb, sr=sr, x_axis='time', y_axis='hz')
plt.colorbar()```