# NOT RUN {
# white noise
ms = modulationSpectrum(runif(16000), samplingRate = 16000,
logSpec = FALSE, power = TRUE)
# harmonic sound
s = soundgen()
ms = modulationSpectrum(s, samplingRate = 16000,
logSpec = FALSE, power = TRUE)
# embellish
ms = modulationSpectrum(s, samplingRate = 16000,
xlab = 'Temporal modulation, Hz', ylab = 'Spectral modulation, 1/KHz',
colorTheme = 'heat.colors', main = 'Modulation spectrum', lty = 3)
# }
# NOT RUN {
# Input can also be a list of waveforms (numeric vectors)
ss = vector('list', 10)
for (i in 1:length(ss)) {
ss[[i]] = soundgen(sylLen = runif(1, 100, 1000), temperature = .4,
pitch = runif(3, 400, 600))
}
# lapply(ss, playme)
ms1 = modulationSpectrum(ss[[1]], samplingRate = 16000) # the first sound
dim(ms1$original)
ms2 = modulationSpectrum(ss, samplingRate = 16000) # all 10 sounds
dim(ms2$original)
# Careful with complex MS of multiple inputs:
ms3 = modulationSpectrum(ss, samplingRate = 16000,
returnComplex = TRUE, aggregComplex = FALSE)
dim(ms3$complex) # complex MS of the first input only
ms4 = modulationSpectrum(ss, samplingRate = 16000,
returnComplex = TRUE, aggregComplex = TRUE)
dim(ms4$complex) # aggregated over inputs
# As with spectrograms, there is a tradeoff in time-frequency resolution
s = soundgen(pitch = 500, amFreq = 50, amDep = 100, samplingRate = 44100)
# playme(s, samplingRate = 44100)
ms = modulationSpectrum(s, samplingRate = 44100,
windowLength = 50, overlap = 0) # poor temporal resolution
ms = modulationSpectrum(s, samplingRate = 44100,
windowLength = 5, overlap = 80) # poor frequency resolution
ms = modulationSpectrum(s, samplingRate = 44100,
windowLength = 15, overlap = 80) # a reasonable compromise
# customize the plot
ms = modulationSpectrum(s, samplingRate = 44100,
kernelSize = 17, # more smoothing
xlim = c(-20, 20), ylim = c(0, 4), # zoom in on the central region
quantiles = c(.25, .5, .8), # customize contour lines
colorTheme = 'heat.colors', # alternative palette
power = 2) # ^2
# NB: xlim/ylim currently won't work properly with logWarp on
# Input can be a wav/mp3 file
ms = modulationSpectrum('~/Downloads/temp/200_ut_fear-bungee_11.wav')
# Input can be path to folder with audio files (average modulation spectrum)
ms = modulationSpectrum('~/Downloads/temp', kernelSize = 11)
# NB: longer files will be split into fragments <maxDur in length
# A sound with ~3 syllables per second and only downsweeps in F0 contour
s = soundgen(nSyl = 8, sylLen = 200, pauseLen = 100, pitch = c(300, 200))
# playme(s)
ms = modulationSpectrum(s, samplingRate = 16000, maxDur = .5,
xlim = c(-25, 25), colorTheme = 'seewave',
power = 2)
# note the asymmetry b/c of downsweeps
# "power = 2" returns squared modulation spectrum - note that this affects
# the roughness measure!
ms$roughness
# compare:
modulationSpectrum(s, samplingRate = 16000, maxDur = .5,
xlim = c(-25, 25), colorTheme = 'seewave', logWarp = NULL,
power = 1)$roughness # much higher roughness
# Plotting with or without log-warping the modulation spectrum:
ms = modulationSpectrum(soundgen(), samplingRate = 16000,
logWarp = NA, plot = TRUE)
ms = modulationSpectrum(soundgen(), samplingRate = 16000,
logWarp = 2, plot = TRUE)
# logWarp and kernelSize have no effect on roughness
# because it is calculated before these transforms:
modulationSpectrum(s, samplingRate = 16000, logWarp = 5)$roughness
modulationSpectrum(s, samplingRate = 16000, logWarp = NA)$roughness
modulationSpectrum(s, samplingRate = 16000, kernelSize = 17)$roughness
# Log-transform the spectrogram prior to 2D FFT (affects roughness):
ms = modulationSpectrum(soundgen(), samplingRate = 16000, logSpec = FALSE)
ms = modulationSpectrum(soundgen(), samplingRate = 16000, logSpec = TRUE)
# Complex modulation spectrum with phase preserved
ms = modulationSpectrum(soundgen(), samplingRate = 16000,
returnComplex = TRUE)
image(t(log(abs(ms$complex))))
# }
Run the code above in your browser using DataLab