Hello all, FIR filtering is simply a convolution, which is an associative operation. However in matlab, filtering a filter then applying that to a data sequence is different than filtering that data sequence twice. In other words: filter(f, 1, filter(f, 1, data)) is NOT the same as filter(filter(f, 1, f), data)) Why is that? --Shafik
FIR paradox in Matlab
Started by ●November 23, 2004
Reply by ●November 23, 20042004-11-23
Shafik wrote:> Hello all, > > FIR filtering is simply a convolution, which is an associative > operation. However in matlab, filtering a filter then applying that to > a data sequence is different than filtering that data sequence twice. > In other words: > > filter(f, 1, filter(f, 1, data)) is NOT the same as > filter(filter(f, 1, f), data)) > > > Why is that? > --Shafik >Usually, a filter alters the relative amplitudes and phases of a signal in a way that depends on frequency. What does it mean to filter an object or process -- in particular, to filter a filter? I guess that filter(f, 1, filter(f, 1, data)) actually performs filter(f, 1, data2), where data2 is filter(f, 1, data1). It is certainly possible to convolve a filters coefficients, thereby creating a longer filter with greater stop-band attenuation and more abrupt transition. Is that what you mean? Jerry -- Engineering is the art of making what you want from things you can get. �����������������������������������������������������������������������
Reply by ●November 23, 20042004-11-23
"Shafik" <shafik@u.arizona.edu> wrote in message news:1101249847.815845.34790@c13g2000cwb.googlegroups.com...> Hello all, > > FIR filtering is simply a convolution, which is an associative > operation. However in matlab, filtering a filter then applying that to > a data sequence is different than filtering that data sequence twice. > In other words: > > filter(f, 1, filter(f, 1, data)) is NOT the same as > filter(filter(f, 1, f), data))I think the second line should be: filter(filter(f, 1, f), 1, data)) i.e. you are missing a ", 1" That change is required to avoid a syntax error. But after changing it, the two outputs are still not symmetric.> Why is that?I'm not sure. Maybe it has something to do with initial filter states assumed by Matlab? But if you change the filtering of the filter to convolving the filter with itself, then it does work. In other words: filter(f, 1, filter(f, 1, data)) is the same as filter(conv(f, f), 1, data) I think the difference is that the filter function truncates the output vector length to match the input length, while the conv operation does not. Compare filter(f,1,f) to conv(f,f) and you will see the difference.
Reply by ●November 23, 20042004-11-23
"Jerry Avins" <jya@ieee.org> wrote in message news:30hu12F2v2cr7U1@uni-berlin.de...> Shafik wrote: > > > Hello all, > > > > FIR filtering is simply a convolution, which is an associative > > operation. However in matlab, filtering a filter then applying that to > > a data sequence is different than filtering that data sequence twice. > > In other words: > > > > filter(f, 1, filter(f, 1, data)) is NOT the same as > > filter(filter(f, 1, f), data)) > > > > > > Why is that? > > --Shafik > > > > Usually, a filter alters the relative amplitudes and phases of a signal > in a way that depends on frequency. What does it mean to filter an > object or process -- in particular, to filter a filter? I guess that > filter(f, 1, filter(f, 1, data)) actually performs filter(f, 1, data2), > where data2 is filter(f, 1, data1). It is certainly possible to convolve > a filters coefficients, thereby creating a longer filter with greater > stop-band attenuation and more abrupt transition. Is that what you mean?I think you got it, Jerry. See my other post where I recommend using the convolve operation to combine the filters rather than filtering the filter.
Reply by ●November 23, 20042004-11-23
I guess this brings up the question of "what are transients", since obviously Matlab removes transients with the "filter" command, and not with "conv" On another side note, why does dspGuru (http://www.dspguru.com/info/faqs/fir/props.htm) state that an FIRs delay is (N-1)/2*Fs, as opposed to (N-1)/Fs. I always thought that with an FIR filter of N taps, you simply need to wait N samples for the first meaningful value to appear. By the way I appreciate all the comments, Im a newbie Masters student in the DSP field, so bare with me :) --Shafik
Reply by ●November 23, 20042004-11-23
Shafik wrote:> I guess this brings up the question of "what are transients", since > obviously Matlab removes transients with the "filter" command, and not > with "conv" > > On another side note, why does dspGuru > (http://www.dspguru.com/info/faqs/fir/props.htm) state that an FIRs > delay is (N-1)/2*Fs, as opposed to (N-1)/Fs. > > I always thought that with an FIR filter of N taps, you simply need to > wait N samples for the first meaningful value to appear. > > By the way I appreciate all the comments, Im a newbie Masters student > in the DSP field, so bare with me :) > > --ShafikHow many times does "I always thought" amount to "I always assumed, but I didn't really think at all"? (It happens to me more often than I like to admit. DspGuru is right. I could explain, but I think you'll be happier to figure it out for yourself. (I'll be away till Sunday, but you can ask again sooner. Plenty here will explain if you ask.) A hint about how to think about it: Suppose that you have a signal that has been oversampled by at least a factor of two. Suppose further that you filter it with an FIR whose output the average of the last two samples. In other words, the coefficients are .5, .5. What is the delay of that filter? (A "filter" whose only coefficient is 1.0 has no delay at all.) Jerry -- Engineering is the art of making what you want from things you can get. �����������������������������������������������������������������������
Reply by ●November 24, 20042004-11-24
I guess its because of the symmetery of the linear-phase. Doesnt that still depend on how the filtering is implemented? It seems like im still missing something.
Reply by ●November 24, 20042004-11-24
Jon Harris wrote:> "Shafik" <shafik@u.arizona.edu> wrote in message > news:1101249847.815845.34790@c13g2000cwb.googlegroups.com... > >>Hello all, >> >>FIR filtering is simply a convolution, which is an associative >>operation. However in matlab, filtering a filter then applying that to >>a data sequence is different than filtering that data sequence twice. >>In other words: >> >>filter(f, 1, filter(f, 1, data)) is NOT the same as >>filter(filter(f, 1, f), data)) > > > I think the second line should be: > filter(filter(f, 1, f), 1, data)) > i.e. you are missing a ", 1" > > That change is required to avoid a syntax error. But after changing it, the two > outputs are still not symmetric. > > >>Why is that? > > > I'm not sure. Maybe it has something to do with initial filter states assumed > by Matlab? > > But if you change the filtering of the filter to convolving the filter with > itself, then it does work. In other words: > > filter(f, 1, filter(f, 1, data)) is the same as > filter(conv(f, f), 1, data) > > I think the difference is that the filter function truncates the output vector > length to match the input length, while the conv operation does not. Compare > filter(f,1,f) to conv(f,f) and you will see the difference. > >Indeed the filter function truncates the signal to match the input length. Whenever you have: filter(h, 1, x) The filter function assumes that h is the filter and x is the data. My feeling is that this assumption can be thought to be acceptable when the length of x is considerably larger than the length of h so the truncation error is small. If you compare the outputs of filter(h, 1, x) with conv(h, x) the amount of truncated signal equals the length of the filter h minus 1. Therefore, what the filter function actually eliminates is the input-off transient of the convolution.
Reply by ●November 24, 20042004-11-24
Shafik wrote:> I guess its because of the symmetery of the linear-phase. Doesnt that > still depend on how the filtering is implemented? It seems like im > still missing something.I assumed -- there it is again -- that when you wrote "FIR" you meant "symmetric FIR". Lifting that assumption, let's consisider the delays four filters, all of them three taps: 1,0,0; 0,1,0; 0,0,1; and 1,0,1. The first three delays are obvious (to me). Make reasonable assumptions and estimate the delay of the last one. Gone Visitin'. Back Sunday. Jerry -- Engineering is the art of making what you want from things you can get. �����������������������������������������������������������������������
Reply by ●November 24, 20042004-11-24
"Shafik" <shafik@u.arizona.edu> wrote in message news:1101260805.236308.324110@z14g2000cwz.googlegroups.com...> I guess this brings up the question of "what are transients", since > obviously Matlab removes transients with the "filter" command, and not > with "conv" > > On another side note, why does dspGuru > (http://www.dspguru.com/info/faqs/fir/props.htm) state that an FIRs > delay is (N-1)/2*Fs, as opposed to (N-1)/Fs.You skipped an important point. The question that the answer you quoted is under is "2.1.4 What is the delay of a linear-phase FIR?" The key phrase here is "linear-phase". This implies symmetric coefficients, which in turns implies that the delay is half the filter length.> I always thought that with an FIR filter of N taps, you simply need to > wait N samples for the first meaningful value to appear.Depends on what you mean by "meaningful". It does take N taps until all the filter coefficients are being "used". But the appearance of meaningful data and the delay are not necessarily the same thing.






