SigPack - the C++ signal processing library
resampling.h
Go to the documentation of this file.
1 // This Source Code Form is subject to the terms of the Mozilla Public
2 // License, v. 2.0. If a copy of the MPL was not distributed with this
3 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
4 #ifndef SP_RESAMPLING_H
5 #define SP_RESAMPLING_H
6 namespace sp
7 {
12 
20 template <class T1> arma::Col<T1> upsample(const arma::Col<T1> &x, const int p)
21 {
22  long int N = x.size();
23  arma::Col<T1> y;
24  y.set_size(p * N);
25  y.zeros();
26  for (long int n = 0; n < N; n++)
27  y[p * n] = x[n];
28  return y;
29 }
30 
37 template <class T1>
38 arma::Col<T1> downsample(const arma::Col<T1> &x, const int q)
39 {
40  arma::Col<T1> y;
41  int N = int(floor(1.0 * x.size() / q));
42  y.set_size(N);
43  for (long int n = 0; n < N; n++)
44  y[n] = x[n * q];
45  return y;
46 }
47 
53 template <class T1> class resampling
54 {
55 private:
57  arma::vec H;
58  arma::vec K;
59  arma::uword P;
60  arma::uword Q;
61 
62 public:
69  resampling(const arma::uword _P, const arma::uword _Q, const arma::vec _H)
70  {
71  P = _P;
72  Q = _Q;
73  H = _H;
74  K = H.n_elem;
75  aa_filt.set_coeffs(H);
76  }
77 
84  resampling(const arma::uword _P, const arma::uword _Q)
85  {
86  P = _P;
87  Q = _Q;
88  arma::uword M = (P > Q) ? P : Q;
89  H = fir1(8 * M, 1.0f / M);
90  K = H.n_elem;
91  aa_filt.set_coeffs(H);
92  }
93 
98 
105  void downfir(const arma::Col<T1> &in, arma::Col<T1> &out)
106  {
107  arma::uword sz = in.n_elem;
108  for (arma::uword n = 0; n < sz; n++)
109  {
110  T1 tmp = aa_filt(in[n]);
111  if (n % Q == 0)
112  out[n / Q] = tmp;
113  }
114  }
115 
122  void upfir(const arma::Col<T1> &in, arma::Col<T1> &out)
123  {
124  arma::uword sz = P * in.n_elem;
125  for (arma::uword n = 0; n < sz; n++)
126  {
127  if (n % P == 0)
128  out[n] = P * aa_filt(in[n / P]);
129  else
130  out[n] = P * aa_filt(0.0);
131  }
132  }
133 
142  void upfirdown(const arma::Col<T1> &in, arma::Col<T1> &out)
143  {
144  arma::uword sz = P * in.n_elem;
145  T1 tmp;
146  for (arma::uword n = 0; n < sz; n++)
147  {
148  if (n % P == 0)
149  tmp = aa_filt(in[n / P]);
150  else
151  tmp = aa_filt(0.0);
152  if (n % Q == 0)
153  out[n / Q] = P * tmp;
154  }
155  }
156 };
158 } // namespace sp
159 #endif
resampling(const arma::uword _P, const arma::uword _Q, const arma::vec _H)
Constructor.
Definition: resampling.h:69
arma::vec K
Number of filter coefficients.
Definition: resampling.h:58
void downfir(const arma::Col< T1 > &in, arma::Col< T1 > &out)
Downsampling with anti alias filter.
Definition: resampling.h:105
A resampling class.
Definition: resampling.h:53
Definition: base.h:7
void upfirdown(const arma::Col< T1 > &in, arma::Col< T1 > &out)
Resampling by a rational P/Q with anti alias filtering.
Definition: resampling.h:142
arma::Col< T1 > upsample(const arma::Col< T1 > &x, const int p)
Upsampling without anti alias filtering.
Definition: resampling.h:20
arma::uword P
Upsampling rate.
Definition: resampling.h:59
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.
Definition: filter.h:607
arma::vec H
Filter coefficients.
Definition: resampling.h:57
void upfir(const arma::Col< T1 > &in, arma::Col< T1 > &out)
Upsampling with anti alias filter.
Definition: resampling.h:122
~resampling()
Destructor.
Definition: resampling.h:97
void set_coeffs(const arma::Mat< T2 > &_b)
Sets coefficients in FIR filter. The internal state and pointers are cleared.
Definition: filter.h:65
FIR_filt< T1, double, T1 > aa_filt
Definition: resampling.h:56
arma::uword Q
Downsampling rate.
Definition: resampling.h:60
resampling(const arma::uword _P, const arma::uword _Q)
Constructor using a fir1 filter with 8*M+1 taps and cutoff 1/M where M=max(P,Q)
Definition: resampling.h:84
arma::Col< T1 > downsample(const arma::Col< T1 > &x, const int q)
Downsampling without anti alias filtering.
Definition: resampling.h:38