我正在使用Accelerate框架执行快速傅里叶变换(FFT),并尝试找到一种方法来创建一个长度为1024的缓冲区以供使用。我可以访问要进行FFT处理的信号的平均峰值和峰值。请问有人能帮助我或给我一些提示吗?
苹果公司在其vDSP编程指南中提供了一些设置FFT的示例。您还应该查看vDSP示例应用程序。虽然代码是针对Mac的,但这些代码也可以直接移植到iOS上。
最近我需要对一个包含64个整数输入波形进行简单的FFT,我使用了以下代码:
static FFTSetupD fft_weights;
static DSPDoubleSplitComplex input;
static double *magnitudes;
+ (void)initialize
{
/* Setup weights (twiddle factors) */
fft_weights = vDSP_create_fftsetupD(6, kFFTRadix2);
/* Allocate memory to store split-complex input and output data */
input.realp = (double *)malloc(64 * sizeof(double));
input.imagp = (double *)malloc(64 * sizeof(double));
magnitudes = (double *)malloc(64 * sizeof(double));
}
- (CGFloat)performAcceleratedFastFourierTransformAndReturnMaximumAmplitudeForArray:(NSUInteger *)waveformArray;
{
for (NSUInteger currentInputSampleIndex = 0; currentInputSampleIndex < 64; currentInputSampleIndex++)
{
input.realp[currentInputSampleIndex] = (double)waveformArray[currentInputSampleIndex];
input.imagp[currentInputSampleIndex] = 0.0f;
}
/* 1D in-place complex FFT */
vDSP_fft_zipD(fft_weights, &input, 1, 6, FFT_FORWARD);
input.realp[0] = 0.0;
input.imagp[0] = 0.0;
// Get magnitudes
vDSP_zvmagsD(&input, 1, magnitudes, 1, 64);
// Extract the maximum value and its index
double fftMax = 0.0;
vDSP_maxmgvD(magnitudes, 1, &fftMax, 64);
return sqrt(fftMax);
}
正如您所看到的,在这个FFT中,我仅使用了实际值来设置输入缓冲区,执行FFT,然后读取幅度。