How does a function sound?
Andy Long
Spring, 2024

Play a “middle A” sine wave for 5 seconds:

Play[Sin[440 2Pi t],{t,0,5}]

That one has constant amplitude (of 1): amplitude multiplies the sound wave. We could make the amplitude a function, and so make the volume increase or decrease. Each max OR min produces a large amplitude, and hence a loud volume.

Here’s a linear increase  in volume:

Play[t/5*Sin[440 2 Pi t],{t,0,5}, SampleDepth->16]

Here’s a quadratic increase  in volume:

Play[t^2/25*Sin[440 2 Pi t],{t,0,5}, SampleDepth->16]

Let’s try a sinusoidal variation in volume:

Plot[Sin[2Pi t],{t,0,5}]
Play[Sin[2Pi t]Sin[440 2Pi t],{t,0,5}]

You may know that doubling or halving the frequency  moves up or down an octave:

Plot[Sin[2Pi t],{t,0,5}]
Play[Sin[2Pi t]Sin[220 2Pi t],{t,0,5}]

By shifting my amplitudes up by 1, I can make the maxes correspond to the loudest pitches, and the mins correspond to the quietest:

Plot[1+Sin[2Pi t],{t,0,5}]
Play[(1+Sin[2Pi t])Sin[220 2Pi t],{t,0,5}]

In the previous examples I used a constant pitch, or frequency: we can also modulate by frequency by a function:

g[t_]:=Sin[t/5 * 880 2Pi t] (* multiply the frequency by t/5 -- so the frequency is increasing linearly: *)
Plot[g[x],{x,0,5}]
Play[g[t],{t,0,5}]

Of course we can increase volume while changing frequency:

Play[t^2/25*Sin[220*(1+t/5) *2 Pi t],{t,0,5}, SampleDepth->1]

Now we can graph some different functions using sounds, by using the function as an amplitude: so I’m keeping the pitch constant (An A), but changing the volume:
You can hear the discontinuity (the jump), and you can hear the zeros!

g[x_]:=Piecewise[{{x^2,x<0},{20-6x,x>0}}]
Plot[g[x],{x,-5,5}]
Play[g[t]*Sin[440 *2 Pi t],{t,-5,5}, SampleDepth->16]

LagrangePolynomial[data_,x_]/;MatrixQ[data]:=
Module[{n=Length[data], xl,yl},
{xl,yl}=Transpose[data];
Sum[Product[(x-xl[[i]])/(xl[[j]]-xl[[i]]),{i,Complement[Range[n],{j}]}]yl[[j]],{j,n}]
]
q[x_]:=LagrangePolynomial[{{.7,0},{1,.7},{2,2}},x]
r[x_]:=LagrangePolynomial[{{4,2},{5,1.4},{5.3,1}},x]

g[x_]:=Piecewise[{{1.5+x^2,x<1},
{q[x],x<2},
{1.5+Exp[-Log[Sqrt[3]]*x+Log[4.5]], x<4},
{r[x],x<6}}]
Plot[g[x],{x,0,5.5},PlotRange->{{0,5.5},{0,4}}]
Play[2*g[t]*Sin[440 *2 Pi t],{t,0,5.5}]

Now here’s one where we adjust both the amplitude and the frequency: I’m using a piecewise (and discontinuous) pitch function, so you’ll hear the function switch octaves; and the function g will determine the volume (and g is also discontinuous).

pitch[x_]:=Piecewise[
{
{1,x<1},
{2,x<2},
{3, x<4},
{4,x<6}
}]
Plot[{g[x],pitch[x]},{x,0,5.5},PlotRange->{{0,5.5},{0,4}}]
Play[2*g[t]*Sin[pitch[t]*220 *2 Pi t],{t,0,5.5}]

Now you’re ready to broadcast your own radio program, using either
AM -- Amplitude Modulation
or
FM -- Frequency Modulation
Have fun!

You can play multiple pitches at the same time, and hence write your own electronic music with Mathematica!

pitcher[x_]:=Piecewise[
{
{1,x<1},
{18/12,x<2},
{2, x<8},
{2+6/12, x<8.25},
{2+5/12, x<20}
}]
Play[2*Sin[pitcher[t]*220 *2 Pi t],{t,0,15}]

Play[
{
2*(g[t]+g[20-t])*Sin[220 *2 Pi t],
Sin[pitcher[t]*220 *2 Pi t]
},
{t,0,20}
]