# Calculating the Spectral Centroid of a Signal

Here is the equation:

$$C_i=\frac\sum_{k=1}^{W_{f_L}}kX_i(k)}{\displaystyle\sum_{k=1}^{W_{f_L}}X_i(k)}$$

The MATLAB code for the equation is:

sum(m.*window) / sum(window);


Let's say length(window) = 882 that contains the first half (abs) of the magnitude spectrum, fs = 44100.

Now calculate m:

m = ( (fs/(2*window)) * [1:window] )';


The length(m) = 882 and increments in 25, which is the sample range (e.g. [25 50 75 100 ... 22050]).

I'm trying to determine a better insight in terms of what is happening when both terms are being divided. It is clear that each element of m is multiplying each element of window, but what is actually happening? The book I'm reading just states that the Spectral Centroid is the center of 'gravity' of the spectrum.

The function you wrote is basically like calculating the center of mass of a body or for that matter the Mean Value of a Distribution.

If you remember from Probability, the Mean is given by:

$$E \left[ y \right] = \sum_i {y}_{i} p \left( {y}_{i} \right)$$

Now, let's think of the Frequency Function (The DFT) as a probability function.
In order to do so we'll normalize it to have a sum of 1.

This is done by dividing each element by the sum of elements.
Then just apply the Mean operator to calculate the Mean of this "Distribution":

$${C}_{i} = \sum_{k} \underset{{y}_{i}}{\underbrace{k}} \underset{ p \left( {y}_{i} \right ) }{\underbrace{ \frac{{X}_{i} \left( k \right)}{\sum_{k} {X}_{i} \left( k \right) } }}$$

The result you'll get is exactly what you have written above.

• Great answers, but still trying to grasp the concept, so bare with me. So, your equation (the DFT) eradicates the need for my m variable, which is used as a weight for each element in X? I'm just a bit confused with the k variable that is in front of the fraction. Is this the mean operator? – user1574598 Mar 29 '17 at 17:15
• Probability functions has weights and values. In out case the probability function is the DFT itself, hence we normalize it to 1. The values are the indices (No need for all you did, you can use $k = \left\{ 0, 1, 2, \ldots \right\}$ and later on to understand the frequency itself relative to the Sampling Frequency. – Royi Mar 29 '17 at 17:29
• Ok, I'm getting there, so to be clear, is the k variable that you have underlined with y(i) that appears to be multiplying the fraction p(y(i)) incrementing like so k = {0,1,2...}, or is it incrementing through the actual magnitude coefficients from X? – user1574598 Mar 30 '17 at 12:38
• Yep. It is the Index Number of the DFT Element. – Royi Mar 30 '17 at 12:39

What you are actually calculating is the first moment of the spectrum, just like the center of mass in mechanics, or the mean value in statistics.

The divisor is there for normalization of the spectrum. Think of $X_N[k] = X_i[k]/\sum_k X_i[k]$ as the normalized spectrum which has a total sum of 1.

This normalized spectrum (just like a probability density in statistics) acts as a weight factor for each of the terms in the window.

Each frequency bin index $k$ is being weighted by a mass which is a fraction of the amplitude spectrum at this bin, divided by the sum of all amplitudes, which can be interpreted as a probability density function. You could rewrite:

$$C_i = \sum_{k=1}^{Wf_L} k. \frac{X_i(k)}{\sum_{k=1}^{Wf_L}X_i(k)}\,.$$

This is discussed in details in How to calculate the mean/center frequency of the spectrum? and answers.

$C_i$ gives you an "index" of where the spectrum is "concentrated".