Question about magnitude

Hello I use this code for analyize sound

What is magnitude ? Is it amplitude? What relation is between magnitude and amplitude and ADC?

Please, edit your post to use code tags. Parts of your code were removed and caused the entire post to be printed in italic. You would have known that if you'd have read the guidelines in the sticky posts on 'how to use this forum'.

Magnitude refers to the distance between a point in the complex plane and the origin. Sometimes, this can be the distance squared.

It is proportional to the sum of the amplitudes of the sinusoidal waves at the input with a frequency that falls within the frequency bin in question. You probably have to divide by the number of frequency bins for it to be correct, if the library doesn't already do this.


Hi pieter I am sorry I edit my post

Amplitude= ((magitude ^2)-(frquency^2))^0.5 Is it true?

What is relation between ampltude and ADC in per bin? How can i calculate ADC in per bin?

When you calculate the FFT of 2n samples, you get a complex number for each of the 2n frequency bins. These frequency bins are symmetrical around the Nyquist frequency (sample frequency / 2), or symmetrical around the origin (i.e. negative frequencies). This means that you only need half of the frequency bins (because the other half is the same). The magnitude is the distance between these complex numbers and the origin: Magnitude(a+bi) = sqrt(a²+b²).

The way the FFT works, it scales the result by the window size (2n). So you have to divide the results by 2n to get the actual magnitude. However, this also includes the negative frequencies, so it will be half of what you would expect. Solution: multiply by two again.

In short: actual magnitude of result (a+bi) = sqrt(a²+b²)/n

Interpretation: if you apply a sine wave with an amplitude of 1 to the input, the magnitude of the resulting frequency bin will be one as well. (Depending on the resolution, and only if the frequency of the sine wave is a natural multiple of the frequency resolution of the FFT, otherwise, it will be smeared out across neighboring bins as well.)

Note: when you're interested in the energy of each frequency bin, or if you want to express it in dB, calculating the square root is a complete waste of resources.

Noisecontrol: What is relation between ampltude and ADC in per bin? How can i calculate ADC in per bin?

Parseval's theorem states that the energy in the time domain is the same as the energy in the frequency domain. However, for a discrete Fourier transform (of which the FFT is an implementation) you have to scale it by a factor of n (or 2n, if you care about negative frequencies).

Your means is if my 2n=128 I must magnitude divid to 64 to be actual magnitude in per bin? Please say one example to do it

Noisecontrol: Your means is if my 2n=128 I must magnitude divid to 64 to be actual magnitude in per bin?


I can't explain it quickly without a good mathematical foundation. It is not an easy topic. You should probably do your own research to try and grasp the mathematics behind all of it. Or just blindly do what everyone else does, without understanding it, that'll probably work just fine in this case.

What do you want to do with the result?

I want to make a sound level meter that show result in dB A_weighting For get a weighing must first analyze sound and then wighted There are a equiption that convert adc to Vrms and then dB dB =20log(Vrms/0.0063)+94-sensivity mic-gain If i can Vrms in per bin .i can calculate dB per bin and then i can weight them and calculate overall SPl in dBA

I already explained how to get the magnitude of the frequency bins. You're interested in the power spectral density, which can be estimated by the magnitude squared.

To convert it to a logarithmic scale, just take the logarithm of the results. This is the power in Bels. Multiply by ten to get decibels.

Now you're still stuck in ADC units. You have to scale everything by a factor of 5V/1024 to convert it to Volts (or Watts). Just add 20*log(5.0/1024) to the result.

Remember: actual magnitude squared of (a+bi) = |a+bi|²/n² = (a²+b²)/n². No need to take the square root. Just use a factor of 10 before your logarithm instead of 20 (because 20 log(x) = 10 log(x²)).

Then add 94 and the mic gain to set your reference level and compensate for the microphone's sensitivity.

Edit: where did you get that factor 1/0.0063 and 94?

20 log(x) = 10 log(x²)).

X is magnitude?

Yes. But that's a mathematical identity that holds true for all (positive) x.

0.0063 get from sensivity of microphone
If mic sensivity be -44dBV the by under equiption you can calculated

In that case, why do you add those other two terms? Keep in mind that 20 log(x/y) = 10 log(x²) - 20 log(y).

I think it is difficult If i convert adc to dB and then do fft on it Does magnitude is equal dB in per bin?

Like this K=analogread(A0) Vrms=(k*5/1023)*0.707 dB=20log(Vrms/0.0063)+94-44-gain

Then dB enter fft lib and show dB per bin

Noisecontrol: I think it is difficult If i convert adc to dB and then do fft on it Does magnitude is equal dB in per bin?

That won't help. You have to do the conversion either way. And if you do it before the FFT, you'll lose a lot of precision. On top of that you would have to divide, which is many times slower than the subtraction you have to do after the log.

No: dB = 10 log (magnitude ²) - c

Where c is the compensation for voltage reference, ADC resolution, amplification and microphone sensitivity. It is constant, so you only have to calculate it once. magnitude² = (a²+b²)/n² And FFT[k] = a+bi for frequency bin k.

As for the Vpeak to VRMS conversion: I don't know, honestly, I'd have to work that out on a piece of paper and verify in MATLAB or Octave.

a and b what is it?

That souce code show magnitude for me

Do that value magnitude set in this equiption?

a = vReal[k], b = vImag[k]

Note that this function calculates the square root. Don't do that, it's unnecessary.

FFT.ComplexToMagnitude(vReal, vImag, samples); /* Compute magnitudes */

This part code calculate magnitude in per frequncey bin You say me that per mag divid to 64 then set in this equiption dB=10log( magnitude^2)- c

Is it true?

Yes. But instead of calculating magnitude[k] ² as sqrt(vReal[k]² + vImag[k]²)², calculate it directly as vReal[k]² + vImag[k]².

Is this a personal project? Is it an assignment for your studies? What’s your background in mathematics and DSP?