1
$\begingroup$

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:

Audacity FFT

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?

fft2

$\endgroup$
2
  • 1
    $\begingroup$ You've tried ListLogLogPlot? $\endgroup$
    – Mr.Wizard
    Apr 23, 2015 at 0:23
  • 2
    $\begingroup$ "By default, Periodogram shows the decibel values of magnitude" $\endgroup$
    – Kuba
    Apr 23, 2015 at 6:25

3 Answers 3

6
$\begingroup$

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.

$\endgroup$
1
  • $\begingroup$ Hmm, I see, I will give this a try later tonight. $\endgroup$ Apr 23, 2015 at 0:51
0
$\begingroup$

Take your $y$ values (in a list myData) and convert them by the definition for decibels:

20 Log[10, #/Min[myData]] & /@ myData

If your data is in the form of {{t1, v1}, {t2, v2}, ...} then in a simple (but inefficient) method:

myData = {{3, 6}, {4, 8}, {5, 2}};

myMin = Min[myData[[All, 2]]];

fixedData = {#[[1]], 20 Log[10, #[[2]]/myMin]} & /@ myData;

ListPlot[fixedData]
$\endgroup$
2
  • $\begingroup$ It is not really clear how this expression is to be used. If I insert it into the expression shown in the question errors result. $\endgroup$ Apr 23, 2015 at 0:40
  • $\begingroup$ I get lots of "Indeterminate" values and things like that. $\endgroup$ Apr 23, 2015 at 0:45
0
$\begingroup$

Not only scaling of Y-axis but also an alignment of X-axis appears to be somehow "brute-force".

I'll show an example on a harmonic signal:

NumberOfObservations = 600;
Signal = Table[Sin[ 2 Pi/40 x], {x, 0, NumberOfObservations}];
ListPlot[Signal, FrameLabel -> {"Time/minute", "Amplitude/Pa"}, 
 ImageSize -> Large, Frame -> True, LabelStyle -> "Author", 
 GridLines -> Automatic]

Signal

We generated a sine wave, amplitude 1, period = 40 minutes (2400 s), 600 samples. This should correspond to magnitude 0 at 0.00042 Hz, the rest should be at the background level.

So, you perform spectral analysis:

FFTValues = 2*Fourier[Signal]/Sqrt[NumberOfObservations];
FFTValuesR = 10 Log10[Abs[
  RotateRight[FFTValues, Floor[NumberOfObservations/2] - 1]]];

swapping the values is very important, such that your frequencies are aligned with the values. The values are already converted to decibel scale. Remember, that decibel scale is reference scale. So in fact you display how the value relates to the arbitrary value. In this case, it is 1 nm, but you should make sure that your reference is consistent with what you'd like to display. In sound, it is a pressure 10W/m2 if I'm not mistaken (please double check!). In systems, e.g. electronics, your reference is input signal.

If it turns out it should be power, not energy/amplitude, use factor 20 instead of 10.

Now, we can also scale frequencies accordingly. Here, for complexity, we had samples in minutes but frequency is in Hz (1/s):

cps2cpm = 1/60;
Dv = cps2cpm/NumberOfObservations;
frequencyValues = 
  Table[Dv (i - NumberOfObservations/2), {i, 1, 
    NumberOfObservations};

Confirm what is the sampling rate of your signal in order to obtain proper units on x-axis. This solution is much more robust than the way you select samples manually. So, finally, the plot:

FFTPlot = 
  ListPlot[Table[{frequencyValues[[i]], FFTValuesR[[i]]}, {i, 1, 
     NumberOfObservations}], 
   PlotRange -> {{0, Dv NumberOfObservations/2}, {-40,0}, 
   FrameLabel -> {"Frequency/Hz", "Magnitude/dB"}, ImageSize -> Large, 
   Frame -> True, LabelStyle -> "Author", 
   GridLines -> Automatic];

You can add an option Filling->Axis and use ListLogLinearPlot (as you did) if you want to make it more "audacity-like". DFT

So the peak matches the expectation. This was simple example for one frequency for demonstration. You could simply use the signal you have.

BONUS: In signal processing, prior to Fourier transform, you might need to multiply the signal by the window function (tapering). You could use HannWindow for example. This could sharpen your peak, and what we see in the plot is an effect of a subtle spectral leakage.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.