可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I've been playing with this now for sometime, I cant work out what I am meant to be doing here.
I am reading in PCM audio data into an audioData array:
recorder.read(audioData,0,bufferSize); //read the PCM audio data into the audioData array
I want to use Piotr Wendykier's JTransform library in order to preform an FFT on my PCM data in order to obtain the frequency.
import edu.emory.mathcs.jtransforms.fft.DoubleFFT_1D;
At the moment I have this:
DoubleFFT_1D fft = new DoubleFFT_1D(1024); // 1024 is size of array for (int i = 0; i
I cant make sense of how to work this, can somebody give me some pointers? Will i have to perform any calculations after this?
I'm sure I'm way off, anything would be greatly appreciated!
Ben
回答1:
If you're just looking for the frequency of a single sinusoidal tone in the input waveform then you need to find the FFT peak with the largest magnitude, where:
Magnitude = sqrt(re*re + im*im)
The index i of this largest magnitude peak will tell you the approximate frequency of your sinusoid:
Frequency = Fs * i / N
where:
Fs = sample rate (Hz) i = index of peak N = number of points in FFT (1024 in this case)
回答2:
Yes you need to use realForward function instead of complexForward, because you pass it a real array and not a complex array from doc.
EDIT:
Or you can get the real part and perform complex to complex fft like this :
double[] in = new double[N]; read ... double[] fft = new double[N * 2]; for(int i = 0; i
I try and I compare results with matlab, and I don't get same results... (magnitude)
回答3:
Since I've spent some hours on getting this to work here's a complete implementation in Java:
import org.jtransforms.fft.DoubleFFT_1D; public class FrequencyScanner { private double[] window; public FrequencyScanner() { window = null; } /** extract the dominant frequency from 16bit PCM data. * @param sampleData an array containing the raw 16bit PCM data. * @param sampleRate the sample rate (in HZ) of sampleData * @return an approximation of the dominant frequency in sampleData */ public double extractFrequency(short[] sampleData, int sampleRate) { /* sampleData + zero padding */ DoubleFFT_1D fft = new DoubleFFT_1D(sampleData.length + 24 * sampleData.length); double[] a = new double[(sampleData.length + 24 * sampleData.length) * 2]; System.arraycopy(applyWindow(sampleData), 0, a, 0, sampleData.length); fft.realForward(a); /* find the peak magnitude and it's index */ double maxMag = Double.NEGATIVE_INFINITY; int maxInd = -1; for(int i = 0; i maxMag) { maxMag = mag; maxInd = i; } } /* calculate the frequency */ return (double)sampleRate * maxInd / (a.length / 2); } /** build a Hamming window filter for samples of a given size * See http://www.labbookpages.co.uk/audio/firWindowing.html#windows * @param size the sample size for which the filter will be created */ private void buildHammWindow(int size) { if(window != null && window.length == size) { return; } window = new double[size]; for(int i = 0; i
FrequencyScanner will return an approximation of the dominant frequency in the presented sample data. It applies a Hamming window to it's input to allow passing in arbitrary samples from an audio stream. Precision is achieved by internally zero padding the sample data before doing the FFT transform. (I know there are better - and far more complex - ways to do this but the padding approach is sufficient for my personal needs).
I testet it against raw 16bit PCM samples created from reference sounds for 220hz and 440hz and the results match.
回答4:
If you're looking for the FFT of an Audio input ( 1D, real data ), should'nt you be using the 1D REAL Fft?