# Output Length of signal filtered using FIR Filters

46 views (last 30 days)
Sanket on 21 Apr 2011
[EDIT: 20110622 20:11 - reformat - WDR]
Hello Experts,
The output of the FIR filters is convolution of the input signal and the filter kernel. In that case, the length of the output signal should be greater than input signal by M-1 points where M is the length of the filter kernel.
x=ecg(500)'+0.25*randn(500,1); %noisy waveform
h=fdesign.lowpass('Fp,Fst,Ap,Ast',0.15,0.2,1,60);
d=design(h,'equiripple'); %Lowpass FIR filter
y=filtfilt(d.Numerator,1,x); %zero-phase filtering
y1=filter(d.Numerator,1,x); %conventional filtering
In the above code, the length of the output is same as the length of my input signal even though I have implemented FIR filtering.
Can someone explain the reason of same length of the output signal? I expected my output signal to be greater than input signal.
Does MATLAB use convolution for filtering?
Sanket

Honglei Chen on 23 Jun 2011
Hi Sanket,
You are correct that if you do convolution, you get a sequence longer than your original signal. However, those extra samples are actually the output due to the internal filter states. I would suggest you to compare the following three results:
x = ones(10,1);
h = ones(10,1);
y1 = conv(h,x);
y2 = filter(h,x);
[y3, zf] = filter(h,x);
You can see that y1 is basically the combination of y3 and zf. It is worth noting that this is only true for a direct form II transposed implementation, but nevertheless it captures the idea. In some sense, you can think that the extra samples you got from convolution is as if the next input is all zero. In real applications, such scenario rarely happens. In addition, it is often the case that one needs to filter continuous data blocks, therefore, the filter needs to retain the original state so that it can properly filter the next data block. This is why the filter command gives the output the same length as the input.
HTH.
##### 2 CommentsShowHide 1 older comment
Honglei Chen on 23 Jun 2011
Yes, this is correct. If you have continuous blocks, you should use zf of the current block as the initial condition for the next block. If you look at the doc for filter, you can see it takes an extra input as the initial condition. By hooking those things together, you can work with continuous blocks.
Alternatively, if you have DSP System Toolbox, you can use dsp.DigitalFilter to achieve the same thing. You can invoke the filter in a loop and the filter will automatically keep track of states for you.

### More Answers (2)

Jarrod Rivituso on 22 Apr 2011
I believe generally convolution is used, though I am no signal processing expert so I can't speak to all filter variations.
The doc page for the basic filter function explains the exact equation used, which looks to me to be pretty much a convolution:
The simple example they have there is telling:
>> data = [1:0.2:4]';
>> windowSize = 5;
>> filter(ones(1,windowSize)/windowSize,1,data)
ans =
0.2000
0.4400
0.7200
1.0400
1.4000
1.6000
1.8000
2.0000
2.2000
2.4000
2.6000
2.8000
3.0000
3.2000
3.4000
3.6000
The first output is 0.2, which kinda shows that the beginning of the vector is padded with zeros, in this case. Again, I'm not sure what other filter variations do.
If you want a true convolution, try the convn function. It will (optionally) output vectors that are longer than the input:
>> data = [1:0.2:4]';
>> windowSize = 5;
>> convn(ones(1,windowSize)/windowSize,1,data)
ans =
0.2000
0.4400
0.7200
1.0400
1.4000
1.6000
1.8000
2.0000
2.2000
2.4000
2.6000
2.8000
3.0000
3.2000
3.4000
3.6000
2.9600
2.2800
1.5600
0.8000

Sanket on 22 Apr 2011
Thanks for your reply Jarrod. The equation in the link seems to be for IIR filter (past outputs also present). I would be really surprised, if they compute the FIR output by simply making the past outputs as zero in the equation shown in the link.
I posted this question more for conceptual reasons. I am happy with the output and I am sure its accurate. But, I would feel better (conceptually) if I would know the computations for FIR filter.
I tried to search online, everyone talks about FIR and convolution. But I have found no links which answers my question.