summaryrefslogtreecommitdiff
path: root/R
diff options
context:
space:
mode:
authorSuraj Yerramilli2015-06-03 15:56:34 +0530
committerSuraj Yerramilli2015-06-03 15:56:34 +0530
commit305f1b038b59cdbeac34e88ed73bace42e6cac8a (patch)
treeb388eec46948d967a49db0390b3973cfff324b95 /R
parentf2d4c0a980fc30dfb9645047c14ce1eedd18ebad (diff)
downloadSysID-R-code-305f1b038b59cdbeac34e88ed73bace42e6cac8a.tar.gz
SysID-R-code-305f1b038b59cdbeac34e88ed73bace42e6cac8a.tar.bz2
SysID-R-code-305f1b038b59cdbeac34e88ed73bace42e6cac8a.zip
added class for storing frequency response objects
Diffstat (limited to 'R')
-rw-r--r--R/idframe.R73
1 files changed, 72 insertions, 1 deletions
diff --git a/R/idframe.R b/R/idframe.R
index f8eeef4..18b5e2c 100644
--- a/R/idframe.R
+++ b/R/idframe.R
@@ -160,4 +160,75 @@ print.summary.idframe <- function(object,...){
cat("Inputs \n")
print(object$inputs)
-} \ No newline at end of file
+}
+
+#' S3 class for storing frequency response data
+#'
+#' @export
+idfrd <- function(response,freq,Ts){
+ out <- list(response=response,freq=freq,Ts=Ts)
+ class(out) <- "idfrd"
+ return(out)
+}
+
+#' @export
+plot.idfrd <- function(object){
+ require(ggplot2);require(reshape2)
+
+ mag <- 20*log10(Mod(object$resp)); phase <- Arg(object$resp)
+ sys_df <- data.frame(Frequency = object$freq,Gain = mag,Phase = phase)
+ melted_sys_df <- melt(sys_df, id.var = c("Frequency"))
+
+ bode <- ggplot(sys_df, aes(x = Frequency)) +
+ geom_line() + scale_x_log10()
+ bode_gain <- bode + aes(y = Gain)
+ bode_phase <- bode + aes(y = Phase)
+
+ multiplot(bode_gain,bode_phase)
+}
+
+# Multiple plot function
+#
+# ggplot objects can be passed in ..., or to plotlist (as a list of ggplot objects)
+# - cols: Number of columns in layout
+# - layout: A matrix specifying the layout. If present, 'cols' is ignored.
+#
+# If the layout is something like matrix(c(1,2,3,3), nrow=2, byrow=TRUE),
+# then plot 1 will go in the upper left, 2 will go in the upper right, and
+# 3 will go all the way across the bottom.
+#
+multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {
+ library(grid)
+
+ # Make a list from the ... arguments and plotlist
+ plots <- c(list(...), plotlist)
+
+ numPlots = length(plots)
+
+ # If layout is NULL, then use 'cols' to determine layout
+ if (is.null(layout)) {
+ # Make the panel
+ # ncol: Number of columns of plots
+ # nrow: Number of rows needed, calculated from # of cols
+ layout <- matrix(seq(1, cols * ceiling(numPlots/cols)),
+ ncol = cols, nrow = ceiling(numPlots/cols))
+ }
+
+ if (numPlots==1) {
+ print(plots[[1]])
+
+ } else {
+ # Set up the page
+ grid.newpage()
+ pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))
+
+ # Make each plot, in the correct location
+ for (i in 1:numPlots) {
+ # Get the i,j matrix positions of the regions that contain this subplot
+ matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))
+
+ print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row,
+ layout.pos.col = matchidx$col))
+ }
+ }
+}