Files
Lyrebird/Tests/LyrebirdTests/HalfbandFIRTests.swift
T
2026-06-01 18:27:40 -07:00

49 lines
1.6 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// HalfbandFIRTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
import Foundation
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
@Suite("HalfbandFIR coefficients")
struct HalfbandFIRTests {
@Test("Length and counts are halfband Type-1 (4k+3) with k=5")
func lengthAndCounts() {
#expect(HalfbandFIR.length == 23)
#expect(HalfbandFIR.foldedCount == 6)
#expect(HalfbandFIR.groupDelayUpsampled == 11)
#expect(HalfbandFIR.groupDelayInputRate == 5)
}
@Test("Center tap held at exactly 0.5")
func centerTapHeldAtOneHalf() {
#expect(HalfbandFIR.centerCoef == 0.5)
}
@Test("Folded coefficient table size matches foldedCount")
func foldedTableSize() {
#expect(HalfbandFIR.foldedCoefs.count == HalfbandFIR.foldedCount)
}
@Test("Folded coefficients are normalised so total FIR DC gain = 1.0 exactly")
func dcGainIsExactlyOne() {
let sumOdd: Double = HalfbandFIR.foldedCoefs.reduce(0.0, +)
let totalDCGain: Double = HalfbandFIR.centerCoef + 2.0 * sumOdd
#expect(abs(totalDCGain - 1.0) < 1e-12)
}
@Test("Folded coefficients alternate sign as expected for halfband sinc lowpass")
func signsAlternate() {
// Hamming-windowed sinc with cutoff π/2 has alternating-sign odd taps.
// Folded ordering (i = 0 → ±11, i = 5 → ±1) → signs −, +, −, +, −, +.
let signs: [Double] = HalfbandFIR.foldedCoefs.map {
$0 > 0 ? 1.0 : ($0 < 0 ? -1.0 : 0.0)
}
#expect(signs == [-1.0, 1.0, -1.0, 1.0, -1.0, 1.0])
}
}