gpt4 book ai didi

具有 Accelerate 框架 vDSP 的 iPhone FFT

转载 作者:技术小花猫 更新时间:2023-10-29 11:00:16 24 4
gpt4 key购买 nike

我在使用 vDSP 实现 FFT 时遇到困难。我理解这个理论,但我正在寻找具体的代码示例。

我有一个 wav 文件的数据如下:

问题一、如何将音频数据进行FFT?

问题 2. 如何从 FFT 中获取输出数据?

问题 3。最终目标是检查低频声音。我该怎么做?

-(OSStatus)open:(CFURLRef)inputURL{
OSStatus result = -1;

result = AudioFileOpenURL (inputURL, kAudioFileReadPermission, 0, &mAudioFile);
if (result == noErr) {
//get format info
UInt32 size = sizeof(mASBD);

result = AudioFileGetProperty(mAudioFile, kAudioFilePropertyDataFormat, &size, &mASBD);

UInt32 dataSize = sizeof packetCount;
result = AudioFileGetProperty(mAudioFile, kAudioFilePropertyAudioDataPacketCount, &dataSize, &packetCount);
NSLog([NSString stringWithFormat:@"File Opened, packet Count: %d", packetCount]);

UInt32 packetsRead = packetCount;
UInt32 numBytesRead = -1;
if (packetCount > 0) {
//allocate buffer
audioData = (SInt16*)malloc( 2 *packetCount);
//read the packets
result = AudioFileReadPackets (mAudioFile, false, &numBytesRead, NULL, 0, &packetsRead, audioData);
NSLog([NSString stringWithFormat:@"Read %d bytes, %d packets", numBytesRead, packetsRead]);
}
}
return result;
}

FFT 代码如下:

log2n = N;
n = 1 << log2n;
stride = 1;
nOver2 = n / 2;

printf("1D real FFT of length log2 ( %d ) = %d\n\n", n, log2n);

/* Allocate memory for the input operands and check its availability,
* use the vector version to get 16-byte alignment. */

A.realp = (float *) malloc(nOver2 * sizeof(float));
A.imagp = (float *) malloc(nOver2 * sizeof(float));
originalReal = (float *) malloc(n * sizeof(float));
obtainedReal = (float *) malloc(n * sizeof(float));

if (originalReal == NULL || A.realp == NULL || A.imagp == NULL) {
printf("\nmalloc failed to allocate memory for the real FFT"
"section of the sample.\n");
exit(0);
}

/* Generate an input signal in the real domain. */
for (i = 0; i < n; i++)

originalReal[i] = (float) (i + 1);

/* Look at the real signal as an interleaved complex vector by
* casting it. Then call the transformation function vDSP_ctoz to
* get a split complex vector, which for a real signal, divides into
* an even-odd configuration. */

vDSP_ctoz((COMPLEX *) originalReal, 2, &A, 1, nOver2);

/* Set up the required memory for the FFT routines and check its
* availability. */

setupReal = vDSP_create_fftsetup(log2n, FFT_RADIX2);

if (setupReal == NULL) {

printf("\nFFT_Setup failed to allocate enough memory for"
"the real FFT.\n");

exit(0);
}

/* Carry out a Forward and Inverse FFT transform. */
vDSP_fft_zrip(setupReal, &A, stride, log2n, FFT_FORWARD);
vDSP_fft_zrip(setupReal, &A, stride, log2n, FFT_INVERSE);

/* Verify correctness of the results, but first scale it by 2n. */
scale = (float) 1.0 / (2 * n);
vDSP_vsmul(A.realp, 1, &scale, A.realp, 1, nOver2);
vDSP_vsmul(A.imagp, 1, &scale, A.imagp, 1, nOver2);

/* The output signal is now in a split real form. Use the function
* vDSP_ztoc to get a split real vector. */
vDSP_ztoc(&A, 1, (COMPLEX *) obtainedReal, 2, nOver2);

/* Check for accuracy by looking at the inverse transform results. */
Compare(originalReal, obtainedReal, n);

谢谢

最佳答案

  1. 您将音频样本数据放入输入的实部,并将虚部归零。

  2. 如果您只对频域中每个 bin 的大小感兴趣,则可以为每个输出 bin 计算 sqrt(re*re + im*im)。如果您只对相对 大小感兴趣,那么您可以删除 sqrt 并只计算平方大小,(re*re + im*im)

  3. 您将查看与您感兴趣的一个或多个频率相对应的一个或多个 bin 的大小(请参阅 (2))。如果您的采样率为 Fs,并且您的 FFT 大小为 N,则输出 bin i 的相应频率由 f = i * Fs/N 给出。相反,如果您对特定频率 f 感兴趣,则感兴趣的区间 ii = N * f/Fs 给出。

附加说明:您需要申请一个合适的 window function (例如 Hann aka Hanning )到您的 FFT 输入数据,然后再计算 FFT 本身。

关于具有 Accelerate 框架 vDSP 的 iPhone FFT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6358764/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com