# How does MATLAB's $\tt upfirdn$ select extra points when downsampling?

I had a signal 512 samples long and I used upfirdn to filter and downsample it by a factor of 2. I thought this would result in a 256 sample long variable, but instead it comes out as 261.

The MATLAB help says this:

The length of the output, yout, is ceil(((length(xin)-1)*p+length(h))/q)

(where xin is the signal, h is the filter, p is the upsample ratio, and q is the downsample ratio) which, if you work it out (filter length is 10), does in fact come out to 261.

What I want to know is how it selects points when downsampling so that I can reproduce the upfirdn behavior in another language (long story, not important). Where does that formula come from?

If you did a straight convolution the output length would be 512+10-1= 521. Now for the decimation if you take the points 1,3,5,7 ... 521 then this gives you (521+1)/2 = 261 points.

To test it in matlab against the upfirdn() output Just do:

out=conv(xin,h);
out2 = out(1:2:end);


Compare the output of out2 withe upfirdn() - There might be some small differences just due to numerical implementations, but the differences should be very small. This is not the most efficient method but it shows you explicitly what is happening. A more efficient implementation is with polyphase filters. Basically, a polyphase implementation just saves you from calculating the output that you just throw away in the decimation step.

The function performs upsampling, filtering and downsampling. The reason for the extra samples is due to the fir filter delay. If you want to reproduce this behaviour for downsampling only you should do the following steps:

1. Take your input signal, xin and add length(h)-1 zeros at the end. This is done because of the filter's delay.
2. Use filter(h,1,xin_padded); to filter the signal.
3. Downsample the filtered signal.
• "The reason for the extra samples is due to the fir filter delay." Now that's some insight I can use! I wish I could select both answers as "correct", I would definitely have chosen this one if it weren't for the fact that the other one answers more precisely the question I asked... But you've given me extra knowledge I can use, so thanks! Jun 14, 2016 at 14:17