Reference value for magnitude of fft output

AI Thread Summary
The discussion revolves around the use of daqacquire and daqdocfft for sound data acquisition and Fourier transformation. The user is trying to understand how the magnitude of the Fourier transform output is calculated, specifically through the line 'mag = 20*log10(xfft)'. This line converts the magnitude to decibels (dB), with a reference value of 1. The user notes an expectation of negative dB values due to the output being less than 1, which is not observed. To set a reference value for the peak magnitude in the spectrum to 0dB, it is suggested to modify the calculation to 'mag = 20*log10(xfft/reference)', allowing for relative scaling based on a chosen reference value. The code itself is confirmed to be functioning correctly, and the explanation clarifies the dB calculation process.
Ricicle
Messages
1
Reaction score
0
Hi,

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.
 
Physics news on Phys.org
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)'
 

Similar threads

Back
Top