# Cheap square root via "op-amp"

Discussion created by KJBob on Feb 20, 2013
Latest reply on Jun 4, 2013 by KJBob

SigmaStudio's square root block provides an immediate sample-to-sample output.  Because it must come up with a result for every sample, however, it uses many instructions.  If your application needs a square-root function in a slower path -- perhaps in the envelope processing of a compressor's side chain, or for a measurement -- you can implement a slow, but accurate, square root with far fewer instructions using the "op-amp" inversion technique shown here.  This may have other uses, too.

Analog designers are used to building functions by placing the "opposite" function in the feedback loop of an op-amp.  For example, using a diode with a more-or-less exponential V-I characteristic in that feedback loop produces a logarithmic function.  You can do the same in SigmaDSP, with its op-amp block.  Oops, you have to make the op-amp block yourself, but it's easy.  It's shown inside the blue box in the schematic below.

As shown this "op-amp" has differential inputs, zero offset, and infinite DC gain -- not bad at all.  Its gain ramps down at a steady 6dB / octave to a respectable 10,000 at 0.1 Hz.  It is unity-gain stable.  In short, easy to use except for one condition:  You must give it a proper feedback loop at all times.  Hardware op-amps usually just "hit the rail" without consequences when driven that way, but this one overflows the DSP in that case.  Thus you must watch your input level, and make your feedback loop work without polarity reversal at all times.

The above circuit uses a multiplier to square its input signal.  Placing that inside the "op-amp" feedback loop yields a square root.  The logic around the multiplier makes it work the same way for positive and negative inputs, to keep everything sane as described above.  The thing looks complicated but uses 65 instructions, of which 38 are for the test signal source and real-time display.  The square-rooter itself uses only 27 instructions.  The down side is that it takes many samples to respond to a changed input, but that may be quite acceptable in some applications.  The response below appears clean -- no overshoot.

This idea may work to produce other "expensive" functions by inverting the corresponding "cheap" one, with the limitations discussed above.