Fixed-size FFT cash
Submitted by zha..@..ok.com
Assigned to Nobody
Link to original bugzilla bug (#675)
Version: 3.2
Description
I find a bug in a function of unsupported/Eigen/FFT file:
Line 221 :
void fwd( MatrixBase<ComplexDerived> & dst, const MatrixBase<InputDerived> & src, Index nfft=-1)
When nfft is bigger than the length of src, this bug appears.Such as:
#include <unsupported/Eigen/FFT>
#include<iostream>
int main()
{
Eigen::VectorXf timevec(10);
timevec.setRandom();
Eigen::VectorXcf freqvec;
Eigen::FFT<float> fft;
fft.fwd(freqvec, timevec,16); //16 is bigger than the length of timevec
std::cout<< "freqvec is:\n"<< freqvec <<std::endl;
return 0;
}
In the file I download, There are:
Line 242: Matrix<src_type,1,Dynamic> tmp;
if (src.size()<nfft) {
tmp.setZero(nfft);
tmp.block(0,0,src.size(),1 ) = src;
}
The statement tmp.block(0,0,src.size(),1 ) will beyond the Matrix.
It should be tmp.block(0,0,1 ,src.size()).
What’s more, when src is a Vector not a RowVector, program will crash when running.
So I think the correct code is:
Matrix<src_type,1,Dynamic> tmp;
if (src.size()<nfft) {
tmp.setZero(nfft);
if(src.cols() !=1)
tmp.block(0,0,1 ,src.size()) = src;
else
tmp.block(0,0,1 ,src.size()) = src.transpose();
}
The code above has been tested, and it works well. I think it maybe rewrite by using more professional condition statement to judge that src is a Vector or RowVector.