I am trying to make a plot of a sound file similar to Audacity's FFT plot which looks like this when the x-axis is on a log scale:
My plot of the same sound file (below) has the problem that the units are in absolute amplitude, not decibels. How can I fix this?
Answer
decibel is a relative unit. I'm pretty sure there is no implied standard reference in audio processing, (it looks like audiologists have a few go-to's e.g. dB HL, but I don't know what Audacity does). That said, you need a reference value. Since you're looking at FFT's the total power might be a good choice. Then decibels will tell you how strong a particular frequency is relative to the signal.
signal = RandomReal[{0,1},1000];(*A random signal*)
fft = signal//Fourier//Abs;
fft = fft[[1;;Ceiling[Length@signal/2]]]; (*Since we're looking at power only take the first N/2 points.*)
fftPowerRef = Total[fft^2]; (* Power is amplitude squared. *)
inDecibels = 10*Log[10, fft^2/fftPowerRef ];
In this case you will always have <0dB since a pure tone would concentrate all the power at one fft sample, and you'd be taking Log[10,1]
If you do the same thing with a signal like signal = Sin[2*pi*#/200]&/@Range[1000]
you should see one main peak with most of the power.
Alternate Reference Power
Using the total power as a reference means that from song to song a value of -10dB won't have much meaning except to give you the relative strength of a particular tone in that data set. You could calibrate your plots by always referencing the same power, for example the power in pure sine wave played at some particular volume, to make the axis meaningful from sample to sample.
You could also get crazy and take the FFT power of a wave that fully utilizes the dynamic range of a particular digitizer. You can reference relative to RMS power, or peak power, etc. etc. etc. It really depends on what your end goal is.
Comments
Post a Comment