9
$\begingroup$

I want to extract the first dimension in TimeSeries,for example:

enter image description here

So I use this code:

data = Normal@AudioLocalMeasurements[ExampleData[{"Audio", "PianoScale"}],"MFCC"]; 
Thread[{data[[All, 1]], data[[All, 2, {1}]]}]

But it is a little complex,I want to know if there is more gentle way to do it?

$\endgroup$
7
$\begingroup$

You can also use TimeSeriesMap or MapAt:

audio = AudioLocalMeasurements[ExampleData[{"Audio", "PianoScale"}],"MFCC"];

res0 = TimeSeriesMap[{First@#}&, audio]["Path"];
res1 = MapAt[{First@#}&, audio["Path"],{{All,2}}];
res0 == res1 == res

True

$\endgroup$
6
$\begingroup$

You can use the "Properties" specification of a TimeSeries, as introduced in the Details section of its reference page. This avoids normalizing it.

For instance, with the properties "Times" and "Values":

audio = AudioLocalMeasurements[ExampleData[{"Audio", "PianoScale"}], "MFCC"];

res = Transpose[{audio["Times"], audio["Values"][[All, {1}]]}];

The symbol res gives the expected output:

data = Normal@audio;
res === Thread[{data[[All, 1]], data[[All, 2, {1}]]}]
(* True *)

Alternatively, you can use the property "Path". Here is a possible way:

resBis = {#1, {#2[[1]]}} & @@@ audio["Path"];
resBis === res
(* True *)

Note that other objects of the language accept the "Properties" specification, once they are constructed through evaluation. These include SparseArray and QuantityArray:

spa = SparseArray[Range[10]];
spa["Properties"]
(* {"AdjacencyLists", "Background", "ColumnIndices", "Density", "MatrixColumns", 
    "NonzeroValues", "PatternArray", "Properties", "RowPointers"} *)

qa = QuantityArray[Range[10], "Meters"];
qa["Properties"]
(* {"Data", "Properties", "Structure"} *)

The specification "Methods" can also be used for these two last:

spa["Methods"]
(* {"AdjacencyLists", "Background", "ColumnIndices", "Density", "MatrixColumns", 
    "MethodInformation", "Methods", "NonzeroPositions", "NonzeroValues", "PatternArray", 
    "Properties", "RowPointers"} *)

qa["Methods"]
(* {"Data", "MethodInformation", "Methods", "Properties", "Structure"} *)
$\endgroup$
1
$\begingroup$

The more elegant way:

In[15]:= ts = 
  AudioLocalMeasurements[ExampleData[{"Audio", "PianoScale"}], "MFCC"];

In[21]:= first = ts["PathComponent", {1}];

In[25]:= data = Normal[ts];

In[26]:= Thread[{data[[All, 1]], data[[All, 2, {1}]]}] == (first[
   "Path"])

Out[26]= True

Or without creating vector values:

In[15]:= ts = 
  AudioLocalMeasurements[ExampleData[{"Audio", "PianoScale"}], "MFCC"];

In[27]:= first = ts["PathComponent", 1];

In[28]:= data = Normal[ts];

In[29]:= Thread[{data[[All, 1]], data[[All, 2, 1]]}] == (first["Path"])

Out[29]= True
$\endgroup$
  • $\begingroup$ (Tip: If you set SetOptions[$FrontEnd, ExportMultipleCellsOptions -> {"IncludeCellLabels" -> False}], then the pesky In[]/Out[] labels won't be copied. It will make it easier for others to test and use your code.) $\endgroup$ – Michael E2 Nov 30 '16 at 0:53
  • $\begingroup$ what is PathComponent.I can not find it in doc $\endgroup$ – partida Nov 30 '16 at 14:29
  • $\begingroup$ td = TemporalData[Array[x, {10, 2}], {0}, ValueDimensions -> 2]; td["DeveloperProperties"] $\endgroup$ – Gosia Dec 1 '16 at 16:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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