Main Page   Namespace List   Compound List   File List   Namespace Members   Compound Members  

convolution.h

Go to the documentation of this file.
00001 
00073 #ifndef __CONVOLUTION_FFT__
00074 #define __CONVOLUTION_FFT__
00075 
00076 #include <algorithm>
00077 
00078 #include "fftw3.h"
00079 
00080 #include "fft/support.h"
00081 
00082 
00083 
00084 namespace FFT{
00085 
00086   
00087   
00088 
00090   template<typename Function>
00091   inline void convolve(const Function& f,
00092                        const Function& g,
00093                        Function* const h);
00094 
00095   
00098   template<typename Function>
00099   inline void convolve(const Function& f,
00100                        const Function& g,
00101                        Function* const h,
00102                        Support&        support);
00103 
00104   
00111   template<typename Function>
00112   void convolve(const Support::buffer_type& F,
00113                 const Function& g,
00114                 Function* const h,
00115                 Support&        support);
00116 
00117   
00120   template<typename Function>
00121   void move_center(const Function&    f,
00122                    const unsigned int x,
00123                    const unsigned int y,
00124                    Function* const    result);
00125 
00126 
00127 
00128   
00129 
00130 
00131 /*
00132   ###########################################################
00133   ###########################################################
00134   ###########################################################
00135   ##############                               ##############
00136   ##############  I M P L E M E N T A T I O N  ##############
00137   ##############                               ##############
00138   ###########################################################
00139   ###########################################################
00140   ###########################################################
00141 */
00142 
00143   
00144   template<typename Function>
00145   void convolve(const Support::buffer_type& F,
00146                 const Function& g,
00147                 Function* const h,
00148                 Support&        support){
00149 
00150     // ################
00151     // ### FFT of g ###
00152     // ################
00153 
00154     // Store g data in the fftw structures.    
00155     support.load_space_data(g);
00156     
00157     // Compute the FFT.
00158     support.space_to_frequency();
00159 
00160 
00161 
00162     
00163     // Multiplication "in place" in the frequency space.    
00164     support.multiply_frequency_data_by_buffer(F,true);
00165 
00166 
00167     
00168 
00169     // ###################
00170     // ### Inverse FFT ###
00171     // ###################
00172     
00173     // Compute inverse FFT transform.
00174     support.frequency_to_space();
00175 
00176     // Return data into the original structures    
00177     support.save_space_data(h);
00178   }
00179 
00180 
00185   template<typename Function>
00186   void convolve(const Function& f,
00187                 const Function& g,
00188                 Function* const h,
00189                 Support&  support){
00190 
00191     // ################
00192     // ### FFT of f ###
00193     // ################
00194 
00195     // We need a temporary buffer.
00196     Support::buffer_type buffer = support.create_buffer();
00197 
00198     // Store f data in the fftw structures.
00199     support.load_space_data(f);
00200     
00201     // Compute the FFT.
00202     support.space_to_frequency();
00203     
00204     // Copy the result in a safe place.
00205     support.save_frequency_data_into_buffer(buffer);
00206 
00207 
00208     convolve(buffer,g,h,support);
00209     
00210     support.destroy_buffer(buffer);
00211     
00212 //     // ################
00213 //     // ### FFT of g ###
00214 //     // ################
00215 
00216 //     // Store g data in the fftw structures.    
00217 //     support.load_space_data(g);
00218     
00219 //     // Compute the FFT.
00220 //     support.space_to_frequency();
00221 
00222 
00223 
00224     
00225 //     // Multiplication "in place" in the frequency space.    
00226 //     support.multiply_frequency_data_by_buffer(buffer,true);
00227 
00228 
00229     
00230 
00231 //     // ###################
00232 //     // ### Inverse FFT ###
00233 //     // ###################
00234     
00235 //     // Compute inverse FFT transform.
00236 //     support.frequency_to_space();
00237 
00238 //     // Return data into the original structures    
00239 //     support.save_space_data(h);
00240 
00241   }
00242 
00243   
00244 
00245   template<typename Function>
00246   void convolve(const Function& f,
00247                 const Function& g,
00248                 Function* const h){
00249 
00250     Support s(f.x_size(),f.y_size());
00251 
00252     convolve(f,g,h,s);
00253   }
00254 
00255   
00256   template<typename Function>
00257   void move_center(const Function&    f,
00258                    const unsigned int x_center,
00259                    const unsigned int y_center,
00260                    Function* const    result){
00261 
00262     typedef unsigned int size_type;
00263 
00264     const size_type width  = f.x_size();
00265     const size_type height = f.y_size();
00266 
00267     Function g(width,height);
00268     
00269     for(size_type x=0;x<width;x++){
00270       for(size_type y=0;y<height;y++){
00271         (*result)(x,y) = f((x + width - x_center) % width,
00272                            (y + height - y_center) % height);
00273       }
00274     }
00275   }
00276 
00277 }
00278 
00279 #endif

Generated on Thu Aug 19 15:55:51 2004 by doxygen1.2.18