## Main function

So there is more than one way to compute a smoothed amplitude envelope (see 6. MacIntyre, et al. 2022). We however follow the procedure originally coded in PRAAT by He & Dellwo (2017; ref 4.) which we reimplemented in R. The main function below follow their procedure. Firstly, we read in a sound file, anthen apply a Hilbert transform to the waveform signal, which yields what is called a complex analytic signal (containing real and imaginary numbers). From this we can extract the amplitude information by applying what is called the complex modulus, and we are left with a 1D signal that will track the contours of that audio waveform . Following He & Dellwo, since we are interested in an approximation of the syllable cycles that tend to occur no shorter than 200ms, we will smooth the signal at a max 5Hz (1000ms/5Hz = 200ms) using a Hanning Window filter. We then downsample the smoothed amplitude envelope to something more workable, namely, to 100Hz time series, which we can later align for example with other signals (e.g., motion tracking data). For a more information about the key procedure, the Hilbert transformation, see (5. Cohen, 20019)

Note, that the function below, allows you to set the resampling rate differently (e.g., 200Hz instead of 100Hz), as well as the hanning window settings (e.g., 8Hz instead of 5Hz).

```
library(seewave)
library(signal)
library(rPraat)
library(gsignal)
library(tuneR)
#####################MAIN FUNCTION TO EXTRACT SMOOTHED ENVELOPE###############################
amplitude_envelope.extract <- function(locationsound, smoothingHz, resampledHz)
{
#read the sound file into R
snd <- rPraat::snd.read(locationsound)
#apply the hilbert on the signal
hilb <- seewave::hilbert(snd$sig, f = snd$fs, fftw =FALSE)
#apply complex modulus
env <- as.vector(abs(hilb))
#smooth with a hanning window filter (not ot be confused with a hann filter)
window_size = snd$fs/smoothingHz
hanning_window <- hann(window_size)
env_smoothed <- filter(env, sides = 2, circular = TRUE, method = "convolution", filter = hanning_window)
#set undeterminable at beginning and end NA's to 0
env_smoothed[is.na(env_smoothed)] <- 0
#resample settings at desired sampling rate
f <- approxfun(1:(snd$duration*snd$fs),env_smoothed)
#resample apply
downsampled <- f(seq(from=0,to=snd$duration*snd$fs,by=snd$fs/resampledHz))
#let function return the downsampled smoothed amplitude envelope
return(downsampled[!is.na(downsampled)])
}
```

Now that we have initialized our main function. We can apply it to our list of audio file locations that we already have. We can further add some time information based on what we decided to be our sampling rate. We then bind the envelope signal with the time information and save it to an output folder.