200 lines
7.1 KiB
Swift
200 lines
7.1 KiB
Swift
// SpectralPhaseShiftTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Testing
|
|
import Foundation
|
|
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
|
|
|
@Suite("SpectralPhaseShift")
|
|
struct SpectralPhaseShiftTests {
|
|
|
|
private static let phaseTolerance: Float = 1e-5
|
|
|
|
/// Smallest signed angular distance from a to b, accounting for ±2π wrap.
|
|
private static func angularDelta(_ a: Float, _ b: Float) -> Float {
|
|
let twoPi: Float = 2 * Float.pi
|
|
var d: Float = a - b
|
|
if d > Float.pi { d -= twoPi }
|
|
else if d < -Float.pi { d += twoPi }
|
|
return d
|
|
}
|
|
|
|
private static func fillBinsWithPhases(_ buf: SpectralBuffer, basePhase: Float = 0.3) {
|
|
buf.dcValue = 1.5
|
|
buf.nyquistValue = -0.7
|
|
let halfSize: Int = buf.fftSize / 2
|
|
for k: Int in 1..<halfSize {
|
|
buf[magnitude: k] = Float(k)
|
|
buf[phase: k] = basePhase + Float(k) * 0.01
|
|
}
|
|
}
|
|
|
|
// MARK: - .deg90
|
|
|
|
@Test func deg90AddsHalfPiToEveryBinPhase() {
|
|
let fftSize: Int = 32
|
|
let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
|
|
Self.fillBinsWithPhases(buf)
|
|
let halfSize: Int = fftSize / 2
|
|
var pre: [Float] = []
|
|
for k: Int in 1..<halfSize { pre.append(buf[phase: k]) }
|
|
|
|
let p: SpectralPhaseShift = SpectralPhaseShift(fftSize: fftSize, shift: .deg90)
|
|
p.process(spectral: buf, engine: makeTestEngine())
|
|
|
|
let halfPi: Float = Float(Double.pi * 0.5)
|
|
for k: Int in 1..<halfSize {
|
|
let delta: Float = Self.angularDelta(buf[phase: k], pre[k - 1])
|
|
#expect(abs(delta - halfPi) < Self.phaseTolerance,
|
|
"bin \(k) phase delta \(delta) ≠ π/2")
|
|
}
|
|
// DC/Nyquist not flipped on 90°.
|
|
#expect(buf.dcValue == 1.5)
|
|
#expect(buf.nyquistValue == -0.7)
|
|
}
|
|
|
|
// MARK: - .deg180
|
|
|
|
@Test func deg180FlipsSignOfDCAndNyquistAndAddsPiToBins() {
|
|
let fftSize: Int = 32
|
|
let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
|
|
Self.fillBinsWithPhases(buf)
|
|
let halfSize: Int = fftSize / 2
|
|
var pre: [Float] = []
|
|
for k: Int in 1..<halfSize { pre.append(buf[phase: k]) }
|
|
|
|
let p: SpectralPhaseShift = SpectralPhaseShift(fftSize: fftSize, shift: .deg180)
|
|
p.process(spectral: buf, engine: makeTestEngine())
|
|
|
|
let pi: Float = Float(Double.pi)
|
|
for k: Int in 1..<halfSize {
|
|
let delta: Float = Self.angularDelta(buf[phase: k], pre[k - 1])
|
|
#expect(abs(abs(delta) - pi) < Self.phaseTolerance,
|
|
"bin \(k) phase delta \(delta) magnitude not π")
|
|
}
|
|
#expect(buf.dcValue == -1.5)
|
|
#expect(buf.nyquistValue == 0.7)
|
|
}
|
|
|
|
// MARK: - .deg270
|
|
|
|
@Test func deg270FourTimesEqualsIdentity() {
|
|
let fftSize: Int = 32
|
|
let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
|
|
Self.fillBinsWithPhases(buf)
|
|
let halfSize: Int = fftSize / 2
|
|
var pre: [Float] = []
|
|
for k: Int in 1..<halfSize { pre.append(buf[phase: k]) }
|
|
|
|
let p: SpectralPhaseShift = SpectralPhaseShift(fftSize: fftSize, shift: .deg270)
|
|
for _ in 0..<4 {
|
|
p.process(spectral: buf, engine: makeTestEngine())
|
|
}
|
|
|
|
for k: Int in 1..<halfSize {
|
|
let delta: Float = Self.angularDelta(buf[phase: k], pre[k - 1])
|
|
#expect(abs(delta) < 1e-4,
|
|
"bin \(k) round-trip residual \(delta) too large")
|
|
}
|
|
}
|
|
|
|
// MARK: - .radians arbitrary
|
|
|
|
@Test func arbitraryRadiansAddsExactOffset() {
|
|
let fftSize: Int = 32
|
|
let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
|
|
Self.fillBinsWithPhases(buf)
|
|
let halfSize: Int = fftSize / 2
|
|
var pre: [Float] = []
|
|
for k: Int in 1..<halfSize { pre.append(buf[phase: k]) }
|
|
|
|
let offset: Double = 1.234
|
|
let p: SpectralPhaseShift = SpectralPhaseShift(fftSize: fftSize, shift: .radians(offset))
|
|
p.process(spectral: buf, engine: makeTestEngine())
|
|
|
|
let target: Float = Float(offset)
|
|
for k: Int in 1..<halfSize {
|
|
let delta: Float = Self.angularDelta(buf[phase: k], pre[k - 1])
|
|
#expect(abs(delta - target) < Self.phaseTolerance,
|
|
"bin \(k) delta \(delta) ≠ \(target)")
|
|
}
|
|
}
|
|
|
|
// MARK: - Magnitude untouched
|
|
|
|
@Test func magnitudeUnchangedAcrossShifts() {
|
|
let fftSize: Int = 32
|
|
let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
|
|
Self.fillBinsWithPhases(buf)
|
|
let halfSize: Int = fftSize / 2
|
|
|
|
let p: SpectralPhaseShift = SpectralPhaseShift(fftSize: fftSize, shift: .deg90)
|
|
p.process(spectral: buf, engine: makeTestEngine())
|
|
|
|
for k: Int in 1..<halfSize {
|
|
#expect(buf[magnitude: k] == Float(k), "bin \(k) magnitude altered")
|
|
}
|
|
}
|
|
|
|
// MARK: - Live radians update
|
|
|
|
@Test func liveRadiansUpdateAffectsNextHop() {
|
|
let fftSize: Int = 32
|
|
let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
|
|
Self.fillBinsWithPhases(buf)
|
|
let phaseBefore: Float = buf[phase: 5]
|
|
|
|
let p: SpectralPhaseShift = SpectralPhaseShift(fftSize: fftSize, shift: .radians(0))
|
|
p.process(spectral: buf, engine: makeTestEngine())
|
|
// No change with offset 0 (within wrap precision).
|
|
#expect(abs(Self.angularDelta(buf[phase: 5], phaseBefore)) < Self.phaseTolerance)
|
|
|
|
p.radians = 0.5
|
|
p.process(spectral: buf, engine: makeTestEngine())
|
|
let delta: Float = Self.angularDelta(buf[phase: 5], phaseBefore)
|
|
#expect(abs(delta - 0.5) < Self.phaseTolerance,
|
|
"expected delta 0.5, got \(delta)")
|
|
}
|
|
|
|
// MARK: - Out-of-range radians normalised
|
|
|
|
@Test func radiansBeyond2PiNormaliseCorrectly() {
|
|
let fftSize: Int = 32
|
|
let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
|
|
Self.fillBinsWithPhases(buf)
|
|
let halfSize: Int = fftSize / 2
|
|
var pre: [Float] = []
|
|
for k: Int in 1..<halfSize { pre.append(buf[phase: k]) }
|
|
|
|
// 5π/2 normalised is π/2.
|
|
let off: Double = Double.pi * 2.5
|
|
let p: SpectralPhaseShift = SpectralPhaseShift(fftSize: fftSize, shift: .radians(off))
|
|
p.process(spectral: buf, engine: makeTestEngine())
|
|
|
|
let target: Float = Float(Double.pi * 0.5)
|
|
for k: Int in 1..<halfSize {
|
|
let d: Float = Self.angularDelta(buf[phase: k], pre[k - 1])
|
|
#expect(abs(d - target) < Self.phaseTolerance,
|
|
"bin \(k) delta \(d) ≠ π/2 (after normalise)")
|
|
}
|
|
}
|
|
|
|
// MARK: - Invalid buffer
|
|
|
|
@Test func invalidBufferIsBypassed() {
|
|
let fftSize: Int = 32
|
|
let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
|
|
Self.fillBinsWithPhases(buf)
|
|
let phaseBefore: Float = buf[phase: 5]
|
|
buf.invalidate()
|
|
|
|
let p: SpectralPhaseShift = SpectralPhaseShift(fftSize: fftSize, shift: .deg90)
|
|
p.process(spectral: buf, engine: makeTestEngine())
|
|
|
|
#expect(buf[phase: 5] == phaseBefore)
|
|
}
|
|
}
|