Message Boards Message Boards


Audio processing and 3D Spectrogram - how to optimize on the Raspberry Pi

Posted 9 years ago
4 Replies
7 Total Likes

I have been fairly successful in creating 3D spectrograms of Bat Calls that I have recorded, but I appear to be limited by the available memory in the Raspberry Pi (No more memory available. Mathematica kernel has shut down). My wav files are recorded at 32-bit, 192000Hz, and are typically 1s to 5s in duration. I can just about process a 1 second file, but 5 seconds is impossible.

The code I am using is shown below, and it occurs to me that as I am only interested in ultrasound frequencies between say 18000Hz and 96000Hz, there is probably a lot of data that I could throw away to give the Pi an easier ride. My knowledge of Mathematica is extremely limited, so I would be grateful for some help or guidance in how to optimize the sound analysis. I have adapted the code below from what I could find by internet searches. Can I throw away some data? If so, how could I achieve this?

Ideally, I would to increase partSize and offset to values of 2500 to give more detail in the plot (see plot example below using these values on a desktop computer).

snd = Import[/home/pi/bat.wav"];

3D Spectrogram

Many thanks in advance.

POSTED BY: hystrix
4 Replies

I suspect your Raspberry Pi is just running out of memory. If so, you can try closing other applications that you have running on your Pi. I don't think the Pi allows for its memory to be upgraded. I can't think of any good tips to lessen the amount of memory used the ListPlot3D (other than perhaps subsampling your spectroGramData before feeding it to ListPlot3D).

POSTED BY: Arnoud Buzing
Posted 9 years ago

I used another Pi with not so many applications running. It does seem that for what I am trying to do, 1 second of audio is about the limit, which is actually is not too much of a limitation. :-)

In fact, I am impressed that I can do this with a Raspberry Pi and Mathematica!

Some results from my Pi...

3D Spectrogram

3D Spectrogram

POSTED BY: hystrix
Posted 9 years ago

I have placed two sounds files here:

bat3.wav is 1 second in duration and bat5.wav is 5 seconds in duration.

Import works for both files; it is the List3DPlot where I get the "no more memory available" error (or sometimes the Pi just freezes).

This is the output when processing bat5.wav:

In[1]:= snd = Import["/home/pi/bat5.wav"];
Out[2]= 7680224
Out[3]= 45122908
Out[4]= 27782692
In[5]:= sndData=snd[[1,1,1]];
In[6]:= sndSampleRate=snd[[1,2]];
In[7]:= min=Min[Abs[Fourier[sndData]]];
Out[8]= 74466116
Out[9]= 28197612
In[10]:= partSize=250;
In[11]:= offset=250;
In[12]:= spectroGramData=Take[20*Log10[Abs[Fourier[#]]/min],{2,partSize/2//Floor}]&/@Partition[sndData,partSize,offset];
Out[13]= 74466116
Out[14]= 32447044
In[15]:= ListPlot3D[spectroGramData?,ColorFunction->"SolarColors",DataRange->Round[{{0,Length[sndData]/sndSampleRate},{18000,sndSampleRate/2}},0.1],ImageSize->800,BaseStyle->{FontFamily->"Arial",FontWeight->Bold,12}, PerformanceGoal -> "Quality"]
During evaluation of In[15]:=

No more memory available.
Mathematica kernel has shut down.
Try quitting other applications and then retry.

bat3.wav works OK with the partSize and offset values shown. Increasing them causes the Pi to freeze. I will experiment to see how far I can push them.

Hope this helps.

POSTED BY: hystrix

Can you provide a link to your sample sound file (bat.wav)?

Depending on what you're trying to do you may be able to split the data up into smaller pieces.

Does the Import usually work (not run out of memory)? If so, what does ByteCount[snd] return?

Also what do you get for MaxMemoryUsed[] and MemoryInUse[] at various steps along the way? (There seem to be four significant steps in your algorithm: the import, the Fourier[] computation, the spectroGramData computation and the visualization (ListPlot3D).

POSTED BY: Arnoud Buzing
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract