MATLAB Answers

C++ code generated by Matlab coder too slow

74 ビュー (過去 30 日間)
Josef Shrbeny
Josef Shrbeny 2019 年 2 月 12 日
コメント済み: Ryan Livingston 2019 年 2 月 13 日
I have an algorithm written in matlab, which detects some artefatcs in blood pulsewave. These algorithm is converted to C++ by maltab coder and I use these C++ files in my C# SW. It works fine.
But, I use several FIRs in my code, one of them is 10000 order FIR. The signal length is around 1M samples, so it take too much time to count it with filter(...) function or conv(..) function. I have tried to count the FIR with convolution theorem, I have used this:
fftLen = 2^nextpow2(length(values));
filteredValues = ifft(fft(values,fftLen).*fft(coefs,fftLen));
It works more then 30x faster in matlab. But if I convert the code to C++ by matlab Coder, it is not so fast. It is more then 10x slower than the matlab version. I use matlab 2017b. Why?

  5 件のコメント

表示 2 件の古いコメント
Walter Roberson
Walter Roberson 2019 年 2 月 12 日
MATLAB detects some patterns of code, and if it finds that the arrays are "large enough" to make it worth while, automatically invokes BLAS or LAPACK libraries to do the work. Those libraries are multi-threaded even without the Parallel Computing toolbox.
Preethi 2019 年 2 月 13 日
Hi Walter,
BLAS and LAPACK are applicable from which version of MATLAB
Walter Roberson
Walter Roberson 2019 年 2 月 13 日
Multithreaded was added in R2007a according to Loren's blog of March 1, 2007.



Ryan Livingston
Ryan Livingston 2019 年 2 月 13 日
編集済み: Ryan Livingston 2019 年 2 月 13 日
If the bottleneck in the generated code is in fft or other MATLAB builtin functions, I'd suggest taking a look at this answer:
In short, those functions use highly optimized libraries in MATLAB as Walter mentioned and also for computing FFTs. The default generated code may be less optimized in favor of portability.
The answer describes that as of R2017b you can instruct MATLAB Coder and Simulink Coder to generate calls to FFTW to improve FFT performance:
There are similar capabilities to generate calls to BLAS and LAPACK for matrix operations and linear algebra:
Note: When generating C++ code that calls FFTW there is a bug in R2017b that was fixed in R2017b_update5:
So if you would like to use FFTW with Coder and generate C++ in R2017b, I'd suggest installing that update.

  2 件のコメント

Josef Shrbeny
Josef Shrbeny 2019 年 2 月 13 日
I have tried FFTW library and it works very fast now
Tnank you
Ryan Livingston
Ryan Livingston 2019 年 2 月 13 日
Great to hear!


その他の回答 (0 件)





Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by