Shepard's Tone: An Auditory Illusion
Create an auditory illusion.
Copy to clipboard.
In[1]:=
![Click for copyable input](assets.en/shepards-tone-an-auditory-illusion/In_103.png)
✖
range = 12;
duration = 10;
lfoFreq = .05;
basePitch = 30;
numOsc = 8;
midiToFreq[m_] := 2^((m - 69)/12)*440.
Generate ramps to control frequencies and amplitudes of the oscillators.
Copy to clipboard.
In[2]:=
![Click for copyable input](assets.en/shepards-tone-an-auditory-illusion/In_104.png)
✖
phasors =
Table[AudioGenerator[{"Sawtooth", lfoFreq, 2. Pi/numOsc (i - 1.)},
duration, SampleRate -> 500]/2. + .5, {i, numOsc}];
AudioPlot[phasors, PlotLayout -> "Overlaid", PlotRange -> All]
Out[2]=
![](assets.en/shepards-tone-an-auditory-illusion/O_46.png)
Create frequencies that will control the oscillators. The frequencies are exactly one octave apart from each other and increase exponentially, so that pitch increases linearly.
Copy to clipboard.
In[3]:=
![Click for copyable input](assets.en/shepards-tone-an-auditory-illusion/In_105.png)
✖
freqs = Table[
midiToFreq[(phasors[[i]])*12*numOsc + basePitch], {i, numOsc}];
AudioPlot[freqs, PlotLayout -> "Overlaid", PlotRange -> All]
Out[3]=
![](assets.en/shepards-tone-an-auditory-illusion/O_47.png)
Create amplitudes that will control the oscillators. Amplitudes go to 0 when the frequencies drop to the minimum value.
Copy to clipboard.
In[4]:=
![Click for copyable input](assets.en/shepards-tone-an-auditory-illusion/In_106.png)
✖
amps = Cos[#*Pi - Pi/2.] & /@ phasors;
AudioPlot[amps, PlotLayout -> "Overlaid", PlotRange -> All]
Out[4]=
![](assets.en/shepards-tone-an-auditory-illusion/O_48.png)
Show the relation between frequency and amplitude of one oscillator.
Copy to clipboard.
In[5]:=
![Click for copyable input](assets.en/shepards-tone-an-auditory-illusion/In_107.png)
✖
AudioPlot[AudioNormalize /@ {freqs[[2]], amps[[2]]}, PlotRange -> All]
Out[5]=
![](assets.en/shepards-tone-an-auditory-illusion/O_49.png)
Combine a bank of oscillators using the created frequencies and amplitudes.
Copy to clipboard.
In[6]:=
![Click for copyable input](assets.en/shepards-tone-an-auditory-illusion/In_108.png)
✖
res = Mean[
Table[amps[[i]] AudioGenerator[{"Sin", freqs[[i]]}], {i, numOsc}]]
Copy to clipboard.
In[7]:=
![Click for copyable input](assets.en/shepards-tone-an-auditory-illusion/In_109.png)
✖
Spectrogram[res, 8192, 4096, HannWindow,
PlotRange -> {All, {0, 10000}}, ImageSize -> Medium]
Out[7]=
![](assets.en/shepards-tone-an-auditory-illusion/O_50.png)