Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

upfirdn

Upsample, apply FIR filter, downsample


Description

Filter and resample a signal using polyphase interpolation.

Usage

upfirdn(x, h, p = 1, q = 1)

Arguments

x

input data, specified as a numeric vector or matrix. In case of a vector it represents a single signal; in case of a matrix each column is a signal.

h

Impulse response of the FIR filter specified as a numeric vector or matrix. If it is a vector, then it represents one FIR filter to may be applied to multiple signals in x; if it is a matrix, then each column is a separate FIR impulse response.

p

Upsampling factor, specified as a positive integer (default: 1).

q

downsampling factor, specified as a positive integer (default: 1).

Details

upfirdn performs a cascade of three operations:

  1. Upsample the input data in the matrix x 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 fir1.

Value

output signal, returned as a vector or matrix. Each column has length ceiling(((length(x) - 1) * p + length(h)) / q).

Note

This function uses a polyphase implementation, which is generally faster than using filter by a factor equal to the downsampling factor, since it only calculates the needed outputs.

Author(s)

Geert van Boxtel, G.J.M.vanBoxtel@gmail.com.

See Also

Examples

## 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.
Fdat <- 48e3
Fcd <- 44.1e3
LM <- scan(text = capture.output(pracma::rats(Fcd / Fdat)), sep = '/',
 quiet = TRUE)
L <- LM[1]; M <- LM[2]

## Generate a 1.5 kHz sinusoid sampled at fDAT for 0.25 seconds. Plot the
## first millisecond.
t <- seq(0, 0.25 - 1 / Fdat, 1 / Fdat)
x <- sin(2 * pi * 1.5e3 * t)
plot(t, x, type = "h", xlim = c(0, 0.001))
points(t, x)

## Design an antialiasing lowpass filter using a Kaiser window.
## band edges 90% and 110%
f <- (Fdat / 2) * min(1 / L, 1 / M)
ko <- kaiserord(c(f - 0.1*f, f + 0.1*f), c(1, 0), c(0.1, 0.1), Fdat)
h <- L * fir1(ko$n, ko$Wc, ko$type, kaiser(ko$n + 1, ko$beta))

y <- upfirdn(x, h, L, M)

delay <- floor(((ko$n - 1) / 2 - (L - 1)) / L) + 1
y <- y[(delay + 1):length(y)]
ty <- seq(0, (length(y) - 1)) / Fcd
points(ty, y, col = "red", pch = 2)

gsignal

Signal Processing

v0.3-1
GPL-3
Authors
Geert van Boxtel [aut, cre] (Maintainer), Tom Short [aut] (Author of 'signal' package), Paul Kienzle [aut] (Majority of the original sources), Ben Abbott [ctb], Juan Aguado [ctb], Muthiah Annamalai [ctb], Leonardo Araujo [ctb], William Asquith [ctb], David Bateman [ctb], David Billinghurst [ctb], Juan Pablo Carbajal [ctb], André Carezia [ctb], Vincent Cautaerts [ctb], Eric Chassande-Mottin [ctb], Luca Citi [ctb], Dave Cogdell [ctb], Carlo de Falco [ctb], Carne Draug [ctb], Pascal Dupuis [ctb], John W. Eaton [ctb], R.G.H Eschauzier [ctb], Andrew Fitting [ctb], Alan J. Greenberger [ctb], Mike Gross [ctb], Daniel Gunyan [ctb], Kai Habel [ctb], Kurt Hornik [ctb], Jake Janovetz [ctb], Alexander Klein [ctb], Peter V. Lanspeary [ctb], Bill Lash [ctb], Friedrich Leissh [ctb], Laurent S. Mazet [ctb], Mike Miller [ctb], Petr Mikulik [ctb], Paolo Neis [ctb], Georgios Ouzounis [ctb], Sylvain Pelissier [ctb], Francesco Potortì [ctb], Charles Praplan [ctb], Lukas F. Reichlin [ctb], Tony Richardson [ctb], Asbjorn Sabo [ctb], Thomas Sailer [ctb], Rolf Schirmacher [ctb], Rolf Schirmacher [ctb], Ivan Selesnick [ctb], Julius O. Smith III [ctb], Peter L. Soendergaard [ctb], Quentin Spencer [ctb], Doug Stewart [ctb], P. Sudeepam [ctb], Stefan van der Walt [ctb], Andreas Weber [ctb], P. Sudeepam [ctb], Andreas Weingessel [ctb]
Initial release
2021-05-02

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.