Upsample, apply FIR filter, and downsample
yout = upfirdn(xin,h)
yout = upfirdn(xin,h,p)
yout = upfirdn(xin,h,p,q)
upfirdn performs a cascade of three operations:
upfirdn has been implemented as a MEX-file for maximum speed, so only the outputs actually needed are computed. The FIR filter is usually a lowpass filter, which you must design using another function such as firpm or fir1.
yout = upfirdn(xin,h) filters the input signal xin with the FIR filter having impulse response h. If xin is a row or column vector, then it represents a single signal. If xin is a matrix, then each column is filtered independently. If h is a row or column vector, then it represents one FIR filter. If h is a matrix, then each column is a separate FIR impulse response sequence. If yout is a row or column vector, then it represents one signal. If yout is a matrix, then each column is a separate output. No upsampling or downsampling is implemented with this syntax.
Change the sampling rate of a 1 kHz sinusoid by a factor of 147/160. This factor is used to convert from 48 kHz (DAT rate) to 44.1 kHz (CD sampling rate).
Fs = 48e3; % Original sampling frequency-48kHz L = 147; % Interpolation/decimation factors M = 160; N = 24*L; h = fir1(N-1,1/M,kaiser(N,7.8562)); h = L*h; % Passband gain is L n = 0:10239; % 10240 samples, 0.213 seconds long x = sin(2*pi*1e3/Fs*n); % Original signal y = upfirdn(x,h,L,M); % 9430 samples, still 0.213 seconds
Plot the first millisecond of the original signal and overlay the resampled version.
stem(n(1:49)/Fs,x(1:49)) hold on stem(n(1:45)/(Fs*L/M),y(13:57),'r','filled') xlabel('Time (s)') ylabel('Signal') legend('Original','Resampled')
If p and q are large and do not have many common factors, you may see this message:
Filter length is too large - reduce problem complexity.
Instead, you should use an interpolation function, such as interp1, to perform the resampling and then filter the input.
Usually the inputs xin and the filter h are vectors, in which case only one output signal is produced. However, when these arguments are arrays, each column is treated as a separate signal or filter. Valid combinations are:
There is one filter and one signal, so the function convolves xin with h. The output signal yout is a row vector if xin is a row; otherwise, yout is a column vector.
There is one filter and many signals, so the function convolves h with each column of xin. The resulting yout will be an matrix with the same number of columns as xin.
There are many filters and one signal, so the function convolves each column of h with xin. The resulting yout will be an matrix with the same number of columns as h.
There are many filters and many signals, so the function convolves corresponding columns of xin and h. The resulting yout is an matrix with the same number of columns as xin and h.
upfirdn uses a polyphase interpolation structure. The number of multiply-add operations in the polyphase structure is approximately (LhLx – pLx)/q where Lh and Lx are the lengths of h(n) and x(n), respectively.
A more accurate flops count is computed in the program, but the actual count is still approximate. For long signals x(n), the formula is often exact.
 Crochiere, R. E., and Lawrence R. Rabiner. Multirate Digital Signal Processing. Englewood Cliffs, NJ: Prentice-Hall, 1983, pp. 88–91.
 Crochiere, R. E. "A General Program to Perform Sampling Rate Conversion of Data by Rational Ratios." Programs for Digital Signal Processing (Digital Signal Processing Committee of the IEEE Acoustics, Speech, and Signal Processing Society, eds.). New York: IEEE Press, 1979, Programs 8.2-1–8.2-7.