IT++ Logo

resampling.h

Go to the documentation of this file.
00001 
00030 #ifndef RESAMPLING_H
00031 #define RESAMPLING_H
00032 
00033 #include <itpp/base/mat.h>
00034 
00035 
00036 namespace itpp
00037 {
00038 
00044 
00045 template<class T>
00046 Vec<T> repeat(const Vec<T> &v, int norepeats)
00047 {
00048   Vec<T> temp(v.length()*norepeats);
00049 
00050   for (int i = 0; i < v.length(); i++) {
00051     for (int j = 0;j < norepeats;j++)
00052       temp(i*norepeats + j) = v(i);
00053   }
00054   return temp;
00055 }
00056 
00058 template<class T>
00059 Mat<T> repeat(const Mat<T> &m, int norepeats)
00060 {
00061   Mat<T> temp(m.rows(), m.cols()*norepeats);
00062 
00063   for (int j = 0; j < m.cols(); j++) {
00064     for (int i = 0;i < norepeats;i++) {
00065       temp.set_col(j*norepeats + i, m.get_col(j));
00066     }
00067   }
00068   return temp;
00069 }
00070 
00072 template<class T>
00073 void upsample(const Vec<T> &v, int usf, Vec<T> &u)
00074 {
00075   it_assert_debug(usf >= 1, "upsample: upsampling factor must be equal or greater than one");
00076   u.set_size(v.length()*usf);
00077   u.clear();
00078   for (int i = 0;i < v.length();i++)
00079     u(i*usf) = v(i);
00080 }
00081 
00082 
00084 template<class T>
00085 Vec<T> upsample(const Vec<T> &v, int usf)
00086 {
00087   Vec<T> u;
00088   upsample(v, usf, u);
00089   return u;
00090 }
00091 
00093 template<class T>
00094 void upsample(const Mat<T> &v, int usf, Mat<T> &u)
00095 {
00096   it_assert_debug(usf >= 1, "upsample: upsampling factor must be equal or greater than one");
00097   u.set_size(v.rows(), v.cols()*usf);
00098   u.clear();
00099   for (int j = 0;j < v.cols();j++)
00100     u.set_col(j*usf, v.get_col(j));
00101 }
00102 
00104 template<class T>
00105 Mat<T> upsample(const Mat<T> &v, int usf)
00106 {
00107   Mat<T> u;
00108   upsample(v, usf, u);
00109   return u;
00110 }
00111 
00113 template<class T>
00114 void lininterp(const Mat<T> &m, int usf, Mat<T> &u)
00115 {
00116   it_assert_debug(usf >= 1, "lininterp: upsampling factor must be equal or greater than one");
00117   int L = (m.cols() - 1) * usf + 1;
00118   u.set_size(m.rows(), L);
00119   for (int i = 0; i < m.rows(); i++) {
00120     for (int j = 0; j < L - 1; j++)
00121       u(i, j) = (m(i, j / usf) + (j % usf) / ((double)usf) * (m(i, (j + usf) / usf) - m(i, j / usf)));
00122     u(i, L - 1) = m(i, m.cols() - 1);
00123   }
00124 }
00125 
00136 template<class T>
00137 Mat<T> lininterp(const Mat<T> &m, double f_base, double f_ups,
00138                  int nrof_samples, double t_start = 0)
00139 {
00140   double t_base = 1 / f_base;
00141   double t_ups = 1 / f_ups;
00142   int rows = m.rows();
00143   int cols = m.cols();
00144   it_assert_debug(f_ups > f_base, "lininterp(): upsampled frequency must be greater than base frequency");
00145   it_assert_debug((t_start >= 0) && (t_start < cols * t_base), "lininterp(): incorrect start time offset");
00146   it_assert_debug((nrof_samples * t_ups + t_start) <= (cols * t_base), "lininterp(): too many samples required or input data to short");
00147   Mat<T> u(rows, nrof_samples);
00148   double curr_time = t_start;
00149 
00150   int i = 0;
00151   int k = 0;
00152   while (i < cols - 1) {
00153     while ((curr_time < (i + 1) * t_base) && (k < nrof_samples)) {
00154       for (int j = 0; j < rows; j++) {
00155         u(j, k) = (m(j, i) * ((i + 1) * t_base - curr_time)
00156                    - m(j, i + 1) * (i * t_base - curr_time)) / t_base;
00157       }
00158       k++;
00159       curr_time += t_ups;
00160     }
00161     i++;
00162   }
00163   return u;
00164 }
00165 
00167 template<class T>
00168 Mat<T> lininterp(const Mat<T> &m, int usf)
00169 {
00170   Mat<T> u;
00171   lininterp(m, usf, u);
00172   return u;
00173 }
00174 
00176 template<class T>
00177 void lininterp(const Vec<T> &v, int usf, Vec<T> &u)
00178 {
00179   it_assert_debug(usf >= 1, "lininterp(): upsampling factor must be equal or greater than one");
00180   int L = (v.length() - 1) * usf + 1;
00181   u.set_size(L);
00182   for (int j = 0; j < L - 1; j++) {
00183     u(j) = (v(j / usf) + (j % usf) / ((double)usf) * (v((j + usf) / usf) - v(j / usf)));
00184   }
00185   u(L - 1) = v(v.length() - 1);
00186 }
00187 
00189 template<class T>
00190 Vec<T> lininterp(const Vec<T> &v, int usf)
00191 {
00192   Vec<T> u;
00193   lininterp(v, usf, u);
00194   return u;
00195 }
00196 
00207 template<class T>
00208 Vec<T> lininterp(const Vec<T> &v, double f_base, double f_ups,
00209                  int nrof_samples, double t_start = 0)
00210 {
00211   double t_base = 1 / f_base;
00212   double t_ups = 1 / f_ups;
00213   int len = v.length();
00214   it_assert_debug(f_ups > f_base, "lininterp(): upsampled frequency must be greater than base frequency");
00215   it_assert_debug((t_start >= 0) && (t_start < len * t_base), "lininterp(): incorrect start time offset");
00216   it_assert_debug((nrof_samples * t_ups + t_start) <= (len * t_base), "lininterp(): too many samples required or input data to short");
00217   Vec<T> u(nrof_samples);
00218   double curr_time = t_start;
00219 
00220   int i = 0;
00221   int k = 0;
00222   while (i < len - 1) {
00223     while ((curr_time < (i + 1) * t_base) && (k < nrof_samples)) {
00224       u(k) = (v(i) * ((i + 1) * t_base - curr_time)
00225               - v(i + 1) * (i * t_base - curr_time)) / t_base;
00226       k++;
00227       curr_time += t_ups;
00228     }
00229     i++;
00230   }
00231   return u;
00232 }
00233 
00238 #ifndef _MSC_VER
00239 
00240 // ----------------------------------------------------------------------
00241 // Instantiations
00242 // ----------------------------------------------------------------------
00243 
00245 extern template vec repeat(const vec &v, int norepeats);
00247 extern template cvec repeat(const cvec &v, int norepeats);
00249 extern template svec repeat(const svec &v, int norepeats);
00251 extern template ivec repeat(const ivec &v, int norepeats);
00253 extern template bvec repeat(const bvec &v, int norepeats);
00254 
00256 extern template mat repeat(const mat &m, int norepeats);
00258 extern template cmat repeat(const cmat &m, int norepeats);
00260 extern template smat repeat(const smat &m, int norepeats);
00262 extern template imat repeat(const imat &m, int norepeats);
00264 extern template bmat repeat(const bmat &m, int norepeats);
00265 
00267 extern template vec upsample(const vec &v, int usf);
00269 extern template cvec upsample(const cvec &v, int usf);
00271 extern template svec upsample(const svec &v, int usf);
00273 extern template ivec upsample(const ivec &v, int usf);
00275 extern template bvec upsample(const bvec &v, int usf);
00276 
00278 extern template mat upsample(const mat &v, int usf);
00280 extern template cmat upsample(const cmat &v, int usf);
00282 extern template smat upsample(const smat &v, int usf);
00284 extern template imat upsample(const imat &v, int usf);
00286 extern template bmat upsample(const bmat &v, int usf);
00287 
00289 extern template void upsample(const vec &v, int usf,  vec &u);
00291 extern template void upsample(const cvec &v, int usf,  cvec &u);
00293 extern template void upsample(const svec &v, int usf,  svec &u);
00295 extern template void upsample(const ivec &v, int usf,  ivec &u);
00297 extern template void upsample(const bvec &v, int usf,  bvec &u);
00298 
00300 extern template void upsample(const mat &v, int usf,  mat &u);
00302 extern template void upsample(const cmat &v, int usf,  cmat &u);
00304 extern template void upsample(const smat &v, int usf,  smat &u);
00306 extern template void upsample(const imat &v, int usf,  imat &u);
00308 extern template void upsample(const bmat &v, int usf,  bmat &u);
00309 
00311 extern template vec lininterp(const vec &v, int usf);
00313 extern template cvec lininterp(const cvec &v, int usf);
00314 
00316 extern template mat lininterp(const mat &v, int usf);
00318 extern template cmat lininterp(const cmat &v, int usf);
00319 
00321 extern template void lininterp(const vec &v, int usf,  vec &u);
00323 extern template void lininterp(const cvec &v, int usf,  cvec &u);
00324 
00326 extern template void lininterp(const mat &v, int usf,  mat &u);
00328 extern template void lininterp(const cmat &v, int usf,  cmat &u);
00329 
00331 extern template mat lininterp(const mat &m, double f_base, double f_ups, int nrof_samples, double t_start);
00333 extern template cmat lininterp(const cmat &m, double f_base, double f_ups, int nrof_samples, double t_start);
00334 
00336 extern template vec lininterp(const vec &v, double f_base, double f_ups, int nrof_samples, double t_start);
00338 extern template cvec lininterp(const cvec &v, double f_base, double f_ups, int nrof_samples, double t_start);
00339 
00340 #endif
00341 
00342 } // namespace itpp
00343 
00344 #endif // #ifndef RESAMPLING_H
00345 
SourceForge Logo

Generated on Sun Jul 26 08:54:57 2009 for IT++ by Doxygen 1.5.9