# [sdiy] Simple circuit challenge: |a-b|

Ashlyn Black ashlyn at ashlynblack.com
Sat Mar 23 08:46:29 CET 2024

```The transistor provides the rectification and the output is taken from
its collector.

For an input range of ±5V and output range of +5V, it works (more or
less.) Yes it's _far_ from perfect and there are some caveats however
the design challenge was for simplicity, not fidelity. :P

Ramshackle fix (+10V output): https://tinyurl.com/26dfwymf

Pursuant to your circuit description: https://tinyurl.com/2xweb2sh

- Ashlyn :3

On 23/3/24 07:39, brianw wrote:
> I might be missing something, but it seems like you'd need two op-amps in parallel, with diodes to combine them.
>
> |a-b| is either (a-b) or (b-a) depending upon which input is greater.
>
> I assume this means you'll need two op-amps. One op-amp has a into the + input and b into the - input, while the other op-amp has a into the - input and b into the + input. These two op-amp outputs will represent (a-b) and (b-a), respectively. From those, a couple of diodes can combine them, and the larger will drive the next stage. You'll probably want a third op-amp to buffer after the diodes.
>
> i.e. When a is greater than b, a-b will be positive, and it will pass through a diode to the final stage where it represents |a-b| correctly. However, when a is less than b, b-a will be positive, and the other op-amp will pass through the other diode to the final stage, where b-a is the same as |a-b|
>
> Full wave rectifiers work on center-tapped transformer outputs, because you start with two waves, both the normal and inverted polarity, feeding the rectifier bridge such that there is always a positive cycle. Without the center tap, you end up with only the positive half cycle of the sine wave, and near-zero volts for the other half of the cycle. A bridge rectifier does not generate the opposite polarity via diodes alone - you need a transformer that feeds both polarities to the two bridge inputs.
>
> By the way, what does the 2N3904 do in the suggested circuit provided? Is it merely an emitter-follower buffer?
>
> Running the simulation, I see that with A=5V and B=-5V, the output is the full +10V (correct absolute value of the difference). However, when A=-5V and B=5V, the output is capped at +5V (incorrect, because it's only half the absolute difference).
>
> After the simulation, I realized that I assumed only positive inputs before the difference, and only the difference would be positive or negative. My suggested circuit will still work for bipolar inputs, with the caveat that the output can be twice as large due to the difference.
>
> Brian
>
>
> On 22/3/24 05:50, Rutger Vlek via Synth-diy wrote:
>> Dear list,
>>
>> while hobbying on an experimental design, I'm looking for a simple analog circuit to give me the absolute value of the difference between two signals (mathematically written as |a-b|).
>>
>> I already have a working circuit, but it's rather big for my needs. It uses an inverting opamp to invert signal A, followed by a bog-standard full wave precision rectifier circuit where -A and B are summed together and being rectified. The total is 3 opamps, 2 diodes and 9 resistors. As I need to repeat the circuit many times in the final application, I'm wondering: is there anything simpler that can do this?
>>
>> Requirements:
>> - working across full audio range
>> - distortion as consequence of diode drops is no problem (might even be beneficial!)
>>
>> Many thanks for any suggestions!
>>
>> Regards,
>>
>> Rutger
>
> ________________________________________________________
> This is the Synth-diy mailing list
> Submit email to:Synth-diy at synth-diy.org
> View archive at:https://synth-diy.org/pipermail/synth-diy/
> Selling or trading? Usemarketplace at synth-diy.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://synth-diy.org/pipermail/synth-diy/attachments/20240323/0cbb1397/attachment.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: uR0rpdtqaA00ueA6.jpeg
Type: image/jpeg
Size: 26019 bytes
Desc: not available
URL: <http://synth-diy.org/pipermail/synth-diy/attachments/20240323/0cbb1397/attachment.jpeg>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: gSKG20zcbnZmeUHO.jpeg
Type: image/jpeg
Size: 48628 bytes
Desc: not available
URL: <http://synth-diy.org/pipermail/synth-diy/attachments/20240323/0cbb1397/attachment-0001.jpeg>
```