Showing posts with label Hard clip. Show all posts
Showing posts with label Hard clip. Show all posts

Saturday, May 11, 2024

Clip detection

I want to detect when the signal on the mix bus is about to clip, to be able to indicate it in the UI.

I initially intended to use the Elliot Sound Products clip detection circuit to do bipolar detection:

There are a few issues with this though. First of all, the output after the diodes is 0-10V, which is too high to be used directly with the mcu inputs.

 Second, as I will not interface this directly with the mcu, but rather with the PCA9539 port expander, it won't trigger an interrupt. Instead, I have to poll the signal - so I need to latch the state whenever a clipping pulse is received, and reset state after I've read it.

It is possible to make an SR-latch using an op amp, as described here https://www.radiolocman.com/shem/schematics.html?di=463517 and here https://www.edn.com/use-an-op-amp-as-a-set-reset-flip-flop/


 After a bit of fiddling with my simulation, I was able to come up with this circuit:

The output of the comparator op amps is +/- 10.4 approx. This is dropped to around 0-9.8 through the diodes. R3 and R4 divides the signal down to around 3.3V. It is then buffered, and inserted into the latch through the diode (dropping an additional 0.6V). R15 and R16 sets the value seen at the negative pole of the last op amp when in2 (reset signal) is turned off. R14 adjusts the reset voltage so we're able to reset the latch, and D3, R17 and R18 reduces the output from +/-10.4V to 0-3v3, ready for the MCU.

After even more fiddling, trying to reduce the number of components, I ended up with this:


It works just as well as the previous one.

Green: input at positive terminal of the right op amp. Blue: Reset signal. Red: latch output

 

Now, I did a lot of testing with this circuit and it has one big issue. At frequencies of around 5kHz (with a triangle input) the comparators are too slow, so the peaks are not high or long enough to properly set the latch. As we get closer to 20kHz it simply stops working. If I lower the detection voltage (replace the 1k resistors with 2k2 for example), it works, but the clipping voltage level changes with frequency. That's not very good - though it may be GOOD ENOUGH.


Enter V2.

Let's do this the proper way.

By replacing the op amps with LM311 comparators, and the op amp-based latch with a real latch, CD4043, things work fine all the way up to 20kHz+:

The 10pF caps are not necessary, and the 10k/100k/10k voltage divider at the input can be replaced with 1k/10k/1k.

The CD4043 is driven from a 3v3/GND PSU, so level shifting is not necessary.

 

An imporant thing to note here is that the top comparator has the collector (pin 7) connected to 3v3 and the emitter (pin 1) connected to GND through a 10k resistor. In this configuration, the output (connected to pin 1) will be high whenever the voltage at the negative input (3) - our signal - is higher than the voltage we compare against (2, 10V), and the voltage range is 0 to 2.7 approx. CMOS inputs are considered ON when the voltage is > 2V (for 3v3 supplies) so this is fine.

For the bottom comparator, the collector is connected to 3v3 through a 10k resistor and the emitter is grounded. The output (connected to pin 7 this time) is positive whenever the voltage at the negative input (3) - our signal - is lower than the voltage we compare against (2, -10V).

As the signals are fed into different latches (there are four available in one CD4043), we actually have separate clipping indicators that we could read if we wanted to. This is not necessary for my use, so I simply combine them using two diodes (again, dropping the voltage, but it still works with the PCA9539).

Positive clipping only

Negative clipping only

 

 

 

Some action shots

White = reset signal
Brown = clipping state, as seen by microcontroller
Orange = output from CD4043 comparator (v2)
Red = output from LM311 comparator for high clipping
Blue = output from op amp latch (v1)
Yellow = input signal from function generator

4.8Hz input, says 19.5Vpp on function generator (fg) but is about 20Vpp measured. Both versions trigger

48Hz, 19.4Vpp on fg. Both versions trigger

517Hz, 19.4Vpp on fg. Only v2 triggers.

517Hz, 19.5Vpp on fg. Both versions trigger again.

5kHz, 19.4Vpp on fg. Vmax is > 10V so both should trigger. Only v2 triggers

5kHz. We have to go all the way to 20.1Vpp on the fg to get v1 to trigger


10kHz, have to increase to 20.5Vpp on fg to get v1 to trigger

20kHz, v1 doesn't trigger even at 20.9Vpp on fg


Tuesday, March 31, 2020

Distortion (continued)

After figuring out what was going on in the distortion circuit, I set out to make a nice switchable distortion for the XM8.

