Files
Lyrebird/Tests/LyrebirdTests/HalfbandUpsampler2xTests.swift
T

89 lines
3.2 KiB
Swift
Raw Permalink 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.
// HalfbandUpsampler2xTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
import Foundation
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
@MainActor
@Suite("HalfbandUpsampler2x")
struct HalfbandUpsampler2xTests {
@Test("DC input becomes DC output after group delay (gain ≈ 1.0)")
func dcGainIsUnity() {
var up: HalfbandUpsampler2x = HalfbandUpsampler2x()
let warmupInputs: Int = HalfbandFIR.length
let measureInputs: Int = 64
var lastEven: Double = 0.0
var lastOdd: Double = 0.0
for _ in 0..<warmupInputs {
(lastEven, lastOdd) = up.process(1.0)
}
// After warmup the FIR is fully primed.
for _ in 0..<measureInputs {
(lastEven, lastOdd) = up.process(1.0)
}
// Even output = full FIR sum at DC = 2 × (sum_odd_folded × 2) = 1.0
// (DC gain of the FIR is exactly 1.0 by construction; ×2 zero-stuff
// gain compensates for the 0.5 average of the zero-stuffed signal).
#expect(abs(lastEven - 1.0) < 1e-9)
// Odd output = exact passthrough of x[n - 5] = 1.0.
#expect(abs(lastOdd - 1.0) < 1e-12)
}
@Test("Odd output is exact x[n - 5] passthrough")
func oddOutputIsExactPassthrough() {
var up: HalfbandUpsampler2x = HalfbandUpsampler2x()
// Push a known sequence and check the odd output exactly tracks x[n - 5].
let inputs: [Double] = (0..<32).map { Double($0) * 0.123 - 0.5 }
var oddOuts: [Double] = []
for x: Double in inputs {
let (_, yOdd) = up.process(x)
oddOuts.append(yOdd)
}
// For n >= 5: oddOuts[n] should equal inputs[n - 5] exactly.
for n: Int in 5..<inputs.count {
#expect(oddOuts[n] == inputs[n - HalfbandFIR.groupDelayInputRate])
}
}
@Test("Reset clears state to zero")
func resetClearsState() {
var up: HalfbandUpsampler2x = HalfbandUpsampler2x()
for _ in 0..<HalfbandFIR.length {
_ = up.process(1.0)
}
up.reset()
let (yEven, yOdd) = up.process(0.0)
#expect(yEven == 0.0)
#expect(yOdd == 0.0)
}
@Test("Impulse response has expected total energy and odd-output spike at delay")
func impulseResponse() {
var up: HalfbandUpsampler2x = HalfbandUpsampler2x()
// Feed a single unit impulse at n = 0, then zeros.
var samples: [Double] = []
let (e0, o0) = up.process(1.0)
samples.append(e0); samples.append(o0)
for _ in 0..<(HalfbandFIR.length + 4) {
let (e, o) = up.process(0.0)
samples.append(e); samples.append(o)
}
// Odd outputs are x[n - 5] passthrough → exactly one nonzero sample
// at output index 2*5 + 1 = 11 (group delay 11 at upsampled rate).
#expect(samples[2 * HalfbandFIR.groupDelayInputRate + 1] == 1.0)
// Other odd outputs must be zero.
var i: Int = 1
while i < samples.count {
if i != 2 * HalfbandFIR.groupDelayInputRate + 1 {
#expect(samples[i] == 0.0)
}
i += 2
}
}
}