Learn R Programming

oce (version 0.1-80)

make.filter: Make a digital filter

Description

make a digital filter

Usage

make.filter(type=c("blackman-harris","rectangular", "hamming", "hann"), m)

Arguments

type
a string indicating the type of filter: "blackman-harris" for the Blackman-Harris filter (a modified raised-cosine filter), "rectangular" for a flat filter, "hamming" for a Hamming filter (a raised-cosin
m
length of filter. This should be an odd number, for any non-rectangular filter.

Value

  • A list of filter coefficients, symmetric about the midpoint and summing to 1. These may be used with filter, which should be provided with argument circular=TRUE to avoid phase offsets.

Details

The filter is suitable for use by filter or convolve. Note that convolve should be faster than filter, but it cannot be used if the time series has missing values. For the Blackman-Harris filter, the half-power frequency is possibly 1/m cycles per time unit (see Examples). When using filter with these filters, use circular=TRUE.

References

F. J. Harris, 1978. On the use of windows for harmonic analysis with the discrete Fourier Transform. Proceedings of the IEEE, 66(1), 51-83 (http://web.mit.edu/xiphmont/Public/windows.pdf.)

Examples

Run this code
library(oce)
x <- c(rep(0,30),rep(1,30),rep(0,30))
plot.ts(x)
x1 <- filter(x, make.filter("blackman-harris", 5))
lines(x1, col='red')
x2 <- filter(x, make.filter("blackman-harris", 10))
lines(x2, col='blue')
legend("topright", lwd=1, col=c("red", "blue"), legend=c("m=5", "m=10"))

# Spectral representation
r <- rnorm(1e4)
r.spec <- spectrum(r, spans=c(21,5,3), plot=FALSE)
length <- 10
f <- make.filter("blackman-harris", length)
r.lowpass <- filter(r, f, circular=TRUE)
r.lowpass.spec <- spectrum(r.lowpass, spans=c(21,5,3), plot=FALSE)

plot(r.spec$freq, r.spec$spec, ylim=c(0.01, 2), log="xy", type="l")
lines(r.lowpass.spec$freq, r.lowpass.spec$spec, col="red")
abline(v=1/length, col="blue")
abline(h=1, col="blue")
abline(h=1/2, col="blue")               # half power

Run the code above in your browser using DataLab