# How Is the MATLAB filter() Function So Fast?

How is the filter() implemented in MATLAB? How is it so fast?
What's the fastest implementation of a FIR filter?

• You may look on the code of Octave and Scipy.
– Mark
Feb 23, 2021 at 5:35

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:

1. Utilizing Vectorized / SIMD Operations (SSE / AVX in x86).
3. Loop Unrolling.

Update (2021):
In recent MATLAB versions the function was accelerated using parallelization and vectorization.

• 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... Nov 18, 2019 at 13:32
• 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 with fft() or directly on data are usually memory bounded and the fft() approach stresses memory more.
– Royi
Nov 18, 2019 at 14:32
• By the way, MATLAB's conv() is much faster than filter() in the cases of FIR filters.
– Royi
Nov 18, 2019 at 14:33
• 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 ? Nov 18, 2019 at 17:54
• And about fft() version, yes the length should be suitable for fastest performance, otherwise you will not get the benefit... Nov 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