Repository Public
1// ORIX DJ2// File: Packages/ORIXDJCore/Sources/ORIXAnalysis/FFT.swift3// Description: Real FFT helper on top of vDSP (magnitude spectra and autocorrelation) used by every analysis stage.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import Accelerate89/// Power-of-two real FFT. Not thread-safe: create one instance per analysis pass.10final class RealFFT {11 let size: Int12 let half: Int13 private let log2n: vDSP_Length14 private let setup: FFTSetup15 private var re: [Float]16 private var im: [Float]1718 init(size: Int) {19 precondition(size >= 8 && size & (size - 1) == 0, "FFT size must be a power of two")20 self.size = size21 self.half = size / 222 self.log2n = vDSP_Length(size.trailingZeroBitCount)23 self.setup = vDSP_create_fftsetup(log2n, FFTRadix(kFFTRadix2))!24 self.re = [Float](repeating: 0, count: size / 2)25 self.im = [Float](repeating: 0, count: size / 2)26 }2728 deinit { vDSP_destroy_fftsetup(setup) }2930 /// |X[k]| for k in 0..<size/2 (mathematical DFT magnitude, no normalisation). `input` holds `size` samples.31 func magnitudes(_ input: UnsafePointer<Float>, into out: UnsafeMutablePointer<Float>) {32 let half = self.half33 re.withUnsafeMutableBufferPointer { r in34 im.withUnsafeMutableBufferPointer { i in35 var split = DSPSplitComplex(realp: r.baseAddress!, imagp: i.baseAddress!)36 input.withMemoryRebound(to: DSPComplex.self, capacity: half) {37 vDSP_ctoz($0, 2, &split, 1, vDSP_Length(half))38 }39 vDSP_fft_zrip(setup, &split, 1, log2n, FFTDirection(FFT_FORWARD))40 i[0] = 0 // packed Nyquist term41 vDSP_zvabs(&split, 1, out, 1, vDSP_Length(half))42 var scale: Float = 0.543 vDSP_vsmul(out, 1, &scale, out, 1, vDSP_Length(half))44 }45 }46 }4748 /// Circular autocorrelation of `size` samples (zero-pad the input to avoid wrap-around). Arbitrary positive scale.49 func autocorrelation(_ input: UnsafePointer<Float>, into out: UnsafeMutablePointer<Float>) {50 let half = self.half51 var power = [Float](repeating: 0, count: half)52 re.withUnsafeMutableBufferPointer { r in53 im.withUnsafeMutableBufferPointer { i in54 var split = DSPSplitComplex(realp: r.baseAddress!, imagp: i.baseAddress!)55 input.withMemoryRebound(to: DSPComplex.self, capacity: half) {56 vDSP_ctoz($0, 2, &split, 1, vDSP_Length(half))57 }58 vDSP_fft_zrip(setup, &split, 1, log2n, FFTDirection(FFT_FORWARD))59 let dc = r[0], nyquist = i[0]60 vDSP_zvmags(&split, 1, &power, 1, vDSP_Length(half))61 for k in 0..<half { r[k] = power[k]; i[k] = 0 }62 r[0] = dc * dc63 i[0] = nyquist * nyquist64 vDSP_fft_zrip(setup, &split, 1, log2n, FFTDirection(FFT_INVERSE))65 out.withMemoryRebound(to: DSPComplex.self, capacity: half) {66 vDSP_ztoc(&split, 1, $0, 2, vDSP_Length(half))67 }68 }69 }70 }71}7273@inline(__always) func nextPowerOfTwo(_ n: Int) -> Int {74 var p = 175 while p < n { p <<= 1 }76 return p77}7879/// Linear interpolation into an array at a fractional index (clamped).80@inline(__always) func interpolate(_ a: [Float], _ x: Double) -> Float {81 if a.isEmpty { return 0 }82 if x <= 0 { return a[0] }83 let last = a.count - 184 if x >= Double(last) { return a[last] }85 let i = Int(x)86 let f = Float(x - Double(i))87 return a[i] + (a[i + 1] - a[i]) * f88}8990/// Sub-sample peak offset (-0.5...0.5) from three neighbouring values around a maximum.91@inline(__always) func parabolicOffset(_ a: Double, _ b: Double, _ c: Double) -> Double {92 let d = a - 2 * b + c93 if d >= -1e-12 { return 0 }94 return max(-0.5, min(0.5, 0.5 * (a - c) / d))95}96