summaryrefslogtreecommitdiff
path: root/R/idframe.R
blob: f261300e854903e2dbc740331030e31c2911c5df (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# class idframe
idframe <- function(output=numeric(0),input=numeric(0),
                     type=c("time","freq")[1],Ts = 1,
                     t.start=0,t.end=NA, tUnit = "seconds", 
                     frequencies = NA, fUnit= "Hz"){
  
  ## Input Validation
  if(!(type %in% c("time","freq"))) # type validation
    stop("Unknown domain type")
  
  if(length(output)!=0 && length(input)!=0){
    if(dim(output)[1]!=dim(input)[1]) # observation validation
      stop("Dimensions don't matach")
  }
  
  # Object Constructor
  dat <- list(output=data.frame(output),input=data.frame(input),type=type,Ts=Ts)
  n <- dim(output)[1]
  p <- dim(output)[2];m <- dim(input)[2]
  
  if(type=="freq"){

    if(is.na(frequencies)){
      frequncies <- seq(0,2*pi,length=n)
    }
    
    dat$frequencies <- frequencies
    dat$fUnit <- fUnit
    
  } else {
    
    if(is.na(t.end)) {
      t.end <- t.start + Ts*(n-1)
    } else {
      dat$Ts <- (t.end-t.start)/(n-1)
    }
    
    dat$tStart <- t.start; dat$tEnd <- t.end
    dat$tUnit <- tUnit
  }
      
  class(dat) <- "idframe"
  return(dat)
}

# plot method for idframe object
plot.idframe <- function(object,...){
  
  p <- dim(object$output)[2];m <- dim(object$input)[2]
  
  if(p!=1 && m!=1){
    oask <- devAskNewPage(TRUE)
    on.exit(devAskNewPage(oask))
    
    for(i in seq(m)){
      for(j in seq(p)){
        par(mfrow=c(2,1),mar=c(3,4,3,2))
        plot(.index(object),object$output[,j],xlab=object$type,
             ylab=colnames(object$output)[j],type="l",...)
        plot(.index(object),object$input[,i],xlab=object$type,
             ylab=colnames(object$input)[i],type="l",...)
      }
    }
  } else {
    par(mfrow=c(2,1))
    plot(.index(object),object$output[,1],xlab=object$type,
         ylab=colnames(object$output),type="l",...)
    plot(.index(object),object$input[,1],xlab=object$type,
         ylab=colnames(object$input),type="l",...)
  }  
}

.index <- function(object){
  if(object$type=="time"){
    return(seq(from=object$tStart,to=object$tEnd,by=object$Ts)) 
  } else {
    return(object$frequencies) 
  }
}

# summary method for idframe object
summary.idframe <- function(object,...){
  out_sum <- summary(object$output)
  in_sum <- summary(object$input)
  
  out <- list(outputs=out_sum,inputs=in_sum,Ts=object$Ts,type=object$type,
              tUnit=object$tUnit,no_of_samples = dim(object$output)[1])
  if(object$type=="time"){
    out$tStart <- object$tStart;out$tEnd <- object$tEnd
  } else{
    out$frequencies <- summary(object$frequencies);out$fUnit <- object$fUnit
  }
  
  class(out) <- "summary.idframe"
  return(out)
}

# print method for summary.idframe object
print.summary.idframe <- function(object,...){
  cat("Domain: ");cat(object$type)
  cat("\t\t Number of samples:");cat(object$no_of_samples)
  cat("\nSampling time: ")
  cat(object$Ts);cat(" ");cat(object$tUnit)
  
  if(object$type=="frequency"){
    cat("\t Frequency Unit: ");print(object$fUnit)
    cat("\n\n Frequeny Summary:")
    print(object$frequencies)
  }
  
  cat("\n\n")
  cat("Outputs \n")
  print(object$outputs)
  cat("\n")
  
  cat("Inputs \n")
  print(object$inputs)
}