SigPack - the C++ signal processing library
 
Loading...
Searching...
No Matches
base.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_BASE_H
5#define SP_BASE_H
6
7namespace sp
8{
13
14const double PI = 3.14159265358979323846;
15const double PI_2 = 6.28318530717958647692;
16
21arma_inline double sinc( double x )
22{
23 if( x == 0.0 )
24 return 1.0;
25 else
26 return std::sin( PI * x ) / ( PI * x );
27}
28
33arma_inline arma::vec sinc( const arma::vec& x )
34{
35 arma::vec out;
36 out.copy_size( x );
37 for( unsigned int n = 0; n < out.size(); n++ )
38 {
39 out( n ) = sinc( x( n ) );
40 }
41 return out;
42}
43
51arma_inline double besseli0( double x )
52{
53 double y = 1.0, s = 1.0, x2 = x * x, n = 1.0;
54 while( s > y * 1.0e-9 )
55 {
56 s *= x2 / 4.0 / ( n * n );
57 y += s;
58 n += 1;
59 }
60 return y;
61}
62
67template <typename T> double angle( const std::complex<T>& x )
68{
69 return std::arg( x );
70}
71
76arma_inline arma::vec angle( const arma::cx_vec& x )
77{
78 arma::vec P;
79 P.copy_size( x );
80 for( unsigned int r = 0; r < x.n_rows; r++ )
81 P( r ) = std::arg( x( r ) );
82 return P;
83}
84
89arma_inline arma::mat angle( const arma::cx_mat& x )
90{
91 arma::mat P;
92 P.copy_size( x );
93 for( unsigned int r = 0; r < x.n_rows; r++ )
94 for( unsigned int c = 0; c < x.n_cols; c++ )
95 P( r, c ) = std::arg( x( r, c ) );
96 return P;
97}
98
103arma_inline arma::vec unwrap( const arma::vec& x )
104{
105 arma::vec P;
106 double pacc = 0, pdiff = 0;
107 const double thr = PI * 170 / 180;
108 P.copy_size( x );
109 P( 0 ) = x( 0 );
110 for( unsigned int r = 1; r < x.n_rows; r++ )
111 {
112 pdiff = x( r ) - x( r - 1 );
113 if( pdiff >= thr )
114 pacc += -PI_2;
115 if( pdiff <= -thr )
116 pacc += PI_2;
117 P( r ) = pacc + x( r );
118 }
119 return P;
120}
121
123
128
134arma_inline arma::vec timevec( const int N, const double Fs )
135{
136 return arma::regspace( 0, N - 1.0 ) / Fs;
137}
138
144template <typename T> arma::Col<T> fftshift( const arma::Col<T>& Pxx )
145{
146 arma::Col<T> x( Pxx.n_elem );
147 x = shift( Pxx, floor( Pxx.n_elem / 2 ) );
148 return x;
149}
150
156template <typename T> arma::Col<T> ifftshift( const arma::Col<T>& Pxx )
157{
158 arma::Col<T> x( Pxx.n_elem );
159 x = shift( Pxx, -ceil( Pxx.n_elem / 2 ) );
160 return x;
161}
162
168template <typename T> arma::Mat<T> fftshift( const arma::Mat<T>& Pxx )
169{
170 arma::uword R = Pxx.n_rows;
171 arma::uword C = Pxx.n_cols;
172 arma::Mat<T> x( R, C );
173 x = arma::shift( Pxx, static_cast<arma::sword>( floor( R / 2 ) ), 0 );
174 x = arma::shift( x, static_cast<arma::sword>( floor( C / 2 ) ), 1 );
175 return x;
176}
177
183template <typename T> arma::Mat<T> ifftshift( const arma::Mat<T>& Pxx )
184{
185 arma::uword R = Pxx.n_rows;
186 arma::uword C = Pxx.n_cols;
187 arma::Mat<T> x( R, C );
188 x = shift( Pxx, -ceil( R / 2 ), 0 );
189 x = shift( x, -ceil( C / 2 ), 1 );
190 return x;
191}
192
194
199
203arma_inline std::string sp_version( void )
204{
205 return std::to_string( SP_VERSION_MAJOR ) + "." + std::to_string( SP_VERSION_MINOR ) + "." + std::to_string( SP_VERSION_PATCH );
206}
207
209// err_handler("Error string")
210// Prints an error message, waits for input and
211// then exits with error
212#define err_handler( msg ) \
213 { \
214 std::cout << "SigPack Error [" << __FILE__ << "@" << __LINE__ << "]: " << msg << std::endl; \
215 std::cin.get(); \
216 exit( EXIT_FAILURE ); \
217 }
218
220// wrn_handler("Warning string")
221// Prints an warning message
222#define wrn_handler( msg ) \
223 { \
224 std::cout << "SigPack warning [" << __FILE__ << "@" << __LINE__ << "]: " << msg << std::endl; \
225 }
227
228} // namespace sp
229#endif
arma::Col< T > fftshift(const arma::Col< T > &Pxx)
1D FFT shift.
Definition base.h:144
arma::Col< T > ifftshift(const arma::Col< T > &Pxx)
1D FFT inverse/reverse shift.
Definition base.h:156
arma_inline arma::vec timevec(const int N, const double Fs)
Generates a linear time vector with specified sample rate. Delta time=1/Fs.
Definition base.h:134
const double PI
... or use arma::datum::pi
Definition base.h:14
arma_inline double besseli0(double x)
Modified first kind bessel function order zero.
Definition base.h:51
double angle(const std::complex< T > &x)
Calculates angle in radians for complex input.
Definition base.h:67
arma_inline double sinc(double x)
A sinc, sin(x)/x, function.
Definition base.h:21
const double PI_2
Definition base.h:15
arma_inline arma::vec unwrap(const arma::vec &x)
Unwraps the angle vector x, accumulates phase.
Definition base.h:103
arma_inline std::string sp_version(void)
SigPack version string.
Definition base.h:203
Definition base.h:8
#define SP_VERSION_MINOR
Definition sigpack.h:13
#define SP_VERSION_MAJOR
Definition sigpack.h:12
#define SP_VERSION_PATCH
Definition sigpack.h:14