在计算ACI

时间:2018-10-01 09:43:28

标签: r

作为Soundscape生态研究的一部分,我需要计算数百个.wav文件的声学复杂度指数(ACI)。

R中的声音生态软件包具有一个非常有用的函数multi_sounds()函数。但是,我在包装中找不到任何幅度滤波器。

seewave软件包具有一个filter(),该滤波器允许通过设置为某些阈值来设置振幅滤波。

我想对一个文件夹中的许多.wav文件使用afilter(),然后对所有过滤的文件应用multi_sounds()。由于我是R的新手,所以我不知所措地组合或循环这两个函数以获得理想的结果。

您能帮我吗?

示例:

afilter (.wav files,f=22050,threshold= 5,plot= F)

multiple_sounds (directory = "C:/Users/pc1/Desktop/sound",
            resultfile = "C:/Users/pc1/Desktop/sound/results1.csv",
            soundindex = "acoustic_complexity",min_freq = 1500, max_freq = 
            12000, j = 5, fft_w = 512)

谢谢

安娜

1 个答案:

答案 0 :(得分:0)

有些代码可能会帮助您入门。

library(soundecology)
library(seewave)
library(tuneR)

# make some wav files
w1 = sawtooth(440, duration = 20, xunit = 'time')
writeWave(w1, "w1.wav")
w2 = noise(kind='white', duration = 20, xunit = 'time')
writeWave(w2, 'w2.wav')

# find the wav files
wavfiles = list.files(pattern='wav')

# a function to be called for each wav
processWav = function(wavfile) {
    wav = readWave(wavfile)
    wav.filtered = afilter(wav, threshold = 5, plot = F, output = 'Wave')
    wav.aci = acoustic_complexity(wav.filtered)
    wav.aci
}

# apply the function to all the wavs
result = sapply(wavs, function(wav) processWav(wav))

result
#                      w1.wav      w2.wav     
#AciTotAll_left        1783.374    600.5361   
#AciTotAll_right       NA          NA         
#AciTotAll_left_bymin  5350.12     1801.61    
#AciTotAll_right_bymin NA          NA         
#aci_fl_left_vals      Numeric,256 Numeric,256
#aci_fl_right_vals     Logical,4   Logical,4  
#aci_left_matrix       List,4      List,4     
#aci_right_matrix      List,4      List,4    

我没有使用multiple_sounds函数,因为它需要读取wav文件,过滤它们并再次将它们写出来,这感觉需要太多处理。通常,sapply函数的运行速度非常快。