00001 // -*- C++ -*- 00002 00011 #include "Convolution.h" 00012 #include <vector> 00013 00014 Convolution::Convolution(double _dt, unsigned int _range) { 00015 integrator = Integrator(); 00016 setup(_dt, _range); 00017 } 00018 00019 Convolution::~Convolution(void) { 00020 } 00021 00022 void Convolution::reset(void) { 00023 f_buffer.clear(); 00024 g_buffer.clear(); 00025 integrator.reset(); 00026 buffer_size = 0; 00027 return; 00028 } 00029 00030 void Convolution::setup(double _dt, unsigned int _range) { 00031 dt = _dt; 00032 range = _range; 00033 integrator.setup(_dt, 0); // integrator range is inf (if range of integrator > 0, integrator remove f(0)*g(t) first) 00034 reset(); 00035 return; 00036 } 00037 00038 void Convolution::update (double _f, double _g) { 00039 f_buffer.push_back(_f); 00040 g_buffer.push_back(_g); 00041 buffer_size++; 00042 if (range > 0 && buffer_size > range) { // restrict buffer size 00043 f_buffer.pop_front(); 00044 g_buffer.pop_front(); 00045 buffer_size--; 00046 } 00047 return; 00048 } 00049 00050 double Convolution::calculate(void) { 00051 // calc f(x) * g(t-x) buffer 00052 std::vector<double> fg_buffer; 00053 for (int i = 0; i < buffer_size; i++) { 00054 fg_buffer.push_back(f_buffer[i] * g_buffer[(buffer_size -1) - i]); 00055 } 00056 // calc integration 00057 integrator.reset(); 00058 for (int i = 0; i < buffer_size; i++) { 00059 integrator.update(fg_buffer[i]); 00060 } 00061 return integrator.calculate(); 00062 }