- Please make sure that you are running the MEX file (not the .m function). Typically codegen command on function 'foo' will produce a 'foo_mex' file. Similarly you will be having 'filter_test_mex' file.
- Using rand() function for testing performance may not be a good idea, since it my produce different numbers for two different cases. Try to save the values from rand() in a mat file and use the same values for different experiments.
Poor MEX performance when running filtfilt
3 views (last 30 days)
Show older comments
Hi all,
I'm currently on the journey of converting my MATLAB data processing script into C. While testing some part of it (filtering data using filtfilt), I noticed that running the generated MEX file is MUCH slower than running the MATLAB function directly.
This is my MATLAB function:
function filter_test(data)
%$codegen
coder.inline('never');
% Initialise filtered data
f_data = data;
% Set the filter coeficients to remove the 50Hz and its harmonics
filter_coeficients = struct();
filter_coeficients.coef_50Hz = [...]; %1x4467 double
filter_coeficients.coef_100Hz = [...]; %1x4467 double
filter_coeficients.coef_150Hz = [...]; %1x4467 double
filter_coeficients.coef_200Hz = [...]; %1x4467 double
filter_coeficients.coef_250Hz = [...]; %1x4467 double
filter_coeficients.coef_300Hz = [...]; %1x4467 double
filter_coeficients.coef_350Hz = [...]; %1x4467 double
filter_coeficients.coef_400Hz = [...]; %1x4467 double
filter_coeficients.coef_450Hz = [...]; %1x4467 double
% Apply all the filters
filter_coeficients_id = fieldnames(filter_coeficients);
for c=1:numel(filter_coeficients_id)
f_data = filtfilt(filter_coeficients.(filter_coeficients_id{c}), 1, f_data);
end
end
I generated the MEX file (.mexw64) with the following command:
codegen('filter_test', '-args', {coder.typeof(double(0), [Inf Inf])})
This was one of the test I made:
data = rand(30000,1);
tic
filter_test(data); % Elapsed time is 1.914894 seconds.
toc
tic
filter_test_mex(data); % Elapsed time is 492.794533 seconds.
toc
The difference is abysmal! Forcing the code to coder.inline('always') made this even worse, as the run time reaches 997.7 seconds.
Am I doing something wrong? I guess the C performance will be similar to the MEX performance? Why is the MEX so slow when compared to MATLAB?
7 Comments
Wilson A N
on 2 Dec 2020
Hi Diogo,
You can save the filter coefficients as a .mat file and attach it here.
Wilson A N
on 11 Dec 2020
Edited: Wilson A N
on 11 Dec 2020
Hi Diogo,
I tried using some random filter coefficients to try and reproduce the issue but I was not able to observe the slowdown you had reported. Below are my results:
MATLAB Simulation time is
1.4649
MEX Simulation time is
3.4633
SIL Simulation time is
4.3738
(SIL simulation is actually checking lib/dll time)
Please update the MATLAB Release in which you are facing the issue along with the filter coefficients. This will help us to look into the issue further.
Answers (1)
Jan
on 11 Dec 2020
Just a remark: With FiltFiltM (https://www.mathworks.com/matlabcentral/fileexchange/32261-filterm ) my Matlab 2018b needs 6.0 instead of 8.0 seconds as for filtfilt.
0 Comments
See Also
Categories
Find more on Performance and Memory in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!