IT++ Logo
resampling.h
Go to the documentation of this file.
1 
29 #ifndef RESAMPLING_H
30 #define RESAMPLING_H
31 
32 #include <itpp/base/mat.h>
33 #include <itpp/itexports.h>
34 
35 
36 namespace itpp
37 {
38 
44 
45 template<class T>
46 Vec<T> repeat(const Vec<T> &v, int norepeats)
47 {
48  Vec<T> temp(v.length()*norepeats);
49 
50  for (int i = 0; i < v.length(); i++) {
51  for (int j = 0;j < norepeats;j++)
52  temp(i*norepeats + j) = v(i);
53  }
54  return temp;
55 }
56 
58 template<class T>
59 Mat<T> repeat(const Mat<T> &m, int norepeats)
60 {
61  Mat<T> temp(m.rows(), m.cols()*norepeats);
62 
63  for (int j = 0; j < m.cols(); j++) {
64  for (int i = 0;i < norepeats;i++) {
65  temp.set_col(j*norepeats + i, m.get_col(j));
66  }
67  }
68  return temp;
69 }
70 
72 template<class T>
73 void upsample(const Vec<T> &v, int usf, Vec<T> &u)
74 {
75  it_assert_debug(usf >= 1, "upsample: upsampling factor must be equal or greater than one");
76  u.set_size(v.length()*usf);
77  u.clear();
78  for (int i = 0;i < v.length();i++)
79  u(i*usf) = v(i);
80 }
81 
82 
84 template<class T>
85 Vec<T> upsample(const Vec<T> &v, int usf)
86 {
87  Vec<T> u;
88  upsample(v, usf, u);
89  return u;
90 }
91 
93 template<class T>
94 void upsample(const Mat<T> &v, int usf, Mat<T> &u)
95 {
96  it_assert_debug(usf >= 1, "upsample: upsampling factor must be equal or greater than one");
97  u.set_size(v.rows(), v.cols()*usf);
98  u.clear();
99  for (int j = 0;j < v.cols();j++)
100  u.set_col(j*usf, v.get_col(j));
101 }
102 
104 template<class T>
105 Mat<T> upsample(const Mat<T> &v, int usf)
106 {
107  Mat<T> u;
108  upsample(v, usf, u);
109  return u;
110 }
111 
113 template<class T>
114 void lininterp(const Mat<T> &m, int usf, Mat<T> &u)
115 {
116  it_assert_debug(usf >= 1, "lininterp: upsampling factor must be equal or greater than one");
117  int L = (m.cols() - 1) * usf + 1;
118  u.set_size(m.rows(), L);
119  for (int i = 0; i < m.rows(); i++) {
120  for (int j = 0; j < L - 1; j++)
121  u(i, j) = (m(i, j / usf) + (j % usf) / ((double)usf) * (m(i, (j + usf) / usf) - m(i, j / usf)));
122  u(i, L - 1) = m(i, m.cols() - 1);
123  }
124 }
125 
136 template<class T>
137 Mat<T> lininterp(const Mat<T> &m, double f_base, double f_ups,
138  int nrof_samples, double t_start = 0)
139 {
140  double t_base = 1 / f_base;
141  double t_ups = 1 / f_ups;
142  int rows = m.rows();
143  int cols = m.cols();
144  it_assert_debug(f_ups > f_base, "lininterp(): upsampled frequency must be greater than base frequency");
145  it_assert_debug((t_start >= 0) && (t_start < cols * t_base), "lininterp(): incorrect start time offset");
146  it_assert_debug((nrof_samples * t_ups + t_start) <= (cols * t_base), "lininterp(): too many samples required or input data to short");
147  Mat<T> u(rows, nrof_samples);
148  double curr_time = t_start;
149 
150  int i = 0;
151  int k = 0;
152  while (i < cols - 1) {
153  while ((curr_time < (i + 1) * t_base) && (k < nrof_samples)) {
154  for (int j = 0; j < rows; j++) {
155  u(j, k) = (m(j, i) * ((i + 1) * t_base - curr_time)
156  - m(j, i + 1) * (i * t_base - curr_time)) / t_base;
157  }
158  k++;
159  curr_time += t_ups;
160  }
161  i++;
162  }
163  return u;
164 }
165 
167 template<class T>
168 Mat<T> lininterp(const Mat<T> &m, int usf)
169 {
170  Mat<T> u;
171  lininterp(m, usf, u);
172  return u;
173 }
174 
176 template<class T>
177 void lininterp(const Vec<T> &v, int usf, Vec<T> &u)
178 {
179  it_assert_debug(usf >= 1, "lininterp(): upsampling factor must be equal or greater than one");
180  int L = (v.length() - 1) * usf + 1;
181  u.set_size(L);
182  for (int j = 0; j < L - 1; j++) {
183  u(j) = (v(j / usf) + (j % usf) / ((double)usf) * (v((j + usf) / usf) - v(j / usf)));
184  }
185  u(L - 1) = v(v.length() - 1);
186 }
187 
189 template<class T>
190 Vec<T> lininterp(const Vec<T> &v, int usf)
191 {
192  Vec<T> u;
193  lininterp(v, usf, u);
194  return u;
195 }
196 
207 template<class T>
208 Vec<T> lininterp(const Vec<T> &v, double f_base, double f_ups,
209  int nrof_samples, double t_start = 0)
210 {
211  double t_base = 1 / f_base;
212  double t_ups = 1 / f_ups;
213  int len = v.length();
214  it_assert_debug(f_ups > f_base, "lininterp(): upsampled frequency must be greater than base frequency");
215  it_assert_debug((t_start >= 0) && (t_start < len * t_base), "lininterp(): incorrect start time offset");
216  it_assert_debug((nrof_samples * t_ups + t_start) <= (len * t_base), "lininterp(): too many samples required or input data to short");
217  Vec<T> u(nrof_samples);
218  double curr_time = t_start;
219 
220  int i = 0;
221  int k = 0;
222  while (i < len - 1) {
223  while ((curr_time < (i + 1) * t_base) && (k < nrof_samples)) {
224  u(k) = (v(i) * ((i + 1) * t_base - curr_time)
225  - v(i + 1) * (i * t_base - curr_time)) / t_base;
226  k++;
227  curr_time += t_ups;
228  }
229  i++;
230  }
231  return u;
232 }
233 
238 
239 
240 // ----------------------------------------------------------------------
241 // Instantiations
242 // ----------------------------------------------------------------------
243 
245 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT vec repeat(const vec &v, int norepeats);
247 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec repeat(const cvec &v, int norepeats);
249 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT svec repeat(const svec &v, int norepeats);
251 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT ivec repeat(const ivec &v, int norepeats);
253 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bvec repeat(const bvec &v, int norepeats);
254 
256 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT mat repeat(const mat &m, int norepeats);
258 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat repeat(const cmat &m, int norepeats);
260 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT smat repeat(const smat &m, int norepeats);
262 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT imat repeat(const imat &m, int norepeats);
264 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bmat repeat(const bmat &m, int norepeats);
265 
267 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT vec upsample(const vec &v, int usf);
269 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec upsample(const cvec &v, int usf);
271 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT svec upsample(const svec &v, int usf);
273 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT ivec upsample(const ivec &v, int usf);
275 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bvec upsample(const bvec &v, int usf);
276 
278 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT mat upsample(const mat &v, int usf);
280 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat upsample(const cmat &v, int usf);
282 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT smat upsample(const smat &v, int usf);
284 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT imat upsample(const imat &v, int usf);
286 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bmat upsample(const bmat &v, int usf);
287 
289 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void upsample(const vec &v, int usf, vec &u);
291 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void upsample(const cvec &v, int usf, cvec &u);
293 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void upsample(const svec &v, int usf, svec &u);
295 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void upsample(const ivec &v, int usf, ivec &u);
297 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void upsample(const bvec &v, int usf, bvec &u);
298 
300 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void upsample(const mat &v, int usf, mat &u);
302 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void upsample(const cmat &v, int usf, cmat &u);
304 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void upsample(const smat &v, int usf, smat &u);
306 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void upsample(const imat &v, int usf, imat &u);
308 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void upsample(const bmat &v, int usf, bmat &u);
309 
311 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT vec lininterp(const vec &v, int usf);
313 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec lininterp(const cvec &v, int usf);
314 
316 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT mat lininterp(const mat &v, int usf);
318 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat lininterp(const cmat &v, int usf);
319 
321 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void lininterp(const vec &v, int usf, vec &u);
323 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void lininterp(const cvec &v, int usf, cvec &u);
324 
326 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void lininterp(const mat &v, int usf, mat &u);
328 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void lininterp(const cmat &v, int usf, cmat &u);
329 
331 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT mat lininterp(const mat &m, double f_base, double f_ups, int nrof_samples, double t_start);
333 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat lininterp(const cmat &m, double f_base, double f_ups, int nrof_samples, double t_start);
334 
336 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT vec lininterp(const vec &v, double f_base, double f_ups, int nrof_samples, double t_start);
338 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec lininterp(const cvec &v, double f_base, double f_ups, int nrof_samples, double t_start);
339 
341 
342 } // namespace itpp
343 
344 #endif // #ifndef RESAMPLING_H
345 
SourceForge Logo

Generated on Sat Jul 6 2013 10:54:25 for IT++ by Doxygen 1.8.2