49 lines
1.6 KiB
Swift
49 lines
1.6 KiB
Swift
// 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])
|
||
}
|
||
}
|