summaryrefslogtreecommitdiff
path: root/R/impulse.R
blob: 7ff9b34d569f54bd4395b4231a7c0fb93de87ba6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#' Estimate Impulse Response Models
#' 
#' \code{impulseest} is used to estimate impulse response models in the
#' given data
#' 
#' @param data an object of class \code{idframe}
#' @param M Order of the FIR Model (Default:\code{30})
#' @param K Transport delay in the estimated impulse response
#' 
#' @seealso \code{\link{plot.impulseest}}, \code{\link{step}}
#' @export
impulseest <- function(data,M=30,K){
  
  N <- dim(data$output)[1]
  ind <- (M+1):N
  
  z_reg <- function(i) data$input[i:(i-M),]
  Z <- t(sapply(ind,z_reg))
  Y <- data$output[ind,]
  
  fit <- lm(Y~Z-1)
  
  out <- list(coefficients=coef(fit),residuals=resid(fit),
              lags=0:M)
  class(out) <- "impulseest"
  return(out)
}

#' Impulse Response Plots
#' 
#' Plots the estimated IR Coefficients
#' 
#' @param model an object of class \code{impulseest}
#' 
#' @seealso \code{\link{impulseest}},\code{\link{step}}
#' @export
plot.impulseest <- function(model){
  plot(model$lags,coef(model),type="h");abline(h=0)
}


#' Step Response Plots
#' 
#' Plots the step response of a system, given the IR model
#' 
#' @param model an object of class \code{impulseest}
#' 
#' @seealso \code{\link{impulseest}},\code{\link{impulse}}
#' @export 
step <- function(model){
  
}