Matching hard and soft

Through some experimenting I came up with the following circuit. It has the same amplitude for the output of both soft and hard clipping. Distortion starts at around 10mV input and output is at 3-400mV. Combining these two involves having an additional cap and resistor in the feedback circuit, and the hard clipping circuit has to be attenuated, meaning we should either switch output attenuation or do this in a VCA later.



Matching hard and soft clipping at 20mV input



Various diodes

I tried simulating various diodes. It seems that all silicon diodes (wow, I just realised I've spelt it silicone elsewhere! Blush...) such as 1N4148, 1N914 and 1N4001 give about the same result in the simulation. An 1N34 germanium diode on the other hand clips MUCH softer:


Soft clipping with 1N34 germanium at 10mV

Soft clipping with 1N34 germanium at 50mV


Soft clipping with 1N4001 at 5mV

Soft clipping with 1N4001 at 10mV

Soft clipping with 1N4001 at 25mV

Soft clipping with 1N4001 at 50mV




Matching hard and soft (silicone) without varying amplification or attenuation

The following circuit gives similar amplitude on the output of both hard and soft clipping, without any changes in amplification or attenuation when switching between hard and soft.

I have simulated this with a 31k resistor but that may be changed to 33k without any issue. With a 33k resistor, low pass cutoff is at 26.7kHz with a 180pF feedback capacitor, 32kHz with 150pF.

The input should be at around 15mV when signal is 5V, which will not clip. Soft clipping starts at around 20mV and hard clipping slightly later. Output is around 7-800mV





I've simulated the circuit with two separate opamps but this may be build using only one and with a single SPDT-switch like the DG419:



PS: Connecting the point between each of the two diodes in the feedback circuit changes nothing. That means that we can actually get away with two diodes less, putting the switch at the bottom of the hard sync instead of top.

Here's a closer look at the effects of clipping at various inputs.

Matching clipping at 15mV input

Matching clipping at 30mV input

Matching clipping at 55mV input

These show the effect on only one type of clipping in 10mV steps from 15mV to 55mV: 

Input, 15mV to 55mV in 10mV steps

Hard clip, 15mV to 55mV in 10mV steps

Soft clip, 15mV to 55mV in 10mV steps




From the plots above it becomes quite clear that distortion changes very quickly in the beginning and slower later. It may be an idea to use an exponential VCA on the input to get a more even rate of change. This, the actual sound and whether or not the audible volume is similar between hard and soft clipping has to be tested in real life.

Filtering (again)


As explained in the previous post, there is more to the tube screamer distortion (of which this is a variation) than just clipping. It also does selective filtering/gain of the input through the use of two shelving filters, and the low pass filter even changes as gain/distortion is increased, rounding off the curve more at increased soft clipping distortion, see https://www.electrosmash.com/tube-screamer-analysis for more about this.

This change may bring more "life" to the distortion circuit as clipping does not stay static when increasing gain. In the circuit above, gain is done pre-clipping, so clipping just becomes "wider". It may be possible to put an OTA in the feedback circuit instead of the tube screamer's potentiometer and thus keep functionality similar to the TS. Again, this has to be tested in practice to see what sounds good.

And in any case, selecting a cap for LP filtering at a lower frequency than that in the TS should be tested.

Voltage controlled distortion using OTA in feedback


Now, this is pretty cool. Last night I thought about how to replace the variable resistor in the feedback circuit of the TS with an OTA. Today I experimented a little and came up with something that seems to work fairly well.

By controlling the OTA with a 0-2.5V CV, we decide how much current should come out of it, which is exactly what changing the resistance does. I was able to find component values that gives fairly good controllability - at 0V CV no current flows through the OTA, and all current in the feedback loop flows through the diodes, giving maximum distortion. At 2.5V CV enough flows through the OTA to turn off the diodes (at least it looks like it).

I am not sure if this is the exact same result as with a potentiometer. Nor am I sure if the filter calculations hold true and if low pass cutoff changes like it does with changing potentiometer resistance. But it is definitely something worth trying.

Changing the CV input resistor to 20k lets us use 0-5V CV btw.

Hard clipping works well too btw, but the OTA cannot be fully off as then no current will flow.

20m input, slight skewing due to 1nF cap in feedback
15mV input, no distortion when OTA CV is 2.5V

Hard clipping, 15mV input



Hard clipping circuit

Voltage controlled input attenuation

One can also control the input, and thus the distortion, using an OTA circuit. The following circuit will let the clipping circuit see an input of around 15mV when CV = 0 and 130mV when CV = 5. CV is offset by R2 to slightly turn on the OTA even in the absence of CV.

At 5V the output is 1.1V which is rather high, but I've chosen to have this as the max and intead limit max distortion in software.

OTA controls input amplitude
At 0V CV output from the OTA buffer is around 20mV and signal distorts slightly. Replace R6 with 3.9k resistor for less distortion if wanted.
At 5V CV output is 130mV and the circuit clips heavily. Output after clipping is around 1.1V




Further work:

Output VCA - output should be around 5V when input to circuit is 20mV (e.g. input is 5V), and it should be possible to amplify output to 150-200% to do max volume in software and allow higher max volume than 5V (inputs may be above 5V when mixing multiple waveforms and  oscillators).


For the XM8 I may also want to look into mixing of dry/wet signal, and inclusion of a bit crusher. (https://en.wikipedia.org/wiki/Bitcrusherhttps://www.reddit.com/r/diypedals/comments/1zck7p/bitcrusher_schematics/) Looking into how the PT2399 works would also be of interest for the global FX. https://www.electrosmash.com/pt2399-analysis

Distortion (clipping)

Seems I can't stop working on my synth these days. There are two major parts missing from my synth voice cards: Digital playback and pre-filter distortion. I am not sure about the need for the last one, so I decided to build that first.

Googling distortion circuits I came across a resource that I have read long ago and forgotten about: Design your own distortion.

This page gives a great step by step introduction to two types of distortion: Hard and soft clipping. It also has some on filtering and tone control but it contains some errors (50nF cap in distortion does not give a lower frequency of 31Hz but 3.1kHz, the shown circuit will not give any distortion and indeed very little gain for frequences lower than 3.1kHz).

I decided to try simulating the circuit in LTSpice (with +/-15v supply lines) before breadboarding it, to get a feel of what it is doing.

Hard clipping

The hard clipping part of the circuit is easy. Two parallel diodes in reverse order from signal to ground after a buffer or similar that disconnects it from the input.

Hard clipping at around 750mV

What is going on? When the input signal, and thus the voltage across the diodes reaches the forward voltage drop of one of the diodes, it switches on, sinking the current to ground. This effectively cuts off the peaks of every wave cleanly. A fairly normal diode voltage drop is around 0.75V, meaning the signal is clipped at 0.75V. The amount of clipping is adjusted by attenuating the input (assuming a +/-5v input) to where you want the clipping to occur.

If one wants a higher clipping point it is also possible to connect diodes in series, going from 0.75V to 1.5V to 2.25V etc.

Hard clipping with series diodes, clips at around 1.5V


Soft clipping

This is slightly more involved. Soft clipping means that instead of brutally chopping off the wave tops, they are rounded off. Soft clipping can be achieved by placing two diodes in parallel / reverse order in the negative feedback loop of an op amp, parallel to the feedback resistor.

I first simulated inverting amplification instead of the non inverting "tube screamer" version on the web page because I could not get that one working due to the HP cutoff error in the article.



Here is what I think is going on, and why this is different to hard clipping:

When the voltage across the feedback resistor Rf is low, the diode will be turned off. Current only flows through Rf. Increasing the voltage (and thus current) gradually switches on the diode. Some of the current starts flowing through the diode while some still flows through the resistor.

Soft clipping currents - the original input current (red) is equal to the sum of the current through the resistor (blue) and diode (green). The diode only starts conducting after a while, but when it does almost all current goes through it.

Abiding to Ohms law, the opamp output voltage is still the current flowing through the resistor times the resistance, but as some of the current required to keep the two opamp inputs equal now flows through the diode, the output voltage is less than the input voltage (given a unity gain configuration where Rin = Rf).

As the input current increases, more and more current flows through the diode, and the amount of current flowing through the resistor flats out, cutting off the top of the input waves.



Another interesting thing to note is at what voltage the cut happens at is affected by the resistance of Rf. With a larger resistance, current starts flowing through the diode earlier (as its "resistance" is lower). Thus, the voltage across the resistor when it starts clipping is also lower, meaning the output is clipped more than with a lower value resistor.

Example values for a +/-1v input are clipped peaks at:

550mV for 1k,
450mV for 10k and
355mV for 100k.


A +/-5v input has peaks at 650mV and +/-15v peaks at 712mV (at 100k?)

Soft clipping with 1k resistor in feedback loop

Soft clipping with 10k resistor in feedback loop

Soft clipping with 100k resistor in feedback loop


Currents with 1k resistor, total current is 1mA

Currents with 10k resistor, total current is 100uA. A proportionally larger part of the input current goes through the diode (the diode "resistance" is still the same so it's easier for the current to go this way) and the output current is clipped earlier.

Currents with 100k resistor, total current is 10uA, an even larger part of the current goes through the diode


The non-inverting version probably works in the same way.

I first had issues getting the non-inverting circuit from Design your own distortion to work because of the HP cutoff error, but finally I found and was able to simulate a similar non-inverting circuit, this one:

https://electronics.stackexchange.com/questions/473989/need-help-designing-and-implementing-an-op-amp-based-distortion-circuit

Non inverting circuit, increased value of filter cap, meaning a lower high pass filter point distorts the wave.
The same circuit as above but without filter cap. The clipping part is more visible here.


The slight skewing is because of the cap in the feedback loop. Gain is (10k + 220) / 220 = 46

It also explains a very important thing: The guitar signal amplitude is 40mV, nothing near the 1-5V I've been experimenting with so far. Taking this into account I was able to simulate a +/-15v version of the original circuit, but WITHOUT the 50nF capacitor in series with the 1k input resistor on the positive terminal of the op amp, an the resistor connected to GND instead of the negative supply (in the original circuit it is connected to ground, but ground is the negative supply as this is a 9V circuit). See the filter section below for an explanation of why removing the 50nF cap was necessary (the cap value is simply wrong due to a calculation error in the article).

Further more, Rf must be much larger than 1k to get distortion, without this all the current flows through Rf and none through the diodes so no clipping happens. I've been working with 30k.

Working soft and hard clipping. The input (green) is multiplied by 31 to see how it matches (the non inverting opamp configuration here amplifies the input 31 times).

I have also simulated various types of diodes for the soft clipping circuit, more about this in a separate post.

Filtering

The webpage on top also talks a lot about filtering. The original article is about distortion for guitars and says that one should aim for keeping frequencies from 40Hz to 30kHz (NB: There is a serious error in the HP filter calculations in the article, see the end of this section). It may be different for a synth but that's a good starting point.

The article discusses high pass and low pass filtering in a non-inverting amplifier. The chosen circuit is often referred to as shelving filters elsewhere, it is not the most common circuit in examples on the web.

Shelving filters actually have two parts, the "normal" high or lowpass filter, and then a second part where it flattens out again at a lower or higher frequency with lower gain, but where the gain stays at this level instead of continuing to drop off, letting all frequencies below (for HP) or above (for LP) pass. - see http://www.linkwitzlab.com/filters.htm#5.

Here are the sub-circuits and formulas needed for calculating the 3dB points:

The full circuit (NB: Other component names than the original):


The second parts of the shelving filter: Both these have gain = 1. To find the frequencies, replace R1 in the HP formula with (R1 + R2), and R2 in the LP formula with (R1*R2) / (R1 + R2).

For example:

Given the values
R1 = 1kOhm
R2 = 10kOhm
C1 = 50nF
C2 = 470pF

For the HP filter:
HP filter frequency: 3.1kHz (Gain = 11)
Lower frequency: 290Hz (Gain = 1 for all freqs below this)

For the LP filter:
LP filter frequency: 33.9kHz  (Gain = 11)
higher frequency: 373kHz (Gain = 1 for all freqs above this)

PS: In the "Design your own distortion" article HP frequency capacitor selection calculation is wrong. 1 / (2 * PI * 1000 * 40) is not 0.039uF, it is 3.98uF. This explains my inability to get the distortion working without removing the cap during my simulation - I simulated using a 400Hz sine wave, this is well below the 3.1kHz HP cutoff, meaning it has unity gain and is not at all distorted.

In general, there is a lot of info about filtering missing from the "Design your own distortion" article. The Tube Screamer analysis at https://www.electrosmash.com/tube-screamer-analysis has a much better description, talking about how the filter frequencies are affected by changes in the gain of the distortion and how bass frequencies are passed undistorted (but also un-amplified).


When deciding on component values, one first chooses the gain, then calculate the necessary capacitors.

See also


https://www.premierguitar.com/articles/diode-stew-1
https://sound-au.com/articles/soft-clip.htm
https://anasounds.com/od-disto-fuzz-differences/
https://www.electrosmash.com/tube-screamer-analysis