Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define PI 3.14159265359
- /*
- Compute the spectral centroid in hertz for an input signal of finite length.
- This is a very simplistic implementation which can be improved upon.
- input -- the input signal buffer.
- sample_count -- the size of the input signal buffer.
- sample_rate -- the sampling rate in hertz.
- */
- double spectral_centroid(const double *input, int sample_count, double sample_rate)
- {
- double aPowerSum = 0.0, bPowerSum = 0.0;
- for (int i = 1; i < sample_count; ++i)
- {
- double a = input[i], b = input[i] - input[i-1];
- aPowerSum += a*a;
- bPowerSum += b*b;
- }
- return sqrt(bPowerSum / aPowerSum) * sample_rate / (2.0*PI);
- }
- //To the extent possible under law, Evan D. Balster has waived all copyright and related or neighboring rights to this Spectral Centroid Algorithm. See: https://creativecommons.org/publicdomain/zero/1.0/
- //This code was updated on 2016-02-09 to correct several math errors pointed out by Oriol Romani Picas.
Add Comment
Please, Sign In to add comment