def stft(sig, frame_size, overlap_fac=0.5, window=np.hanning):
""" short time fourier transform of audio signal """
win = window(frame_size)
hop_size = int(frame_size - np.floor(overlap_fac * frame_size))
# zeros at beginning (thus center of 1st window should be for sample nr. 0)
samples = np.append(np.zeros(np.floor(frame_size / 2.0)), sig)
# cols for windowing
cols = np.ceil((len(samples) - frame_size) / float(hop_size)) + 1
# zeros at end (thus samples can be fully covered by frames)
samples = np.append(samples, np.zeros(frame_size))
frames = stride_tricks.as_strided(
samples,
shape=(cols, frame_size),
strides=(
samples.strides[0] * hop_size,
samples.strides[0]
)
).copy()
frames *= win
return np.fft.rfft(frames)
评论列表
文章目录