Created
April 8, 2010 02:53
-
-
Save endolith/359724 to your computer and use it in GitHub Desktop.
spectral centroid calculation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# http://en.wikipedia.org/wiki/Spectral_centroid | |
from __future__ import division | |
from numpy import abs, sum, linspace | |
from numpy.fft import rfft | |
spectrum = abs(rfft(signal)) | |
normalized_spectrum = spectrum / sum(spectrum) # like a probability mass function | |
normalized_frequencies = linspace(0, 1, len(spectrum)) | |
spectral_centroid = sum(normalized_frequencies * normalized_spectrum) | |
# Spectral centroid is calculated as a ratio. Multiply by your maximum frequency bin to get real frequency. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment