test: iir filter works but is not exact -- over 0.01 abs error

This commit is contained in:
2026-03-02 22:33:46 +01:00
parent 6ff7db4d93
commit c5acce5699
5 changed files with 100 additions and 7 deletions

View File

@@ -3,6 +3,15 @@
//
#include "iir_filter.h"
#include <iostream>
#define DEBUG_IIR 0
#if (DEBUG_IIR == 1)
#define DEBUG_PRINT(expr) do { expr; } while (0)
#else
#define DEBUG_PRINT(expr) while(0) { expr; }
#endif
Buf::Buf(size_t N): size(N), n(0) {
data.resize(N);
@@ -25,7 +34,7 @@ double Filt::peek() {
for (size_t i = offset; i < this->size; i++) {
//size_t n = (this->n - i + shift - 1) % this->size; // unsigned % size ... bad if u is negative
size_t n = (this->size + this->n - i + shift - 1) % this->size;
//std::cout << " t[" << i << "] * v[" << n << "]" << std::endl;
DEBUG_PRINT(std::cout << " t[" << i << "] * v[" << n << "]" << std::endl);
sum += this->data[n] * this->taps[i];
}
return sum;
@@ -38,13 +47,13 @@ IirFilter::IirFilter(std::vector<double> b, std::vector<double> a) : x(b.size(),
if (b.size() != a.size()) throw std::invalid_argument("b.size() != a.size()");
}
double IirFilter::filter(double val) {
//std::cout << "x.filter(" << val << ")" << std::endl;
DEBUG_PRINT(std::cout << "x.filter(" << val << ")" << std::endl);
double xv = x.filter(val);
//std::cout << "xv=" << xv << std::endl;
//std::cout << "y.peek()" << std::endl;
DEBUG_PRINT(std::cout << "xv=" << xv << std::endl);
DEBUG_PRINT(std::cout << "y.peek()" << std::endl);
double yv = y.peek();
//std::cout << "yv=" << yv << std::endl;
//std::cout << "---" << std::endl;
DEBUG_PRINT(std::cout << "yv=" << yv << std::endl);
DEBUG_PRINT(std::cout << "---" << std::endl);
double yo = xv - yv;
y.push(yo);
return yo;