R/filt_plot.R

Defines functions plot_filt

Documented in plot_filt

#'Plot filter response
#'
#'Plot the filter characteristics of a set of filter coefficients. Returns a
#'figure showing the impulse response, step response, frequency response, and
#'magnitude response of a set of filter coefficients.
#'
#'@param b Vector of filter coefficients
#'@param n_fft Number of FFT points
#'@param fs Sampling rate
#'@export
plot_filt <- function(b,
                      n_fft = 512,
                      fs = 1) {

  n <- length(b)
  freqs <- seq(0, 1, by = 1 / n_fft) * fs / 2
  samps <- seq(1, n) - round(n / 2)

  graphics::par(mfrow = c(2, 2))
  graphics::plot(samps,
       b,
       type = "l",
       main = "Impulse response")
  graphics::plot(samps,
       cumsum(b),
       type = "l",
       main = "Step response")

  if (n < (2 * n_fft)) {
    tmp <- rep(0, n_fft * 2)
    tmp[1:n] <- b
    b <- tmp
  }
  z <- stats::fft(b)
  z <- z[1:(trunc(length(z) / 2) + 1)]
  plot(freqs,
       abs(z),
       type = "l",
       main = "Frequency response")

  plot(freqs,
       20 * log10(abs(z)),
       type = "l",
       main = "Magnitude response")


}
craddm/firfiltR documentation built on May 22, 2019, 12:41 p.m.