How is the filter()
implemented in MATLAB? How is it so fast?
What's the fastest implementation of a FIR filter?
2 Answers
Actually it seems MATLAB implementation of the filter()
function is pretty straight forward and not fast.
For a fast implementation, have a look at FilterM by Jan Simon.
Update
In the latest releases of MATLAB (From R2016b and above) the performance of the filter()
function has improved.
The methods to accelerate those operations are usually based on:
- Utilizing Vectorized / SIMD Operations (SSE / AVX in
x86
). - Multi Threading.
- Loop Unrolling.
Update (2021):
In recent MATLAB versions the function was accelerated using parallelization and vectorization.
-
$\begingroup$ MATLAB uses a time domain Direct Form II Transposed Canonical structure to realize its filter() or conv() functions. It's not specifically designed to be fast. An FFT based implementation would be faster or fastest when data lengths are suitable... $\endgroup$– Fat32Nov 18, 2019 at 13:32
-
$\begingroup$ Actually
fft()
based will be faster only in the cases dimensions are really perfect for it which doesn't happen often in my experience. Filtration both withfft()
or directly on data are usually memory bounded and thefft()
approach stresses memory more. $\endgroup$– RoyiNov 18, 2019 at 14:32 -
$\begingroup$ By the way, MATLAB's
conv()
is much faster thanfilter()
in the cases of FIR filters. $\endgroup$– RoyiNov 18, 2019 at 14:33 -
$\begingroup$ I don't know your MATLAB version but in my(old) version its help says
The conv function is an M-file that uses the filter primitive.
... So I don't how how it could be faster ? May be newer versions use other approaches ? $\endgroup$– Fat32Nov 18, 2019 at 17:54 -
$\begingroup$ And about fft() version, yes the length should be suitable for fastest performance, otherwise you will not get the benefit... $\endgroup$– Fat32Nov 18, 2019 at 17:55
filter() implements an IIR-filter. You mention FIR filter. While IIR is a generalization of FIR, it makes some sense to think of them as two different things wrgt optimization.
Generally, MATLAB may use libraries or point optimizations implemented in FORTRAN, C or Assembly using algorithmic optimizations, as well as SIMD, multi threading or any other clever trick that makes the function faster while still deemed sufficiently general and accurate
Octave
andScipy
. $\endgroup$