DSPRelated.com
Forums

FFT Questions

Started by Raeldor July 25, 2010
On Jul 26, 1:56&#4294967295;pm, Jerry Avins <j...@ieee.org> wrote:
> On 7/26/2010 4:43 PM, Tim Wescott wrote: > > > On 07/26/2010 01:07 PM, Raeldor wrote: > > &#4294967295; &#4294967295;... > > >> Thank you both for your explanations. Looks like i have some more > >> reading to do! :) Is there a standard formula to convert the returned > >> values into decibels? > > > Yes. The FFT returns complex numbers (or phasors, depending on where you > > learned this stuff). For your purposes, they're a number pair (a, b), > > where the estimated signal for that particular frequency bin is a * > > cos(w*t) + b * sin(w*t), w = 2 * pi * frequency. The magnitude of the > > signal is sqrt(a^2 + b^2); to convert that to Decibels you need to take > > the logarithm to the base 10 of the magnitude, and multiply by 20, then > > add a constant to take the effect of all the gains in your system into > > account: > > > mag(dB) = 20 * log(sqrt(a^2 + b^2)), or if you know your logarithms: > > mag(dB) = 10 * log(a^2 + b^2). > > Plus an additive constant to set the scale. When the constant is zero, > a^2 + b^2 = 1 becomes 0 dB. > > Jerry > -- > Engineering is the art of making what you want from things you can get. > &#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;
Thank you. So if I'm just interested in a 'loudness' or magnitude and am not worried about whether it is decibels, I can just use the sqrt(a^2*b^2)? I tried it, and for some numbers it's retuning NaN (not a number). I'm populating it with a sample squaretooth wave, could that results in NaN for some frequency bins? Thanks Ray
On 07/26/2010 02:19 PM, Raeldor wrote:
> On Jul 26, 1:56 pm, Jerry Avins<j...@ieee.org> wrote: >> On 7/26/2010 4:43 PM, Tim Wescott wrote: >> >>> On 07/26/2010 01:07 PM, Raeldor wrote: >> >> ... >> >>>> Thank you both for your explanations. Looks like i have some more >>>> reading to do! :) Is there a standard formula to convert the returned >>>> values into decibels? >> >>> Yes. The FFT returns complex numbers (or phasors, depending on where you >>> learned this stuff). For your purposes, they're a number pair (a, b), >>> where the estimated signal for that particular frequency bin is a * >>> cos(w*t) + b * sin(w*t), w = 2 * pi * frequency. The magnitude of the >>> signal is sqrt(a^2 + b^2); to convert that to Decibels you need to take >>> the logarithm to the base 10 of the magnitude, and multiply by 20, then >>> add a constant to take the effect of all the gains in your system into >>> account: >> >>> mag(dB) = 20 * log(sqrt(a^2 + b^2)), or if you know your logarithms: >>> mag(dB) = 10 * log(a^2 + b^2). >> >> Plus an additive constant to set the scale. When the constant is zero, >> a^2 + b^2 = 1 becomes 0 dB. >> >> Jerry >> -- >> Engineering is the art of making what you want from things you can get. >> &#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295; > > Thank you. So if I'm just interested in a 'loudness' or magnitude and > am not worried about whether it is decibels, I can just use the > sqrt(a^2*b^2)? I tried it, and for some numbers it's retuning NaN > (not a number). I'm populating it with a sample squaretooth wave, > could that results in NaN for some frequency bins?
You want an add inside the square root, not a multiply. But yes, that should work -- even with a multiply it should give a wrong number but not an NaN. Sumpin's wrong... The dB scale was invented to mimic our perception of sound level: one 'Bell' is roughly equivalent to "that sounds about twice as loud". So displaying sound levels in dB is a pretty good match for what a listener would perceive. -- Tim Wescott Wescott Design Services http://www.wescottdesign.com Do you need to implement control loops in software? "Applied Control Theory for Embedded Systems" was written for you. See details at http://www.wescottdesign.com/actfes/actfes.html
On Jul 26, 4:23&#4294967295;pm, Tim Wescott <t...@seemywebsite.com> wrote:
> On 07/26/2010 02:19 PM, Raeldor wrote: > > > > > > > On Jul 26, 1:56 pm, Jerry Avins<j...@ieee.org> &#4294967295;wrote: > >> On 7/26/2010 4:43 PM, Tim Wescott wrote: > > >>> On 07/26/2010 01:07 PM, Raeldor wrote: > > >> &#4294967295; &#4294967295; ... > > >>>> Thank you both for your explanations. Looks like i have some more > >>>> reading to do! :) Is there a standard formula to convert the returned > >>>> values into decibels? > > >>> Yes. The FFT returns complex numbers (or phasors, depending on where you > >>> learned this stuff). For your purposes, they're a number pair (a, b), > >>> where the estimated signal for that particular frequency bin is a * > >>> cos(w*t) + b * sin(w*t), w = 2 * pi * frequency. The magnitude of the > >>> signal is sqrt(a^2 + b^2); to convert that to Decibels you need to take > >>> the logarithm to the base 10 of the magnitude, and multiply by 20, then > >>> add a constant to take the effect of all the gains in your system into > >>> account: > > >>> mag(dB) = 20 * log(sqrt(a^2 + b^2)), or if you know your logarithms: > >>> mag(dB) = 10 * log(a^2 + b^2). > > >> Plus an additive constant to set the scale. When the constant is zero, > >> a^2 + b^2 = 1 becomes 0 dB. > > >> Jerry > >> -- > >> Engineering is the art of making what you want from things you can get. > >> &#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295; > > > Thank you. &#4294967295;So if I'm just interested in a 'loudness' or magnitude and > > am not worried about whether it is decibels, I can just use the > > sqrt(a^2*b^2)? &#4294967295;I tried it, and for some numbers it's retuning NaN > > (not a number). &#4294967295;I'm populating it with a sample squaretooth wave, > > could that results in NaN for some frequency bins? > > You want an add inside the square root, not a multiply. &#4294967295;But yes, that > should work -- even with a multiply it should give a wrong number but > not an NaN. &#4294967295;Sumpin's wrong... > > The dB scale was invented to mimic our perception of sound level: one > 'Bell' is roughly equivalent to "that sounds about twice as loud". &#4294967295;So > displaying sound levels in dB is a pretty good match for what a listener > would perceive. > > -- > > Tim Wescott > Wescott Design Serviceshttp://www.wescottdesign.com > > Do you need to implement control loops in software? > "Applied Control Theory for Embedded Systems" was written for you. > See details athttp://www.wescottdesign.com/actfes/actfes.html
Sorry, that was a coding error I'm getting real numbers now each time. It's starting to look a bit more meaningful though. If I use just sqrt(a^2+b^2) then I get a peak a what looks like the base frequency, plus smaller descending peaks at what look like the harmonics. However, if I modify to be 10*log(sqrt(a^2+b^2)) then it looks a little different... I still get the same number of peaks at the same positions, but there is a curved run-up and drop-off to the peak (sorry, I'm sure there's probably a technical term for this! :P). Is this just a effect of the decibel scale being more sensitive at lower amplitudes? Thanks again Ray
On 07/26/2010 08:44 PM, Raeldor wrote:
> On Jul 26, 4:23 pm, Tim Wescott<t...@seemywebsite.com> wrote: >> On 07/26/2010 02:19 PM, Raeldor wrote: >> >> >> >> >> >>> On Jul 26, 1:56 pm, Jerry Avins<j...@ieee.org> wrote: >>>> On 7/26/2010 4:43 PM, Tim Wescott wrote: >> >>>>> On 07/26/2010 01:07 PM, Raeldor wrote: >> >>>> ... >> >>>>>> Thank you both for your explanations. Looks like i have some more >>>>>> reading to do! :) Is there a standard formula to convert the returned >>>>>> values into decibels? >> >>>>> Yes. The FFT returns complex numbers (or phasors, depending on where you >>>>> learned this stuff). For your purposes, they're a number pair (a, b), >>>>> where the estimated signal for that particular frequency bin is a * >>>>> cos(w*t) + b * sin(w*t), w = 2 * pi * frequency. The magnitude of the >>>>> signal is sqrt(a^2 + b^2); to convert that to Decibels you need to take >>>>> the logarithm to the base 10 of the magnitude, and multiply by 20, then >>>>> add a constant to take the effect of all the gains in your system into >>>>> account: >> >>>>> mag(dB) = 20 * log(sqrt(a^2 + b^2)), or if you know your logarithms: >>>>> mag(dB) = 10 * log(a^2 + b^2). >> >>>> Plus an additive constant to set the scale. When the constant is zero, >>>> a^2 + b^2 = 1 becomes 0 dB. >> >>>> Jerry >>>> -- >>>> Engineering is the art of making what you want from things you can get. >>>> &#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295; >> >>> Thank you. So if I'm just interested in a 'loudness' or magnitude and >>> am not worried about whether it is decibels, I can just use the >>> sqrt(a^2*b^2)? I tried it, and for some numbers it's retuning NaN >>> (not a number). I'm populating it with a sample squaretooth wave, >>> could that results in NaN for some frequency bins? >> >> You want an add inside the square root, not a multiply. But yes, that >> should work -- even with a multiply it should give a wrong number but >> not an NaN. Sumpin's wrong... >> >> The dB scale was invented to mimic our perception of sound level: one >> 'Bell' is roughly equivalent to "that sounds about twice as loud". So >> displaying sound levels in dB is a pretty good match for what a listener >> would perceive. >> >> -- >> >> Tim Wescott >> Wescott Design Serviceshttp://www.wescottdesign.com >> >> Do you need to implement control loops in software? >> "Applied Control Theory for Embedded Systems" was written for you. >> See details athttp://www.wescottdesign.com/actfes/actfes.html > > Sorry, that was a coding error I'm getting real numbers now each > time. It's starting to look a bit more meaningful though. If I use > just sqrt(a^2+b^2) then I get a peak a what looks like the base > frequency, plus smaller descending peaks at what look like the > harmonics. However, if I modify to be 10*log(sqrt(a^2+b^2)) then it > looks a little different... I still get the same number of peaks at > the same positions, but there is a curved run-up and drop-off to the > peak (sorry, I'm sure there's probably a technical term for > this! :P).
> Is this just a effect of the decibel scale being more > sensitive at lower amplitudes?
Yes -- the run up and run down are still there in the linear magnitude plot, they're just not apparent. -- Tim Wescott Wescott Design Services http://www.wescottdesign.com Do you need to implement control loops in software? "Applied Control Theory for Embedded Systems" was written for you. See details at http://www.wescottdesign.com/actfes/actfes.html
On Jul 26, 11:44&#4294967295;pm, Raeldor <rael...@gmail.com> wrote:
> On Jul 26, 4:23&#4294967295;pm, Tim Wescott <t...@seemywebsite.com> wrote: > > > > > > > On 07/26/2010 02:19 PM, Raeldor wrote: > > > > On Jul 26, 1:56 pm, Jerry Avins<j...@ieee.org> &#4294967295;wrote: > > >> On 7/26/2010 4:43 PM, Tim Wescott wrote: > > > >>> On 07/26/2010 01:07 PM, Raeldor wrote: > > > >> &#4294967295; &#4294967295; ... > > > >>>> Thank you both for your explanations. Looks like i have some more > > >>>> reading to do! :) Is there a standard formula to convert the returned > > >>>> values into decibels? > > > >>> Yes. The FFT returns complex numbers (or phasors, depending on where you > > >>> learned this stuff). For your purposes, they're a number pair (a, b), > > >>> where the estimated signal for that particular frequency bin is a * > > >>> cos(w*t) + b * sin(w*t), w = 2 * pi * frequency. The magnitude of the > > >>> signal is sqrt(a^2 + b^2); to convert that to Decibels you need to take > > >>> the logarithm to the base 10 of the magnitude, and multiply by 20, then > > >>> add a constant to take the effect of all the gains in your system into > > >>> account: > > > >>> mag(dB) = 20 * log(sqrt(a^2 + b^2)), or if you know your logarithms: > > >>> mag(dB) = 10 * log(a^2 + b^2). > > > >> Plus an additive constant to set the scale. When the constant is zero, > > >> a^2 + b^2 = 1 becomes 0 dB. > > > >> Jerry > > >> -- > > >> Engineering is the art of making what you want from things you can get. > > >> &#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295; > > > > Thank you. &#4294967295;So if I'm just interested in a 'loudness' or magnitude and > > > am not worried about whether it is decibels, I can just use the > > > sqrt(a^2*b^2)? &#4294967295;I tried it, and for some numbers it's retuning NaN > > > (not a number). &#4294967295;I'm populating it with a sample squaretooth wave, > > > could that results in NaN for some frequency bins? > > > You want an add inside the square root, not a multiply. &#4294967295;But yes, that > > should work -- even with a multiply it should give a wrong number but > > not an NaN. &#4294967295;Sumpin's wrong... > > > The dB scale was invented to mimic our perception of sound level: one > > 'Bell' is roughly equivalent to "that sounds about twice as loud". &#4294967295;So > > displaying sound levels in dB is a pretty good match for what a listener > > would perceive. > > > -- > > > Tim Wescott > > Wescott Design Serviceshttp://www.wescottdesign.com > > > Do you need to implement control loops in software? > > "Applied Control Theory for Embedded Systems" was written for you. > > See details athttp://www.wescottdesign.com/actfes/actfes.html > > Sorry, that was a coding error I'm getting real numbers now each > time. &#4294967295;It's starting to look a bit more meaningful though. &#4294967295;If I use > just sqrt(a^2+b^2) then I get a peak a what looks like the base > frequency, plus smaller descending peaks at what look like the > harmonics. &#4294967295;However, if I modify to be 10*log(sqrt(a^2+b^2)) then it > looks a little different... I still get the same number of peaks at > the same positions, but there is a curved run-up and drop-off to the > peak (sorry, I'm sure there's probably a technical term for > this! :P). &#4294967295;Is this just a effect of the decibel scale being more > sensitive at lower amplitudes? > > Thanks again > Ray- Hide quoted text - > > - Show quoted text -
Use 20, instead of 10, in your deciBel equation OR Use 10, but drop the sqrt in the log(). Dirk