137 lines
5.5 KiB
Swift
137 lines
5.5 KiB
Swift
// HalfbandRoundTripTests.swift
|
||
// Lyrebird
|
||
//
|
||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||
// SPDX-License-Identifier: MIT
|
||
|
||
import Testing
|
||
import Foundation
|
||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||
|
||
@MainActor
|
||
@Suite("Halfband oversampling round-trip")
|
||
struct HalfbandRoundTripTests {
|
||
|
||
@Test("2× up → 2× down preserves DC at unity gain")
|
||
func roundTripDC2x() {
|
||
var up: HalfbandUpsampler2x = HalfbandUpsampler2x()
|
||
var dn: HalfbandDownsampler2x = HalfbandDownsampler2x()
|
||
var last: Double = 0.0
|
||
for _ in 0..<128 {
|
||
let (a, b) = up.process(1.0)
|
||
last = dn.process(a, b)
|
||
}
|
||
#expect(abs(last - 1.0) < 1e-9)
|
||
}
|
||
|
||
@Test("2× up → 2× down preserves a low-frequency sine RMS within tight tolerance")
|
||
func roundTripSineRMS2x() {
|
||
var up: HalfbandUpsampler2x = HalfbandUpsampler2x()
|
||
var dn: HalfbandDownsampler2x = HalfbandDownsampler2x()
|
||
|
||
let length: Int = 4096
|
||
let warmup: Int = 128
|
||
let normalisedFreq: Double = 1.0 / 32.0 // cycles per input sample, well below Fs/4
|
||
let twoPi: Double = 2.0 * .pi
|
||
|
||
var inputs: [Double] = [Double](repeating: 0.0, count: length)
|
||
var outputs: [Double] = [Double](repeating: 0.0, count: length)
|
||
for n: Int in 0..<length {
|
||
let x: Double = sin(twoPi * normalisedFreq * Double(n))
|
||
inputs[n] = x
|
||
let (a, b) = up.process(x)
|
||
outputs[n] = dn.process(a, b)
|
||
}
|
||
let inputRMS: Double = rmsValue(Array(inputs[warmup..<length]))
|
||
let outputRMS: Double = rmsValue(Array(outputs[warmup..<length]))
|
||
// Tolerance allows for normal Hamming-window passband ripple of the
|
||
// 23-tap halfband (≈ ±0.1 dB ≈ ±1.2 % at the working rate).
|
||
#expect(abs(outputRMS / inputRMS - 1.0) < 1.5e-2)
|
||
}
|
||
|
||
@Test("4× up → 4× down preserves DC at unity gain")
|
||
func roundTripDC4x() {
|
||
var up: HalfbandUpsampler4x = HalfbandUpsampler4x()
|
||
var dn: HalfbandDownsampler4x = HalfbandDownsampler4x()
|
||
var last: Double = 0.0
|
||
for _ in 0..<256 {
|
||
let (a, b, c, d) = up.process(1.0)
|
||
last = dn.process(a, b, c, d)
|
||
}
|
||
#expect(abs(last - 1.0) < 1e-9)
|
||
}
|
||
|
||
@Test("4× up → 4× down preserves a low-frequency sine RMS within tight tolerance")
|
||
func roundTripSineRMS4x() {
|
||
var up: HalfbandUpsampler4x = HalfbandUpsampler4x()
|
||
var dn: HalfbandDownsampler4x = HalfbandDownsampler4x()
|
||
|
||
let length: Int = 8192
|
||
let warmup: Int = 256
|
||
let normalisedFreq: Double = 1.0 / 64.0
|
||
let twoPi: Double = 2.0 * .pi
|
||
|
||
var inputs: [Double] = [Double](repeating: 0.0, count: length)
|
||
var outputs: [Double] = [Double](repeating: 0.0, count: length)
|
||
for n: Int in 0..<length {
|
||
let x: Double = sin(twoPi * normalisedFreq * Double(n))
|
||
inputs[n] = x
|
||
let (a, b, c, d) = up.process(x)
|
||
outputs[n] = dn.process(a, b, c, d)
|
||
}
|
||
let inputRMS: Double = rmsValue(Array(inputs[warmup..<length]))
|
||
let outputRMS: Double = rmsValue(Array(outputs[warmup..<length]))
|
||
// Tolerance allows for normal Hamming-window passband ripple of the
|
||
// 23-tap halfband (≈ ±0.1 dB ≈ ±1.2 % at the working rate).
|
||
#expect(abs(outputRMS / inputRMS - 1.0) < 1.5e-2)
|
||
}
|
||
|
||
@Test("Image rejection — synthetic 2× upsampled aliasing tone is suppressed by the downsampler")
|
||
func imageRejectionDownsample2x() {
|
||
var dn: HalfbandDownsampler2x = HalfbandDownsampler2x()
|
||
// Feed the downsampler a sine at 0.45 cycles per upsampled sample
|
||
// (i.e. 0.9 · Fs at the input rate of 2 · Fs). This is well into the
|
||
// halfband stopband (>0.25 of working rate), simulating a typical
|
||
// image generated by a non-bandlimited 2× zero-stuff or by a
|
||
// nonlinearity inserted between up and down stages. The downsampler
|
||
// alone must attenuate it heavily.
|
||
let length: Int = 4096
|
||
let warmup: Int = 256
|
||
let normalisedFreq: Double = 0.45 // cycles per upsampled-rate sample
|
||
let twoPi: Double = 2.0 * .pi
|
||
|
||
var inputs: [Double] = [Double](repeating: 0.0, count: length)
|
||
var outputs: [Double] = [Double](repeating: 0.0, count: length / 2)
|
||
for n: Int in 0..<length {
|
||
inputs[n] = sin(twoPi * normalisedFreq * Double(n))
|
||
}
|
||
for n: Int in 0..<(length / 2) {
|
||
outputs[n] = dn.process(inputs[2 * n], inputs[2 * n + 1])
|
||
}
|
||
let inputRMS: Double = rmsValue(Array(inputs[warmup..<length]))
|
||
let outputRMS: Double = rmsValue(Array(outputs[(warmup / 2)..<(length / 2)]))
|
||
// 23-tap Hamming-windowed halfband: ~−40 dB stopband. Single
|
||
// application → output RMS at most a few percent of input RMS.
|
||
#expect(outputRMS / inputRMS < 0.1)
|
||
}
|
||
|
||
@Test("Reset on 4× cascade clears all stages")
|
||
func reset4xClearsAllStages() {
|
||
var up: HalfbandUpsampler4x = HalfbandUpsampler4x()
|
||
var dn: HalfbandDownsampler4x = HalfbandDownsampler4x()
|
||
for _ in 0..<64 {
|
||
let (a, b, c, d) = up.process(1.0)
|
||
_ = dn.process(a, b, c, d)
|
||
}
|
||
up.reset()
|
||
dn.reset()
|
||
let (a, b, c, d) = up.process(0.0)
|
||
#expect(a == 0.0)
|
||
#expect(b == 0.0)
|
||
#expect(c == 0.0)
|
||
#expect(d == 0.0)
|
||
let y: Double = dn.process(0.0, 0.0, 0.0, 0.0)
|
||
#expect(y == 0.0)
|
||
}
|
||
}
|