Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Frequency analyzer problem

Discussion in 'Scripting' started by luditemu6ici, Mar 11, 2013.

  1. luditemu6ici

    luditemu6ici

    Joined:
    Aug 14, 2010
    Posts:
    36
    I'm working on an audio frequency analyzer for my game. I use the GetSpectrumData function to get the frequency values and I use BlackmanHarris for FFT function. The problem is that the function I guess returns a very wide range of frequencies and a lot of the lower frequeny values stay at zero almost all the time.

    Here is how I fill my frequency array:

    Code (csharp):
    1.  
    2. var lspec = new float[64];
    3. var rspec = new float[64];
    4.  
    5.  
    6. audio.GetSpectrumData (lspec, 0, FFTWindow.BlackmanHarris);
    7.   audio.GetSpectrumData (rspec, 1, FFTWindow.BlackmanHarris);
    8.  
    9.   for(i=0; i<lspec.length; i++){
    10.    spectrumData[i] = (Mathf.Abs(lspec[i])+Mathf.Abs(rspec[i]))/2;
    11.   }
    12.  

    If I try to map the spectrum array into a bar visualizer the problem I described appears - the bars to the left stay zero or close to zero and only several bars on the rights side get significantly changed. The image I'm providing is showing my visualizer and how it behaves.

    What I need to achieve is this:
    http://www.youtube.com/watch?v=YxIWaRuXilM


    I need to be able to tell high frequency sounds from low frequency ones.

    Does anyone know what I've done wrong ?
     
  2. luditemu6ici

    luditemu6ici

    Joined:
    Aug 14, 2010
    Posts:
    36
  3. Dark-Protocol

    Dark-Protocol

    Joined:
    Nov 19, 2011
    Posts:
    279
    Here's what I think it happens: You have accidentaly inverted your visualizer so the lower frequencies are at the right.

    If that's the case, you should know something about human hearing and music. First of all, hearing is logarythmic. That means that the lower tones don't differ too much in frequency from each other while the higher tones differ a lot. Given that, we know that people can hear within 20hz and 20Khz. Then, because your hearing is logarythimc, you can't expect the highest notes to be at the 20 khz value. More on that: the middle tones are not at the middle of 20hz and 20khz either. The music frequency range is between 20hz and about 4khz I think.

    So you pull 64 samples from GetSpectrumData. If what I said is correct and I'm not completely sure, because I'm not an audio guru, you need only the first few samples from all those 64. And to get a 64 sample array that would map to the frequencies 20hz - 4Khz, you will need to pull a much bigger array of data from GetSpectrumData and use it's first 64 elements.

    This would be pretty bad though as pulling more samples would mean more processor power for something that you will not actually use (as you only need a part of that data).

    I don't know if there's a better way to do it and I don't know if my suggestion is 100% right soo I'd be happy to see other people's oppinions on that :)

    Cheers!
     
  4. dave_mm0

    dave_mm0

    Joined:
    Feb 8, 2013
    Posts:
    26
    My own guess is that the two first frequency samples being empty is normal. There are two things that might cause this.

    First, the mp3 encoding is not lossless, meaning that there some of the original data will be lost. Since humans can't hear below 20Hz or higher than 20kHz, mp3 will delete these parts of the spectrum (I think 128mbps mp3s cutoff anything out of the 20Hz-16kHz range.

    Second, the BlackmanHarris window will actually reduce the lower and higher frequencies of your spectrum., so that when you map your frequencies directly to a visualizer, you will get a nice bell curve. Try a rectangular window instead. A nice comprise between the two is the Hamming window.

    Try a larger spectrum, say 1024 samples (about 1/40th of a second on a 128mbps mp3) and see if the lower bands are still empty. You should get a few more that are empty. This will give you a lot more information about your sound and is still well bellow the "real-time" threshold.
     
  5. luditemu6ici

    luditemu6ici

    Joined:
    Aug 14, 2010
    Posts:
    36
    I think more samples would result in what he said

    Or if not then that would be great :) But I will try anyway :)

    Thanks