summaryrefslogtreecommitdiff
path: root/R/idframe.R
blob: 2b8cae73f61b33693066ff328949d062dcb1dc77 (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
# class idframe
idframe <- function(output=numeric(0),input=numeric(0),
                     type=c("time","freq")[1],Ts = 1,
                     outputnames = colnames(output),inputnames = colnames(input),
                     t.start=0,t.end=NA, timeUnit = "seconds", 
                     frequencies = NA, freqUnit= "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(outputnames==NULL && length(output)!=0)
    outputnames <- sapply(1:p,FUN=function(x){paste("y",as.character(x),sep="")})
  
  if(inputnames==NULL && length(input)!=0)
    inputnames <- sapply(1:m,FUN=function(x){paste("u",as.character(x),sep="")})
  
  colnames(dat$output) <- outputnames
  colnames(dat$input) <- inputnames
  
  if(type=="freq"){

    if(is.na(frequencies)){
      frequncies <- seq(0,2*pi,length=n)
    }
    
    dat$frequencies <- frequencies
    dat$freqUnit <- freqUnit
    
  } 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$timeUnit <- timeUnit
  }
      
  class(dat) <- "idframe"
  return(dat)
}

# print method for idframe class
#print.idframe <- function(object,...){
#  print(object)
#}

# 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))
        plot(.index(object),object$output[,p],xlab=object$type,
             ylab=colnames(object$output)[p],type="l",...)
        plot(.index(object),object$input[,m],xlab=object$type,
             ylab=colnames(object$input)[m],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,...){
  
}