编辑 2
在我的其他SO question中查找有关此问题的更多信息。
编辑 1
我更新了这篇文章,因此内容可能与 Alexey、Hicham、Jonathan 和 Mat 的评论不一致。
下面的代码使用这个 FFT来辅助发病检测。当该方法被调用一次时,一切正常,我得到了一个很好的值日志。当第二次调用该方法时,我得到nan
或垃圾。有任何想法吗?
{
NSLog(@"Running onset.");
NSMutableArray *mutableArrayOfFlags = [[NSMutableArray alloc] init];
OnsetsDS *ods = malloc(sizeof *ods);
float* odsdata = (float*) malloc(onsetsds_memneeded(ODS_ODF_RCOMPLEX, 512, 11));
onsetsds_init(ods, odsdata, ODS_FFT_FFTW3_HC, ODS_ODF_RCOMPLEX, 512, 11, 44100);
int i;
int x;
bool onset;
for (i = 0; i < vocalBuffer.numFrames; i=i+512){
// convert vocal int to double
double (*vocalData)[2] = malloc(2 * 512 * sizeof(double));
for (x = 0; x < 512; x++){
*vocalData[x] = (double)vocalBuffer.buffer[i+x];
}
// init malloc output double
double (*outPutDoubleFFTData)[2];
outPutDoubleFFTData = malloc(2 * 512 * sizeof(double));
fft(512, vocalData, outPutDoubleFFTData);
int z;
// init malloc float fft data
float *floatFFTData;
floatFFTData = malloc(512 * sizeof(float));
for (z = 0; z < 512; z++){
floatFFTData[z] = (float)*outPutDoubleFFTData[z];
if (i==512*20) {
// NSLog(@"PRE POST %f - %f",*vocalData[z], floatFFTData[z]);
NSLog(@"PRE POST FLOAT %f - %f - %f",*vocalData[z], (*outPutDoubleFFTData)[z], floatFFTData[z]);
}
}
onset = onsetsds_process(ods, floatFFTData);
free((*outPutDoubleFFTData));
free(floatFFTData);
free(vocalData);
if (onset){
printf("onset --> %i\n", i);
NSNumber *integer = [[NSNumber alloc] initWithInt:i];
[mutableArrayOfFlags addObject:integer];
}
}
free(ods->data); // Or free(odsdata), they point to the same thing in this case
free(ods);
return [[NSArray alloc] initWithArray:mutableArrayOfFlags];
}
调用第一次方法的日志:
2012-10-20 11:22:19.625 XX[4125:1903] PRE POST FLOAT 4.000000 - 7979.000000 - 7979.000000
2012-10-20 11:22:19.628 XX[4125:1903] PRE POST FLOAT 25.000000 - 0.000000 - 861.794861
2012-10-20 11:22:19.635 XX[4125:1903] PRE POST FLOAT 32.000000 - 861.794875 - 248.516144
2012-10-20 11:22:19.640 XX[4125:1903] PRE POST FLOAT 22.000000 - 92.284860 - -190.525833
2012-10-20 11:22:19.645 XX[4125:1903] PRE POST FLOAT 23.000000 - 248.516141 - 37.045593
2012-10-20 11:22:19.648 XX[4125:1903] PRE POST FLOAT 30.000000 - -33.565115 - 7.444437
调用第二次消息的日志。
2012-10-20 11:22:36.353 XX[4125:3e07] PRE POST FLOAT 4.000000 - 7979.000000 - 7979.000000
2012-10-20 11:22:36.358 XX[4125:3e07] PRE POST FLOAT 25.000000 - 53979063281237364484736793729327605401034441222848177467876829146104162439787488863720409331484927794377967278456986000075570355992521879340404128702782598833969629491268820332191001022225312452183861587484411698307560976546539765760.000000 - inf
2012-10-20 11:22:36.364 XX[4125:3e07] PRE POST FLOAT 32.000000 -