20 template <
class T1,
class T2,
class T3>
class FIR_filt 81 arma::Mat<T2> b_mat = arma::conv_to<arma::Mat<T2>>::from(_b_col);
89 arma::Col<T2>
get_coeffs() {
return arma::conv_to<arma::Col<T2>>::from(b); }
108 for (arma::uword m = cur_p; m <
M; m++)
109 out += b[p++] * buf[m];
110 for (arma::uword m = 0; m <
cur_p; m++)
111 out += b[p++] * buf[m];
127 arma::Mat<T3>
filter(
const arma::Mat<T1> &in)
129 arma::uword sz = in.n_elem;
130 arma::Mat<T3> out(sz, 1);
131 for (arma::uword n = 0; n < sz; n++)
132 out[n] = this->
operator()(in[n]);
135 arma::Col<T3>
filter(
const arma::Col<T1> &in)
137 arma::Mat<T1> in_col = arma::conv_to<arma::Mat<T1>>::from(in);
138 return arma::conv_to<arma::Col<T3>>::from(
filter(in_col));
148 const arma::uword _L = 1)
177 arma::Mat<T1> buf_tmp(M, 1);
178 for (arma::uword m = 0; m <
M; m++)
180 buf_tmp(m) =
buf((cur_p + m + 1) % M);
187 if (blk_ctr++ % L == 0)
189 b += 2 * mu * K / double(L);
202 void setup_nlms(
const arma::uword _N,
const double _mu,
const T2 _c,
203 const arma::uword _L = 1)
234 arma::Mat<T1> buf_tmp(M, 1);
235 for (arma::uword m = 0; m <
M; m++)
237 buf_tmp(m) =
buf((cur_p + m + 1) % M);
241 T1 S = c + arma::as_scalar(buf_tmp.t() * buf_tmp);
242 K += _err * buf_tmp / S;
245 if (blk_ctr++ % L == 0)
247 b += 2 * mu * K / double(L);
260 void setup_newt(
const arma::uword _N,
const double _mu,
const T2 _c,
261 const arma::uword _L = 1)
273 X_tpz.set_size(M, L);
295 arma::Mat<T1> buf_tmp(M, 1);
296 for (arma::uword m = 0; m <
M; m++)
298 buf_tmp(m) =
buf((cur_p + m + 1) % M);
303 X_tpz.col(blk_ctr % L) = buf_tmp;
306 if (blk_ctr++ % L == 0)
309 arma::Mat<T1> Rxx = X_tpz * X_tpz.t() /
L;
312 b += mu * pinv(Rxx + c * I) * K /
L;
324 void setup_rls(
const arma::uword _N,
const double _lmd,
const double _P0)
356 arma::Mat<T1> buf_tmp(M, 1);
357 for (arma::uword m = 0; m <
M; m++)
359 buf_tmp(m) =
buf((cur_p + m + 1) % M);
363 T1 S = lmd + arma::as_scalar(buf_tmp.t() * P * buf_tmp);
365 P = (P - K * buf_tmp.t() *
P) / lmd;
379 void setup_kalman(
const arma::uword _N,
const double _P0,
const double _Q0,
414 arma::Mat<T1> buf_tmp(M, 1);
415 for (arma::uword m = 0; m <
M; m++)
417 buf_tmp(m) =
buf((cur_p + m + 1) % M);
421 T1 S = arma::as_scalar(R + buf_tmp.t() * P * buf_tmp);
430 P = P - K * buf_tmp.t() * P +
Q;
477 template <
class T1,
class T2,
class T3>
class IIR_filt 516 void set_coeffs(
const arma::Col<T2> &_b,
const arma::Col<T2> &_a)
551 for (arma::uword m = b_cur_p; m <
M; m++)
552 out += b[p++] * b_buf[m];
553 for (arma::uword m = 0; m < b_cur_p; m++)
554 out += b[p++] * b_buf[m];
564 for (arma::uword n = a_cur_p + 1; n < N; n++)
565 out -= a[p++] * a_buf[n];
566 for (arma::uword n = 0; n < a_cur_p; n++)
567 out -= a[p++] * a_buf[n];
569 a_buf[a_cur_p] = out;
585 arma::Col<T3>
filter(
const arma::Col<T1> &in)
587 arma::uword sz = in.size();
588 arma::Col<T3> out(sz);
589 for (arma::uword n = 0; n < sz; n++)
590 out[n] = this->
operator()(in[n]);
607 arma_inline arma::vec
fir1(
const arma::uword
M,
const double f0)
609 arma::vec
b(M + 1), h(M + 1);
611 for (arma::uword m = 0; m < M + 1; m++)
613 b[m] = f0 * h[m] *
sinc(f0 * (m - M / 2.0));
615 return b / arma::sum(
b);
626 arma_inline arma::vec
fir1_hp(
const arma::uword
M,
const double f0)
630 arma::vec
b(M + 1), h(M + 1);
632 for (arma::uword m = 0; m < M + 1; m++)
634 b[m] = h[m] * (
sinc(m - M / 2.0) - f0 *
sinc(f0 * (m - M / 2.0)));
638 std::complex<double> i(0, 1);
640 arma::vec fv = arma::regspace(0,
double(M));
641 nrm = abs(arma::sum(exp(-i * fv *
PI) %
b));
655 arma_inline arma::vec
fir1_bp(
const arma::uword
M,
const double f0,
659 err_handler(
"Frequencies must be [0 < f0 < f1 < 1]");
661 arma::vec
b(M + 1), h(M + 1);
663 for (arma::uword m = 0; m < M + 1; m++)
666 h[m] * (f1 *
sinc(f1 * (m - M / 2.0)) - f0 *
sinc(f0 * (m - M / 2.0)));
670 double fc = (f0 + f1) / 2;
671 std::complex<double> i(0, 1);
673 arma::vec fv = arma::regspace(0,
double(M));
674 nrm = abs(arma::sum(exp(-i * fv *
PI * fc) %
b));
688 arma_inline arma::vec
fir1_bs(
const arma::uword
M,
const double f0,
694 err_handler(
"Frequencies must be [0 < f0 < f1 < 1]");
696 arma::vec
b(M + 1), h(M + 1);
698 for (arma::uword m = 0; m < M + 1; m++)
700 b[m] = h[m] * (
sinc(m - M / 2.0) - f1 *
sinc(f1 * (m - M / 2.0)) +
701 f0 *
sinc(f0 * (m - M / 2.0)));
704 return b / arma::sum(
b);
716 arma_inline arma::vec
fd_filter(
const arma::uword
M,
double fd)
722 for (arma::uword m = 0; m <
M; m++)
724 h(m) = w(m) *
sinc(m - M / 2.0 - fd);
726 h = h / arma::sum(h);
738 arma_inline arma::cx_vec
freq(
const arma::vec
b,
const arma::vec a,
739 const arma::uword
K = 512)
742 arma::uword
M = b.size();
743 arma::uword N = a.size();
744 std::complex<double> b_tmp, a_tmp, i(0, 1);
745 for (arma::uword k = 0; k <
K; k++)
747 b_tmp = std::complex<double>(
b(0), 0);
748 for (arma::uword m = 1; m <
M; m++)
749 b_tmp +=
b(m) * (cos(m *
PI * k / K) - i * sin(m *
PI * k / K));
750 a_tmp = std::complex<double>(a(0), 0);
751 for (arma::uword n = 1; n < N; n++)
752 a_tmp += a(n) * (cos(n *
PI * k / K) - i * sin(n *
PI * k / K));
753 h(k) = b_tmp / a_tmp;
765 arma_inline arma::vec
freqz(
const arma::vec
b,
const arma::vec a,
766 const arma::uword
K = 512)
768 arma::cx_vec f =
freq(b, a,
K);
779 arma_inline arma::vec
phasez(
const arma::vec
b,
const arma::vec a,
780 const arma::uword
K = 512)
782 arma::cx_vec f =
freq(b, a,
K);
arma::Mat< T1 > R
Adaptive filter Measurement noise.
double mu
Adaptive filter step size.
void lms_adapt(const T3 _err)
LMS Filter update function.
double lmd
Adaptive filter RLS forgetting factor.
void set_coeffs(const arma::Col< T2 > &_b_col)
Sets coefficients in FIR filter (col format) The internal state and pointers are cleared.
void update_coeffs(const arma::Mat< T2 > &_b)
Updates coefficients in FIR filter without clearing the internal states.
arma::Mat< T3 > filter(const arma::Mat< T1 > &in)
Filter function.
arma::uword cur_p
Pointer to current sample in buffer.
arma::uword N
Nr of AR filter taps.
arma_inline double sinc(double x)
A sinc, sin(x)/x, function.
double angle(const std::complex< T > &x)
Calculates angle in radians for complex input.
arma::Mat< T1 > P
Adaptive filter Inverse corr matrix (estimated accuracy)
void rls_adapt(const T3 _err)
RLS Filter update function.
arma_inline arma::vec blackmanharris(const arma::uword N)
Blackman-Harris window. Symmetric BH4 window.
arma_inline arma::vec fd_filter(const arma::uword M, double fd)
Fractional delay function. Fractional delay filter design using windowed sinc method. Actual delay is M/2+fd samples for even nr of taps and (M-1)/2+fd for odd nr of taps Best performance if -1 < fd < 1.
arma_inline arma::vec phasez(const arma::vec b, const arma::vec a, const arma::uword K=512)
Frequency phase response function. Calculates the frequency phase response.
arma::Col< T2 > get_coeffs()
Get coefficients from FIR filter.
arma_inline arma::vec fir1_hp(const arma::uword M, const double f0)
FIR highpass design function. FIR design using windows method (hamming window). NB! Returns size M+1...
arma_inline arma::vec fir1_bs(const arma::uword M, const double f0, const double f1)
FIR bandstop design function. FIR design using windows method (hamming window). NB! Returns size M+1...
void adapt_disble(void)
Stop adapt.
void setup_newt(const arma::uword _N, const double _mu, const T2 _c, const arma::uword _L=1)
LMS-Newton Filter function setup. (Affine Projection Algorithm)
arma_inline arma::vec hamming(const arma::uword N)
Hamming window.
arma::uword blk_ctr
Adaptive filter block length counter.
arma::uword a_cur_p
Pointer to current sample in AR buffer.
void clear(void)
Clears the internal states and pointer.
arma_inline arma::vec fir1_bp(const arma::uword M, const double f0, const double f1)
FIR bandpass design function. FIR design using windows method (hamming window). NB! Returns size M+1...
arma::Mat< T1 > get_P(void)
Get P.
arma::uword b_cur_p
Pointer to current sample in MA buffer.
T2 c
Adaptive filter NLMS regulation const.
arma::Mat< T1 > Q
Adaptive filter Process noise.
void setup_lms(const arma::uword _N, const double _mu, const arma::uword _L=1)
LMS Filter function setup.
arma::Mat< T1 > K
Adaptive filter gain vector.
T3 operator()(const T1 &in)
Filter operator.
void setup_rls(const arma::uword _N, const double _lmd, const double _P0)
RLS Filter function setup.
arma::Mat< T1 > get_K(void)
Get K.
void set_step_size(const double _mu)
Set step size.
arma::Mat< T1 > buf
Signal buffer.
arma::Col< T2 > b
MA Filter coefficients.
arma::Col< T2 > a
AR Filter coefficients.
arma::Mat< T2 > b
Filter coefficients.
arma::Col< T1 > b_buf
MA Signal buffer.
arma_inline arma::cx_vec freq(const arma::vec b, const arma::vec a, const arma::uword K=512)
Frequency response function. Calculates the frequency response.
void clear(void)
Clears the internal states and pointers.
void nlms_adapt(const T3 _err)
NLMS Filter update function.
arma_inline arma::vec freqz(const arma::vec b, const arma::vec a, const arma::uword K=512)
Frequency magnitude response function. Calculates the frequency magnitude response.
arma::uword L
Adaptive filter block length.
double get_step_size(void)
Get step size.
arma::Col< T3 > filter(const arma::Col< T1 > &in)
arma_inline arma::vec fir1(const arma::uword M, const double f0)
FIR lowpass design function. FIR lowpassdesign using windows method (hamming window). NB! Returns size M+1.
T3 operator()(const T1 &in)
Filter operator.
const double PI
... or use arma::datum::pi
arma::uword M
Nr of MA filter taps.
void adapt_enable(void)
Start adapt.
arma::uword M
Nr of filter taps.
void update_coeffs(const arma::Col< T2 > &_b, const arma::Col< T2 > &_a)
Updates coefficients in filter without clearing the internal states.
void newt_adapt(const T3 _err)
LMS-Newton Filter update function.
void setup_nlms(const arma::uword _N, const double _mu, const T2 _c, const arma::uword _L=1)
NLMS Filter function setup.
void set_coeffs(const arma::Mat< T2 > &_b)
Sets coefficients in FIR filter. The internal state and pointers are cleared.
void set_coeffs(const arma::Col< T2 > &_b, const arma::Col< T2 > &_a)
Sets coefficients in IIR filter. The internal state and pointers are cleared.
arma::Col< T1 > a_buf
AR Signal buffer.
arma::Mat< T1 > X_tpz
Adaptive filter Toeplitz for Corr matrix calc.
void kalman_adapt(const T3 _err)
Kalman Filter update function.
void setup_kalman(const arma::uword _N, const double _P0, const double _Q0, const double _R0)
Kalman Filter function setup.
arma::Col< T3 > filter(const arma::Col< T1 > &in)
Filter function.
arma::uword do_adapt
Adaptive filter enable flag.