# Simulate butterworth filter with initial condition of state vector in R

I'm working in R with the packages signal and control (don't know wether these are even the best for my use case).

I have a high pass filter (butterworth, of order 1, some chosen cutoff frequency) and a (onedimensional) signal $$s$$.

I would like to filter $$s$$ using my high pass filter in a way, that pretends, that my high pass filter has previously seen the input $$s$$ for an infinite amount of time.

I got a Python script, that does this using functions from the package signal. The procedure is as follows:

• Create the filter (in tf-representation)
• Switch to state space representation Calculate the initial condition for the state vector
• Pass the filter, the input, the initial condition for the state vector to the function lsim2 of Python's signal package.
• There you have the results

In R I can accomplish all but the second to last step. Using the lsim function of control package doe not give the same results as in the Python scripts. I also tried the filter function from the package signal but this doesn't seem to allow me to provide an initial condition for the state space vectors.

Any hints on how to achieve this would be greatly appreciated.

I'm coming over from math.stackexchange so this is my first question. Please be gentle, if due to my lack of experience here, the question is not perfectly posed.

Thanks in advance!

## 1 Answer

If you want to filter signals with an IIR filter, it's best to use the "standard" filter function of the language. That would be filter() for Matlab, lfilter() for Python and for R I believe this is signal::filter().

All of these functions allow you to specify the initial state. The exact details of how this is done, depends on the specific implementation of the filter.

If you don't specify any initial state the functions typically assume it's zero.

If I understand you correctly, you want to initialize the filter with the condition of $$s[n] = s, n < 0$$, i.e. a non-zero constant.

Apparently signal::filter() allows you to specify the arguments. initX and initY. I would assume the length of this arguments must match the filter order. You can set initX= s, for a first order filter. For an Nth order filter you would have to repeat s N times. You would also have to calculate the steady state output for a constant input, but since it's a high pass filter, that should just be zero, so initY = 0 shuld work.

CAVEAT: I don't speak R and don't have an R development system to work with. This answer is derived from "best practices" and reading some documentation.