# Initial conditions for the filter delays

32 views (last 30 days)
hoda kazemzadeh on 22 Jun 2018
Edited: Vasishta Kanthi on 24 Sep 2018
How can I use initial conditions for the filter delays when I have to use filter function in matlab?
[y1,zi] = filter(b,a,x1);
[y2]=filter(b,a, x2,zi) ;
y=[y1;y2];
Is that what it should be?

Vasishta Kanthi on 24 Sep 2018
Edited: Vasishta Kanthi on 24 Sep 2018
Long Answer: It depends on y1 and y2... lets say:
x=randn(1,10000);
and you want to filter this signal, but instead of the whole signal, you do it in blocks, such that:
x1=x(1:5000);
x2=x(5001:end);
then your code will definitely work, because in order to have continuity, the delay of block x1 shud be given to block x2. Although,
[y1,zi]=filter(b,a,x1);
y2=filter(b,a,x2,zi);
y=[y1;y2];
the 'y=[y1;y2]' is a bit strange to me... in the sense, im not sure how your vectors are called upon... if 'x' is a column vector, then this works, otherwise, this is definitely odd to use. Instead, it should be y=[y1,y2]. This way, you preserve the continuity by having a vector, instead of a matrix.
if your signal is a stereo signal, then you definitely shouldn't introduce any intitialization …. the sound will be completely odd... lets say:
x=randn(2,10000);
y1=x(1,:);
y2=x(2,:);
y=[y1;y2];
Then this becomes a stereo signal. So,
[b,a]=butter(n,w0,'low')
filtered_signal=filter(b,a,y);
is the right way, and both the channels will have the same filter delay. I hope this is what you're looking for :-)
I used the 'butter' filter type as an example. In general, all filters, regardless, FIR or IIR, they all introduce delay, and so, if you really want to keep the output phase response the same as the input phase response, then for FIR, use the grpdelay() function to calculate the delay samples, and shift your time signal accordingly, and for IIR, use filtfilt() function instead of filter(), because IIR filter delay is frequency dependent.
Cheers!!!