how to interpret the magnitude of FFT - Page 1
Forum section: electronics / beginners

author: engineheat
author_rank: Frequent Contributor
timestamp: June 19, 2019, 06:26:38 pm
content: Hi,In one of my project, I record an audio using a mic connected to a PC, and calculate the FFT using Python. I used PyAudio for the recording. Upon calculating the magnitude, I noticed that its range can vary depending on the format (16 bit vs 32 bit) of the recording. I don't know if I did something wrong or is there an explanation for this. So how do you magnitude of, say, 150 at 2000Hz or magnitude of 1200 at 4000Hz? Are there any physical meanings to the numbers or are they meaningful only in a relative sense?Furthermore, I want to take the audio data and convert it to a A-weighted decibel reading much like those given in handheld decibel meters. Is this something I can do from the FFT? A simple example would be nice.Thanks

author: radiolistener
author_rank: Super Contributor
timestamp: June 19, 2019, 08:53:35 pm
content: Quote from: engineheat on June 19, 2019, 06:26:38 pmyes, it depends on FFT length and sample resolution (bits). You're need to normalize FFT result by divide magnitudes with FFT length multiplied by half of the sample max value power(2, N)/2, where N is sample resolution in bits. Also, you can do it by translate your magnitude to decibels:magnitude = sqrt(re*re + im*im);db = 20 * log10(magnitude);and then you can substract constant to normalize it and get power relative to maximum (full scale):dbNormalized = db - 20 * log10(fftLength * pow(2,N)/2)after that you will get normalized value in decibels.For example, if you get FFT for the recording length 10000 samples, and 16 bits per sample, then calculations will be the following:dbNormalized = 20 * log10(magnitude) - 20 * log10(10000 * pow(2, 16)/2) = 20 * (log10(magnitude) - log10(10000 * 32768))0 dB means maximum allowed (full scale) amplitude for ADC.-20 dB means 10 times smaller amplitude than maximum.-40 dB means 100 times smaller amplitude than maximum.-60 dB means 1000 times smaller amplitude than maximum.etcIf you know the power of maximum allowed amplitude in dBm, you can add it and get absolute value of the power.For example, if your 16 bit ADC has 10 dBm power for maximum allowed amplitude (full scale), then:absolute power in dBm = dbNormalized + 10 dBmQuote from: engineheat on June 19, 2019, 06:26:38 pmI'm not familiar with A-weighting scale, but as I understand, you're need to correct normalized decibels with some correction table. Just substract some value taken from the table. I don't know how to calculate this correction table.

author: engineheat
author_rank: Frequent Contributor
timestamp: June 20, 2019, 03:47:59 pm
content: Quote from: radiolistener on June 19, 2019, 08:53:35 pmThanks. So the normalized FFT will always range from 0 to 1?For a given sound, can the normalized FFT change drastically if one uses a different PC (or sound card) to do the recording? I guess it can since you mentioned dbm at max amplitude, which is ADC dependent.

author: dmills
author_rank: Super Contributor
timestamp: June 27, 2019, 01:55:56 pm
content: Yep, ADC, Preamp gain and Mic dependent, there is a reason sound pressure meters come with calibrators...Incidentally you can save on a square root by taking advantage of the way logs work with powers:\[dB=20\log((Re^2 + Im^2)^{0.5}) = 10\log(Re^2 + Im^2)\]

author: engineheat
author_rank: Frequent Contributor
timestamp: June 28, 2019, 03:49:24 am
content: Quote from: dmills on June 27, 2019, 01:55:56 pmThanks, but calibration is probably not necessary if I just want to compare the relative difference of two signals right? So if I record using the same equipment and signal A has double the amplitude at 1000hz compared to signal B, I can conclude that signal A has double the amplitude at 1000 hz right?Granted there might be some minor details that makes the above statement not exactly true in real life, but you get my gist.

author: radiolistener
author_rank: Super Contributor
timestamp: June 30, 2019, 03:36:09 pm
content: Quote from: engineheat on June 28, 2019, 03:49:24 amyes, if you're don't need to know absolute value, there is no need for calibration sources.But you're still needs for calibration source with known value, just for test purposes.In order to make sure, that all works as expectedThere are a lot of possible issues which may affect results, for example insufficient precision of float values, calculation mistakes and other. So, you're need for some test equipment which allows to make sure that all works as expected.

author: engineheat
author_rank: Frequent Contributor
timestamp: July 01, 2019, 02:00:54 pm
content: Quote from: radiolistener on June 30, 2019, 03:36:09 pmI generated mathematically some sine waves with known frequency and amplitude, my fft perform as expected. In this case, the magnitude is a number I know.I played some tune from Youtube (say, constant 1k hz), and the frequency is correct as well. It's just that the amplitude part was hard to make sense of, hence this question. But it does go up or down as I move the sound source closer or further from the mic.what would be a proper way to calibrate it? maybe I can obtain a sound level meter. It says herehttps://www.prosoundweb.com/topics/audio/sound_level_meters_the_primer_what_how_why_techniques_more/that "All SLMs feature an omnidirectional measurement quality condenser microphone, a mic preamp, frequency weighting networks, an RMS detector circuit, averaging circuits, the meter display, AC and DC outputs used to feed other measurement devices or for recording (see Figure 1, below)" and that "A sound level meter (SLM) is a device used to make frequency-weighted sound pressure level measurements displayed in dB-SPL. 0.0 dB-SPL is the threshold of hearing, and is equal to 20uPa (microPascals). This correlates to what one would aurally perceive when in a deep cave or in a large anechoic chamber."So if I know how frequency weighting works, and caliberate my magnitudes with the proper scaling so that 0.0db = 20uPA, I just built myself a sound level meter?

