32class FIRFilterTest :
public UnitTest
34 template <
typename Type>
37 static void fillRandom (Random& random, Type* buffer,
size_t n)
39 for (
size_t i = 0; i < n; ++i)
40 buffer[i] = (2.0f * random.nextFloat()) - 1.0f;
43 static bool checkArrayIsSimilar (Type* a, Type* b,
size_t n)
noexcept
45 for (
size_t i = 0; i < n; ++i)
46 if (std::abs (a[i] - b[i]) > 1e-6f)
54 template <
typename Type>
55 struct Helpers<SIMDRegister<Type>>
57 static void fillRandom (Random& random, SIMDRegister<Type>* buffer,
size_t n)
62 static bool checkArrayIsSimilar (SIMDRegister<Type>* a, SIMDRegister<Type>* b,
size_t n)
noexcept
64 return Helpers<Type>::checkArrayIsSimilar (
reinterpret_cast<Type*
> (a),
65 reinterpret_cast<Type*
> (b),
71 template <
typename Type>
72 static void fillRandom (Random& random, Type* buffer,
size_t n) { Helpers<Type>::fillRandom (random, buffer, n); }
74 template <
typename Type>
75 static bool checkArrayIsSimilar (Type* a, Type* b,
size_t n)
noexcept {
return Helpers<Type>::checkArrayIsSimilar (a, b, n); }
79 template <
typename SampleType,
typename NumericType>
80 static void reference (
const NumericType* firCoefficients,
size_t numCoefficients,
81 const SampleType* input, SampleType* output,
size_t n)
noexcept
83 if (numCoefficients == 0)
85 zeromem (output,
sizeof (SampleType) * n);
89 HeapBlock<SampleType> scratchBuffer (numCoefficients
97 SampleType* buffer = scratchBuffer.getData();
100 zeromem (buffer,
sizeof (SampleType) * numCoefficients);
102 for (
size_t i = 0; i < n; ++i)
104 for (
size_t j = (numCoefficients - 1); j >= 1; --j)
105 buffer[j] = buffer[j-1];
107 buffer[0] = input[i];
111 for (
size_t j = 0; j < numCoefficients; ++j)
112 sum += buffer[j] * firCoefficients[j];
119 struct LargeBlockTest
121 template <
typename FloatType>
122 static void run (FIR::Filter<FloatType>& filter, FloatType* src, FloatType* dst,
size_t n)
124 AudioBlock<const FloatType> input (&src, 1, n);
125 AudioBlock<FloatType> output (&dst, 1, n);
126 ProcessContextNonReplacing<FloatType> context (input, output);
128 filter.process (context);
132 struct SampleBySampleTest
134 template <
typename FloatType>
135 static void run (FIR::Filter<FloatType>& filter, FloatType* src, FloatType* dst,
size_t n)
137 for (
size_t i = 0; i < n; ++i)
138 dst[i] = filter.processSample (src[i]);
142 struct SplitBlockTest
144 template <
typename FloatType>
145 static void run (FIR::Filter<FloatType>& filter, FloatType* input, FloatType* output,
size_t n)
148 for (
size_t i = 0; i < n; i += len)
150 len = jmin (n - i, n / 3);
151 auto* src = input + i;
152 auto* dst = output + i;
154 AudioBlock<const FloatType> inBlock (&src, 1, len);
155 AudioBlock<FloatType> outBlock (&dst, 1, len);
156 ProcessContextNonReplacing<FloatType> context (inBlock, outBlock);
158 filter.process (context);
164 template <
typename TheTest,
typename SampleType,
typename NumericType>
165 void runTestForType()
167 Random random (8392829);
169 for (
auto size : {1, 2, 4, 8, 12, 13, 25})
171 constexpr size_t n = 813;
173 HeapBlock<char> inputBuffer, outputBuffer, refBuffer;
174 AudioBlock<SampleType> input (inputBuffer, 1, n), output (outputBuffer, 1, n), ref (refBuffer, 1, n);
175 fillRandom (random, input.getChannelPointer (0), n);
177 HeapBlock<char> firBlock;
178 AudioBlock<NumericType> fir (firBlock, 1,
static_cast<size_t> (size));
179 fillRandom (random, fir.getChannelPointer (0),
static_cast<size_t> (size));
181 FIR::Filter<SampleType> filter (*
new FIR::Coefficients<NumericType> (fir.getChannelPointer (0),
static_cast<size_t> (size)));
182 ProcessSpec spec {0.0, n, 1};
183 filter.prepare (spec);
185 reference<SampleType, NumericType> (fir.getChannelPointer (0),
static_cast<size_t> (size),
186 input.getChannelPointer (0), ref.getChannelPointer (0), n);
188 TheTest::template run<SampleType> (filter, input.getChannelPointer (0), output.getChannelPointer (0), n);
189 expect (checkArrayIsSimilar (output.getChannelPointer (0), ref.getChannelPointer (0), n));
193 template <
typename TheTest>
194 void runTestForAllTypes (
const char* unitTestName)
198 runTestForType<TheTest, float, float>();
199 runTestForType<TheTest, double, double>();
201 runTestForType<TheTest, SIMDRegister<float>,
float>();
202 runTestForType<TheTest, SIMDRegister<double>,
double>();
209 :
UnitTest (
"FIR Filter", UnitTestCategories::dsp)
212 void runTest()
override
214 runTestForAllTypes<LargeBlockTest> (
"Large Blocks");
215 runTestForAllTypes<SampleBySampleTest> (
"Sample by Sample");
216 runTestForAllTypes<SplitBlockTest> (
"Split Block");
220static FIRFilterTest firFilterUnitTest;
UnitTest(const String &name, const String &category=String())
void beginTest(const String &testName)
void expect(bool testResult, const String &failureMessage=String())
static ElementType * getNextSIMDAlignedPtr(ElementType *ptr) noexcept
static constexpr size_t SIMDRegisterSize
static constexpr size_t size() noexcept