def get_spectral_features(audio, fs, lf_limit=20):
"""
This function calculates the spectral centroid and spectral spread of an audio array.
:param audio: Audio array
:param fs: Sample rate of audio file
:param lf_limit: Low frequency limit, in Hz, to be analysed. Defaults to 20Hz.
:return: Returns the spectral centroid and spectral spread
"""
# use a hanning window
window = np.hanning(len(audio))
next_pow_2 = int(pow(2, np.ceil(np.log2(len(window)))))
# get frequency domain representation
spectrum = np.fft.fft((window * audio), next_pow_2)
spectrum = np.absolute(spectrum[0:int(len(spectrum) / 2) + 1])
freq = np.arange(0, len(spectrum), 1) * (fs / (2.0 * (len(spectrum) - 1)))
# find lowest frequency index, zeros used to unpack result
lf_limit_idx = np.where(freq >= lf_limit)[0][0]
spectrum = spectrum[lf_limit_idx:]
freq = freq[lf_limit_idx:]
# calculate centroid and spread
centroid = sum(spectrum * freq) / float(sum(spectrum))
spread = np.sqrt(sum(((freq - centroid) ** 2) * spectrum) / sum(spectrum))
return centroid, spread
评论列表
文章目录