I have some MATLAB code that filters an input signal using filter
:
CUTOFF = 0.05;
FS = 5000;
[b, a] = butter(1, CUTOFF / (FS / 2), 'high');
% b = [0.99996859, -0.99996859]
% a = [1.0, -0.99993717]
dataAfter = filter(b, a, dataBefore);
I'm trying to convert this code to C#. I have already got the butter
function to work pretty fast, but now I'm stuck converting the filter
function.
I have read the MATLAB filter documentation and Python Scipy.signal filter documentation, but there is a term present in the transfer function definition that I don't understand.
Here is the "rational transfer function" definition from the linked documentation:
b[0] + b[1]z^(-1) + ... + b[M]z^(-M)
Y(z) = _______________________________________ X(z)
a[0] + a[1]z^(-1) + ... + a[N]z^(-N)
Correct me if i'm wrong, but z
is the current element of input data, and Y(z)
is the output?
If the above this is true, what is X(z)
in this equation?
I want to understand this to implement it in C#, if there is an equivalent option then please enlighten me.
In the
More About
section of the matlab docs as you pointed out, they describe:Rearranging:
Thus,
X(z)
is thez-domain
transform of the input vectorx
(seeDigital Filter
). It is important to mention that, also in the docs they give an alternate representation of the transfer function as adifference equation
Which lends itself better to be ported into code. One possible implementation in
C#
, could be (using this answer as reference
)Driver:
Output
UPDATE
If the coefficient vectors
a
andb
have a fixed length of 2 the filtering function can be simplified to: