Envelopes and ADCs

Started by FiveseveN, November 06, 2021, 06:54:58 AM

Previous topic - Next topic

FiveseveN

So I've ran into a situation that you might find interesting :icon_mrgreen:
What I'm trying to do is implement a sort of digitally-controlled noise gate, where the principal goal is to use the least parts (or board space) possible, since it's kind of an afterthought in an already crowded project.
The players in this match are: a buffered guitar signal as the source and a spare op amp I'm looking to use, and on the other side the 12-bit SAR ADC of an ESP32.
An important caveat of this ADC is that it's not quite linear:



So an envelope going from 0V upwards isn't very useful when you're interested in the lowest values, it would need a .3V or so bias, which means at least a couple of extra resistors (reasonable).
Here are the solutions I've come up with:
• Proper (full wave, with gain) envelope detector. Best performance, most parts (unsurprisingly). Doing the averaging in code is essentially free, so the rectifier part should be sufficient. That saves a big cap but probably needs one more op amp anyway.
• Precision rectifier (half wave, with gain). Less sensitive but much simpler.
• All the gain, then scale down. Take the ~7.5V p-p signal from the op amp and scale it about 1/2.3 so it sits in the ADC range and remains biased around the middle: least parts (2 resistors for gain + 2 for the divider), probably worst performance.
• All the gain, then clip. Same concept but clip the tops at ~3.3V p-p instead of attenuating, for better "signal" to noise ratio. Maybe not advantageous since it involves about as many parts as the first option.
The last two would provide signal biased around 1.65V, using the most linear part of the ADC and taking an absolute value instead of rectifying in the analog domain. It's still to early in the day for me to figure if that halves the resolution. I also had a dream about auto-calibration of the bias point but then I realized it needs to mute the input signal, which means more complexity and parts. I'm sure there are fancier ways to do that in code, but then we'd have to move to the DSP forum ;D.
I'm working up the courage to try them on a breadboard but I'm confident you folks will point out something I've overlooked since I hardly ever deal with envelope detection.
Quote from: R.G. on July 31, 2018, 10:34:30 PMDoes the circuit sound better when oriented to magnetic north under a pyramid?

Mark Hammer

I'm certainly not one of them, but studio folks will often view different studio compressors as better suited to this or that signal source.  I suppose some of that sorting into category A, B, C, etc. has something to do with the limits or characteristics of the control element - FET, LDR, OTA, etc.  But I imagine some of it also has to do with the characteristics of the envelope detection, with different rectifier circuits responding better and worse to the particular dynamics and qualitative aspects than others.

So, from that perspective, the question to start with is "What characteristics of a guitar signal are the ones you want to regulate/shape?".

ElectricDruid

I had a crack at this on a cheap PIC. Similar thing, minimum parts, not fantastic ADC.

What I did was bias the ADC input to the midpoint voltage (2.5V in my case) with a couple of 100K resistors, and then feed the audio in with a cap (100n, IIRC). The ADC sampled the audio at about 22KHz, which is fast enough not to miss anything important, but gives you twice as much time for processing as sampling at a full audio rate like 44KHz would do.
The ADC provided 10 bits. I used 9 bits, ignoring the lowest bit. I did the full wave rectification digitally, simply by looking to see if the most significant bit was set or clear. If it's set, clear it. If it's clear, invert the lowest 8-bits of the 9.
That gives you an absolute value for the signal 0-255.

There's lots that's wrong with this from a "serious DSP" point of view. Doing FW rectification like that makes proper DSP engineers scream and run about shouting "Aliasing! aliasing!". And an 8 bit signal isn't a lot of dynamic range, and dynamic range is what we're after.

BUT an 8 bit signal is cheap and quick to calculate with and the even cheaper fullwave rectification actually works well enough.  Once you've got it, you can do you favourite ripple filtering or peak hold algorithm or whatever to give you the final envelope  output. It's important to remember that analog envelope followers are inherently a compromise between speed and ripple, and they're not actually that good! They're *ok*, but this is not something that analog processing does brilliantly. Consequently, it's not that hard to do something at least equally good, even with a very basic chip like I used. With a far more powerful 32-bit chip, you should be in clover, and it's more a question of how much work and clever complications you can put in.



Vivek

#3
Could this article be useful ?

http://rockman.fr/Reviews/SG.htm

It talks about High Frequency detection to open gate but Envelope follower to close gate

and gate is a VCF, rather than VCA that is either open or closed



Gate opens extremely fast upon detecting High Frequency, and closes relatively slowly in response to slow decay of input

FiveseveN

Thanks for the input, everyone! Looks like I got a bit distracted trying out the H&K Black Spirit 200 but I did manage to break out the breadboard. Turns out the rectified option does offer more range so that's what I settled on:



The 4.5V bias becomes ~3V, the exact value being calibrated at first run. Subtracting the default analogRead() from this value gives plenty to work with:



Now here's the next dilemma: should R5 go to the analog or digital side of ground? I need to watch that course on mixed signal layouts again.
Quote from: R.G. on July 31, 2018, 10:34:30 PMDoes the circuit sound better when oriented to magnetic north under a pyramid?

anotherjim

R5 to whatever ground the ADC uses I suspect. It will be reading the voltage across R5 after all which will transfer across the internal sampling cap.
You might want to rescale R2/R5 down as a high source impedance (>10k) can cause the sampling cap to low pass the input (and add a time delay). 4k7/10k will be solid.