Filtering Complex Data With gsignal

[This article was first published on Get Your Data On, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Filtering Complex Data with gsignal

Introduction

The gsignal packageis a new signal processing library ported from Octave. If you use Matlab or Octave, gsignal contains many of the signal processing functions you would expect to find. In this post, I’m going to compare filterComplex function to the gsignal::filterfunction.

Background

A while back I postedabout filtering complex data in R. The issue is, the base R function filter cannot filter complex data. This is because the function from the signal package signal::filter is based on base R function filter. To get around this issue I defined the function filterComplex.

The function filterComplex filters real or complex data. The function inputs the moving average (MA) parameters band the autoregressive (AR) parameters a, and the data x. If the data is complex, it makes two filter calls: one with the real part and one with the imaginary part. If the data is real it makes one call.

filterComplex <- function(b, a, x) {
  if (is.complex(x)) {
    y <- signal::filter(b,a,Re(x)) + 1i*signal::filter(b,a,Im(x))
  } else y <- signal::filter(b,a,x)
  return(y)
}

Comparing Complex Filters

Makeing Complex Data

The function makeCG inputs the number of samples to generate N and the variance of the samples v. The function then outputs N zero mean complex Gaussian samples with variance v.

makeCG <- function(N,v=1) {(sqrt(v/2))*rnorm(N) +
(sqrt(v/2))*1i*rnorm(N)}

Below we use makeCG to make 64 zero mean complex Gaussian samples with variance 1.

N <- 64
x <- makeCG(N)

Filter Comlex Data

Here we filter data with filterComplex and gsignal::filter. Setting a to 1 results in a moving average filter. Setting b as below produces a 6 tap simple moving average filter.

a <- 1
b <- rep(1/6,6)

We could have use any parameters, but the ones above were chosen for simplicity. Now let’s filter the data with both filters.

y_filterComplex <- filterComplex(b,a,x)
y_gsignal       <- gsignal::filter(b,a,x)

Plot Outputs

The figure below contains the output of the two filters above. The green circles are from filterComplex and the red pluses are from gsignal::filter. They overlap showing they produce the same results.

Time the Functons

Now we time the functions to see if one is faster. Since both are going to be fast I’m going to use 10000 samples.

N <- 10000
x <- makeCG(N)

First we time filterComplex.

sTime <- Sys.time()
y_filterComplex <- filterComplex(b,a,x)
eTime <- Sys.time()
eTime-sTime
## Time difference of 0.006738901 secs

Now we time gsignal::filter.

sTime <- Sys.time()
y_gsignal <- gsignal::filter(b,a,x)
eTime <- Sys.time()
eTime-sTime
## Time difference of 0.002675056 secs

As you may have expected, the package function is faster.

Conclusion

In this post I briefly compared the complex filtering function I wrote filterComplex to the gsignal package function gsignal::filter. The gsignal::filtersuccessfully filtered complex data and was faster than the one I wrote.

To leave a comment for the author, please follow the link and comment on their blog: Get Your Data On.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)