Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a spectrum of frequencies from WAV/RIFF using linux command line

How to generate a file that contains the spectrum of frequencies of a WAV/RIFF sound file?

I would like to use the Linux command line.

I know the cool SoX function to generate PNG spectrograms:

sox sound.wav -n spectrogram

example spectrogram created with SoX

But I do not need a visual representation of the spectrum of frequencies. I just want to get the spectrum of frequencies in a data file so I can work on them. I believe that there must be an option using SoX. SoX needs to generate that data before plotting it. How to get this?

Not sure, maybe the second solution is exporting the WAV file into the data file. Each sample from the data file is a measurement of the position of the membrane at a moment in time. So this is not a spectrum of frequencies.

sox sound.wav file.dat

How to convert those membrane positions into the spectrum I need?

like image 638
zsedc Avatar asked Feb 13 '14 13:02

zsedc


3 Answers

You can try the stat option in sox.

play track.wav stat -freq
like image 114
Domingo Avatar answered Nov 02 '22 22:11

Domingo


What you are looking for is called a Fourier Transform, or Fast Fourier Transform (FFT). The FFT is a mathematical algorithm that transforms time domain samples (i.e. the membrane positions, as you put it, at points in time), which are contained in a .wav file - into frequency components. If you Google FFT, you will find much more information, including source code that you can compile and reuse in Linux. See How do I plot the spectrum of a wav file using FFT? for a good start.

like image 29
mti2935 Avatar answered Nov 02 '22 22:11

mti2935


How about:

sox sound.wav -n stat -freq &> file.dat

This will produce a file.dat with content like this:

// snip
23941.406250  175.471481
23953.125000  180.637909
23964.843750  188.179977
23976.562500  515.783813
23988.281250  1035.087280
Samples read:            618496
Length (seconds):      6.442667
Scaled by:         2147483647.0
Maximum amplitude:     0.999969
Minimum amplitude:    -1.000000
Midline amplitude:    -0.000015
Mean    norm:          0.232281
Mean    amplitude:     0.018063
RMS     amplitude:     0.382168
Maximum delta:         1.999969
Minimum delta:         0.000000
Mean    delta:         0.285226
RMS     delta:         0.483500
Rough   frequency:         9665
Volume adjustment:        1.000
like image 6
Wilbert Avatar answered Nov 02 '22 22:11

Wilbert