Search code examples
raudiotime-serieswavacoustics

Acoustic complexity index time series output


I have a wav file and I would like to calculate the Acoustic Complexity Index at each second and receive a time series output.

I understand how to modify other settings within a function like seewave::ACI() but I am unable to find out how to output a time series data frame where each row is one second of time with the corresponding ACI value.

For a reproducible example, this audio file is 20 seconds, so I'd like the output to have 20 rows, with each row printing the ACI for that 1-second of time.

library(soundecology)
data(tropicalsound)
acoustic_complexity(tropicalsound)

In fact, I'd like to achieve this is a few other indices, for example:

soundecology::ndsi(tropicalsound)
soundecology::acoustic_evenness(tropicalsound)

Solution

  • You can subset your wav file according to the samples it contains. Since the sampling frequency can be obtained from the wav object, we can get one-second subsets of the file and perform our calculations on each. Note that you have to set the cluster size to 1 second, since the default is 5 seconds.

    library(soundecology)
    data(tropicalsound)
    
    f <- [email protected]
    
    starts <- head(seq(0, length(tropicalsound), f), -1)
    
    aci <- sapply(starts, function(i) {
      aci <- acoustic_complexity(tropicalsound[i + seq(f)], j = 1)
      aci$AciTotAll_left
    })
    
    nds <- sapply(starts, function(i) {
      nds <- ndsi(tropicalsound[i + seq(f)])
      nds$ndsi_left
    })
    
    aei <- sapply(starts, function(i) {
      aei <- acoustic_evenness(tropicalsound[i + seq(f)])
      aei$aei_left
    })
    

    This allows us to create a second-by-second data frame representing a time series of each measure:

    data.frame(time = 0:19, aci, nds, aei)
    #>    time      aci       nds      aei
    #> 1     0 152.0586 0.7752307 0.438022
    #> 2     1 168.2281 0.4171902 0.459380
    #> 3     2 149.2796 0.9366220 0.516602
    #> 4     3 176.8324 0.8856127 0.485036
    #> 5     4 162.4237 0.8848515 0.483414
    #> 6     5 161.1535 0.8327568 0.511922
    #> 7     6 163.8071 0.7532586 0.549262
    #> 8     7 156.4818 0.7706808 0.436910
    #> 9     8 156.1037 0.7520663 0.489253
    #> 10    9 160.5316 0.7077717 0.491418
    #> 11   10 157.4274 0.8320380 0.457856
    #> 12   11 169.8831 0.8396483 0.456514
    #> 13   12 165.4426 0.6871337 0.456985
    #> 14   13 165.1630 0.7655454 0.497621
    #> 15   14 154.9258 0.8083035 0.489896
    #> 16   15 162.8614 0.7745876 0.458035
    #> 17   16 148.6004 0.1393345 0.443370
    #> 18   17 144.6733 0.8189469 0.458309
    #> 19   18 156.3466 0.6067827 0.455578
    #> 20   19 158.3413 0.7175293 0.477261
    

    Note that this is simply a demonstration of how to achieve the desired output; you would need to check the literature to determine whether it is appropriate to use these measures over such short time periods.