0
$\begingroup$

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)

$\endgroup$
2
  • $\begingroup$ 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. $\endgroup$
    – bbgodfrey
    Commented Apr 11, 2015 at 22:16
  • $\begingroup$ 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 ? $\endgroup$
    – penguin77
    Commented Apr 11, 2015 at 23:09

1 Answer 1

4
$\begingroup$

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"]

Mathematica graphics

So the sound file contains the above elements. You can import each on its own.

 fs = Import[fname, "SampleRate"]

Mathematica graphics

You can look at first few milliseconds of the sound

 data = Import[fname, "Data"];
 {nChannel, nSamples} = Dimensions[data]

Mathematica graphics

So there is only one channel. This not stero sound wave. Using ListPlot

ListLinePlot[data[[1, 1 ;; 200]], AxesLabel -> {"sample", "amplitude"}]

Mathematica graphics

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]

Mathematica graphics

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}]

Mathematica graphics

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]

Mathematica graphics

 Position[py, Max[py]]

Mathematica graphics

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)

$\endgroup$
2
  • 1
    $\begingroup$ It depends on the human:) $\endgroup$ Commented Apr 11, 2015 at 23:56
  • $\begingroup$ @belisarius thanks for the link. I had no idea human can make sound with such low frequency. $\endgroup$
    – Nasser
    Commented Apr 12, 2015 at 0:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.