0

I was using this pseudo code

for (int i = 0; i < bufferSize /2 ; i++)
        {
            if (magnitude[i] > max_magnitude)
            {
                count++;
                max_magnitude = magnitude[i];
                max_magnitude_index = i;
            }
        }

my samplerate 48000 and buffersize = 2048*2*2

(for avoiding error when calculation in fft return error (n != 2)

When I am trying to calculate the frequency , then magnitude picks some times goes wrong and I get wrong data , like 45000, 33000.

in logcat its shown as following:

  MAX MAGNITUDE INDEX = 43 FREQ : 503 count = 12  
  MAX MAGNITUDE INDEX = 43 FREQ : 503 count = 6  
  MAX MAGNITUDE INDEX = 43 FREQ : 503 count = 11  
  MAX MAGNITUDE INDEX = 4053 FREQ : 47496 count = 10  
  MAX MAGNITUDE INDEX = 4053 FREQ : 47496 count = 14

for generating tons I am using onlinegenerator. I set it to 500 and test it.

and i cant understand how to avoid these errors in magnitude calculations. Whats wrong ? code work good, i can get frequency of sound from 100 to 19500 (cant test more then 19500 hertc) . But picks of magnitude sometimes very strange.

Community
  • 1
  • 1
Peter
  • 2,480
  • 6
  • 39
  • 60
  • `bufferSize / 2 ` is probably the wrong upper bound - it should be `N / 2`, where N is the FFT size, so probably `2048 / 2` in your case ? – Paul R Aug 22 '16 at 12:47
  • when i set 2048 then i get error in FFT class "throw new RuntimeException("n is not a power of 2");" or i m not right understand how to use buffersize. – Peter Aug 22 '16 at 12:50
  • What is the size of your FFT ? What is the size of your buffer (in bytes, and in samples) ? – Paul R Aug 22 '16 at 12:51
  • every where 2048*2*2 – Peter Aug 22 '16 at 12:53
  • i need range with 48000 in samplerate. But if buffersize = 2048 - than i get error "n is not a power of 2" – Peter Aug 22 '16 at 12:54
  • It sounds like you are mixing up buffer size and FFT size. Also 2048 *is* a power of 2, so you might need to do some debugging to see what value you are actually passing to the FFT routine. If you need any more help you will need to create a [mcve], otherwise it's just guess-work. – Paul R Aug 22 '16 at 12:57
  • im updating my code , there full my function , please check it :( – Peter Aug 22 '16 at 12:58
  • As I said above, it looks like you are mixing up buffer sizes (in bytes) and FFT and other sizes (in samples). If you have 2 bytes per sample then at least one of your sizes is wrong. – Paul R Aug 22 '16 at 13:04
  • @PaulR maybe u can help me to fix this ? realy cant understand :( – Peter Aug 22 '16 at 13:05
  • Sorry, I haven't got time to debug your code for you. Get rid of the hard-coded `48000*2*2` though and define separate constants for (a) FFT size (in samples), and (b) buffer size (in bytes), and use these constants appropriately - that would be a good start. – Paul R Aug 22 '16 at 13:11
  • 1
    i think i fixed it make bufferSize/4 on magnitude calculations. Anywhere now i dont have numbers like 45k+ – Peter Aug 22 '16 at 13:41

0 Answers0