编写目标c(具有c低级功能)功能来检测音高。在“w [i] = i;”行上获得EXC_BAD_ACCESS代码= 2在第一个for循环中。在我们的迭代中,numberofSamples = 32768.在i = 16332之后的第一个循环中出现错误。由于某种原因填充了大约一半的数组后,指针不再有效。单线程应用程序,没有别的东西应该在运行时乱搞东西。我想知道这个设置中是否存在任何可能导致此指针错误的基本错误。我可以按要求提供更多细节。谢谢!
NSArray* getMainFrequency(NSMutableArray *input,int numberOfSamples){
const int log2n = log(numberOfSamples)/log(2);
const int n = 1 << log2n;
const int nOver2 = n / 2;
int sFreq=44100;
double fFreq=0,maxMag=0,realFreq=0,t,alpha=0.6;
double mag[n/2+1],dblInput[numberOfSamples],w[numberOfSamples];
FFTSetupD fftSetup = vDSP_create_fftsetupD (log2n, kFFTRadix2);
for(int i=0;i<numberOfSamples;i++){
dblInput[i]=[input[i] doubleValue];
w[i] = i;
}
DSPDoubleSplitComplex fft_data;
fft_data.realp = malloc(nOver2 * sizeof(double));
fft_data.imagp = malloc(nOver2 * sizeof(double));
vDSP_ctozD((DSPDoubleComplex *)dblInput, 2, &fft_data, 1, nOver2);
vDSP_fft_zripD (fftSetup, &fft_data, 1, log2n, kFFTDirection_Forward);
for (int i = 0; i < nOver2; ++i){
fft_data.realp[i] *= 0.5;
fft_data.imagp[i] *= 0.5;
}
for (int i = 48; i < 2974; ++i){
mag[i]=pow((pow(fft_data.realp[i],2)+pow(fft_data.imagp[i],2)),0.5);
}
for(int i=48;i <= 2974;i++){ //to get a max of 4000 hZ hopefully?
if (mag[i]>maxMag){
maxMag = mag[i];
fFreq = i;
}
}
realFreq=fFreq/(numberOfSamples/2+1)*sFreq/2;
return [NSArray arrayWithObjects: @(maxMag),@(realFreq),nil];
}
}