Last active
June 22, 2016 14:48
-
-
Save cosacog/d46e9ddd9e7e0ff06281049cfa2105a9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DetectOnset <- function(data, intvl_thr){ | |
# dataからオンセットを取り出す | |
# params: | |
# data: 1chのデータ | |
# intvl_thr: これ以下の間隔の時は1つながりのトリガーとする | |
thr <- (median(data) + max(data))*0.5 | |
bin_data <- data > thr | |
bin_onset <- which((tail(bin_data,-1) == 1) & (head(bin_data,-1) == 0)) + 1 | |
bin_ofset <- which((tail(bin_data,-1) == 0) & (head(bin_data,-1) == 1)) | |
# exclude | |
bin_ofset <- bin_ofset[bin_ofset > bin_onset[1]] | |
bin_onset <- bin_onset[bin_onset < max(bin_ofset)] | |
# interval 計算 | |
bin_intvl <- tail(bin_onset,-1) - head(bin_ofset,-1) | |
# intervalが短いとこはホントのonsetではないので外す | |
idx_onset <- bin_onset[which(bin_intvl > intvl_thr) + 1] | |
return(idx_onset) | |
} | |
## sample | |
path_csv <- '/path/to/CSV' | |
data_csv <- read.table(path_csv, sep=',', header=F, skip = 17) | |
data_trig <- data_csv[,4] | |
data_phot <- data_csv[,5] | |
onset_trig <- DetectOnset(data_trig, 500) | |
onset_phot <- DetectOnset(data_phot, 500) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
フォトセンサーを脳波計で計測した時のデータを解析するスクリプト