SigPack - the C++ signal processing library
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 
7 namespace sp
8 {
13 
14 const double PI = 3.14159265358979323846;
15 const double PI_2 = 6.28318530717958647692;
16 
21 arma_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 
33 arma_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 
51 arma_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 
67 template <typename T> double angle(const std::complex<T> &x)
68 {
69  return std::arg(x);
70 }
71 
76 arma_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 
89 arma_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 
103 arma_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 }
122 
127 
133 arma_inline arma::vec timevec(const int N, const double Fs)
134 {
135  return arma::regspace(0, N - 1.0) / Fs;
136 }
137 
143 template <typename T> arma::Col<T> fftshift(const arma::Col<T> &Pxx)
144 {
145  arma::Col<T> x(Pxx.n_elem);
146  x = shift(Pxx, floor(Pxx.n_elem / 2));
147  return x;
148 }
149 
155 template <typename T> arma::Col<T> ifftshift(const arma::Col<T> &Pxx)
156 {
157  arma::Col<T> x(Pxx.n_elem);
158  x = shift(Pxx, -ceil(Pxx.n_elem / 2));
159  return x;
160 }
161 
167 template <typename T> arma::Mat<T> fftshift(const arma::Mat<T> &Pxx)
168 {
169  arma::uword R = Pxx.n_rows;
170  arma::uword C = Pxx.n_cols;
171  arma::Mat<T> x(R, C);
172  x = arma::shift(Pxx, static_cast<arma::sword>(floor(R / 2)), 0);
173  x = arma::shift(x, static_cast<arma::sword>(floor(C / 2)), 1);
174  return x;
175 }
176 
182 template <typename T> arma::Mat<T> ifftshift(const arma::Mat<T> &Pxx)
183 {
184  arma::uword R = Pxx.n_rows;
185  arma::uword C = Pxx.n_cols;
186  arma::Mat<T> x(R, C);
187  x = shift(Pxx, -ceil(R / 2), 0);
188  x = shift(x, -ceil(C / 2), 1);
189  return x;
190 }
191 
193 
198 
202 arma_inline std::string sp_version(void)
203 {
204  return std::to_string(SP_VERSION_MAJOR) + "." +
205  std::to_string(SP_VERSION_MINOR) + "." +
206  std::to_string(SP_VERSION_PATCH);
207 }
208 
210 // err_handler("Error string")
211 // Prints an error message, waits for input and
212 // then exits with error
213 #define err_handler(msg) \
214  { \
215  std::cout << "SigPack Error [" << __FILE__ << "@" << __LINE__ \
216  << "]: " << msg << std::endl; \
217  std::cin.get(); \
218  exit(EXIT_FAILURE); \
219  }
220 
222 // wrn_handler("Warning string")
223 // Prints an warning message
224 #define wrn_handler(msg) \
225  { \
226  std::cout << "SigPack warning [" << __FILE__ << "@" << __LINE__ \
227  << "]: " << msg << std::endl; \
228  }
229 
231 } // namespace sp
232 #endif
arma_inline double sinc(double x)
A sinc, sin(x)/x, function.
Definition: base.h:21
double angle(const std::complex< T > &x)
Calculates angle in radians for complex input.
Definition: base.h:67
arma::Col< T > ifftshift(const arma::Col< T > &Pxx)
1D FFT inverse/reverse shift.
Definition: base.h:155
arma_inline std::string sp_version(void)
SigPack version string.
Definition: base.h:202
Definition: base.h:7
arma_inline arma::vec unwrap(const arma::vec &x)
Unwraps the angle vector x, accumulates phase.
Definition: base.h:103
arma_inline double besseli0(double x)
Modified first kind bessel function order zero.
Definition: base.h:51
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:133
#define SP_VERSION_MAJOR
Definition: sigpack.h:46
#define SP_VERSION_MINOR
Definition: sigpack.h:47
#define SP_VERSION_PATCH
Definition: sigpack.h:48
arma::Col< T > fftshift(const arma::Col< T > &Pxx)
1D FFT shift.
Definition: base.h:143
const double PI
... or use arma::datum::pi
Definition: base.h:14
const double PI_2
Definition: base.h:15