0

如果已经问过这个问题,并且我对这个问题的措辞是 R 的新手,请提前道歉。

有什么方法可以让我的代码对声音文件进行二次采样更有效率吗?我有来自 9 个站点的 148 小时的录音,对于每个站点,我每 5 分钟对 12 个 1 分钟片段进行二次采样。我目前这样做的方式有效,但非常耗时。有没有办法让这段代码更有效率?

###hour 3###
setwd("C:/Users/Cex/Desktop/R Studio/Audiomoth Files/Rural/Hodkinson")
hod3a<-readWave("20200820_020000.wav", from = 0, to = 1, units="minutes")
hod3b<-readWave("20200820_020000.wav", from = 5, to = 6, units="minutes")
hod3c<-readWave("20200820_020000.wav", from = 10, to = 11, units="minutes")
hod3d<-readWave("20200820_020000.wav", from = 15, to = 16, units="minutes")
hod3e<-readWave("20200820_020000.wav", from = 20, to = 21, units="minutes")
hod3f<-readWave("20200820_020000.wav", from = 25, to = 26, units="minutes")
hod3g<-readWave("20200820_020000.wav", from = 31, to = 32, units="minutes")
hod3h<-readWave("20200820_020000.wav", from = 35, to = 36, units="minutes")
hod3i<-readWave("20200820_020000.wav", from = 40, to = 41, units="minutes")
hod3j<-readWave("20200820_020000.wav", from = 45, to = 46, units="minutes")
hod3k<-readWave("20200820_020000.wav", from = 50, to = 51, units="minutes")
hod3l<-readWave("20200820_020000.wav", from = 55, to = 56, units="minutes")

setwd("C:/Users/Cex/Desktop/R Studio/Audiomoth Files/Rural/Hodkinson/hour - Copy (3)")
writeWave(hod3a, filename="hod3a.wav",extensible=FALSE)
writeWave(hod3b, filename="hod3b.wav",extensible=FALSE)
writeWave(hod3c, filename="hod3c.wav",extensible=FALSE)
writeWave(hod3d, filename="hod3d.wav",extensible=FALSE)
writeWave(hod3e, filename="hod3e.wav",extensible=FALSE)
writeWave(hod3f, filename="hod3f.wav",extensible=FALSE)
writeWave(hod3g, filename="hod3g.wav",extensible=FALSE)
writeWave(hod3h, filename="hod3h.wav",extensible=FALSE)
writeWave(hod3i, filename="hod3i.wav",extensible=FALSE)
writeWave(hod3j, filename="hod3j.wav",extensible=FALSE)
writeWave(hod3k, filename="hod3k.wav",extensible=FALSE)
writeWave(hod3l, filename="hod3l.wav",extensible=FALSE)

setwd("C:/Users/Cex/Desktop/R Studio/Audiomoth Files/Rural/Hodkinson/hour - Copy (3)")
multiple_sounds(directory = "C:/Users/Cex/Desktop/R Studio/Audiomoth Files/Rural/Hodkinson/hour - Copy (3)", resultfile = "ndsi_resultS2.csv", soundindex = "ndsi", no_cores = "-2")
multiple_sounds(directory = "C:/Users/Cex/Desktop/R Studio/Audiomoth Files/Rural/Hodkinson/hour - Copy (3)", resultfile = "adi_results.csv", soundindex = "acoustic_diversity", no_cores = "-2")
multiple_sounds(directory = "C:/Users/Cex/Desktop/R Studio/Audiomoth Files/Rural/Hodkinson/hour - Copy (3)", resultfile = "aei_results.csv", soundindex = "acoustic_evenness", no_cores = "-2")
multiple_sounds(directory = "C:/Users/Cex/Desktop/R Studio/Audiomoth Files/Rural/Hodkinson/hour - Copy (3)", resultfile = "aci_results.csv", soundindex = "acoustic_complexity", no_cores = "-2")
multiple_sounds(directory = "C:/Users/Cex/Desktop/R Studio/Audiomoth Files/Rural/Hodkinson/hour - Copy (3)", resultfile = "H_results.csv", soundindex = "H", no_cores = "-2")

