IT++ Logo
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
34 
35 namespace itpp
36 {
37 
43 
44 template<class T>
45 Vec<T> repeat(const Vec<T> &v, int norepeats)
46 {
47  Vec<T> temp(v.length()*norepeats);
48 
49  for (int i = 0; i < v.length(); i++) {
50  for (int j = 0;j < norepeats;j++)
51  temp(i*norepeats + j) = v(i);
52  }
53  return temp;
54 }
55 
57 template<class T>
58 Mat<T> repeat(const Mat<T> &m, int norepeats)
59 {
60  Mat<T> temp(m.rows(), m.cols()*norepeats);
61 
62  for (int j = 0; j < m.cols(); j++) {
63  for (int i = 0;i < norepeats;i++) {
64  temp.set_col(j*norepeats + i, m.get_col(j));
65  }
66  }
67  return temp;
68 }
69 
71 template<class T>
72 void upsample(const Vec<T> &v, int usf, Vec<T> &u)
73 {
74  it_assert_debug(usf >= 1, "upsample: upsampling factor must be equal or greater than one");
75  u.set_size(v.length()*usf);
76  u.clear();
77  for (int i = 0;i < v.length();i++)
78  u(i*usf) = v(i);
79 }
80 
81 
83 template<class T>
84 Vec<T> upsample(const Vec<T> &v, int usf)
85 {
86  Vec<T> u;
87  upsample(v, usf, u);
88  return u;
89 }
90 
92 template<class T>
93 void upsample(const Mat<T> &v, int usf, Mat<T> &u)
94 {
95  it_assert_debug(usf >= 1, "upsample: upsampling factor must be equal or greater than one");
96  u.set_size(v.rows(), v.cols()*usf);
97  u.clear();
98  for (int j = 0;j < v.cols();j++)
99  u.set_col(j*usf, v.get_col(j));
100 }
101 
103 template<class T>
104 Mat<T> upsample(const Mat<T> &v, int usf)
105 {
106  Mat<T> u;
107  upsample(v, usf, u);
108  return u;
109 }
110 
112 template<class T>
113 void lininterp(const Mat<T> &m, int usf, Mat<T> &u)
114 {
115  it_assert_debug(usf >= 1, "lininterp: upsampling factor must be equal or greater than one");
116  int L = (m.cols() - 1) * usf + 1;
117  u.set_size(m.rows(), L);
118  for (int i = 0; i < m.rows(); i++) {
119  for (int j = 0; j < L - 1; j++)
120  u(i, j) = (m(i, j / usf) + (j % usf) / ((double)usf) * (m(i, (j + usf) / usf) - m(i, j / usf)));
121  u(i, L - 1) = m(i, m.cols() - 1);
122  }
123 }
124 
135 template<class T>
136 Mat<T> lininterp(const Mat<T> &m, double f_base, double f_ups,
137  int nrof_samples, double t_start = 0)
138 {
139  double t_base = 1 / f_base;
140  double t_ups = 1 / f_ups;
141  int rows = m.rows();
142  int cols = m.cols();
143  it_assert_debug(f_ups > f_base, "lininterp(): upsampled frequency must be greater than base frequency");
144  it_assert_debug((t_start >= 0) && (t_start < cols * t_base), "lininterp(): incorrect start time offset");
145  it_assert_debug((nrof_samples * t_ups + t_start) <= (cols * t_base), "lininterp(): too many samples required or input data to short");
146  Mat<T> u(rows, nrof_samples);
147  double curr_time = t_start;
148 
149  int i = 0;
150  int k = 0;
151  while (i < cols - 1) {
152  while ((curr_time < (i + 1) * t_base) && (k < nrof_samples)) {
153  for (int j = 0; j < rows; j++) {
154  u(j, k) = (m(j, i) * ((i + 1) * t_base - curr_time)
155  - m(j, i + 1) * (i * t_base - curr_time)) / t_base;
156  }
157  k++;
158  curr_time += t_ups;
159  }
160  i++;
161  }
162  return u;
163 }
164 
166 template<class T>
167 Mat<T> lininterp(const Mat<T> &m, int usf)
168 {
169  Mat<T> u;
170  lininterp(m, usf, u);
171  return u;
172 }
173 
175 template<class T>
176 void lininterp(const Vec<T> &v, int usf, Vec<T> &u)
177 {
178  it_assert_debug(usf >= 1, "lininterp(): upsampling factor must be equal or greater than one");
179  int L = (v.length() - 1) * usf + 1;
180  u.set_size(L);
181  for (int j = 0; j < L - 1; j++) {
182  u(j) = (v(j / usf) + (j % usf) / ((double)usf) * (v((j + usf) / usf) - v(j / usf)));
183  }
184  u(L - 1) = v(v.length() - 1);
185 }
186 
188 template<class T>
189 Vec<T> lininterp(const Vec<T> &v, int usf)
190 {
191  Vec<T> u;
192  lininterp(v, usf, u);
193  return u;
194 }
195 
206 template<class T>
207 Vec<T> lininterp(const Vec<T> &v, double f_base, double f_ups,
208  int nrof_samples, double t_start = 0)
209 {
210  double t_base = 1 / f_base;
211  double t_ups = 1 / f_ups;
212  int len = v.length();
213  it_assert_debug(f_ups > f_base, "lininterp(): upsampled frequency must be greater than base frequency");
214  it_assert_debug((t_start >= 0) && (t_start < len * t_base), "lininterp(): incorrect start time offset");
215  it_assert_debug((nrof_samples * t_ups + t_start) <= (len * t_base), "lininterp(): too many samples required or input data to short");
216  Vec<T> u(nrof_samples);
217  double curr_time = t_start;
218 
219  int i = 0;
220  int k = 0;
221  while (i < len - 1) {
222  while ((curr_time < (i + 1) * t_base) && (k < nrof_samples)) {
223  u(k) = (v(i) * ((i + 1) * t_base - curr_time)
224  - v(i + 1) * (i * t_base - curr_time)) / t_base;
225  k++;
226  curr_time += t_ups;
227  }
228  i++;
229  }
230  return u;
231 }
232 
237 #ifndef _MSC_VER
238 
239 // ----------------------------------------------------------------------
240 // Instantiations
241 // ----------------------------------------------------------------------
242 
244 extern template vec repeat(const vec &v, int norepeats);
246 extern template cvec repeat(const cvec &v, int norepeats);
248 extern template svec repeat(const svec &v, int norepeats);
250 extern template ivec repeat(const ivec &v, int norepeats);
252 extern template bvec repeat(const bvec &v, int norepeats);
253 
255 extern template mat repeat(const mat &m, int norepeats);
257 extern template cmat repeat(const cmat &m, int norepeats);
259 extern template smat repeat(const smat &m, int norepeats);
261 extern template imat repeat(const imat &m, int norepeats);
263 extern template bmat repeat(const bmat &m, int norepeats);
264 
266 extern template vec upsample(const vec &v, int usf);
268 extern template cvec upsample(const cvec &v, int usf);
270 extern template svec upsample(const svec &v, int usf);
272 extern template ivec upsample(const ivec &v, int usf);
274 extern template bvec upsample(const bvec &v, int usf);
275 
277 extern template mat upsample(const mat &v, int usf);
279 extern template cmat upsample(const cmat &v, int usf);
281 extern template smat upsample(const smat &v, int usf);
283 extern template imat upsample(const imat &v, int usf);
285 extern template bmat upsample(const bmat &v, int usf);
286 
288 extern template void upsample(const vec &v, int usf, vec &u);
290 extern template void upsample(const cvec &v, int usf, cvec &u);
292 extern template void upsample(const svec &v, int usf, svec &u);
294 extern template void upsample(const ivec &v, int usf, ivec &u);
296 extern template void upsample(const bvec &v, int usf, bvec &u);
297 
299 extern template void upsample(const mat &v, int usf, mat &u);
301 extern template void upsample(const cmat &v, int usf, cmat &u);
303 extern template void upsample(const smat &v, int usf, smat &u);
305 extern template void upsample(const imat &v, int usf, imat &u);
307 extern template void upsample(const bmat &v, int usf, bmat &u);
308 
310 extern template vec lininterp(const vec &v, int usf);
312 extern template cvec lininterp(const cvec &v, int usf);
313 
315 extern template mat lininterp(const mat &v, int usf);
317 extern template cmat lininterp(const cmat &v, int usf);
318 
320 extern template void lininterp(const vec &v, int usf, vec &u);
322 extern template void lininterp(const cvec &v, int usf, cvec &u);
323 
325 extern template void lininterp(const mat &v, int usf, mat &u);
327 extern template void lininterp(const cmat &v, int usf, cmat &u);
328 
330 extern template mat lininterp(const mat &m, double f_base, double f_ups, int nrof_samples, double t_start);
332 extern template cmat lininterp(const cmat &m, double f_base, double f_ups, int nrof_samples, double t_start);
333 
335 extern template vec lininterp(const vec &v, double f_base, double f_ups, int nrof_samples, double t_start);
337 extern template cvec lininterp(const cvec &v, double f_base, double f_ups, int nrof_samples, double t_start);
338 
339 #endif
340 
341 } // namespace itpp
342 
343 #endif // #ifndef RESAMPLING_H
344 
SourceForge Logo

Generated on Fri Mar 21 2014 17:14:14 for IT++ by Doxygen 1.8.1.2