I have a number of .wav files in a folder. I want to:

  • Import each file as data,
  • cut each list into a certain number of small lists,
  • export each list as .csv.

The .wav files have a length of approx. 25 s with 48 kHz (approx. 1,400,000 samples).

What's the bottleneck here? The for loops? What would be a faster formulation?

Thanks in advance.

fps = 48000;
fullpath = (* "path to folder" *)
peaklist = {69373, 194369, 328822, 449814, 578921, 693582, 816820, 954078, 1084577, 1213780};

For[mic = 1, mic <= 82, mic++,
  sig = Import[fullpath<>"S__"<>StringTake["_"<>ToString[mic],-2<>"_Take01_M24.wav","Data"]//Flatten;

  For[peak=1, peak<=Length[peaklist], peak++,
    Export[fullpath<>"Cut\\"<>plate<>"_Take"<>ToString[peak]<>"_Mic"<>ToString[mic]<>".csv",sig[[von ;; bis]]];


I found the .csv export to be horribly slow. Since I have simple lists I will export them as binaries:

a = RandomReal[1, 48000];

AbsoluteTiming[Export["test.csv", a]]
{2.5001, "test.csv"}

file = "test.bin";
BinaryWrite[file, a, "Real32"]
{0.0061165, Null}

1 Answer 1


Not a complete answer, just some thoughts as a way to go ...

First, I would get a list of all Imported files:

files=(Flatten@Import[fullpath <> "S__"<>StringTake["_"<>ToString[mic],-2]<>"_Take01_M24.wav", "Data"] &) /@ Range[82];

This is your command Import and your mic changed to Slot going from 1 to 82. Next, cutting ranges:

von=peaklist-4800, bis=von+48000-1, 

no need to compute these inside For loop. And all the exporting can be done in one step, adjust for your needs:

Function[u, MapIndexed[Export[fullpath <> "_" <> ToString[u] <> "_" <>
   ToString[First[#2]] <> ".csv", #1] &, (files[[u]][[#]] & /@ 
 MapThread[Span, {von, bis}])]] /@ Range[82]

Here u is a dummy variable (each imported file), for each file we Export cutted parts, cutting ranges are given by MapThread[Span, {von, bis}]. Exported file name consists of fullpath <> _ <> filenumber (u = mic) <> _ <> cuttedpartnumber.

  • $\begingroup$ Thanks for the reply, I'll dig into it. I have avoided pure functions so far but maybe it's the right time to get more comfortable with them. $\endgroup$
    – Niko_P
    Aug 29, 2019 at 17:14

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.