*by Joseph Rickert*

R would probably not be the first language that pops into the mind of an engineer who is looking for signal processing software. However, I think anyone who looks into what R can do in this area would be very pleasantly surprised. The bulk of R’s basic signal processing capability comes from the signal package which was ported over from the open source project Octave. This package contains a number of functions for filtering, filter generation, resampling, interpolation and the visualization of filter models that Matlab packages up in its Signal Processing Toolbox. The functions in the R's signal package retain the look and feel of MatLab originals. Working with these functions should make it easy for anyone familiar with MatLab to make the transition to R. Here is some sample code adapted from D.M. Etter’s book "Engineering Problem Solving with Matlab" for computing the transfer functions for four digital filters and plotting the results. Remember that a transfer function H(z) may be described as the ratio of two complex ploynomials:

H(z) = B(z) / A(z) where

A(z) = a(0) + a(1) * z^-1 + . . . + a(n) * z^-n and

B(z) = b(0) + b(1) * z^-1 + . . . + b(n) * z^-n

# Digital Tranfer Function Example # from pages 333 - 337 of Engineering Problem Solving with Matlab # D.M. Etter, Prentice Hall 1993 # Compute and plot digital transfer functions library(signal) data(package="signal") # to see the data sets availale in the package signal ls("package:signal") # to list all of the objects in signal lsf.str("package:signal") # to list all of the functions in signal # Filter H1(z) B1 <- c(0.2066,0.4131,0.2066) # Coefficients of numerator polynomial A1 <- c(1,-0.3695,0.1958) # Coefficients of denominator polynomial H1z <- freqz(B1,A1,100) # Compute the transfer function # Filter H2(z) B2 <- c(0.894,-1.789,0.894) A2 <- c(1,-1.788,0.799) H2z <- freqz(B2,A2,100) # Filter H3(z) B3 <- c(0.42,0,-0.42) A3 <- c(1,-0.443,0.159) H3z <- freqz(B3,A3,100) # Filter h4(z) B4 <- c(0.5972,0.4425,0.5972) A4 <- c(1,0.4425,0.1584) H4z <- freqz(B4,A4,100) # Convenience function to draw multiple plots hPlot <- function(H){ text <- deparse(substitute(H)) # get the name of the filter for the title c <- substr(text,4,4) plot(H$f,abs(H$h), col="red", ylim=c(0,1), xlab="Normalized Frequency", ylab="Magnitude", main=paste("Filter H",c,"(z)",sep="") ) } par(mfrow=c(2,2)) plotList <- list(H1z,H2z,H3z,H4z) lapply(plotList,hPlot) # plot(H4z) # Look at the default plot

Created by Pretty R at inside-R.org

The following graph which reproduces the plots in Etter’s example shows the magnitueds of the transfer functions for four different filters plotted against normalized frequency.

This next graph which shows some characteristics of the sample filter H4(z), was produced by simply running: plot(H4z).

In addition to the basics, R contains some very nice implementations of signal processing algorithms that have applications in statistical analysis. For starters, R’s time series capabilities are second to none. As was noted in a recent post neither Matlab nor the legacy statistical software package SAS can match the scope and depth of the time series tools listed in R’s Time Series Task View.

The fftw package provides a wrapper around the fast, award winning FFTW library of C subroutines developed at MIT. The dlm package for fitting Maximum Likelihood and Bayesian dynamic linear models includes dlmFilter(), a flexible function based on singular value decompositions for Kalman filtering. The vignette for the dlm package provides a brief introduction to the underlying theory which is further developed in the Springer book “Dynamic Linear Models with R” by the Campagnoli, Petrone and Petris,

There are several R packages for wavelet analysis. Some have a specialized focus, however the following four packages provide functions useful for general signal processing. The wavelets package contains functions for wavelet filters, transforms and multiresolution analysis. The waved package which is described in the paper “The WaveD Transform in R: Performs Fast Translation-Invariant Wavelet Deconvolution” by Raimondo and Stewart contains functions for the deconvolution on noisy signals. The package waveslim which contains functions for 1, 2 and 3 dimensional signal processing supports the text: “An Introduction to Wavelets and Other Filtering Methods in Finance and Economics" by Gençay, Selçuk and Whitcher. Finally, the wavethresh package which contains a number of functions for wavelet transforms and statistics is the basis for Nason’s book: “Wavelet Methods in Statistics with R”. I think anyone looking to learn something about wavelets would find this last book to be a very good read.

## Comments

You can follow this conversation by subscribing to the comment feed for this post.