Fastest method for calculating convolution

后端 未结 3 685
逝去的感伤
逝去的感伤 2021-02-04 12:45

Anybody know about the fastest method for calculating convolution? Unfortunately the matrix which I deal with is very large (500x500x200) and if I use convn in MATL

3条回答
  •  自闭症患者
    2021-02-04 13:01

    i have 2 way to calc fastconv

    and 2 betther than 1

    1- armadillo you can use armadillo library for calcing 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 for calcing fast conv in this way you must init fft of your code in 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);
    }
    

提交回复
热议问题