From 305f1b038b59cdbeac34e88ed73bace42e6cac8a Mon Sep 17 00:00:00 2001 From: Suraj Yerramilli Date: Wed, 3 Jun 2015 15:56:34 +0530 Subject: added class for storing frequency response objects --- NAMESPACE | 2 ++ R/idframe.R | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- man/idfrd.Rd | 12 ++++++++++ 3 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 man/idfrd.Rd diff --git a/NAMESPACE b/NAMESPACE index 9e587d7..e6eaa0b 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,6 +1,7 @@ # Generated by roxygen2 (4.1.1): do not edit by hand S3method(plot,idframe) +S3method(plot,idfrd) S3method(plot,impulseest) S3method(predict,detrend) S3method(print,summary.idframe) @@ -9,6 +10,7 @@ S3method(summary,idframe) export(dataSlice) export(detrend) export(idframe) +export(idfrd) export(impulseest) export(read.idframe) export(read.table.idframe) 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)) + } + } +} diff --git a/man/idfrd.Rd b/man/idfrd.Rd new file mode 100644 index 0000000..c44a83a --- /dev/null +++ b/man/idfrd.Rd @@ -0,0 +1,12 @@ +% Generated by roxygen2 (4.1.1): do not edit by hand +% Please edit documentation in R/idframe.R +\name{idfrd} +\alias{idfrd} +\title{S3 class for storing frequency response data} +\usage{ +idfrd(response, freq, Ts) +} +\description{ +S3 class for storing frequency response data +} + -- cgit