Как говорит @anddt, dfreq
- хороший вариант, хотя часто требуется некоторая настройка различных параметров, таких как threshold
и wl
. Вот игрушечный пример с некоторыми составными данными, содержащими шум.
library(seewave)
library(tuneR)
chirp = sine(freq = 20000, duration = 0.01, xunit = 'time')
silence_0.2 = silence(duration = 0.2, xunit = 'time')
silence_0.1 = silence(duration = 0.1, xunit = 'time')
noise_0.2 = noise(kind='pink', duration=0.2, xunit = 'time')
noise_0.1 = noise(kind='pink', duration=0.1, xunit = 'time')
signal = bind(silence_0.2, chirp, noise_0.1, silence_0.2, chirp, silence_0.1, noise_0.2, chirp, noise_0.2, silence_0.2)
# threshold removes noise, wl is the window length of the fourier transform, smaller
# values give more accuracy for time but noise gets more troublesome
peaks = data.frame(dfreq(signal, threshold = 10, wl = 128, plot=F))
peaks[is.na(peaks)] = 0
names(peaks) = c('time', 'frequency')
peaks$frequency[peaks$frequency < 19.9 | peaks$frequency > 20.1] = 0
startindices = which(diff(peaks$frequency) > 19)
endindices = which(diff(peaks$frequency) < -19)
starttimes = peaks[startindices, 1]
endtimes = peaks[endindices, 1]
plot(signal, col='grey')
abline(v = starttimes, col='green')
abline(v = endtimes, col='red')
Результат выглядит следующим образом. Зеленые вертикальные линии для начала и красные вертикальные линии для конца щебня.