# Create a sound to be filtered
s = soundgen(sylLen = 500, samplingRate = 16000,
amFreq = 25, amDep = 50,
addSilence = 50, plot = TRUE)
# playme(s, 16000)
# Filter to remove the rapid AM at 25 Hz
s_filt = fade(filterSoundByMS(s, samplingRate = 16000,
amCond = 'abs(am) > 22 & abs(am) < 28',
action = 'stop',
plot = TRUE))
# NB: clicks are often introduced at the onset and offset - fade and renormalize
s_filt = s_filt / max(abs(s_filt))
# playme(s_filt, samplingRate = 16000)
spectrogram(s_filt, 16000, windowLength = 25)
# Using filterMS()
ms = modulationSpectrum(s, 16000, returnComplex = TRUE)$detailed$complex
# Remove all AM over 25 Hz
filterMS(ms, amCond = 'abs(am) > 25', protect = FALSE)
filterMS(ms, amCond = 'abs(am) > 25', protect = TRUE)
filterMS(ms, amCond = 'abs(am) > 25', protect = TRUE, smooth = NA)
# amCond and fmCond are OR-conditions
filterMS(ms, amCond = 'abs(am) > 15', fmCond = 'abs(fm) > 5', action = 'stop')
filterMS(ms, amCond = 'abs(am) > 15', fmCond = 'abs(fm) > 5', action = 'pass')
filterMS(ms, amCond = 'abs(am) > 10 & abs(am) < 25', action = 'stop')
# jointCond is more flexible
filterMS(ms, jointCond = 'am * fm < 5', action = 'stop')
filterMS(ms, jointCond = 'am^2 + (fm*3)^2 < 200', action = 'pass')
# So:
filterMS(ms, jointCond = 'abs(am) > 5 | abs(fm) < 5') # general
# ...is the same as:
filterMS(ms, amCond = 'abs(am) > 5', fmCond = 'abs(fm) < 5') # faster
if (FALSE) {
data('speechEx', package = 'soundgen')
samplingRate = [email protected]
playme(speechEx)
spectrogram(speechEx)
# Remove AM above 3 Hz from a bit of speech (removes most temporal details)
s_filt1 = fade(filterSoundByMS(speechEx, amCond = 'abs(am) > 3',
action = 'stop',
smooth = c(0.5, 2)))
spectrogram(s_filt1, samplingRate = samplingRate)
playme(s_filt1, samplingRate)
# Remove FM above 3 cycles/kHz (masks f0 but preserves formants)
s_filt3 = fade(filterSoundByMS(speechEx,
fmCond = 'abs(fm) > 3', action = 'stop',
logSpec = TRUE, smooth = c(1, 5),
protect = c('level', 'envelope')))
spectrogram(s_filt3, samplingRate = samplingRate)
playme(s_filt3, samplingRate)
# Joint spectro-temporal filtering with a symmetric condition
s_filt4 = fade(filterSoundByMS(speechEx,
jointCond = 'am^2 + (fm * 3)^2 < 300', action = 'stop',
logSpec = TRUE, dynamicRange = 60, smooth = c(1, 10),
protect = c('level', 'meanSpectrum')))
spectrogram(s_filt4, samplingRate = samplingRate)
playme(s_filt4, samplingRate)
# Process all files in a folder, save filtered audio and plots
s_filt = filterSoundByMS('~/Downloads/temp2',
saveAudio = TRUE, savePlots = TRUE,
amCond = 'abs(am) > 15', fmCond = 'abs(fm) > 5',
action = 'stop', smooth = c(0.5, 5),
nIter = 10)
}
Run the code above in your browser using DataLab