# create a butterworth filter with -3dB (half-power) at [1, 5] Hz
# and -60dB stop-band attenuation at [0.5, 6] Hz
sample_rate <- 20
nyquist <- sample_rate / 2
specs <- buttord(
Wp = c(1, 5) / nyquist,
Ws = c(0.5, 6) / nyquist,
Rp = 3,
Rs = 60
)
filter <- butter(specs)
# filter quality is poor because the AR-coefficients
# creates singular matrix with unstable inverse,
# this will cause `filtfilt` to fail
check_filter(
b = filter$b, a = filter$a,
# frequencies (normalized) where power is evaluated
w = c(1, 5, 0.5, 6) / nyquist,
# expected power
r_expected = c(3, 3, 60, 60)
)
Run the code above in your browser using DataLab