0

Suppose I have ADC microphone with a given SNR in the sense that is related to its quantization. From this tutorial we have the following connection how bit-depth determines SNR, Eq. (9): $$ \mathrm{SNR} = 6.02 \times N + 1.76 \ \mathrm{dB} $$ where $N$ is the bit-depth. I would like to invert this relation in Python. Generate a float32 signal and quantize it as if it was recorded by an ADC microphone with the given SNR.

My original Python code was:

bits = int((snr_mic - 1.76)/6.02)
quant = 2**bits
out_audio = np.floor(audio * quant).astype(int)/quant

Later, I modified the code to this:

bits = np.floor((snr_mic - 1.76)/6.02) + 1
quant = 2**bits
out_audio = (audio * quant).astype(int)/quant

Since it enforces a signal simulated to be recorded with 0 dB SNR mic to be all 0's. However, even signals simulated to be recorded with 5 dB of SNR mic was all 0's. So I probably have bug in my code, probably related to the fine difference between floor and int for negative numbers.

What is the correct way to do it and have nonzero quantized signals for a given nonzero SNR mic?

Triceratops
  • 398
  • 2
  • 13
  • What exactly do you mean by "ADC microphone". Do you want to model A_ the noise of an ADC quantization. B) the self noise of a microphone or C) the typically acoustic background noise that dominates most microphone recordings? These are all very different things. If someone uses the term "SNR of a microphone recording" they typically mean C) or B). A) is rarely a significant issue – Hilmar Feb 22 '22 at 07:09
  • A 1-bit microphone would have an SNR of 7.8dB -- why are you surprised that a calculation that specifies fewer than one bit(s) has all zeros as an output? As a debugging aid, printing out your 'bits' and 'quant' numbers would probably be helpful. – TimWescott Jul 21 '22 at 19:28

1 Answers1

1

5 dB is not a sensible value for a formula whose minimum result would be 7.78 dB for $N=1$. So, your test case is "broken", and correctly, you get a 0-bit quantization (i.e., no values at all).

Even for higher SNRs: This sounds pretty reasonable!

Your audio will rarely come close to maximum amplitude. Think about this: if you only had, say, two bits, then that's 1 bit for the sign (think your audio as shifted to be centered around 0 if it helps), and 1 bit leading to two amplitude steps. If your audio recording always keeps more than 6 dB headroom (to avoid clipping!), then that quantization leads to all-zero.

Audio ADCs are pretty good, and I'd have no problem calling anything with a QSNR < 32 dB "atrocious".


Note that your considerations also has another shortcoming:

The formula you cite only applies to signals that are uniformly distributed over exactly the full ADC range. You'll be very hard tasked finding an audio signal that even remotely looks like that! For clipping / linearity / crest factor reasons, you need to have headroom for the vast majority of audio samples. Then, speech, music and many noise sources do not resemble uniformly distributed values:

Unnormalized histogram: triangle with tails nearly reaching max. amplitude Unnormalized 100-bin histogram over the values in Devin Townsend's Kingdom, a song that definitely tries to also achieve high amplitudes.

Unnormalized Histogram: Pretty much a triangle Unnormalized 100-bin histogram over the values in Nightwish's Angels Fall First, a song that contains clean vocals and acoustic guitar playing.

As you can see, assuming all values are equally probable is very far from the truth; accordingly, the actual quantization noise (the difference between quantized and original signal) does not follow the oft-cited formula. Whether or not that's a problem to you, I can't tell: it depends on what you want to show. However, "I have an audio SNR of x dB, thus I must have an ADC with (x-1.76)/6.02" is a fallacy.

Marcus Müller
  • 30,525
  • 4
  • 34
  • 58
  • 3
    Most signals will be distributed over a quantization interval as long as the signal spans a few quantization levels between samples (similar to making random white noise generators through modulo math). The main point about the formula is that it is applicable to the signal being a sine wave specifically (with a variance of Vpp/8 compared to the variance of noise as q/12). – Dan Boschen Feb 21 '22 at 18:08
  • That comment should have more than my upvote, @DanBoschen – Marcus Müller Feb 21 '22 at 19:35
  • Just being helpful (I hope and not coming across as too critical) but if you agree, please update your answer and I will upvote and delete my comments (I is the magnitude distribution of the noise that is uniform while the signal distribution is bowl shaped as a sinusoid spends more time at the edges). I’ve confirmed other distributions and it all matches it is just a matter of peak to average for whatever waveform you test with and what exactly “peak” is. (Easy to define for a sine wave). – Dan Boschen Feb 21 '22 at 21:22
  • Thank you for your answer. Let us suppose I still want to simulate quantization using that model and formula. I understand that SNR < 8 dB will give me rubbish, but still: what is the correct implementation of the quantization? – Triceratops Feb 22 '22 at 09:07
  • 1
    Your quantization model as is looks correct, you'd want to use int instead of floor (floor(-0.1) == -1), to get lower average error. – Marcus Müller Feb 22 '22 at 09:18
  • @DanBoschen I meant to fix the answer, but didn't get around to doing it yet. Also, it deserves a bit more math, namely, if we model things as having headroom (i.e., the amplitude PDF having support that's a true subinterval spanning only a fraction of the full ADC scale), does that have significant influence on the QSNR (my guess now, after considering your comment, is: it has influence, but not much, as we reduce error within the values that actually occur, but we also reduce signal power) – Marcus Müller Feb 22 '22 at 09:26
  • @MarcusMüller You refer to the 2nd block of code, right? If I use int instead of floor I would need to cancel the +1? For positive numbers int should be the same floor. – Triceratops Feb 22 '22 at 09:44
  • 2
    @MarcusMüller See https://dsp.stackexchange.com/questions/40259/what-are-advantages-of-having-higher-sampling-rate-of-a-signal/40261#40261 I simulated this in the past with backing off from full-scale and decreasing number of bits and different distribution with interesting results-- if someone ever asks a related question I can post those results, but it is completely independent of back-off (reduces db for db) as long as we meet the first criteria I gave on what makes the quantization noise approximately a white noise process with uniform distribution. – Dan Boschen Feb 22 '22 at 13:08
  • 1
    Also rounding and truncation give the same error other than introducing a DC offset (which typically is not included in noise power similar to not including the mean in the standard deviation and variance). – Dan Boschen Feb 22 '22 at 13:11
  • In my applications there is no DC component. All the signals are simulated by Python. My current code is bits = np.floor((snr_mic - 1.76)/6.02) + 1 ; quant = 2**bits ; out_audio = (audio * quant).astype(int)/quant ; where ; is end command. I think it is correct but I don't know if to use int or floor because of the different effects on negative numbers. – Triceratops Feb 22 '22 at 16:12