0

i need your help. in want to use the FFT on my audio file. i want to cut my audio file in more little buffer array and use my FFT with all sub buffer.

why ?? because i need to know and see (with plot data) how my fréquence have particularity. i want to know, how a noise start in my audio file.

that is my FFT code . i dont know what im doing wrong.

thx for your help

EDITING CODE

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)

}
  • 1
    What is the problem/question? – Ahmed Fasih Jan 26 '17 at 17:00
  • When I use this code, my FFT returns incorrect values to me. By discussing with someone more competent in signal processing, I was advised to decompose my song selong a multiple of the rate sample in order to create pieces of x second in which they will apply the FFT. So if in those if I find a specific curve or closer to the one I want, I might consider that the noise I am looking for in my audio is the. So the number of decomposition will give me time from the beginning before fell on. The place where my timer should start. – Cyril Jeanneret Jan 26 '17 at 17:32

1 Answers1

0

Basically, instead of making

frameCount = UInt32(audioFile.length)

you want a to use a much smaller for frameCount (such as 4096, for roughly 1/10th of a second of audio), and then loop through the audio file, reading and processing each smaller chunk of data of frameCount length (doing shorter windowed FFTs) separately, instead of processing the entire file in one huge FFT.

Note that when looping over the audio file sample data and doing a series of FFTs of the same length, you only need to do the fftSetup once.

If you want, you can vector sum sets of resulting magnitude vectors over a longer time period to reduce the time resolution and length of your printout. This is a form of Welch's method of spectral density estimation.

hotpaw2
  • 70,107
  • 14
  • 90
  • 153
  • thank you so much. i think it's great. i use the AVEngine to apply filter on my audio file and i have one tap on bus to save the audio file when my filter was applied. to save my new file, i pass my mixernode to my buffer with framecount = 4096. if i have correctly understand what do you mean, that might even work no ? i think its ok, because i use 21 buffer for a song with duration of 2 seconde. i have one last question about that. i take de magnitude with my FFT. i dont know what is the index of my normalizedMagnitudes. the value is the magnitude but my index i dont know ... sorry – Cyril Jeanneret Jan 26 '17 at 23:56