dsp.h

Go to the documentation of this file.
00001 /*
00002  *    OpenRDK : OpenSource Robot Development Kit
00003  *    Copyright (C) 2007, 2008  Daniele Calisi, Andrea Censi (<first_name>.<last_name>@dis.uniroma1.it)
00004  *
00005  *    This program is free software: you can redistribute it and/or modify
00006  *    it under the terms of the GNU General Public License as published by
00007  *    the Free Software Foundation, either version 3 of the License, or
00008  *    (at your option) any later version.
00009  *
00010  *    This program is distributed in the hope that it will be useful,
00011  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *    GNU General Public License for more details.
00014  *
00015  *    You should have received a copy of the GNU General Public License
00016  *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017  *
00018  */
00019 
00020 #ifndef RDK2_GEOMETRY_DSP
00021 #define RDK2_GEOMETRY_DSP
00022 
00023 #include <cmath>
00024 #include <algorithm>
00025 #include <sstream>
00026 #include <assert.h>
00027 #include <cmath>
00028 #include <vector>
00029 #include <rdkcore/geometry/dmatrix.h>
00030 
00031 namespace RDK2 { namespace Geometry {
00032     
00035     template <typename X, typename Y>
00036     void normalize(const X * x, size_t length, Y * y, Y nMin, Y nMax) {
00037         if(!length) return;
00038         
00039         X cMin = x[0];
00040         X cMax = cMin;
00041         
00042         for(size_t i=0;i<length;i++) {
00043             cMin = std::min(cMin, x[i]);
00044             cMax = std::max(cMax, x[i]);
00045         }
00046     
00047         if(cMin==cMax) {
00048             for(size_t i=0;i<length;i++)
00049                 y[i] = (Y) nMin;
00050         } else {
00051             for(size_t i=0;i<length;i++)
00052                 y[i] = (Y) (nMin + (nMax-nMin)*(x[i]-cMin)/(cMax-cMin));
00053         }
00054     }
00055 
00058     template <typename X, typename Y>
00059     void normalize_sum(const X * x, size_t length, Y * y, Y tot) {
00060         Y sum = 0;
00061         for(size_t t=0;t<length;t++) 
00062             sum += x[t];
00063                 
00064         //assert(sum!=0);
00065         
00066         double isum = sum!=0?tot/sum:1;
00067             
00068         for(size_t t=0;t<length;t++) 
00069             y[t] = x[t] * isum;
00070     }
00071 
00072     
00073     template<class Numeric>
00074     Numeric modulus(Numeric a, Numeric b) {
00075         return (a%b+b)%b;
00076     }
00077     
00079     template<class E, class F, class R>
00080     R mult(const E*r1, const F*r2, int size) {
00081         R r=0;
00082         for(int i=0;i< size ;i++) {
00083             E e = r1[i];
00084             F f = r2[i];
00085             r += (R) (e*f);
00086         }
00087         return r;
00088     }
00089     
00095     template<class E, class F, class R>
00096     void cross_correlation(
00097         const E* r1,  int r1_size, 
00098         const F* r2,  int r2_size, 
00099         int deltaMin, int deltaMax, R* result) 
00100     {
00101         assert(deltaMax>deltaMin);
00102         int result_size = deltaMax - deltaMin + 1;
00103         for(int a=0;a<result_size;a++) {
00104             int diff = deltaMin + a;
00105             int r1_offset, r2_offset;
00106             
00107             if(diff>0) {
00108                 r1_offset = diff;
00109                 r2_offset = 0;
00110             } else {
00111                 r1_offset = 0;
00112                 r2_offset = -diff;
00113             }
00114             
00115             int len = std::min(r1_size-r1_offset, r2_size-r2_offset);
00116             
00117             result[a] = mult<E,F,R>( r1+r1_offset, r2+r2_offset, len);
00118         }
00119     }
00120 
00121 
00122 
00124     template <typename X, typename Y>
00125     DMatrix<Y> normalize(const DMatrix<X>& m, Y nMin, Y nMax) {
00126         DMatrix<Y> result(m.rows(), m.columns());
00127         normalize<X,Y>(m.getElems(), m.rows()*m.columns(), result.getElems(), nMin, nMax);
00128         return result;
00129     }
00130 
00132     template<class X>
00133     std::vector<X> getAsVector(DMatrix<X>& m) {
00134         std::vector<X> v;
00135         for(int i=0;i<m.rows();i++)
00136             for(int j=0;j<m.columns();j++)
00137                 v.push_back(m[i][j]);
00138         return v;
00139     }
00140     
00143     template<class X, class Y,class Z>
00144     void convolve(
00145         const DMatrix<X>& x, 
00146         const DMatrix<Y>& y,
00147         DMatrix<Z>& z) {
00148         // x and z of same size
00149         assert(z.columns() == x.columns());
00150         assert(z.rows() == x.rows());
00151         // y should be square and of odd size
00152         assert(y.rows() == y.columns());
00153         assert(y.rows()%2);
00154         
00155         for(int i=0; i<z.rows(); i++)
00156         for(int j=0; j<z.columns(); j++) {
00157             size_t m = y.rows() >> 1;
00158             DMatrix<X> around = x.around(i, j,  m);
00159             
00160             double value;
00161             y.product(around, value);
00162             z[i][j] = value;
00163         }
00164     }
00165 
00166 
00167     template<class X>
00168     X computeAverage(const std::vector<X>& values) {
00169         assert(values.size());
00170         X accumulator=0;
00171         for(size_t a=0;a<values.size();a++)
00172             accumulator+=values[a];
00173             
00174         return accumulator/values.size();
00175     }
00176     
00177     
00178     template<class X>
00179     X computeVariance(const std::vector<X>& values) {
00180         assert(values.size());
00181         X average = computeAverage(values);
00182         X accumulator=0;
00183         for(size_t a=0;a<values.size();a++)
00184             accumulator += square(values[a]-average);
00185         return accumulator/values.size();
00186     }
00187     
00188     template<class X>
00189     X computeSigma(const std::vector<X>& values) {
00190         assert(values.size());
00191         return sqrt(computeVariance(values));
00192     }
00193     
00194 
00195 }} // ns
00196 
00197 #endif

Generated on Tue Mar 3 15:04:24 2009 for OpenRDK by  doxygen 1.5.6