SigPack - the C++ signal processing library
adaptive_filter.cpp
Go to the documentation of this file.
1 
9 #include "sigpack.h"
10 
11 using namespace std;
12 using namespace sp;
13 
14 int main()
15 {
16  // Number of samples
17  int N = 400;
18 
19  // Create a FIR filter and adaptive
22 
23  // Filter coeffs.
24  arma::vec b = "-0.2 -0.1 0.1 0.3 0.7";
25  arma::vec c = "-0.4 0.0 0.2 0.1 0.4";
26  G.set_coeffs(b);
27  int M = b.size();
28 
29  // Setup adaptive filter
30  // Ghat.setup_rls(M, 0.8, 5);
31  // Ghat.setup_nlms(M,0.6,0.001);
32  // Ghat.setup_lms(M,0.05);
33  Ghat.setup_kalman(M, 10, 1, 5);
34  // Ghat.setup_newt(M,0.99,0.01,15);
35 
36  // Signal vectors
37  arma::vec x(N, arma::fill::randn); // Input sig
38  arma::vec y(N, arma::fill::zeros); // Model sig
39  arma::vec d(N, arma::fill::zeros); // Output sig
40  arma::vec z(N, arma::fill::randn); // Measurement noise
41  arma::vec e(N, arma::fill::zeros); // Err sig
42 
43  // Log matrix
44  arma::mat Wlog(M, N);
45 
46  // Apply G to input signal and add some measurement noise
47  for (int n = 0; n < N; n++)
48  {
49  if (n == 200)
50  G.update_coeffs(c);
51  d(n) = G(x(n)) + 0.0001 * z(n);
52  }
53 
54  // Filter - sample loop
55  for (int n = 0; n < N; n++)
56  {
57  // Apply adaptiv filter
58  y(n) = Ghat(x(n));
59 
60  // Calc error
61  e(n) = d(n) - y(n);
62 
63  // Update filter
64  // Ghat.rls_adapt(e(n));
65  // Ghat.nlms_adapt(e(n));
66  // Ghat.lms_adapt(e(n));
67  Ghat.kalman_adapt(e(n));
68  // Ghat.newt_adapt(e(n));
69  // Save to log
70  Wlog.col(n) = Ghat.get_coeffs();
71  }
72 
73  // Display result
74  cout << "Filter coeffs: " << c.t() << endl;
75  cout << "Estimated coeffs: " << Ghat.get_coeffs().t() << endl;
76  gplot gp0, gp1;
77  gp0.window("Plot", 10, 10, 500, 500);
78  // gp0.set_output("Wlog.png");
79  gp0.set_term("qt");
80  gp0.plot_add_mat(Wlog, "b");
81  gp0.plot_show();
82 
83  gp1.window("Plot2", 600, 10, 500, 500);
84  // gp1.set_output("LMS_err.png");
85  arma::vec J = 10 * log10(e % e);
86  gp1.set_term("qt");
87  gp1.plot_add(J, "|Error|^2");
88  gp1.plot_show();
89 
90  return 0;
91 }
void update_coeffs(const arma::Mat< T2 > &_b)
Updates coefficients in FIR filter without clearing the internal states.
Definition: filter.h:96
Gnuplot class.
Definition: gplot.h:25
arma::Col< T2 > get_coeffs()
Get coefficients from FIR filter.
Definition: filter.h:89
Definition: base.h:7
void plot_show(void)
Show plots.
Definition: gplot.h:395
int main()
void plot_add_mat(const arma::mat &y)
Push multiple plot, each row gives a plot without label.
Definition: gplot.h:354
FIR/MA filter class.
Definition: filter.h:20
void set_term(const char *ttype)
Set output terminal.
Definition: gplot.h:759
void window(const int fig, const char *name, const int x, const int y, const int width, const int height)
Configure the figure used Windows environment.
Definition: gplot.h:182
void plot_add(const T1 &x, const T2 &y, const std::string lb, const std::string ls="lines")
Push plot y vs. x with label and linespec.
Definition: gplot.h:316
void set_coeffs(const arma::Mat< T2 > &_b)
Sets coefficients in FIR filter. The internal state and pointers are cleared.
Definition: filter.h:65
void kalman_adapt(const T3 _err)
Kalman Filter update function.
Definition: filter.h:409
void setup_kalman(const arma::uword _N, const double _P0, const double _Q0, const double _R0)
Kalman Filter function setup.
Definition: filter.h:379