spectral centroid

21 thoughts on “spectral centroid

    • You are correct, the comment and the implementation do not fit together perfectly. I should remove the square in future revisions.

      In my defense I can only note that there are many different definitions of the Spectral Centroid, and none of them is more correct than the other one…

  1. By looking to your code for spectral centroid and spectral rolloff.. the last answer you convert to Hz. If we want to use as features, it is need in Hz or index?

    % convert from index to Hz
    vsc = vsc / size(X,1) * f_s/2;

    % convert from index to Hz
    vsc = vsc / size(X,1) * f_s/2;

    thank you

  2. Sir, I am trying to use the code bit I am getting these errors. Do I have to put some other values. Kindly Reply:

    Error in ComputeFeature (line 79)
    v = hFeatureFunc(X, f_s);

    Error in trial (line 2)
    [v, t] = ComputeFeature (‘SpectralCentroid’, x, fs);

  3. Sir, that error I got when I tried to run:
    [x,fs] = wavread(‘abc.wav’);
    [v, t] = ComputeFeature (‘SpectralCentroid’, x, fs);
    Sir, I really need your help. If its possible.

      • sir, first problem I am getting is that how to use the code actually. I have both computefeature file as well as other feature files. But how to compute any feature. I tried that spectral centroid eg you gave in the other site and I am getting the above error. Also
        @param afAudioData: time domain sample data, dimension channels X samples
        I don’t know how to find the time domain sample data from the wav file.
        Actually I am new to matlab that is why its hard for me to understand this.

        • Why don’t you look at a Matlab tutorial first online, and then try to figure the code out? I think that would be a more efficient approach.

          • sir, I have seen many codes but these are the ones which suits my problem best. The only problem I am having here is to which program to run to get the values for the feature. Sir, I am working on classification of musical instruments.

            • Why don’t you look at a Matlab tutorial first online, and then try to figure the code out? I think that would be a more efficient approach.

  4. Hello,
    reading your Book and comparing it with the code here I got a small question:
    On p. 196 it is written that it is enough to compute K/2 fourier coefficients of the DFT. It did not fully understand why, but this is ok so far.
    But still – the DFT “consists of exactly K complex values” (with K = Framelength). With this said I still have K frequency bins in my spectrum X, don’t I?
    In the code above you iterate the complete vector, or not?
    But on p. 45 in your book the range goes from k=0 until K/2 – 1.
    Iterating up to K would feel intuitive, but I do not understand the K/2 – 1. Thanks in advance for your help!
    Elke

    • Hi Elke,

      the short answer to this is that for the spectral centroid we are dealing with the magnitude spectrum, not the complex spectrum.

      There are, however, several points in your comment that are confusing to many students:

      • – indices: the indices in the book run from 0 (C-Style) so if you have a sum from 0 to K/2-1, you have K/2 values
      • – the Fourier transform can be computed for complex input signals as well. Then, we would end up with the full range of K complex values; with a real-valued input signal, however, the symmetry allows us to discard roughly half of the values as redundant, ending up with K/2 complex values (i.e., K values overall). Sidenote: strictly speaking, it will be K/2-1 complex values plus two real values, but don’t let that confuse you.
      • – It would be conceivable to add the symmetry point at index K/2 to the sum (then the sum would have K/2+1 values). However, the magnitude at this point depends on its phase so I excluded it from my definitions.

      I hope this answers your question. I realize that the long answer might even be more confusing – sorry if that is the case.
      Alexander

      • Thank you very much for your detailed answer! It helped – especially it helped to formulate a more detailed question:

        For the spectral centroid we’re basically iterating over all k frequency bins of the magnitude spectrum. So I could write the sum as \sum_k=0^#k if #k would be the numbers of frequency bins, right?

        Given K = block size (= framelength = num of samples per frame).
        –> Now I need to know how #k relates to K
        So because of the above mentioned facts #k = K/2.

        Do I really only ‘have’ K/2 frequency bins?

        Could I, for the sake of simplicity, also write the sum as mentioned above (without relation to K, but by simply introducing a new variable #k)?

        Thank you again very much for your help!

        • It is the weighted sum, yes. So if you wanted, you could replace the vector multiplication in my code
          [0:size(X,1)-1]*X
          with (untested)
          numerator = 0;
          for (k = 1:size(X,1)) numerator = numerator + (k-1)*X(k, blockindex); end

          As for the number of frequency bins: why don’t you just experiment with matlab’s fft and abs functions to see how many bins you have and if they are redundant or not?

          • Well at the moment I am just working with your book and not with matlab (I usually work only with pythons matlab-like matplotlib) – but you’re right this would be a good idea to explore everything in a more intuitive way.

            Thank for your help!

Leave a Reply

Your email address will not be published. Required fields are marked *