def Specgram(X, W, H):
"""A function to compute the spectrogram of a signal
:parm X: N x 1 Audio Signal
:param W: Window Size
:param H HopSize
:returns: S, an N x NBins spectrogram array
"""
Q = W/H
if Q - np.floor(Q) > 0:
print('Warning: Window size is not integer multiple of hop size\n')
win = np.hamming(W)
NWin = int(np.floor((len(X) - W)/float(H)) + 1)
S = np.zeros((NWin, W))
for i in range(NWin):
x = X[i*H:i*H+W]
S[i, :] = np.abs(np.fft.fft(win*x))
#Second half of the spectrum is redundant for real signals
if W % 2 == 0:
#Even Case
S = S[:, 0:W/2]
else:
#Odd Case
S = S[:, 0:(W-1)/2+1]
return S
评论列表
文章目录