I have to perform a FFT for a .wav file in an iPhone application. I have seen FFT algorithms out there, but how would I go about parsing a WAV file and using in such an algorithm?
Should I convert the WAV file to NSData or something else?
Has anyone successfully performed a FFT of a WAV file in the iPhone before? I tried to use iPhoneFFT for this, without success.
For example, the code there states
// 2. Then fill up an array with data with your own function
fillUpInputSignalWithMyData(myFFT.inputData);
How would I provide the data from a WAV file to something like this?
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 have created MyAudioFile class which contains
-(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;
}
Thats it.I am getting the power spectrum array.I have checked with sample sine wave .Its working fine.
Study your C data types.
An FFT commonly can take data as an C array of one of several C data types (not an NSArray)... this includes the iOS 4 built-in Accelerate Framework FFTs. The types might be an array of short ints, long ints, floats or doubles. Performing FFTs using short floats is usually the fastest on most recent iOS devices. Check which data type your chosen FFT requires. Many require a real vector and an imaginary vector as complex inputs.
The most common wav file just has 1 chunk with a 44 byte header, then raw PCM data as an array of short ints, either mono or stereo interleaved. Just convert this array into the data type required by the FFT. For mono data something as simple as this may work:
for (i=0;i<n;i++) {
myFloatArray_RealPart[i] = wavInts[i+22];
myFloatArray_ImaginaryPart[i] = 0.0;
}
(But you need to verify the type of wave file you have and the FFT parameters required to make sure!)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With