android getting sound frequencies real time?

后端 未结 3 480
不思量自难忘°
不思量自难忘° 2020-12-24 08:06

I have been trying to get the sound frequency(number) in real time using fft and i am having run time errors. can any one help?

package com.example.recordsou         


        
3条回答
  •  忘掉有多难
    2020-12-24 08:31

    Did you solved the problem? The crush is occurred because of the ArrayIndexOutOfBoundsException.

    So, modify your code to :

                    double[] re = new double[blockSize];
                    double[] im = new double[blockSize];
                    double[] magnitude = new double[blockSize];
    
                    // Calculate the Real and imaginary and Magnitude.
                    for(int i = 0; i < blockSize+1; i++){
                        try {
                            // real is stored in first part of array
                            re[i] = toTransform[i * 2];
                            // imaginary is stored in the sequential part
                            im[i] = toTransform[(i * 2) + 1];
                            // magnitude is calculated by the square root of (imaginary^2 + real^2)
                            magnitude[i] = Math.sqrt((re[i] * re[i]) + (im[i] * im[i]));
                        }catch (ArrayIndexOutOfBoundsException e){
                            Log.e("test", "NULL");
                        }
                    }
    
                    double peak = -1.0;
                    // Get the largest magnitude peak
                    for(int i = 0; i < blockSize; i++){
                        if(peak < magnitude[i])
                            peak = magnitude[i];
                    }
                    // calculated the frequency
                    frequency = Double.toString((sampleRate * peak)/blockSize);
    

提交回复
热议问题