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
36namespace itpp
37{
38
45template<class T>
46Vec<T> repeat(const Vec<T> &v, int norepeats)
47{
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
58template<class T>
59Mat<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
72template<class T>
73void 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
84template<class T>
85Vec<T> upsample(const Vec<T> &v, int usf)
86{
87 Vec<T> u;
88 upsample(v, usf, u);
89 return u;
90}
91
93template<class T>
94void 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
104template<class T>
105Mat<T> upsample(const Mat<T> &v, int usf)
106{
107 Mat<T> u;
108 upsample(v, usf, u);
109 return u;
110}
111
113template<class T>
114void 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
136template<class T>
137Mat<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
167template<class T>
168Mat<T> lininterp(const Mat<T> &m, int usf)
169{
170 Mat<T> u;
171 lininterp(m, usf, u);
172 return u;
173}
174
176template<class T>
177void 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
189template<class T>
190Vec<T> lininterp(const Vec<T> &v, int usf)
191{
192 Vec<T> u;
193 lininterp(v, usf, u);
194 return u;
195}
196
207template<class T>
208Vec<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");
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
239
240// ----------------------------------------------------------------------
241// Instantiations
242// ----------------------------------------------------------------------
243
245ITPP_EXPORT_TEMPLATE template ITPP_EXPORT vec repeat(const vec &v, int norepeats);
247ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec repeat(const cvec &v, int norepeats);
249ITPP_EXPORT_TEMPLATE template ITPP_EXPORT svec repeat(const svec &v, int norepeats);
251ITPP_EXPORT_TEMPLATE template ITPP_EXPORT ivec repeat(const ivec &v, int norepeats);
253ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bvec repeat(const bvec &v, int norepeats);
254
256ITPP_EXPORT_TEMPLATE template ITPP_EXPORT mat repeat(const mat &m, int norepeats);
258ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat repeat(const cmat &m, int norepeats);
260ITPP_EXPORT_TEMPLATE template ITPP_EXPORT smat repeat(const smat &m, int norepeats);
262ITPP_EXPORT_TEMPLATE template ITPP_EXPORT imat repeat(const imat &m, int norepeats);
264ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bmat repeat(const bmat &m, int norepeats);
265
267ITPP_EXPORT_TEMPLATE template ITPP_EXPORT vec upsample(const vec &v, int usf);
269ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec upsample(const cvec &v, int usf);
271ITPP_EXPORT_TEMPLATE template ITPP_EXPORT svec upsample(const svec &v, int usf);
273ITPP_EXPORT_TEMPLATE template ITPP_EXPORT ivec upsample(const ivec &v, int usf);
275ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bvec upsample(const bvec &v, int usf);
276
278ITPP_EXPORT_TEMPLATE template ITPP_EXPORT mat upsample(const mat &v, int usf);
280ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat upsample(const cmat &v, int usf);
282ITPP_EXPORT_TEMPLATE template ITPP_EXPORT smat upsample(const smat &v, int usf);
284ITPP_EXPORT_TEMPLATE template ITPP_EXPORT imat upsample(const imat &v, int usf);
286ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bmat upsample(const bmat &v, int usf);
287
289ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void upsample(const vec &v, int usf, vec &u);
291ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void upsample(const cvec &v, int usf, cvec &u);
293ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void upsample(const svec &v, int usf, svec &u);
295ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void upsample(const ivec &v, int usf, ivec &u);
297ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void upsample(const bvec &v, int usf, bvec &u);
298
300ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void upsample(const mat &v, int usf, mat &u);
302ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void upsample(const cmat &v, int usf, cmat &u);
304ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void upsample(const smat &v, int usf, smat &u);
306ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void upsample(const imat &v, int usf, imat &u);
308ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void upsample(const bmat &v, int usf, bmat &u);
309
311ITPP_EXPORT_TEMPLATE template ITPP_EXPORT vec lininterp(const vec &v, int usf);
313ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec lininterp(const cvec &v, int usf);
314
316ITPP_EXPORT_TEMPLATE template ITPP_EXPORT mat lininterp(const mat &v, int usf);
318ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat lininterp(const cmat &v, int usf);
319
321ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void lininterp(const vec &v, int usf, vec &u);
323ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void lininterp(const cvec &v, int usf, cvec &u);
324
326ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void lininterp(const mat &v, int usf, mat &u);
328ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void lininterp(const cmat &v, int usf, cmat &u);
329
331ITPP_EXPORT_TEMPLATE template ITPP_EXPORT mat lininterp(const mat &m, double f_base, double f_ups, int nrof_samples, double t_start);
333ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat lininterp(const cmat &m, double f_base, double f_ups, int nrof_samples, double t_start);
334
336ITPP_EXPORT_TEMPLATE template ITPP_EXPORT vec lininterp(const vec &v, double f_base, double f_ups, int nrof_samples, double t_start);
338ITPP_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
General array class.
Definition array.h:105
void set_size(int n, bool copy=false)
Resizing an Array<T>.
Definition array.h:257
int length() const
Returns the number of data elements in the array object.
Definition array.h:157
#define it_assert_debug(t, s)
Abort if t is not true and NDEBUG is not defined.
Definition itassert.h:107
Matrix Class Definitions.
Mat< bin > bmat
bin matrix
Definition mat.h:508
itpp namespace
Definition itmex.h:37

Generated on Tue Aug 17 2021 10:59:15 for IT++ by Doxygen 1.9.8