Get a spectrum of frequencies from WAV/RIFF using Linux command line
Asked Answered
N

3

12

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?

Nevers answered 13/2, 2014 at 13:54 Comment(1)
For an explanation of what the output from this command means see: #47453388Homo
G
7

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
Groundsel answered 9/10, 2019 at 18:48 Comment(0)
L
6

You can try the stat option in sox.

play track.wav stat -freq
Laden answered 3/5, 2017 at 21:40 Comment(0)
L
4

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.

Levigate answered 13/2, 2014 at 14:17 Comment(2)
Thanks. What about using it directly from SOX? Sox must do FFT to generate spectrum of frequencies data before plotting this spectrogram. It works very fast.Nevers
I agree that SOX must be doing an FFT internally to generate the spectrum. But, I'm not sure that there is a way to make SOX output the raw FFT data. But, you can use SOX to pull the numeric samples from the wav file, which you would need to input to an FFT program. See answer by Randall Cook at #15032044 for more ideas about this.Levigate

© 2022 - 2024 — McMap. All rights reserved.