LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (http://www.linuxquestions.org/questions/linux-software-2/)
-   -   Why does "sox stat -freq" give me different data multiple times? (http://www.linuxquestions.org/questions/linux-software-2/why-does-sox-stat-freq-give-me-different-data-multiple-times-927589/)

schachwizard 02-04-2012 11:16 AM

Why does "sox stat -freq" give me different data multiple times?
 
When I run this (with 1.wav being my sound file):
Code:

sox 1.wav -n stat -freq
I get this:
Code:

0.000000  0.231731
10.766602  2.418857
21.533203  11.277147
32.299805  4.486618
43.066406  7.143557
53.833008  0.893461
64.599609  3.215172
75.366211  0.481062
86.132812  0.767702
96.899414  1.322751
107.666016  0.363204
118.432617  0.219829
...
...

The left hand column then repeats about 20 times, from 0 to about 22000. The right hand column changes in each repetition. Now, I thought that this command would give me the frequencies on the left, and the respective decibel levels on the right, but I don't know why I'm getting different data 20 times.

I'm trying to determine the peaks in frequency of the sound file. Is this the right way to do this?

neonsignal 02-04-2012 09:12 PM

When you transform from the time domain to the frequency domain, you have to sample a number of points in the time domain in order to represent the signal in the frequency domain. Rather than sample the entire file (which would give only a single frequency spectrum), sox samples groups of data points (4096 samples) and gives a spectrum, then repeats this for the length of the file. This enables you to use the data to show the frequency spectrum as it changes over time. For example, if your file was sampled at 44100Hz, then you will see a frequency spectrum for each tenth second interval.

If you actually want the spectrum of the entire file, then you may want to average these individual power spectra together.

schachwizard 02-05-2012 07:24 AM

Do you have any suggestions for how to average these power spectra together? I've tried using grep, and have been able to separate the the lines with a particular frequency, but I can't figure out how to average the power spectra, as they're on the same line, just separated by a space. I think I need sed, or something similar, but I don't know enough to use it properly.

Thanks!

neonsignal 02-05-2012 08:51 AM

If you just want to look at a particular frequency, you could just use awk to do the additions, for example:
Code:

sox 1.wav -n stat -freq |& grep '^0\.0' | awk '{s+=$2; ++c} END {print s/c}'
If you want to average the whole set of 2048 frequencies, then you may have to write something a bit more extensive in your language of choice, for example:
Code:

sox 1.wav -n stat -freq |& head -n -15 | \
perl -lne 'split; @sum[$i++%2048] += $_[1]; END { $i/=2048; foreach (@sum) {print $_/$i} }'

Also keep in mind that this analysis using sox assumes a single channel audio source.

You could also consider using audacity, which can analyze spectra and export these, if you don't need to automate the process.

schachwizard 02-17-2012 11:09 AM

Sorry for the very late reply, but I was able to use this perfectly.
Code:

sox 1.wav -n stat -freq |& grep '^0.0' | awk '{s+=$2; ++c} END {print s/c}
I just wrote a simple bash script that looped it for all the files I needed, and for all the frequencies. Thanks a lot!

---------- Post added 02-17-12 at 12:09 PM ----------

Sorry for the very late reply, but I was able to use this perfectly.
Code:

sox 1.wav -n stat -freq |& grep '^0.0' | awk '{s+=$2; ++c} END {print s/c}
I just wrote a simple bash script that looped it for all the files I needed, and for all the frequencies. Thanks a lot!


All times are GMT -5. The time now is 07:59 AM.