Message Boards Message Boards

4
|
7722 Views
|
5 Replies
|
13 Total Likes
View groups...
Share
Share this post:

Playing sound with non-constant sample rate

I have a dataset of the form

{{a1,t1},{a2,t2},...,{an,tn}}

where ai is the level and ti is the time of that level. Is it possible to play this as a sound in Mathematica?

Example data (written in a different form for clarity):

ai:   0.0006    0.0407    0.0808    0.1210    0.1611    0.2013    0.2415    0.2816
ti:   0.0001    0.0074    0.0147    0.0220    0.0293    0.0366    0.0439    0.0512

The ti need not necessarily be equispaced, the units of ti are seconds.

Note: This is also posted on Mathematica Stack Exchange.

5 Replies

Here another version using Play. Imagine you got an analytic function:

f[t_] := Sin[440 2 Pi t Sin[10 t]]

It is easy to play it:

Play[f[t], {t, 0, 1}]

enter image description here

Now if you do not have function but just irregularly sampled data:

data = {#, f[#]} & /@ {0}~Join~RandomReal[1, 10000]~Join~1;
Short[data]
(*{{0,0},{0.366519495348991`,0.749201172621884`}, ....,{0.19676826435559214`,-0.8154545405493382`}}*)

you can recreate sound closely using Interpolation given that sampling is detailed:

g = Interpolation[data, InterpolationOrder -> 0];
Play[g[t], {t, 0, 1}]

enter image description here

Note sound is different but close enough.

POSTED BY: Vitaliy Kaurov

Something like this should work (though there will be no audible sound in the case of your data since it is just a straight line) -- note that the timeseries data is of the form

{{t1,a1},{t2,a2},...,{tn,an}}

rather than the reversed form that you gave.

So with this understanding and your date example one does

timeSeries = TimeSeries[
  Transpose[{{0.0001, 0.0074, 0.0147, 0.0220, 0.0293, 0.0366, 0.0439, 
     0.0512}, {0.0006, 0.0407, 0.0808, 0.1210, 0.1611, 0.2013, 0.2415,
      0.2816}}]
  ]

and then

ListPlay[timeSeries, SampleRate -> 1000]

Here is a different example with random amplitudes at random times:

times = Flatten[{0, Sort@RandomReal[{0, 5}, 100], 5}] 

randomTS = 
 TimeSeries@
  Table[{times[[i]], 10 RandomReal[{-1, 1}]}, {i, 1, Length[times]}] 

ListPlay[randomTS, SampleRate -> 8000] 
POSTED BY: David Reiss

Another approach here is to use TimeSeriesResample on the timeseries...

POSTED BY: David Reiss

David & Vitaly,

Thank you very much for your suggestions, I have been using David's solution in the above post. Unfortunately, the solution proposed by Vitaly below was taking far too long given that I have datasets that are approx 21000000 points long.

Following on from this method, is there a way to obtain the SpectrogramArray of this? I cannot work out how to ensure that the non-uniform spacing of the points is considered.

Many thanks,

Daniel

Resampling? These are uneven events:

t = Sort[RandomReal[10 Pi, 1000]];
v = Sin[t Sin[t]];
ts = TimeSeries[v, {t}];

And this resamples evenly:

ListPlay[ts[Range[.1, 9.9 Pi, .01]]]

enter image description here

POSTED BY: Vitaliy Kaurov
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract