SigPack - the C++ signal processing library
fir_iir.cpp

Output

Filter coeffs:
-0.0051 0.0134 0.1474 0.3444 0.3444 0.1474 0.0134 -0.0051
Impulse response:
-0.0051 0.0134 0.1474 0.3444 0.3444 0.1474 0.0134 -0.0051 0 0 0
*0 0 0 0
IIR theor. impulse response:
0.2500 0.7250 0.9025 0.8123 0.7310 0.6579 0.5921 0.5329 0.4796
*0.4317 0.3885 ... IIR output: 0.2500 0.7250 0.9025 0.8123 0.7310
*0.6579 0.5921 0.5329 0.4796 0.4317 0.3885 0.3496 0.3147 0.2832
*0.2549
Delay three samples:
0 0 0 0.2500 0.7250 0.9025 0.8123 0.7310 0.6579
*0.5921 0.5329 0.4796 0.4317 0.3885 0.3496

Source

#include "sigpack.h"
using namespace arma;
using namespace sp;
int main()
{
// Filter coeffs.
vec b;
int N = 15;
// Create a FIR filter
b = fir1(7, 0.35);
fir_filt.set_coeffs(b);
vec X(N); // Input sig
X.zeros();
X[0] = 1; // Impulse
vec Y(N); // Output sig
Y.zeros();
// Filter - sample loop
for (int n = 0; n < N; n++)
{
Y[n] = fir_filt(X[n]);
}
std::cout << "Filter coeffs: \n" << b.t() << std::endl;
std::cout << "Impulse response:\n" << Y.t() << std::endl;
// Create a IIR filter
vec a;
b << 0.25 << 0.5 << 0.25 << endr;
a << 1.0 << -0.9 << endr;
iir_filt.set_coeffs(b, a);
Y = iir_filt.filter(X);
std::cout
<< "IIR theor. impulse response: \n 0.2500 0.7250 0.9025 0.8123 "
" 0.7310 0.6579 0.5921 0.5329 0.4796 0.4317 0.3885 ..."
<< std::endl;
std::cout << "IIR output: \n" << Y.t() << std::endl;
Delay<double> del(3);
std::cout << "Delay three samples:\n" << del.delay(Y).t() << std::endl;
return 0;
}