SigPack - the C++ signal processing library
 
Loading...
Searching...
No Matches
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
7namespace sp
8{
13
21template <class T1> arma::Col<T1> upsample( const arma::Col<T1>& x, const int p )
22{
23 long int N = x.size();
24 arma::Col<T1> y;
25 y.set_size( p * N );
26 y.zeros();
27 for( long int n = 0; n < N; n++ )
28 y[p * n] = x[n];
29 return y;
30}
31
38template <class T1> 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
53template <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;
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;
92 }
93
98 {
99 }
100
107 void downfir( const arma::Col<T1>& in, arma::Col<T1>& out )
108 {
109 arma::uword sz = in.n_elem;
110 for( arma::uword n = 0; n < sz; n++ )
111 {
112 T1 tmp = aa_filt( in[n] );
113 if( n % Q == 0 )
114 out[n / Q] = tmp;
115 }
116 }
117
124 void upfir( const arma::Col<T1>& in, arma::Col<T1>& out )
125 {
126 arma::uword sz = P * in.n_elem;
127 for( arma::uword n = 0; n < sz; n++ )
128 {
129 if( n % P == 0 )
130 out[n] = P * aa_filt( in[n / P] );
131 else
132 out[n] = P * aa_filt( 0.0 );
133 }
134 }
135
144 void upfirdown( const arma::Col<T1>& in, arma::Col<T1>& out )
145 {
146 arma::uword sz = P * in.n_elem;
147 T1 tmp;
148 for( arma::uword n = 0; n < sz; n++ )
149 {
150 if( n % P == 0 )
151 tmp = aa_filt( in[n / P] );
152 else
153 tmp = aa_filt( 0.0 );
154 if( n % Q == 0 )
155 out[n / Q] = P * tmp;
156 }
157 }
158};
159
161} // namespace sp
162#endif
FIR/MA filter class.
Definition filter.h:22
void set_coeffs(const arma::Mat< T2 > &_b)
Sets coefficients in FIR filter. The internal state and pointers are cleared.
Definition filter.h:70
A resampling class.
Definition resampling.h:54
FIR_filt< T1, double, T1 > aa_filt
Definition resampling.h:56
void downfir(const arma::Col< T1 > &in, arma::Col< T1 > &out)
Downsampling with anti alias filter.
Definition resampling.h:107
arma::uword P
Upsampling rate.
Definition resampling.h:59
void upfir(const arma::Col< T1 > &in, arma::Col< T1 > &out)
Upsampling with anti alias filter.
Definition resampling.h:124
arma::vec K
Number of filter coefficients.
Definition resampling.h:58
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::vec H
Filter coefficients.
Definition resampling.h:57
void upfirdown(const arma::Col< T1 > &in, arma::Col< T1 > &out)
Resampling by a rational P/Q with anti alias filtering.
Definition resampling.h:144
~resampling()
Destructor.
Definition resampling.h:97
resampling(const arma::uword _P, const arma::uword _Q, const arma::vec _H)
Constructor.
Definition resampling.h:69
arma_inline arma::vec fir1(const arma::uword M, const double f0)
FIR lowpass design function. FIR lowpassdesign using windows method (hamming window)....
Definition filter.h:637
arma::Col< T1 > upsample(const arma::Col< T1 > &x, const int p)
Upsampling without anti alias filtering.
Definition resampling.h:21
arma::Col< T1 > downsample(const arma::Col< T1 > &x, const int q)
Downsampling without anti alias filtering.
Definition resampling.h:38
Definition base.h:8