Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Reference value for magnitude of fft output

  1. Sep 12, 2009 #1

    I'm using a version of daqacquire to acquire a sample of sound data. This program also uses daqdocfft to perform a fourier transform, allowing me to view the frequencies that are present in my data sample.

    My problem is that I don't understand how the function is calculating the magnitude of the output values of the fourier transform. The code is as follows:

    xfft = abs(fft(data));

    % Avoid taking the log of 0.
    index = find(xfft == 0);
    xfft(index) = 1e-17;

    mag = 20*log10(xfft);
    mag = mag(1:floor(blocksize/2));
    f = (0:length(mag)-1)*Fs/blocksize;
    f = f(:);

    The line 'mag = 20*log10(xfft);' seems to assign a dB value to the output, referenced to 1. If this was the case, I would expect a plot of the spectrum to have negative magnitudes, since they would all be less than 1. However this is not the case.

    Ideally what I would like to do is assign a reference value to the peak magnitude in the spectrum, so that it would be set to 0dB, and all other values would be relative to it.

    I'd be grateful if anyone could tell me how to do this, or at least explain to me what this code is using as 0dB, and what value is being referenced.

    Thanks for any help.
  2. jcsd
  3. Sep 13, 2009 #2


    User Avatar
    Science Advisor
    Gold Member

    There is nothing wrong with that code as far as I can see.
    The line 'mag = 20*log10(xfft)' does indeed calculate the magnitude in dB (the 20 comes from the fact that it is calculating the power spectrum 20=2*10) referenced to 1 and if the variable xfft contains values smaller than 1 it will give negative magnitude values.

    The only thing you need to do in order to reference your scale to another value is to write 'mag = 20*log10(xfft/reference)'
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook