Tuesday, February 16, 2010

Spectral Centroid in Audio

The center of mass of the spectrum.

x(n) is the magnitude of bin number, f(n) is the center frequency of that bin. The following MATLAB function is from the exchange website;

function C = SpectralCentroid(signal,windowLength, step, fs)
signal = signal / max(abs(signal));
curPos = 1;
L = length(signal);
numOfFrames = floor((L-windowLength)/step) + 1;
H = hamming(windowLength);
m = ((fs/(2*windowLength))*[1:windowLength])';
C = zeros(numOfFrames,1);
for (i=1:numOfFrames)
window = H.*(signal(curPos:curPos+windowLength-1));
FFT = (abs(fft(window,2*windowLength)));
FFT = FFT(1:windowLength);
FFT = FFT / max(FFT);
C(i) = sum(m.*FFT)/sum(FFT);
if (sum(window.^2)<0.010)
C(i) = 0.0;
end
curPos = curPos + step;
end
C = C / (fs/2);

2 comments:

  1. Anonymous7:30 AM

    how to run this code??? please prove the steps.

    ReplyDelete
  2. please explain this code, especially m

    ReplyDelete