-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeatures.R
132 lines (125 loc) · 3.51 KB
/
features.R
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
frequency <- function(wave, min=0, max=200) {
output <- c( NA, NA, NA, NA, NA)
fspectrum <- spec(wave, f=f)
peaks <- localpeaks(fspectrum)
peaksT <- data.frame(peaks)
peaksT <- peaksT[peaksT$freq > min,]
peaksT <- peaksT[peaksT$freq < max,]
peaksT <- peaksT[order(peaksT$amp, decreasing=TRUE),]
output[1] <- peaksT[1,1]
#Harmonic1
h1 <- output[1]*2
h1_margin <- 0.01
h1_range <- c(h1*(1-h1_margin), h1*(1+h1_margin))
hT <- subset(peaksT, peaksT$freq > h1_range[1])
hT <- subset(hT, hT$freq < h1_range[2])
if (length(hT[,1]) == 1) {
h1_found <- hT[1,]
output[2] <- h1_found$freq
output[3] <- h1_found$amp
}
#Harmonic2
h2 <- output[1]*3
h2_margin <- 0.01
h2_range <- c(h2*(1-h2_margin), h2*(1+h2_margin))
hT <- subset(peaksT, peaksT$freq > h2_range[1])
hT <- subset(hT, hT$freq < h2_range[2])
if (length(hT[,1]) == 1) {
h2_found <- hT[1,]
output[4] <- h2_found$freq
output[5] <- h2_found$amp
}
return(output)
}
time <- function(wave, f) {
#type #chirpL # chirpI
output <- c("continuous", NA, NA, NA)
times <- timer(wave, f=f, msmooth=c(50,0), threshold=10)
#Chirp interval
#Sort pause periods into order of size
sorted <- times$p[order(times$p)]
#find transition from small to large
tolerance <- 1
running_mean <- c()
transition <- c()
for (i in 1:length(sorted)) {
running_mean[i] <- sorted[i]
if (sorted[i] > mean(running_mean) * (1 + tolerance) ) {
transition <- c(transition, sorted[i])
}
}
if (length(transition) > 0) {
output[1] <- "chirp"
longer <- times$p[times$p > transition[1]]
output[2] <- min(longer)
output[3] <- max(longer)
#Chirp length
start.diffs <- diff(times$s.start)
chirps <- c()
chirp <- c()
for (i in 1:length(start.diffs)) {
if (length(chirp) == 0) {
#this is the start of a chirp
chirp[1] <- times$s.start[i]
}
if (start.diffs[i] >= transition[1]) {
#This is the end of the chirp
chirp[2] <- times$s.end[i]
chirps <- c(chirps, c(chirp))
chirp <- c()
}
}
chirp.d <- c()
for (i in 1:(length(chirps)/2)) {
chirp.d[i] <- chirps[2*i] - chirps[(2*i)-1]
}
output[4] <- mean(chirp.d)
}
return(output)
}
time <- function(wave, f) {
#type #chirpL # chirpI
output <- c("continuous", NA, NA, NA)
times <- timer(wave, f=f, msmooth=c(50,0), threshold=10)
#Chirp interval
#Sort pause periods into order of size
sorted <- times$p[order(times$p)]
#find transition from small to large
tolerance <- 5
running_mean <- c()
transition <- c()
for (i in 1:length(sorted)) {
running_mean[i] <- sorted[i]
if (sorted[i] > mean(running_mean) * (1 + tolerance) ) {
transition <- c(transition, sorted[i])
}
}
if (length(transition) > 0) {
output[1] <- "chirp"
longer <- times$p[times$p > transition[1]]
output[2] <- min(longer)
output[3] <- max(longer)
#Chirp length
start.diffs <- diff(times$s.start)
chirps <- c()
chirp <- c()
for (i in 1:length(start.diffs)) {
if (length(chirp) == 0) {
#this is the start of a chirp
chirp[1] <- times$s.start[i]
}
if (start.diffs[i] >= transition[1]) {
#This is the end of the chirp
chirp[2] <- times$s.end[i]
chirps <- c(chirps, c(chirp))
chirp <- c()
}
}
chirp.d <- c()
for (i in 1:(length(chirps)/2)) {
chirp.d[i] <- chirps[2*i] - chirps[(2*i)-1]
}
output[4] <- mean(chirp.d)
}
return(output)
}