views:

146

answers:

3

Hi

I am using Ooura FFT to compute the FFT of the accelerometer data in windows of 1024 samples. The code works fine, but then for some reason it produces very strange outputs, i.e. continuous spectrum with amplitudes of the order of 10^200.

Here is the code:

 OouraFFT *myFFT=[[OouraFFT alloc] initForSignalsOfLength:1024 NumWindows:10]; //       had to allocate it

 UIAcceleration *tempAccel = nil;


 double *input=(double *)malloc(1024 * sizeof(double));
 double *frequency=(double *)malloc(1024*sizeof(double));

 if (input)

 {

 //NSLog(@"%d",[array count]);
 for (int u=0; u<[array count]; u++)
 {
  tempAccel = (UIAcceleration *)[array objectAtIndex:u];
  input[u]=tempAccel.z;
  //NSLog(@"%g",input[u]);
 }

 }

 myFFT.inputData=input; // specifies input data to myFFT


 [myFFT calculateWelchPeriodogramWithNewSignalSegment]; // calculates FFT


 for (int i=0;i<myFFT.dataLength;i++) // loop to copy output of myFFT, length of spectrumData is half of input data, so copy twice
 {

  if (i<myFFT.numFrequencies)
  {
   frequency[i]=myFFT.spectrumData[i]; // 
  }
  else 

  {
   frequency[i]=myFFT.spectrumData[myFFT.dataLength-i]; // copy twice
  }

 }





 for (int i=0;i<[array count];i++)

 {
  TransformedAcceleration *NewAcceleration=[[TransformedAcceleration alloc]init];  
  tempAccel=(UIAcceleration*)[array objectAtIndex:i];

  NewAcceleration.timestamp=tempAccel.timestamp;
  NewAcceleration.x=tempAccel.x;
  NewAcceleration.y=tempAccel.z;
  NewAcceleration.z=frequency[i];
  [newcurrentarray addObject:NewAcceleration]; // this does not work

  //[self replaceAcceleration:NewAcceleration];
  //[NewAcceleration release];
  [NewAcceleration release];
 }

 TransformedAcceleration *a=nil;//[[TransformedAcceleration alloc]init]; // object containing fft of x,y,z accelerations


 for(int i=0; i<[newcurrentarray count]; i++)
 {
  a=(TransformedAcceleration *)[newcurrentarray objectAtIndex:i];
  //NSLog(@"%d,%@",i,[a printAcceleration]);
  fprintf(fp,[[a printAcceleration] UTF8String]);  //this is going wrong somewhow
 }

 fclose(fp);

 [array release];
 [myFFT release];
 //[array removeAllObjects];

 [newcurrentarray release];

 free(input);
 free(frequency);
A: 

Hey there, have you tried plotting the accelerometer values? If there are any abrupt changes, the FFT will go out of range.

Have you tried plotting the FFT? Can you post images of what that looks like? If it looks normal for most of the time, than goes out of range occasionally, I'm betting you should smooth your accelerometer data beforehand.

Also, why are you copying spectrumData twice? That array is already set up to provide you with plottable data.

Last, are you overlapping the segments of data you provide to OouraFFT? It's designed to be fed segments of data with around 50% overlap. You might get weird edge effects otherwise.

alexbw
Hi thanks for your response. I am copying spectrumData twice to have it the same size as the input data. In matlab I plot the spectrum using fftshit. I attached some pictures. The errors don't happen occasionally, they happen quite often.
Yan
Can't upload the images. It says that I need a reputation of 10 as a new user to be able to upload the image. Cheers
Yan
Don't get why you want the spectrumData to be the same size as the input data. When it's calculated, its imaginary and real parts are combined, which is why it's half the size. Copy just once.Are you overlapping your inputData?Go to oscopeapp.com and leave me a comment with your email, and we can exchange pictures from there.
alexbw
A: 

![url=http://www.freeimagehosting.net/]http://www.freeimagehosting.net/uploads/2c2e01e31f.jpg

Yan
This is not an answer - if you want to add an image then edit your original question and include it there.
Paul R
+1  A: 

You need to calloc the input and output arrays. It's a bug in my code that will NOT be fixed, as this library is made obsolete by Apple's new Accelerate framework.

alexbw