-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeature-extractor.R
73 lines (65 loc) · 1.84 KB
/
feature-extractor.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
library(tuneR);
library(seewave);
source("functions.R")
source("features.R")
recordings <- c(
"data/gryllus_campestris.wav"
# "data/gryllotalpa_gryllotalpa.wav"
# "data/metrioptera_roeselii.wav",
# "data/chorthippus_albomarginatus.wav"
)
#Read the waveform
time <- function(wave, f) {
#type #chirpL # chirpI
output <- c("continuous", NA, NA, NA)
times <- timer(wave, f=f, msmooth=c(50,0), threshold=15)
#Chirp interval
#Sort pause periods into order of size
sorted <- times$p[order(times$p)]
#find transition from small to large
tolerance <- 20
running_mean <- c(sorted[1])
transition <- c()
for (i in 1:(length(sorted)-1)) {
if (sorted[i] > mean(running_mean) * (1 + tolerance) ) {
print(c(sorted[i], (mean(running_mean) * (1 + tolerance))))
transition <- c(transition, sorted[i])
}
running_mean[i+1] <- sorted[i+1]
}
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)
}
data <- matrix(ncol = 9)
for (i in 1:length(recordings)) {
wave <- readWave(recordings[i]);
f <- [email protected];
#wave <- cutw(wave, f, from=0, to=5);
data <- rbind(data, c(frequency(wave, min=1, max=40), time(wave, f)))
}