###hour 4###
setwd("C:/Users/Cex/Desktop/R Studio/Audiomoth Files/Rural/Hodkinson")
hod4a<-readWave("20200820_030000.wav", from = 0, to = 1, units="minutes")
hod4b<-readWave("20200820_030000.wav", from = 5, to = 6, units="minutes")
hod4c<-readWave("20200820_030000.wav", from = 10, to = 11, units="minutes")
hod4d<-readWave("20200820_030000.wav", from = 15, to = 16, units="minutes")
hod4e<-readWave("20200820_030000.wav", from = 20, to = 21, units="minutes")
hod4f<-readWave("20200820_030000.wav", from = 25, to = 26, units="minutes")
hod4g<-readWave("20200820_030000.wav", from = 31, to = 32, units="minutes")
hod4h<-readWave("20200820_030000.wav", from = 35, to = 36, units="minutes")
hod4i<-readWave("20200820_030000.wav", from = 40, to = 41, units="minutes")
hod4j<-readWave("20200820_030000.wav", from = 45, to = 46, units="minutes")
hod4k<-readWave("20200820_030000.wav", from = 50, to = 51, units="minutes")
hod4l<-readWave("20200820_030000.wav", from = 55, to = 56, units="minutes")


setwd("C:/Users/Cex/Desktop/R Studio/Audiomoth Files/Rural/Hodkinson/hour - Copy (4)")
writeWave(hod4a, filename="hod4a.wav",extensible=FALSE)
writeWave(hod4b, filename="hod4b.wav",extensible=FALSE)
writeWave(hod4c, filename="hod4c.wav",extensible=FALSE)
writeWave(hod4d, filename="hod4d.wav",extensible=FALSE)
writeWave(hod4e, filename="hod4e.wav",extensible=FALSE)
writeWave(hod4f, filename="hod4f.wav",extensible=FALSE)
writeWave(hod4g, filename="hod4g.wav",extensible=FALSE)
writeWave(hod4h, filename="hod4h.wav",extensible=FALSE)
writeWave(hod4i, filename="hod4i.wav",extensible=FALSE)
writeWave(hod4j, filename="hod4j.wav",extensible=FALSE)
writeWave(hod4k, filename="hod4k.wav",extensible=FALSE)
writeWave(hod4k, filename="hod4l.wav",extensible=FALSE)

setwd("C:/Users/Cex/Desktop/R Studio/Audiomoth Files/Rural/Hodkinson/hour - Copy (4)")
multiple_sounds(directory = "C:/Users/Cex/Desktop/R Studio/Audiomoth Files/Rural/Hodkinson/hour - Copy (4)", resultfile = "ndsi_resultS2.csv", soundindex = "ndsi", no_cores = "-2")
multiple_sounds(directory = "C:/Users/Cex/Desktop/R Studio/Audiomoth Files/Rural/Hodkinson/hour - Copy (4)", resultfile = "adi_results.csv", soundindex = "acoustic_diversity", no_cores = "-2")
multiple_sounds(directory = "C:/Users/Cex/Desktop/R Studio/Audiomoth Files/Rural/Hodkinson/hour - Copy (4)", resultfile = "aei_results.csv", soundindex = "acoustic_evenness", no_cores = "-2")
multiple_sounds(directory = "C:/Users/Cex/Desktop/R Studio/Audiomoth Files/Rural/Hodkinson/hour - Copy (4)", resultfile = "aci_results.csv", soundindex = "acoustic_complexity", no_cores = "-2")
multiple_sounds(directory = "C:/Users/Cex/Desktop/R Studio/Audiomoth Files/Rural/Hodkinson/hour - Copy (4)", resultfile = "H_results.csv", soundindex = "H", no_cores = "-2")

我目前必须每小时检查一次代码并手动更改小时数,这需要大量时间。我希望我已经正确传达了这一点,非常感谢任何帮助。

4

1 回答 1

0

我们可以创建两个sequence 并用 循环它们,通过指定,作为循环值Map读取数据并将其存储在readWavefromtolist

readLst <- Map(function(i, j) 
     readWave("20200820_020000.wav", from = i, to = j,
           units = "minutes"), seq(0, 55, by = 5), seq(1,  56, by = 5))

同样用于写入文件

lapply(seq_along(readLst), function(i)
         writeWave(readLst[[i]], 
      filename= paste0("hod3" letters[i],  ".wav"), extensible=FALSE))
于 2020-11-04T22:02:40.207 回答