我需要你的帮助。想在我的音频文件上使用 FFT。我想将我的音频文件切割成更小的缓冲区数组,并将我的 FFT 与所有子缓冲区一起使用。
为什么 ??因为我需要知道并查看(使用绘图数据)我的频率如何具有特殊性。我想知道,我的音频文件中的噪音是如何开始的。
那是我的 FFT 代码。我不知道我做错了什么。
谢谢你的帮助
编辑代码
func FFT (buffer: AVAudioPCMBuffer){
let frameCount = buffer.frameCapacity
let log2n = UInt(round(log2(Double(frameCount))))
print (" log2n \(log2n)");
let bufferSizePOT = Int(1 << log2n)
print (" bufferSizePot \(bufferSizePOT)");
let inputCount = bufferSizePOT / 2
let fftSetup = vDSP_create_fftsetup(log2n, Int32(kFFTRadix2))
var realp = [Float](repeating: 0, count: inputCount)
var imagp = [Float](repeating: 0, count: inputCount)
var output = DSPSplitComplex(realp: &realp, imagp: &imagp)
let windowSize = bufferSizePOT
var transferBuffer = [Float](repeating: 0, count: windowSize)
var window = [Float](repeating: 0, count: windowSize)
vDSP_hann_window(&window, vDSP_Length(windowSize), Int32(vDSP_HANN_NORM))
vDSP_vmul((buffer.floatChannelData?.pointee)!, 1, window,
1, &transferBuffer, 1, vDSP_Length(windowSize))
let temp = UnsafePointer<Float>(transferBuffer)
temp.withMemoryRebound(to: DSPComplex.self, capacity: transferBuffer.count) { (typeConvertedTransferBuffer) -> Void in
vDSP_ctoz(typeConvertedTransferBuffer, 2, &output, 1, vDSP_Length(inputCount))
}
// Do the fast Fourier forward transform, packed input to packed output
vDSP_fft_zrip(fftSetup!, &output, 1, log2n, FFTDirection(FFT_FORWARD))
//---------------------------------------------------
var magnitudes = [Float](repeating: 0.0, count: inputCount)
vDSP_zvmags(&output, 1, &magnitudes, 1, vDSP_Length(inputCount))
var normalizedMagnitudes = [Float](repeating: 0.0, count: inputCount)
vDSP_vsmul(sqrt(x: magnitudes), 1, [2.0 / Float(inputCount)],
&normalizedMagnitudes, 1, vDSP_Length(inputCount))
for f in 0..<normalizedMagnitudes.count
{
print("\(f), \(normalizedMagnitudes[f])")
}
vDSP_destroy_fftsetup(fftSetup)
}