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

Sunday, February 5, 2023

Distortion again

After discovering that my hard clipping circuit introduced a lot of noise on the gnd line, I decided to put a 1k resistor before the clipping diodes. When doing soft clipping, this put the 1k inside the feedback loop, but this didn't seem to matter too much.

However, now the soft and hard clipping modes are very similar, to the point that I wonder if it is really necessary to have a hard clipping mode at all.

Soft clipping, four diodes and 1k in feedback


Hard clipping, two diodes


Hard clipping, four diodes

Now, it's very hard to tell the difference with a sine input, so perhaps I should redo this with a more discrete function to see how it actually performs. But more importantly, I should try this out in practice.

Soft saw(ish)



Hard saw(ish), two diodes

Soft triangle


Hard triangle, two diodes

Soft triangle, two diodes

If the conclusion is that hard and soft clipping is too similar, I may also look at ways of improving the soft clipping to keep amplitudes more the same without having to compensate in the output VCA.

Update:
TWO diodes soft clipping is much less similar, perhaps that's the reasonable option - only two diodes for clipping

Here is a +/-5v with matching soft and hard clipping - without any amplitude compensation:

The output ends up at around 7v in both cases, which is pretty cool. We COULD trim it down to 5v again but we could also just leave it.

Now, there is ONE problem with all of this - the input will only distort if less than +/-5v. Once we start summing oscillators stuff we'll get distortion from the get go. Not sure exactly how to handle this as we want to be able to get distortion from a single osc too, so perhaps this is better solved in dry/wet mixing? If not, we need to attenuate the input further or use an input gain vca which i REALLY don't want to do, especially if we have an output VCA as well (we do already have two output VCAs so it may be solved in software though).

Update 2:
I did a change of the circuit to use a 68k/1k resistor divider instead of a 33k/1k, and to compensate for this I put a 240k feedback resistor in place of the 120k at the output gain stage to keep unity gain.

Now 10V is allowed through the circuit undistorted. When I send +/-5v through, distortion starts at around 50% turn of the CV pot. For +/-10V it starts at around 25%. 

With the output gain at max distortion set to get a +/-10V distorted output for a 10V input, I get a +/-8V output for a soft clipped 5V, so a significant gain. For hard clipping it is even more, the clipping level is around +/-9v for 5v input (though the slope is likely not as steep.

Again, I need to try this in practice to see what feels best.

+/-5V input with max soft clipping

+/-10V input with max soft clipping

+/-5V input with max hard clipping

+/-10V input with max hard clipping

In practice the big difference is that with the 10v pass option we may actually end up clipping against the +/-112v rails if we don't carefully adjust the gain through the cycle.

With the 5v pass option, we clip much sooner, and the max output is much lower. It can never reach 10V even if the input is 10V.

All in all it DOES look like the 5v version performs fairly well. Without any gain correction - and the gain CV set to about 3.3V, we do get a bit of gain for the 5V input as distortion increases. For 10v there is a bit of distortion from the start, more for hard than soft clipping.

Soft clipping, 5v in, no distortion

Soft clipping, 5v in, max distortion

Soft clipping, 10v in, no distortion set gives a bit of distortion on output

Soft clipping, 10v in, max distortion gives a higher output than for 5v max distortion.



Hard clipping, 5v max distortion

Hard clipping, 10v max distortion, not much higher than for 5v

Hard clipping, 10v no distortion. Distorts from the start, and more than for soft clipping


Modified circuit with unity gain at max vca cv (or without cvm just a buffer).


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