# Calculating functions of sounds

I've seen that it is possible to create a sound from functions:

Sound[{Play[Sin[1000 t (1 + t^2)], {t, 0, .2}],
Play[Sin[500 t (1 + t^3)], {t, 0, .5}]}]


Is is possible to do the opposite? (to create a function of an input sound)

• Welcome to Mathematica.SE! I suggest that: 1) You take the introductory Tour now! 2) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! 3) As you receive help, try to give it too, by answering questions in your area of expertise. – bbgodfrey Apr 11 '15 at 22:16
• SampledSoundList that could be originated from an input sound, is a sound primitive representing a list of values for amplitude and sampling rate, from which a function could be interpolated. However for what purpose ? – penguin77 Apr 11 '15 at 23:09

Given a sounds wave, you can read the samples, and plot part of the sounds wave

fname = "ExampleData/rule30.wav";
ele = Import["ExampleData/rule30.wav", "Elements"] So the sound file contains the above elements. You can import each on its own.

 fs = Import[fname, "SampleRate"] You can look at first few milliseconds of the sound

 data = Import[fname, "Data"];
{nChannel, nSamples} = Dimensions[data] So there is only one channel. This not stero sound wave. Using ListPlot

ListLinePlot[data[[1, 1 ;; 200]], AxesLabel -> {"sample", "amplitude"}] The above is the first 200 samples. Given that there is 44100 samples per second, you can see this is very short amount of time.

You can try looking at the whole 2 seconds worth of sound

 ListLinePlot[data, AxesLabel -> {"sample", "amplitude"}, PlotRange -> All] But to obtain a mathematical function to fit this is not easy. You can look at the harmonics in the sounds, and see which ones has most energy by looking at the fft.

py = Fourier[data[[1, All]], FourierParameters -> {1, -1}];
nUniquePts = Ceiling[(nSamples + 1)/2]
py = py[[1 ;; nUniquePts]];
py = Abs[py];
py = py/nSamples;
py = py^2;

If[OddQ[nSamples], py[[2 ;; -1]] = 2*py[[2 ;; -1]],
py[[2 ;; -2]] = 2*py[[2 ;; -2]]];

f = (Range[0, nUniquePts - 1] fs)/nSamples;
ListPlot[Transpose[{f, py}], Joined -> True,
FrameLabel -> {{"|H(f)|", None}, {"hz", "Magnitude spectrum"}},
ImageSize -> 400, Frame -> True, RotateLabel -> False,
GridLines -> Automatic, GridLinesStyle -> Dashed,
PlotRange -> {{1, 500}, All}] So the above says that there is a harmonic of frequency about 200 Hz of which has most energy (largest amplitude), and few others around it.

You can find the bin where this max frequency is using

 Max[py] Position[py, Max[py]] So this gives an idea of the content. (this sound file must be a sound of something not made by human, the frequency content is too low)

• – Dr. belisarius Apr 11 '15 at 23:56
• @belisarius thanks for the link. I had no idea human can make sound with such low frequency. – Nasser Apr 12 '15 at 0:04