Main Content

upfirdn

Upsample, apply FIR filter, and downsample

Description

yout = upfirdn(xin,h) filters the input signal xin using an FIR filter with impulse response h. No upsampling or downsampling is implemented with this syntax.

yout = upfirdn(xin,h,p) specifies the integer upsampling factor p.

example

yout = upfirdn(xin,h,p,q) specifies the integer downsampling factor q.

Examples

collapse all

Change the sample rate of a signal by a rational conversion factor from the DAT rate of 48 kHz to the CD sample rate of 44.1 kHz. Use the rat function to find the numerator L and the denominator M of the rational factor.

Fdat = 48e3;
Fcd = 44.1e3;
[L,M] = rat(Fcd/Fdat)
L = 147
M = 160

Generate a 1.5 kHz sinusoid sampled at fDAT for 0.25 seconds. Plot the first millisecond of the signal.

t = 0:1/Fdat:0.25-1/Fdat;
x = sin(2*pi*1.5e3*t);
stem(t,x)
xlim([0 0.001])
hold on

Design an antialiasing lowpass filter using a Kaiser window. Set the filter band edges as 90% and 110% of the cutoff frequency, (fDAT/2)×min(1/L,1/M). Specify a passband ripple of 5 dB and a stopband attenuation of 40 dB. Set the passband gain to L.

f = (Fdat/2)*min(1/L,1/M);
d = designfilt('lowpassfir', ...
    'PassbandFrequency',0.9*f,'StopbandFrequency',1.1*f, ...
    'PassbandRipple',5,'StopbandAttenuation',40, ...
    'DesignMethod','kaiserwin','SampleRate',48e3);
h = L*tf(d);

Use upfirdn with the filter h to resample the sinusoid. Compute and compensate for the delay introduced by the filter. Generate the corresponding resampled time vector.

y = upfirdn(x,h,L,M);

delay = floor(((filtord(d)-1)/2-(L-1))/L);
y = y(delay+1:end);
t_res = (0:(length(y)-1))/Fcd;

Overlay the resampled signal on the plot.

stem(t_res,y,'*')
legend('Original','Resampled','Location','southeast')
hold off

Input Arguments

collapse all

Input signal, specified as a vector or matrix. If xin is a vector, then it represents a single signal. If xin is a matrix, then each column is filtered independently. For more details, see Tips.

Data Types: single | double

Filter impulse response, specified as a vector or matrix. If h is a vector, then it represents one FIR filter. If h is a matrix, then each column is a separate FIR impulse response sequence. For more details, see Tips.

Data Types: single | double

Upsampling factor, specified as a positive integer.

Data Types: single | double

Downsampling factor, specified as a positive integer.

Data Types: single | double

Output Arguments

collapse all

Output signal, returned as a vector or matrix. Each column of yout has length ceil(((length(xin)-1)*p+length(h))/q).

If the input to upfirdn is single precision, the function returns yout in single precision.

Note

Since upfirdn performs convolution and rate changing, the output signals yout have a different length than the input signals xin. The number of rows of yout is approximately p/q times the number of rows of xin.

Tips

The valid combinations of the sizes of xin and h are:

  1. xin is a vector and h is a vector.

    The inputs are 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 vector; otherwise, yout is a column vector.

  2. xin is a matrix and h is a vector.

    The inputs are one filter and many signals, so the function convolves h with each column of xin. The resulting yout is a matrix with the same number of columns as xin.

  3. xin is a vector and h is a matrix.

    The inputs are multiple filters and one signal, so the function convolves each column of h with xin. The resulting yout is a matrix with the same number of columns as h.

  4. xin is a matrix and h is a matrix, both with the same number of columns.

    The inputs are multiple filters and multiple signals, so the function convolves corresponding columns of xin and h. The resulting yout is a matrix with the same number of columns as xin and h.

Algorithms

upfirdn uses a polyphase interpolation structure. The number of multiply-add operations in the polyphase structure is approximately (LhLxpLx)/q where Lh and Lx are the lengths of h(n) and x(n), respectively. For long signals, this formula is often exact.

upfirdn performs a cascade of three operations:

  1. Upsample the input data in the matrix xin by a factor of the integer p (inserting zeros)

  2. FIR filter the upsampled signal data with the impulse response sequence given in the vector or matrix h

  3. Downsample the result by a factor of the integer q (throwing away samples)

The FIR filter is usually a lowpass filter, which you must design using another function such as firpm or fir1.

Note

The function resample performs an FIR design using firls, followed by rate changing implemented with upfirdn.

References

[1] 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.

[2] Crochiere, R. E., and Lawrence R. Rabiner. Multirate Digital Signal Processing. Englewood Cliffs, NJ: Prentice-Hall, 1983.

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Version History

Introduced before R2006a

expand all