A modulation spectrum says how much of a sound's envelope pattern moves at each rate (Hz) and
each density (cycles per octave). Spectrotemporal ripples
defines it and goes from a pattern to its spectrum. This page goes the other way: from a
spectrum, measured and edited or drawn from scratch, to a sound that has it.
A magnitude is not enough to make a sound. The spectrum has dropped the phase of the
modulations, which says when each event happens, and the fine structure under each band's
envelope. ModulationSpectrum.to_sound(carrier=...) puts envelopes with the spectrum's
magnitudes on a carrier, always named: a sound lends its own modulation phase and fine
structure, while carrier="tone" draws a random modulation phase and puts the envelopes on
steady tones.
The sentence is read by the two talkers every speech page
uses, and every demo on it plays both.
import os
import urllib.request
import matplotlib.pyplot as plt
import numpy as np
import sonore as so
plt.rcParams.update({"font.size": 9, "axes.titlesize": 10, "figure.dpi": 100})
def fetch(path):
"""A file from the sonore repository, by its path there: the local copy when this runs from
the repository root, otherwise downloaded from GitHub to the same relative path."""
if not os.path.exists(path):
os.makedirs(os.path.dirname(path) or ".", exist_ok=True)
urllib.request.urlretrieve("https://raw.githubusercontent.com/choyun1/sonore/main/" + path, path)
return path
def finish(snd):
"""How every sound in the gallery is played: 5 ms ramps, RMS 0.1, peak at most 0.95."""
snd = snd.ramp(5e-3).normalize(rms=0.1)
return snd.normalize(peak=0.95) if snd.peak > 0.95 else snd
Code
FS = 44100
SPEAKERS = {"Male talker": "bdl", "Female talker": "slt"}
talkers = {
label: finish(so.load(fetch(f"docs/speech/{speaker}_arctic_a0131.flac")))
for label, speaker in SPEAKERS.items()
}
def measure(snd, f_lo):
"""The modulation spectrum on the page's grid: 12 bands per octave from f_lo to 8 kHz."""
return so.ModulationSpectrum.octave(snd, f_lo=f_lo, f_hi=8000)
bank = so.cosine_filterbank(
f_lo=125, f_hi=8000, spacing=1 / 12, scale="octave"
) # what measure(snd, 125) uses
def cochleagram(ax, snd, f_lo):
"""The sound's envelopes in 24 bands per octave."""
bank = so.cosine_filterbank(f_lo=f_lo, f_hi=8000, spacing=1 / 24, scale="octave")
bank.analyze(snd).envelopes(lowpass=200, fs=1000).plot(ax, db_range=30, colorbar=False)
ax.set_title("Envelopes (cochleagram)")
def spectra(axes, snd, target, f_lo):
"""The target spectrum, if there is one, and the spectrum measured from the sound."""
if target is not None:
target.plot(axes[0], db_range=30, wt_max=20, wf_max=4, colorbar=False)
axes[0].set_title("Target modulation spectrum")
measure(snd, f_lo).plot(axes[-1], db_range=30, wt_max=20, wf_max=4, colorbar=False)
axes[-1].set_title("Measured from the sound")
def show(snd, target=None, f_lo=250):
"""The sound's cochleagram, the target spectrum if there is one, and the spectrum measured
from the sound. Returns the figure and the panel the playhead follows."""
n_panels = 2 if target is None else 3
fig, axes = plt.subplots(1, n_panels, figsize=(4 * n_panels, 3.4), layout="constrained")
cochleagram(axes[0], snd, f_lo)
spectra(axes[1:], snd, target, f_lo)
return fig, [axes[0]]
def show_pair(sounds, targets=None, f_lo=125):
"""One column per talker: the cochleagram above, and below it the talker's target spectrum
(if there is one) beside the spectrum measured from the sound. Returns the figure and, for
each talker, the panel the playhead follows."""
fig = plt.figure(figsize=(10, 6.4), layout="constrained")
columns = fig.subfigures(1, 2)
playhead = {}
for column, (label, snd) in zip(columns, sounds.items(), strict=True):
target = None if targets is None else targets[label]
layout = [["top"], ["measured"]] if target is None else [["top"] * 2, ["target", "measured"]]
top, *bottom = column.subplot_mosaic(layout).values()
cochleagram(top, snd, f_lo)
top.set_title(f"{label}: envelopes (cochleagram)")
spectra(bottom, snd, target, f_lo)
playhead[label] = [top]
return fig, playhead
Press play and a line follows the sound across every time axis in its plots. Click any time
axis to play from that point. Start with your volume low.
To run this page yourself, open it as a notebook in Colab or download it. It is the same code, and it fetches the recordings it needs from the sonore repository.
Same spectrum, different sound
Each talker's modulation spectrum, measured in 12 bands per octave from 125 Hz to 8 kHz, then
heard with the sentence's own modulation phase thrown away. to_sound with carrier="tone"
draws a random phase, builds the envelopes from it and the stored magnitudes, and puts each one
on a steady tone at its band's center. The random phase leaves the zero-rate column alone,
since that column (its phase as well as its magnitudes) is the sentence's long-term spectrum:
which bands are loud.
A sentence
The sentence, read by each talker. Its modulation spectrum is brightest at low rates and
densities, as for most natural sounds (Singh & Theunissen, 2003); how that modulation changes over
the sentence is on Modulation spectrogram. The speech-shaped
noise on Tones and noises has the long-term spectrum of speech but none of
this: its modulation spectrum is a single line at zero rate. In the cochleagrams the lowest
harmonics show as separate lines, farther apart for the female talker (Harmonics sample the
envelope); at the slow rates shown here the
two modulation spectra are much alike.
Male talker
mt1_a_sentence_male_talker.flac, 2.5 s, mono
Female talker
mt1_a_sentence_female_talker.flac, 2.6 s, mono
Code
spectra_of = {label: measure(snd, f_lo=125) for label, snd in talkers.items()}
sounds = talkers
fig, playhead = show_pair(sounds)
Its modulation spectrum, random modulation phase
The same magnitudes with a random modulation phase. The syllables, the pauses and the onsets
are gone from the envelopes, which change at the sentence's rates and densities but never at
its moments. A random phase also asks for envelopes below zero, which no envelope can be;
to_sound clips them at zero, with a warning, and that is why the measured spectrum is
smoother than the target. The long-term spectrum is kept only roughly. A two-dimensional
modulation spectrum does not say which bands carry which modulation, so the random phase
spreads the sentence's modulation into bands that were quiet, and clipping turns it into level
there.
sounds = {
label: finish(spectra_of[label].to_sound(carrier="tone", fs=snd.fs, rng=0))
for label, snd in talkers.items()
}
fig, playhead = show_pair(sounds, targets=spectra_of)
The same draw as envelopes, against the sentence's own envelopes: how many values were
clipped, how closely the two arrays correlate, and how often the summed envelope is more than
30 dB below its peak, as it is in the pauses.
Code
def summed_quiet(envelopes):
"""Share of the time the envelope summed over bands is more than 30 dB below its peak."""
summed = envelopes.sum(axis=1)
return np.mean(summed < summed.max() * 10 ** (-30 / 20))
for label, snd in talkers.items():
own = bank.analyze(snd).envelopes(fs=1000).data[:, 1:-1, 0] # (time, band), edge bands dropped
twin = spectra_of[label].to_envelopes(rng=0).data[:, 1:-1, 0] # the draw mt2 heard; clipped values are 0
clipped, correlation = np.mean(twin == 0), np.corrcoef(own.ravel(), twin.ravel())[0, 1]
print(
f"{label}: {clipped:.0%} clipped, correlation {correlation:.2f}, "
f"quiet {summed_quiet(own):.1%} of the time in the sentence, {summed_quiet(twin):.1%} in the twin"
)
Male talker: 34% clipped, correlation 0.07, quiet 8.2% of the time in the sentence, 0.0% in the twin
Female talker: 38% clipped, correlation 0.16, quiet 15.4% of the time in the sentence, 0.0% in the twin
So a modulation spectrum fixes how a sound changes, not when. The two envelope arrays barely
correlate, and the twin has no pauses. Sounds made this way, with a natural sound's modulation
spectrum and a random modulation phase, have been used to ask what auditory neurons respond
to (Hsu et al., 2004).
A drawn spectrum
A target can be drawn from scratch as a few patches on the rate × density plane.
so.ModulationBlob(rate, density) is a Gaussian bump at that rate [Hz] and density
[cycles/octave], with a standard deviation of half an octave in rate and a quarter of a cycle
per octave in density by default. The signs follow the
ripples: a positive rate and density is a downward
sweep. A ripple is a blob of no width.
A drawing sets a shape, not a depth, so from_blobs takes the depth separately: rms_depth,
the rms of the envelopes about their mean, relative to the mean (0.2 by default). Envelopes
cannot go below zero, and a random phase reaches only a limited depth before they would: a
random-phase field is close to Gaussian, whose peaks lie several standard deviations from its
mean, while a single ripple's peak is only \sqrt{2} of them away. A drawn target that would
need clipping is refused, with the largest depth that fits.
A drawn blob, on tones
Downward sweeps around 4 Hz and half a cycle per octave, on steady tones at the band centers.
Unlike a ripple, the sweeps come at irregular moments and with varying slopes, since the blob
spreads over a range of rates and densities and the phase is random. The faint patch at
negative rates along the bottom is the tail of the blob's mirror image at (−4 Hz, −0.5
cycle/octave), which every real pattern has.
The same target on a noise: each envelope multiplies the fine structure of a band of noise
instead of a tone, with the same drawn modulation phase. The sweeps are still there, under a
hiss whose own fluctuations fill the whole modulation spectrum. A noise passed as the carrier
gives only its fine structure; bands of noise as they come would bring random envelopes of
their own on top.
The same target on each talker's sentence, drawn for as long as that sentence and a little
shallower (rms_depth=0.15): the default depth would push some of the female talker's
envelope values below zero with this phase, and is refused. A sound as carrier lends its own
modulation phase as well as its fine structure, so the sweeps now come when the sentence's
syllables did, in each talker's voice: the sentence keeps its fine structure and gets new
envelopes. A noise vocoder makes the opposite
trade, keeping the envelopes and replacing the fine structure with noise.
short_targets = {
label: so.ModulationSpectrum.from_blobs(so.ModulationBlob(4, 0.5), duration=snd.duration, rms_depth=0.15)
for label, snd in talkers.items()
}
on_sentence = {label: short_targets[label].to_sound(carrier=snd) for label, snd in talkers.items()}
sounds = {label: finish(snd) for label, snd in on_sentence.items()}
fig, playhead = show_pair(sounds, targets=short_targets, f_lo=250)
How much of each sound's measured modulation power lies where the target has its power (within
20 dB of its peak), leaving out the zero-rate column, which holds the long-term spectrum:
Code
def share_in_target(snd, target):
"""Share of the measured modulation power, off the zero-rate column, inside the target."""
measured = measure(snd, f_lo=250)
measured_power = 10 ** (measured.level / 10)
in_target = target.level > target.level.max() - 20
moving = measured.w_t != 0
return measured_power[in_target & moving].sum() / measured_power[:, moving].sum()
carriers = [("tones", on_tones, target), ("noise", on_noise, target)]
carriers += [(label.lower(), on_sentence[label], short_targets[label]) for label in talkers]
for name, snd, drawn in carriers:
print(f"{name:>13}: {share_in_target(snd, drawn):.0%}")
tones: 97%
noise: 31%
male talker: 19%
female talker: 15%
The carrier matters as much as the target. A steady tone at each band's center adds almost no
modulation of its own, so the target survives. Noise and speech fluctuate inside every band,
and those fluctuations land in the measured spectrum on top of the drawn one; a tone vocoder
keeps fast envelope modulations better than a noise vocoder for the same reason
(Noise or tones). That is why "tone" is the carrier to try
first for hearing a target.
Random spectrograms
To study how listeners pick out a sound they have never heard before, McDermott, Wrobleski &
Oxenham (2011) needed sounds that were novel yet shared the coarse statistics of natural
sources. They drew spectrograms, one level in dB per ERB band and 20 ms window, from a
Gaussian whose correlations fall off exponentially in time and in frequency, as those of
spoken words and animal calls roughly do, and imposed them on noise. That is a drawn
modulation spectrum too: a random phase on a spectrum makes a Gaussian field, and exponential
correlations make a spectrum peaked at zero rate and density, a single blob at the origin,
drawn in dB. so.gaussian_spectrogram draws such a spectrogram as a TemporalEnvelope, and to_sound
puts it on the fine structure of a noise. The defaults are the paper's: 39 bands from 20 Hz to
4 kHz and correlation lengths of about 8.8 ERB and 154 ms. The paper gives no spread for the
levels; the default standard deviation of 14.1 dB is carried over from an earlier
implementation, not from the paper. Each figure shows the spectrogram that was drawn above the
envelopes measured on the sound.
Drawing in dB is also what lets these go deep: any level in dB is a positive envelope, so
nothing is clipped. Blobs can be drawn the same way, with
from_blobs(..., scale="db", sd_db=...) in place of rms_depth.
Code
def random_spectrogram_sound(seed, **kwargs):
"""A drawn spectrogram, and the sound made by putting it on a noise's fine structure."""
env = so.gaussian_spectrogram(1.0, FS, rng=seed, **kwargs)
sound = finish(env.to_sound(carrier=so.gaussian_noise(1.0, FS, rng=100 + seed)))
return env, sound
def show_spectrograms(env, sound):
fig, axes = plt.subplots(2, 1, figsize=(10, 5.2), sharex=True, layout="constrained")
env.plot(axes[0])
axes[0].set_title("Drawn spectrogram")
env.filterbank.analyze(sound).envelopes().plot(axes[1])
axes[1].set_title("Envelopes of the sound")
return fig, list(axes)
A random spectrogram
The paper's settings. Each draw is a new sound; this one is seed 1.
A measured spectrum can be edited and heard. with_gain(g) multiplies every cell by
g(rate, density); lambda r, d: abs(r) <= 4 keeps only the modulations at 4 Hz and below and
removes everything faster. Elliott & Theunissen (2009) filtered the modulation spectrum of
speech's spectrogram in a similar way to find which modulations intelligibility needs. With the
sentence itself as carrier, its own modulation phase and fine structure go back under the
edited magnitudes, so wherever the gain is 1 the timing is kept.
Modulations above 4 Hz removed
The edit, put straight back on each sentence. The measured spectrum keeps some power above
4 Hz, less than the sentence had but more than the edit asks for: the sentence's fine
structure brings fast modulation of its own back into every band.
slow = {
label: spectrum.with_gain(lambda rate, density: np.abs(rate) <= 4)
for label, spectrum in spectra_of.items()
}
edited = {label: slow[label].to_sound(carrier=snd) for label, snd in talkers.items()}
sounds = {label: finish(snd) for label, snd in edited.items()}
fig, playhead = show_pair(sounds, targets=slow)
The same edit, after 20 iterations
The same edit with 20 rounds of a search in the manner of Griffin & Lim (1984): analyze the
sound, keep its fine structure and modulation phase, impose the edited magnitudes again, and
resynthesize. Each round brings the sound's own spectrum closer to the edit. This is the
slowest cell on the page: each round is one analysis and one synthesis.
on_tones_edit = {
label: slow[label].to_sound(carrier="tone", fs=snd.fs, rng=0) for label, snd in talkers.items()
}
sounds = {label: finish(snd) for label, snd in on_tones_edit.items()}
fig, playhead = show_pair(sounds, targets=slow)
The modulation power left between 6 and 40 Hz, relative to each talker's sentence:
Code
def fast_share(snd):
"""Share of the measured modulation power at 6-40 Hz."""
measured = measure(snd, f_lo=125)
power = 10 ** (measured.level / 10)
fast = (np.abs(measured.w_t) >= 6) & (np.abs(measured.w_t) <= 40)
return power[:, fast].sum() / power.sum()
for label, snd in talkers.items():
print(label)
for name, version in [("edit", edited), ("20 iterations", searched), ("on tones", on_tones_edit)]:
print(f" {name:>13}: {10 * np.log10(fast_share(version[label]) / fast_share(snd)):.1f} dB")
Male talker
edit: -4.1 dB
20 iterations: -16.3 dB
on tones: -13.6 dB
Female talker
edit: -4.2 dB
20 iterations: -15.2 dB
on tones: -13.6 dB
None of them reaches the edit exactly, which removed everything there. A sound's envelopes are
not free: they come from filtering one waveform, so most arrays of magnitudes belong to no
sound at all, and the search finds a sound whose spectrum is close, never equal.
Timing from one sound, magnitudes from another
A sound as carrier lends its modulation phase, so one sound's magnitudes can be heard with
another sound's timing. to_envelopes(carrier=...) gives the envelopes alone, which can then go
on either sound's fine structure. Here each talker's sentence and as much rain trade halves.
Code
rain_long = so.load(fetch("docs/textures/rain.flac")).mono()
rains = {
label: so.Sound(rain_long.resample(snd.fs).data[: snd.n_samples], snd.fs)
for label, snd in talkers.items()
}
rain_spectra = {label: measure(rain, f_lo=125) for label, rain in rains.items()}
Rain magnitudes, sentence timing
Rain's modulation magnitudes and fine structure, with each sentence's modulation phase. The
rain now swells and fades with the syllables.
The other way round: each sentence's magnitudes and fine structure, with rain's modulation
phase. The voice is still there, but its events come at rain's moments.
Listen for which words can still be made out in each: the first keeps the sentence's timing
and the second its modulation magnitudes.
Twins band by band
A two-dimensional modulation spectrum says how much modulation there is at each rate and
density, but not which bands carry it. A random phase therefore spreads modulation into bands
that were steady or quiet, as in the sentence's twin above. A narrower description keeps it:
each band's own modulation spectrum, close to the per-band modulation power that McDermott &
Simoncelli (2011) use among their texture statistics. A twin of that keeps each band's
envelope magnitudes over rate and randomizes only its phase in time, band by band. Both kinds
of twin below keep each band's own fine structure, so only the envelopes differ.
Code
def band_twin(snd, rng=0):
"""Each band keeps its envelope's magnitude spectrum over time (and so its level); the phase is
random, band by band. On the sound's own fine structure."""
subbands = bank.analyze(snd)
envelopes = subbands.envelopes(fs=1000).data[:, :, 0] # (time, band)
magnitude = np.abs(np.fft.rfft(envelopes, axis=0))
phase = np.random.default_rng(rng).uniform(0, 2 * np.pi, magnitude.shape)
phase[0] = 0 # each band's mean stays
rebuilt = np.fft.irfft(magnitude * np.exp(1j * phase), n=len(envelopes), axis=0)
twin_envelopes = so.TemporalEnvelope(np.maximum(rebuilt, 0), 1000, bank)
return (twin_envelopes * subbands.tfs()).to_sound()
def plane_twin(snd, rng=0):
"""The same modulation spectrum on the plane, a random phase, on the sound's own fine structure."""
plane = measure(snd, f_lo=125)
return (plane.to_envelopes(rng=rng) * bank.analyze(snd).tfs()).to_sound()
def texture(name, seconds=3.5):
snd = so.load(fetch(f"docs/textures/{name}.flac")).mono()
return so.Sound(snd.data[: int(seconds * snd.fs)], snd.fs)
The crickets' twin on the plane. The long-term spectrum is kept, so the chirps stay at the
top, but some of their modulation spreads into the bands below, which were nearly silent.
The fire's twin band by band. Every band keeps its level and its modulation spectrum, but the
crackles are gone: a crackle is a brief event in many bands at once, and that alignment lives
in the phase.
So a band-by-band twin keeps a steady texture such as crickets, but not the sparse events of
a fire. McDermott & Simoncelli's statistics also include each band's envelope skew and
kurtosis, which measure sparseness, and correlations between bands, which measure how events
line up across them; What the statistics do hears a
fire synthesized with some of them left out.
What this page leaves out
Drawing with a mouse. Targets here are written in code, as blobs or as edits. sonore-sketch, a separate browser app built on sonore, draws them with a mouse: blobs on the plane, or cuts on a sound's measured spectrum.
dB targets. A spectrum measured with scale="db" (of the log envelope, as Elliott & Theunissen, 2009, define it), or blobs drawn with scale="db", also goes back to sound and never needs clipping, but the linear spectrum of the result is not the one drawn. Apart from the random spectrograms above, none is heard here.
The other modulation spectra. A modulation spectrogram changes over time and has no route back to sound, and a sound texture's per-band modulation power is set through its statistics, not here.