I have 2 ways to calculate fastconv
and 2 more than 1
1- armadillo you can use the armadillo library to compute conv with this code
cx_vec signal(1024,fill::randn); cx_vec code(300,fill::randn); cx_vec ans = conv(signal,code);
2-use fftw ans sigpack and armadillo library to compute fast conv so you have to initialize the fft of your code in the constructor
FastConvolution::FastConvolution(cx_vec inpCode) { filterCode = inpCode; fft_w = NULL; } cx_vec FastConvolution::filter(cx_vec inpData) { int length = inpData.size()+filterCode.size(); if((length & (length - 1)) == 0) { } else { length = pow(2 , (int)log2(length) + 1); } if(length != fftCode.size()) initCode(length); static cx_vec zeroPadedData; if(length!= zeroPadedData.size()) { zeroPadedData.resize(length); } zeroPadedData.fill(0); zeroPadedData.subvec(0,inpData.size()-1) = inpData; cx_vec fftSignal = fft_w->fft_cx(zeroPadedData); cx_vec mullAns = fftSignal % fftCode; cx_vec ans = fft_w->ifft_cx(mullAns); return ans.subvec(filterCode.size(),inpData.size()+filterCode.size()-1); } void FastConvolution::initCode(int length) { if(fft_w != NULL) { delete fft_w; } fft_w = new sp::FFTW(length,FFTW_ESTIMATE); cx_vec conjCode(length,fill::zeros); fftCode.resize(length); for(int i = 0; i < filterCode.size();i++) { conjCode.at(i) = filterCode.at(filterCode.size() - i - 1); } conjCode = conj(conjCode); fftCode = fft_w->fft_cx(conjCode); }
javad
source share