def dft_np(signal, hop_size=256, fft_size=512):
n_hops = len(signal) // hop_size
s = []
hann_win = hann(fft_size)
for hop_i in range(n_hops):
frame = signal[(hop_i * hop_size):(hop_i * hop_size + fft_size)]
frame = np.pad(frame, (0, fft_size - len(frame)), 'constant')
frame *= hann_win
s.append(frame)
s = np.array(s)
N = s.shape[-1]
k = np.reshape(np.linspace(0.0, 2 * np.pi / N * (N // 2), N // 2), [1, N // 2])
x = np.reshape(np.linspace(0.0, N - 1, N), [N, 1])
freqs = np.dot(x, k)
reals = np.dot(s, np.cos(freqs)) * (2.0 / N)
imags = np.dot(s, np.sin(freqs)) * (2.0 / N)
return reals, imags
评论列表
文章目录