OouraFFT的输出有时会纠正,但有时会完全错误。为什么?

时间:2010-03-17 15:43:58

标签: iphone algorithm fft

我正在使用Ooura FFT来计算1024个样本窗口中加速度计数据的FFT。代码工作正常,但由于某种原因,它产生非常奇怪的输出,即幅度大约为10 ^ 200的连续频谱。

以下是代码:

 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);

2 个答案:

答案 0 :(得分:1)

你需要calloc输入和输出数组。这是我的代码中的一个错误,不会修复,因为这个库已经被Apple的新Accelerate框架淘汰了。

答案 1 :(得分:0)

嘿,你有没有试过绘制加速度计的值?如果有任何突然变化,FFT将超出范围。

您是否尝试绘制FFT?你能发布看起来像什么的图像吗?如果它在大多数情况下看起来正常,而不是偶尔超出范围,我打赌你应该预先平滑你的加速度计数据。

另外,为什么要复制spectrumData两次?该数组已经设置为为您提供可绘制的数据。

最后,您是否重叠了您提供给OouraFFT的数据段?它被设计为馈送数据段,重叠率约为50%。否则你可能会得到奇怪的边缘效应。