glue_conv_meat.hpp

Go to the documentation of this file.
00001 // Copyright (C) 2010 NICTA and the authors listed below
00002 // http://nicta.com.au
00003 // 
00004 // Authors:
00005 // - Conrad Sanderson (conradsand at ieee dot org)
00006 // 
00007 // This file is part of the Armadillo C++ library.
00008 // It is provided without any warranty of fitness
00009 // for any purpose. You can redistribute this file
00010 // and/or modify it under the terms of the GNU
00011 // Lesser General Public License (LGPL) as published
00012 // by the Free Software Foundation, either version 3
00013 // of the License or (at your option) any later version.
00014 // (see http://www.opensource.org/licenses for more info)
00015 
00016 
00017 //! \addtogroup glue_conv
00018 //! @{
00019 
00020 
00021 //! rudimentary implementation of the convolution operation
00022 
00023 template<typename T1, typename T2>
00024 inline
00025 void
00026 glue_conv::apply(Mat<typename T1::elem_type>& out, const Glue<T1,T2,glue_conv>& X)
00027   {
00028   arma_extra_debug_sigprint();
00029   
00030   
00031   typedef typename T1::elem_type eT;
00032   
00033   const unwrap_check<T1> A_tmp(X.A, out);
00034   const unwrap_check<T2> B_tmp(X.B, out);
00035   
00036   const Mat<eT>& A = A_tmp.M;
00037   const Mat<eT>& B = B_tmp.M;
00038   
00039   arma_debug_check( ( (A.is_vec() == false) || (B.is_vec() == false) ), "conv(): inputs must be vectors"  );
00040   arma_debug_check( ( (A.n_elem   == 0    ) || (B.n_elem   == 0    ) ), "conv(): zero-length input given" );
00041   
00042   const Mat<eT>& h = (A.n_elem <= B.n_elem) ? A : B;
00043   const Mat<eT>& x = (A.n_elem <= B.n_elem) ? B : A;
00044   
00045   
00046   const u32   h_n_elem = h.n_elem;
00047   const u32   x_n_elem = x.n_elem;
00048   const u32 out_n_elem = h_n_elem + x_n_elem - 1;
00049   
00050   
00051   (A.n_cols == 1) ? out.set_size(out_n_elem, 1) : out.set_size(1, out_n_elem);
00052   
00053   
00054   const eT*   h_mem = h.memptr();
00055   const eT*   x_mem = x.memptr();
00056         eT* out_mem = out.memptr();
00057   
00058   
00059   for(u32 out_i = 0; out_i < (h_n_elem-1); ++out_i)
00060     {
00061     eT acc = eT(0);
00062     
00063     u32 h_i = out_i;
00064     
00065     for(u32 x_i = 0; x_i <= out_i; ++x_i, --h_i)
00066       {
00067       acc += h_mem[h_i] * x_mem[x_i];
00068       }
00069     
00070     out_mem[out_i] = acc;
00071     }
00072   
00073   
00074   for(u32 out_i = h_n_elem-1; out_i < out_n_elem - (h_n_elem-1); ++out_i)
00075     {
00076     eT acc = eT(0);
00077    
00078     u32 h_i = h_n_elem - 1;
00079     
00080     for(u32 x_i = out_i - h_n_elem + 1; x_i <= out_i; ++x_i, --h_i)
00081       {
00082       acc += h_mem[h_i] * x_mem[x_i];
00083       }
00084       
00085     out_mem[out_i] = acc;
00086     }
00087   
00088   
00089   for(u32 out_i = out_n_elem - (h_n_elem-1); out_i < out_n_elem; ++out_i)
00090     {
00091     eT acc = eT(0);
00092     
00093     u32 h_i = h_n_elem - 1;
00094     
00095     for(u32 x_i = out_i - h_n_elem + 1; x_i < x_n_elem; ++x_i, --h_i)
00096       {
00097       acc += h_mem[h_i] * x_mem[x_i];
00098       }
00099     
00100     out_mem[out_i] = acc;
00101     }
00102   
00103   
00104   }
00105 
00106 
00107 
00108 //! @}