ios - 将实时 FFT 设置传递给执行函数
<p><p>我正在尝试使用 Novacaine 对 iOS 中的麦克风数据进行实时 FFT,但我似乎无法将 FFT 设置和环形缓冲区传递给我的 FFT 函数。 </p>
<p>为了测试,我的计划是在 viewWillAppear 方法中进行设置,然后在按下按钮时执行我的 FFT 函数。然后,我将为我的缓冲区释放内存并在窗口关闭时销毁 FFTsetup。 </p>
<p>这是我目前所拥有的。我尝试了几种将参数传递给 viewWillAppear 的变体,但似乎没有任何效果。欢迎提出任何建议。</p>
<pre><code>- (void)viewWillAppear:(BOOL)animated
{
;
ringBuffer = new RingBuffer(8192, 2);
audioManager = ;
[audioManager setInputBlock:^(float *data, UInt32 numFrames, UInt32 numChannels) {
// Setup FFT here
int numSamples = 8192;
// Setup the length
vDSP_Length log2n = log2f(numSamples);
// Calculate the weights array. This is a one-off operation.
FFTSetup fftSetup = vDSP_create_fftsetup(log2n, FFT_RADIX2);
// For an FFT, numSamples must be a power of 2, i.e. is always even
int nOver2 = numSamples/2;
// Populate *window with the values for a hamming window function
float *window = (float *)malloc(sizeof(float) * numSamples);
vDSP_hamm_window(window, numSamples, 0);
// Window the samples
vDSP_vmul(data, 1, window, 1, data, 1, numSamples);
// Define complex buffer
COMPLEX_SPLIT A;
A.realp = (float *) malloc(nOver2*sizeof(float));
A.imagp = (float *) malloc(nOver2*sizeof(float));
}];
}
- (IBAction)buttonPressed:(id)sender {
// do myFFT here
// Pack samples:
// C(re) -> A, C(im) -> A
vDSP_ctoz((COMPLEX*)data, 2, &A, 1, numSamples/2);
//Perform a forward FFT using fftSetup and A
//Results are returned in A
vDSP_fft_zrip(fftSetup, &A, 1, log2n, FFT_FORWARD);
//Convert COMPLEX_SPLIT A result to magnitudes
float *amp = (float *)malloc(sizeof(float) * numSamples);
amp = A.realp/(numSamples*2);
float max = 0;
int indexOfMax = -1;
for(int i=1; i<numSamples; i++) {
amp=A.realp*A.realp+A.imagp*A.imagp;
//printf("i[%ld]: %.1f %ldHz \n", (long)i, amp, (long)22000 * i/numSamples);
if(amp > max) {
max = amp;
indexOfMax = i;
}
}
long fmax = ((long)indexOfMax - numSamples/2)*44100/4096;
printf("max frequency is %ld\n", fmax);
free(amp);
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>实时音频输入模块不用于执行任何繁重的计算,例如 FFT。相反,输入 block 应该(快速!)将数据复制到足够大的环形缓冲区中。</p>
<p>稍后,在 displayLink 计时器期间或按下按钮时,您可以检查环形缓冲区以查看是否有足够的新数据,然后执行 FFT。</p>
<p>您的代码似乎还将 FFT 大小 numSamples 与回调接收到的(通常小得多的)实际样本数量(即 numFrames)混淆了。在为 numFrames 调用了足够多的回调以求和等于或大于 FFT 大小之前,您无法执行 FFT。</p></p>
<p style="font-size: 20px;">关于ios - 将实时 FFT 设置传递给执行函数,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/28758626/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/28758626/
</a>
</p>
页:
[1]