How do you execute FFT WAV file on iPhone?

I need to execute FFT for a WAV file in an iPhone application. I saw FFT algorithms there, but how would I start to parse a WAV file and use it in such an algorithm?

Should I convert a WAV file to NSData or something else?

Has anyone successfully completed an FFT WAV file on an iPhone before? I tried using iPhoneFFT for this without success.

For example, the code contains

// 2. Then fill up an array with data with your own function fillUpInputSignalWithMyData(myFFT.inputData); 

How can I provide data from a wav file something like this?

+4
source share
3 answers
 MyAudioFile *audioFile = [[MyAudioFile alloc]init]; OSStatus result = [audioFile open:var ofType:@"wav"]; int numFrequencies=16384; int kNumFFTWindows=10; OouraFFT *myFFT = [[OouraFFT alloc] initForSignalsOfLength:numFrequencies*2 andNumWindows:kNumFFTWindows]; for(long i=0; i<myFFT.dataLength; i++) { myFFT.inputData[i] = (double)audioFile.audioData[i]; } [myFFT calculateWelchPeriodogramWithNewSignalSegment]; for (int i=0;i<=8192;i++) { NSLog(@"the spectrum data %d is %f ",i,myFFT.spectrumData[i]); } 

I created a class MyAudioFile containing

 -(OSStatus)open:(NSString *)fileName ofType:(NSString *)fileType{ OSStatus result = -1; CFStringRef filePath=fileName; CFURLRef audioFileURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (CFStringRef)filePath, kCFURLPOSIXPathStyle, false); //open audio file result = AudioFileOpenURL (audioFileURL, kAudioFileReadPermission, 0, &mAudioFile); if (result == noErr) { //get format info UInt32 size = sizeof(mASBD); result = AudioFileGetProperty(mAudioFile, kAudioFilePropertyDataFormat, &size, &mASBD); UInt32 dataSize = sizeof packetCount; result = AudioFileGetProperty(mAudioFile, kAudioFilePropertyAudioDataPacketCount, &dataSize, &packetCount); NSLog([NSString stringWithFormat:@"File Opened, packet Count: %d", packetCount]); UInt32 packetsRead = packetCount; UInt32 numBytesRead = -1; if (packetCount > 0) { //allocate buffer audioData = (SInt16*)malloc( 2 *packetCount); //read the packets result = AudioFileReadPackets (mAudioFile, false, &numBytesRead, NULL, 0, &packetsRead, audioData); NSLog([NSString stringWithFormat:@"Read %d bytes, %d packets", numBytesRead, packetsRead]); } } else NSLog([NSString stringWithFormat:@"Could not open file: %@", filePath]); CFRelease (audioFileURL); return result; } 

Here he is. I get an array of power spectrum. I checked with a sine wave. It is working fine.

+6
source

Explore your C data types.

An FFT can usually receive data as a C array of one of several C data types (not NSArray) ... this includes built-in FEP accelerators with acceleration Fixs iOS 4. Types can be an array of short ints, long ints, float or double. FFT using short floats is usually the fastest on the latest iOS devices. Check what type of data is required for your chosen FFT. Many require a real vector and an imaginary vector as complex inputs.

The most common wav file has only 1 piece with a 44-byte header, and then the raw PCM data as an array of short ints, mono or stereo interleaved. Just convert this array to the data type needed for FFT. For monodata, it can be that simple:

 for (i=0;i<n;i++) { myFloatArray_RealPart[i] = wavInts[i+22]; myFloatArray_ImaginaryPart[i] = 0.0; } 

(But you need to check the type of wave file that you have and the FFT parameters needed to ensure!)

+4
source

Well, according to: http://github.com/alexbw/iPhoneFFT/blob/master/OouraFFT.h

You're using:

 double *inputData; 

So, you will be a fopen () wav file, get rid of the headers and get a double array.

+1
source

All Articles