Go Down

Topic: Question about magnitude (Read 4230 times) previous topic - next topic

Noisecontrol

Jun 05, 2018, 12:02 pm Last Edit: Jun 05, 2018, 12:26 pm by Noisecontrol
Hello
I use this code for analyize sound

https://github.com/kosme/arduinoFFT/blob/master/Examples/FFT_03/FFT_03.ino



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

PieterP

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.

Pieter

Noisecontrol

#2
Jun 05, 2018, 12:37 pm Last Edit: Jun 05, 2018, 12:40 pm by Noisecontrol
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?

PieterP

#3
Jun 05, 2018, 01:00 pm Last Edit: Jun 05, 2018, 01:21 pm by PieterP
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.

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).

Noisecontrol

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

PieterP

#5
Jun 05, 2018, 01:28 pm Last Edit: Jun 05, 2018, 01:31 pm by PieterP
Your means is if my 2n=128
I must magnitude divid to 64 to be actual magnitude in per bin?
Yes.

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?

Noisecontrol

#6
Jun 05, 2018, 02:07 pm Last Edit: Jun 05, 2018, 02:14 pm by Noisecontrol
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

PieterP

#7
Jun 05, 2018, 02:28 pm Last Edit: Jun 05, 2018, 02:36 pm by PieterP
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?

Noisecontrol

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

X is magnitude?

PieterP

#9
Jun 05, 2018, 02:37 pm Last Edit: Jun 05, 2018, 02:37 pm by PieterP
Yes. But that's a mathematical identity that holds true for all (positive) x.

Noisecontrol

#10
Jun 05, 2018, 02:43 pm Last Edit: Jun 05, 2018, 02:44 pm by Noisecontrol
0.0063 get from sensivity of microphone
If mic sensivity be -44dBV the by under equiption you can calculated

PieterP

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).

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?

Noisecontrol

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

PieterP

#14
Jun 05, 2018, 03:01 pm Last Edit: Jun 05, 2018, 03:11 pm by PieterP
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.

Go Up