myFun <- function(file) {tmp <- readWave(file, from=1, to = max.cycle.length * (sufficent.cycles + 1), units="samples")@left;
                         #approx(tmp, n=ceiling(length(tmp)/scale.down.factor), method="linear")$y
}

PreprocessTrainingData <- function(training.folder){   #Read wave files,  Normalize, Align signals, Read labels,
file_list <- list.files(training.folder, pattern=training.files.pattern, full.names = TRUE, recursive = TRUE, include.dirs = TRUE)
## Read folder wave files, reduce frequency, and store them as columns of a matrix and normalize them
data.list <- lapply(file_list, myFun)
dataset <- do.call("cbind", data.list)
dataset <- scale(dataset)         ## normalize
dataset[is.nan(dataset)] <- 0     ## resolve NaN resulting from all zeroes sequences (having undefined sd) when scaling
## Read class labels 
label_file_list <- list.files(training.folder, pattern="REFERENCE.csv", full.names = TRUE, recursive = TRUE, include.dirs = TRUE)
label.data.list <- lapply(label_file_list, read.csv, stringsAsFactors=FALSE, header=FALSE)
selection <- grep(training.labels.pattern, label.data.list[[1]]$V1)  ## for test consideration, please remove
label.dataset <- do.call("rbind", label.data.list[selection])$V2
label.dataset <- label.dataset[selection]                            ## for test consideration, please remove

## Align signals
Cycle.length <- c()
excluded <- floor(min.cycle.length/scale.down.factor)   ## first coefficients are always the max, need to exclude this portion
nn <- length(dataset[,1])
for (i in 1:ncol(dataset)){
  auto.corr <- acf(dataset[,i], lag.max=(max.cycle.length/scale.down.factor)-1, plot=FALSE); ## to speed up, stop after one cycle lag
  best.cycle.length <- excluded + which.max(auto.corr$acf[-c(1:excluded)])
  offset <- which.max(dataset[1:(best.cycle.length+Eps), i])   ## the location of the hihest value in first cycle will be my offset for alignment
  dataset[1:(nn-offset+1),i] <- dataset[offset:nn, i]
  Cycle.length <- c(Cycle.length, best.cycle.length)
}
dataset <- dataset[1:(max.cycle.length*sufficent.cycles),]

list(dataset, label.dataset)
}

PreprocessTestSequence <- function(test_record){
  ## Read test wave file, reduce frequency, and store them as columns of a matrix and normalize them
  data.list <- lapply(test_record, myFun)
  test.dataset <- do.call("cbind", data.list)
  test.dataset <- scale(test.dataset)         ## normalize
  test.dataset[is.nan(test.dataset)] <- 0     ## resolve NaN resulting from all zeroes sequences (having undefined sd) when scaling
  
  ## Align signals
  excluded <- floor(min.cycle.length/scale.down.factor)   ## first coefficients are always the max, need to exclude this portion
  nn <- length(test.dataset[,1])
  for (i in 1:ncol(test.dataset)){
    auto.corr <- acf(test.dataset[,i], lag.max=(max.cycle.length/scale.down.factor)-1, plot=FALSE); ## to speed up, stop after one cycle lag
    best.cycle.length <- excluded + which.max(auto.corr$acf[-c(1:excluded)])
    offset <- which.max(test.dataset[1:(best.cycle.length+Eps), i])   ## the location of the hihest value in first cycle will be my offset for alignment
    test.dataset[1:(nn-offset+1),i] <- test.dataset[offset:nn, i]
  }
  test.dataset <- test.dataset[1:(max.cycle.length*sufficent.cycles),]
  as.matrix(test.dataset)
}