author: engineheat
author_rank: Frequent Contributor
timestamp: July 03, 2019, 03:44:19 pm
content: Based on some reading, here's my understanding of how sound recording works:Microphone outputs a voltage based on sound pressure. The microphone voltage is amplified and the amplified voltage goes into an ADC. The amplifier gain is such that the maximum microphone output is matched to the maximum input voltage of the ADC. The ADC converts the input voltage into a number, and this number's magnitude can depend on the number of bits. So let's say the max input voltage is 5V, this would be 255 for 8 bit, or 65535 for 16 bit.When I record an audio stream using something like Python, the audio is composed of numbers that the ADC outputs, and that can vary based on if I used 8 bit or 16 bit encoding. Therefore, the FFT magnitude will vary too.Please correct me if I'm wrong.

author: bson
author_rank: Unknown
timestamp: July 05, 2019, 07:00:44 pm
content: The ADC output is typically signed, relative to a reference voltage, generally set somewhere around half the full scale. Like 1.8V or 2.048V for a 3.3V ADC. The resulting FFT magnitudes are always absolute, |s|, relative to 0. If you have an offset (bias) this will end up in the first bin - which is 0Hz (with a value relative to the ADC zero). If the offset is negative the first bin's magnitude will be a positive value (|s(wt=0)|) with a 180˚ phase. Technically, FFT wants a 0 mean input, meaning the sum of all samples is 0, and if you want a mathematically correct result this is what you need to provide. (This goes hand in hand with RMS measurements, which also require a 0 mean.) But the error diminishes with an increasing number of signal cycles.

author: hamster_nz
author_rank: Super Contributor
timestamp: July 06, 2019, 12:13:57 am
content: Quote from: engineheat on July 03, 2019, 03:44:19 pmYou are pretty much correct. I would just add that there is a low-pass filter in front of the ADC (or implemented inside the ADC) to remove signals with a frequency higher than around 45% of the sample rate.If you scale the samples as being from -1.0 to 1.0 (rather than -128 through 127 for 8-bit signed samples or -32768 through 32767 for 16-bit signed samples) then the FFT magnitude will remain constant too.If you haven't found it yet, you might find this site very interesting:http://www.dspguide.com/- all the chapters are available as PDF files. It is very well written.

author: lordvader88
author_rank: Frequent Contributor
timestamp: July 08, 2019, 03:32:04 pm
content: So what does a FFT do ? Does it take a waveform, and make a spectrum of the power over some frequency range like a spectrum analyzer ?

author: IDEngineer
author_rank: Super Contributor
timestamp: July 08, 2019, 05:06:56 pm
content: Quote from: lordvader88 on July 08, 2019, 03:32:04 pmThe way I help people understand the FFT (more properly in this context, the DFT or Discrete Fourier Transform) is as follows:Imagine a piano with its sounding board and all its tuned strings. If you play a sine wave nearby, the strings will resonate with the sine wave. Those strings tuned closest to the sine wave's frequency will resonate the best. Thus you could discern an input frequency by measuring the resonance of the strings. This is an example of mechanical coupling; the stringsmechanicallyresonate based on their tuned frequency.A more complex waveform, built up from multiple sine waves, would cause multiple piano strings to resonate. The intensity of the resonance would give you an indication of the spectral content at that string's frequency. You could then plot the strength of each string's resonance on a graph, giving you a representation like the spectrum analyzer you mentioned.In the DFT, the discrete frequencies of the piano strings are represented by "bins". Each binmathematicallyresonates when the analyzed signal has content at its "tuned frequency". If you had a sine wave centered on a single bin's frequency, analyzing that sine wave would result in that bin "resonating".Like the piano example, a more complex waveform built up from multiple sine waves would cause multiple bins to "resonate". The intensity of each DFT bin gives you an indication of the spectral content at that bin's frequency. You can then plot the magnitude of each bin on a graph like a spectrum analyzer.This is a vastly simplified explanation that leaves out a lot of details. For example, the "Discrete" aspect of the DFT is important: There are a finite number of bins, representing a finite number of frequencies, and so there will be always be intermediate frequencies "between the bins" that won't be perfectly represented. But hopefully this piano sounding board analogy will help you grok the basic concept of how the DFT works at a macro level. From here, you can go as deep as you'd like!

author: engineheat
author_rank: Frequent Contributor
timestamp: July 16, 2019, 02:19:59 pm
content: Thanks.I've spent some effort trying to understand how to calibrate the FFT to get an accurate SPL measurement and get an A weighted decibel reading. But I wonder if this is even necessary if all I want to do is compare two signals? Say I have a classification task where I need to classify two different sound, one is from a drum and one is from a piano key. The two sounds have different peaks among the frequency range, or even if they have the same peak, the magnitude is different. I'm interested in things like "sound A has a magnitude at 4khz that's twice the magnitude at 2khz" and "sound B has the same magnitude at 4khz as 2khz". Knowing this will allow me to classify the sound and just using the raw FFT data without calibration would be enough right?Assuming I use a mic with a flat frequency response, is the above approach for classification reasonable?