Electronics

# Performing Mathematical Operations With Op-Amps In LTspice  Written by

Skill Level: Intermediate

## Introduction

This tutorial will demonstrate how you can use operational amplifiers to perform various mathematical operations.

I will be using LTspice to create and simulate the circuits shown in this tutorial. It is expected that you have a basic understanding of the SPICE simulator in general and the LTspice application in particular. If you are new to LTspice, or would just like to refresh your knowledge, please see our Getting Started With LTspice For Mac tutorial (if you are using macOS) or the learning resources section at the bottom of the LTspice home page (if you are using Windows) before proceeding with this one.

The resources created for this tutorial are available on GitHub for your reference.

## What Is Needed

• macOS Or Windows Based Computer
• LTspice

## Background Information

Operational amplifiers (op-amps) are often used for buffering, amplifying, and filtering analog signals. But did you know that you can also use them for performing mathematical operations? As the “operational” part of their name implies, op-amps were originally designed to perform mathematical operations in early analog computers. In this tutorial, we will create and simulate a few examples demonstrating scalar multiplication and division, negation, addition, subtraction, integration, and differentiation using the LTspice circuit simulator.

I chose to use the OP07 op-amp for these simulations because it is a good general purpose precision operational amplifier already included in LTspice by default, so no extra model library is required.

To keep things simple, I won’t be going into the circuit analysis for how each of the op-amp circuits works, but I will provide the most pertinent parameters, equations, and example component values for stable operations. My intent is to provide you with a toolbox of example op-amp based circuits that you can use for performing mathematical operations in your own projects.

## Scalar Multiplication With The Non-Inverting Amplifier

The non-inverting amplifier is a commonly used op-amp configuration for boosting small signals from sensors and other low voltage devices to larger signals that can more easily be utilized by higher voltage circuits.

The relationship between the output voltage (Vout) and the input voltage (Vin) is governed by the following equation

$$\large V_{out} = (1 + \frac{R_f}{R_g}) V_{in}$$

where Rf represents the feedback resistor and Rg represents the grounded resistor.

As you can see from the above equation, this circuit amplifies the input signal by a scalar multiplier that is always greater than one. The larger the value of Rf is compared to Rg, the larger the amplification, or gain, of the circuit. For instance, if Rf is twice as large as Rg, then the circuit’s gain would be 3. If Rf is nine times larger than Rg, then the gain would be 10.

For the component values specified in the above schematic, we should see a voltage relationship of

$$\large V_{out} = (1 + \frac{R_f}{R_g}) V_{in} = (1 + \frac{500K}{100K}) V_{in} = 6 V_{in} \\ \large = (6) (1m) = 6 \; mV$$

Hence, a 6 mV amplitude signal should be seen at the output for a 1 mV signal at the input.

Let’s simulate the circuit and see how close we get. Create a schematic, named op-amp_non-inverting_amplifier.asc, in LTspice like the one shown above.

Vcc and Vee are the +/-5 VDC power sources that supply the OP07 op-amp. Vin is defined as a sinusoidal voltage source with a DC offset of 0, an amplitude of 1 mV, and a frequency of 1 KHz.

The .tran 10u 5m SPICE directive will perform a transient analysis with a step time of 10 µs and a stop time of 5 ms.

Run the circuit simulation and view the Vin and Vout waveforms. Indeed, we can see the output has a peak amplitude of just about 6 mV.

Instead of viewing the waveforms to verify your simulation results, you could let SPICE itself do it for you.

* RMS calculations
.meas tran vin_rms rms V(Vin) from=3m to=4m
.meas tran vout_rms rms V(Vout) from=3m to=4m
.meas tran gain_rms param vout_rms/vin_rms
* AVG calculations
.meas tran vin_avg avg V(Vin) from=3m to=4m
.meas tran vout_avg avg V(Vout) from=3m to=4m
.meas tran gain_avg param vout_avg/vin_avg

These directives tell SPICE to measure the values of Vin and Vout over a full periodic cycle, 3 ms to 4 ms in this case. Then calculate the gain, Vout/Vin, for the circuit based on those values. The root-mean-square (RMS) based statements work well when the input and output are both centered about the x-axis. The average statements work better when the values include DC offsets. An example of the latter would be to add a DC offset of 10 mV to the sinusoidal Vin input; Vout would then be centered about 60 mV.

Enter the above SPICE directives somewhere in the schematic and simulate the circuit again. The results of the measure directives will be printed in the SPICE Error Log. I got a gain_rms value of 5.9985. This is almost exactly our expected value of 6. Great!

Try using some different resistor and input voltage values and see how the simulations compare to your expectations. Keep in mind, that the Vcc and Vee power supplies are +5 V and -5 V respectively, so your Vout output voltage can not go beyond a little less than those values, otherwise, clipping of the output will occur.

This section demonstrated how the non-inverting amplifier can be used to perform scalar multiplication operations.

## Scalar Multiplication/Division And Negation With The Inverting Amplifier

The inverting amplifier is also used for many of the same purposes as the non-inverting amplifier. While the output is inverted, it does, however, have a simpler input to output voltage relationship than the non-inverting amplifier.

The relationship between the output voltage (Vout) and the input voltage (Vin) is given by

$$\large V_{out} = -\frac{R_f}{R_i} V_{in}$$

where Rf represents the feedback resistor and Ri represents the input resistor.

This relationship is a simple ratio of the resistor values, along with the negative sign. This means that this circuit can perform both scalar multiplication (Rf is larger than Ri) and division (Rf is smaller than Ri) of the input signal, thereby either amplifying or reducing the signal respectively. If the resistors are equal, unity gain negation is achieved. This last feature can be handy to post process a circuit that produces an inverted signal than what we want.

For the component values specified in the schematic, we should see a voltage relationship of

$$\large V_{out} = -\frac{R_f}{R_i} V_{in} = -\frac{500K}{100K} V_{in} = -5 V_{in} \\ \large = -(5) (1m) = -5 \; mV$$

Therefore, a -5 mV amplitude signal should be seen at the output for a 1 mV signal at the input. Since the input is a simple sinusoidal signal centered about the x-axis, another way of looking at the waveforms is that the output signal is out of phase (shifted) with respect to the input signal by 180 degrees. If the input signal (Vin) contained a DC offset, the output would be inverted and scaled as a result.

Again, create and simulate the above circuit, named op-amp_inverting_amplifier.asc, and see how close we get.

Viewing the Vin and Vout waveforms, you should see an inverted (shifted) 5 mV amplitude signal at the output this time for the same 1 mV input signal.

The SPICE directives to measure the gain for this circuit are the same as those used for the previous one.

* RMS calculations
.meas tran vin_rms rms V(Vin) from=3m to=4m
.meas tran vout_rms rms V(Vout) from=3m to=4m
.meas tran gain_rms param vout_rms/vin_rms
* AVG calculations
.meas tran vin_avg avg V(Vin) from=3m to=4m
.meas tran vout_avg avg V(Vout) from=3m to=4m
.meas tran gain_avg param vout_avg/vin_avg

I got a gain_rms value of 4.99872, once again, extremely close to our expected value of -5, but without the minus sign due to the nature of how RMS values are calculated. The gain_avg value of -4.9878 does, however, reflect the minus sign. Honestly, I would have expected the gain_avg value to be indeterminate since both the Vin and Vout signals should have average values of 0 over a full cycle. But, I guess that will be research for another day.

Again, play around with some different component values and compare the simulations with your expectations.

This section described how the inverting amplifier can be used to perform scalar multiplication and division along with negation operations.

## Addition And Weighted Averaging With The Inverting Summing Amplifier

The inverting summing amplifier is often used for summing, averaging, weighted inputs, digital-to-analog converters (DAC), audio mixers, etc.

It is almost identical to the inverting amplifier from the previous section, but ties in additional inputs at the inverting terminal of the op-amp.

The input to output voltage relationship of this amplifier is governed by the following equation.

$$\large V_{out} = -R_f (\frac{V_a}{R_a} + \frac{V_b}{R_b} + \ldots + \frac{V_n}{R_n})$$

where Rf represents the feedback resistor and Ra, Rb, …, and Rn represent the input resistors for the Va, Vb, …, and Vn inputs respectively.

As you can see from the above equation, the output of this circuit is the inverted sum of the input voltages scaled by their respective Rf/Rn ratios. This provides the ability to give different weights to each of the inputs by using differently sized input resistors. If all resistors are equal, a simple addition of the input signals occurs. If you do not want an inverted result, this would be a good example of when to use the simple negation feature of the inverting amplifier from the previous section attached to the output of this circuit.

For the configuration and component values shown in the schematic, three inputs with all input resistors being three times the value of the feedback resistor, the output will be an inverted simple average of the input signals.

$$\large V_{out} = -R_f (\frac{V_a}{R_a} + \frac{V_b}{R_b} + \frac{V_c}{R_c}) \\ \large = -100K (\frac{V_a}{300K} + \frac{V_b}{300K} + \frac{V_c}{300K}) \\ \large = -\frac{V_a + V_b + V_c}{3} = -\frac{1m + 2m +3m}{3} = -2 \; mV$$

Create and simulate the op-amp_inverting_summing_amplifier.asc circuit.

View the Va, Vb, Vc, and Vout waveforms. Va, Vb, and Vc have amplitudes of 1 mV, 2 mV, and 3 mV each respectively. Vout has an amplitude of 2 mV with the signal shifted by half a period (0.5 ms).

The SPICE measurement statements for this amplifier are as follows.

* RMS calculations
.meas tran va_rms rms V(Va) from=3m to=4m
.meas tran vb_rms rms V(Vb) from=3m to=4m
.meas tran vc_rms rms V(Vc) from=3m to=4m
.meas tran vout_rms rms V(Vout) from=3m to=4m
.meas tran average_rms param (va_rms+vb_rms+vc_rms)/3
* AVG calculations
.meas tran va_avg avg V(Va) from=3m to=4m
.meas tran vb_avg avg V(Vb) from=3m to=4m
.meas tran vc_avg avg V(Vc) from=3m to=4m
.meas tran vout_avg avg V(Vout) from=3m to=4m
.meas tran average_avg param (va_avg+vb_avg+vc_avg)/3

Along with the addition of the extra inputs, the gain_ parameters were replaced with average_ parameters. When simulated, the actual output values (vout_) can be compared to the expected values (average_). I got a vout_rms value of 0.0014138 and an average_rms value of 0.00141413. Again, almost identical.

This section described how the inverting summing amplifier can be used to perform summing, weighted inputs, and averaging operations.

## Subtraction With The Differential Amplifier

The differential amplifier provides the means to subtract one signal from another.

The relationship between the output voltage (Vout) and the input voltages (Vp and Vn) is

$$\large V_{out} = (1 + \frac{R_f}{R_n}) (\frac{R_g}{R_p + R_g}) V_p \; – \; \frac{R_f}{R_n} V_n$$

where Rf represents the feedback resistor, Rg represents the grounded resistor, and Rp and Rn represent the input resistors for the Vp and Vn inputs respectively.

This equation may look a bit complex, but we can reduce it to a simpler set of equations when certain resistor relationships are maintained.

When Rp = Rn and Rf = Rg,

$$\large V_{out} = \frac{R_f}{R_p} (V_p \; – \; V_n)$$

When all resistors are equal,

$$\large V_{out} = V_p \; – \; V_n$$

For the component values shown in the schematic, the output will be a simple subtraction of the input signals since all resistors are equal.

$$\large V_{out} = (1 + \frac{R_f}{R_n}) (\frac{R_g}{R_p + R_g}) V_p \; – \; \frac{R_f}{R_n} V_n \\ \large = (1 + \frac{100K}{100K}) (\frac{100K}{100K + 100K}) V_p \; – \; \frac{100K}{100K} V_n \\ \large = V_p \; – \; V_n = 3m \; – 1m = 2 \; mV$$

Create and simulate the op-amp_differential_amplifier.asc circuit.

View the Vp, Vn, and Vout waveforms. Vp and Vn have amplitudes of 3 mV and 1 mV each respectively. Vout has an amplitude of 2 mV.

The SPICE measurement directives for this amplifier are as follows.

* RMS calculations
.meas tran vp_rms rms V(Vp) from=3m to=4m
.meas tran vn_rms rms V(Vn) from=3m to=4m
.meas tran vout_rms rms V(Vout) from=3m to=4m
.meas tran diff_rms param vp_rms-vn_rms
* AVG calculations
.meas tran vp_avg avg V(Vp) from=3m to=4m
.meas tran vn_avg avg V(Vn) from=3m to=4m
.meas tran vout_avg avg V(Vout) from=3m to=4m
.meas tran diff_avg param vp_avg-vn_avg

This time, we compare the actual vout_ values to the expected diff_ values. I got values of 0.0014138 for vout_rms and 0.00141413 for diff_rms. About the same.

This section described how the differential amplifier can be used to perform subtraction based operations.

## Integration With The Integrating Amplifier

The integrating amplifier, or integrator, is used for, you guessed it, integrating a signal. This can be handy, for instance, when you have a signal representing an object’s speed and you want to know its position at a given point in time.

With the addition of the feedback capacitor, determining the component values necessary for building an integrator requires a bit more effort than we have seen already with the other amplifiers. There are now five equations governing the basic operation of the integrating amplifier. In particular, the frequency range for stable operation (integration) is limited by the circuit’s resistor and capacitor values.

$$\large f_c = \frac{1}{2 \pi R_f C_f}$$

The cutoff frequency (fc) establishes the lower limit of the integrator’s operating frequency range. It is determined by the feedback resistor (Rf) and the feedback capacitor (Cf).

$$\large f_0 = \frac{1}{2 \pi R_i C_f}$$

The 0 dB frequency (f0) establishes the upper limit of the integrator’s operating frequency range. It is determined by the input resistor (Ri) and the feedback capacitor (Cf).

$$\large 10 f_c < f_s < f_0$$

The operating frequency range (fs) is the full range of frequencies where the integrator is able to perform the integration operation properly. It is specified as ten times the cutoff frequency (fc) for the lower bound and as the 0 dB frequency (f0) for the upper bound.

$$\large R_c = R_i || R_f$$

The compensation, or biasing, resistor (Rc) limits the DC offset errors in the integrator’s output over time. It is defined as the parallel resistance of the input resistor (Ri) and the feedback resistor (Rf).

$$\large V_{out} = -\frac{1}{R_i C_f} \int V_{in}(t) \, dt$$

And finally, the relationship between the integrator’s output voltage (Vout) and its input voltage (Vin). The result of the integration is scaled by the values of the input resistor (Ri) and the feedback capacitor (Cf). The integrator’s output is also inverted compared to its input.

If you integrate a sine wave, you get a cosine wave. If you integrate a square wave, you get a triangle wave. I thought the latter would make a better example, so I switched the SINE based input voltage source (Vin) we have been using for a PULSE based voltage source instead. The resulting square wave alternates between -1 and +1 V at a frequency of 1 KHz.

I also wanted an operating frequency range between 500 Hz and 2 KHz so that we could use the same 1 KHz input frequency that we have been using so far, plus a little extra wiggle room.

I selected the circuit’s component values to fit those criteria and ended up with the following results.

$$\large f_c = \frac{1}{2 \pi R_f C_f} = \frac{1}{2 \pi (3.3M) (1n)} = 48.2 \; Hz$$

$$\large f_0 = \frac{1}{2 \pi R_i C_f} = \frac{1}{2 \pi (75K) (1n)} = 2.12 \; KHz$$

$$\large 10 f_c < f_s < f_0 \\ \large => (10) (48.2) < f_s < 2.12K \\ \large => 482 \; Hz < f_s < 2.12 \; KHz$$

$$\large R_c = R_i || R_f = \frac{(75K)(3.3M)}{75K + 3.3M} = 73.3 \; KΩ$$

$$\large V_{out} = -\frac{1}{R_i C_f} \int V_{in}(t) \, dt \\ \large = -\frac{1}{(75K) (1n)} (1) (0.5m) = -6.67 \; V$$

This last one might need some explanation. The result of the integration, without the scaling factor (-1/RiCf), is shown as (1)(0.5m). Since another way of looking at integration is that it is the area of the region under a function’s curve, the region’s area for half the period of a square wave is rectangular. Therefore, the rectangle’s area is calculated as the input voltage’s amplitude (1 V) multiplied by the half period (0.5 ms) of the input voltage’s 1 KHz frequency signal. The final result, with the scaling factor included, is -6.67 V. This means that the resulting triangle wave will rise with a slope of 6.67 V over the half period when the input is -1 V and fall with a slope 6.67 V over the half period when the input is +1 V. Being that the slope covers the full voltage swing, the actual amplitude of the triangle wave will be half that, or 3.33 V.

Create the integrator circuit, named op-amp_integrating_amplifier.asc, with the specified components and values, run the simulation, and view the Vin and Vout waveforms. You may have noticed that the SPICE transient analysis directive changed a little. The integrator circuit needs a little time to settle down before the output properly reflects the integration of the input signal. The simulation time was changed to start at 20 ms and end at 25 ms.

You should see a square wave with an amplitude of 1 V and a frequency of 1 KHz for the input voltage (Vin) and a triangle wave with an amplitude of 3.33 V and a frequency of 1 KHz for the output voltage (Vout).

I don’t believe that the SPICE measurement directives make much sense for the integrator, so I omitted them.

This section described how the integrating amplifier can be used to perform integration operations.

## Differentiation With The Differentiating Amplifier

The differentiating amplifier, or differentiator, is used for differentiating a signal. For example, this can be used, in the opposite direction of the integrator, when you know an object’s position but want to determine its speed at a given point in time.

Here are the necessary equations for the operation of the differentiator. These equations are fairly similar to those used for the integrator, but we gain another cutoff frequency with the addition of the second capacitor.

$$\large f_0 = \frac{1}{2 \pi R_f C_i}$$

$$\large f_{ci} = \frac{1}{2 \pi R_i C_i}$$

$$\large f_{cf} = \frac{1}{2 \pi R_f C_f}$$

$$\large f_0 < f_s < \frac{min(f_{ci}, \, f_{cf})}{10}$$

$$\large R_c = R_i || R_f$$

$$\large V_{out} = -R_f C_i \frac{d}{dt} V_{in}(t)$$

The operating frequency range (fs) is a little different in that the 0 dB frequency (f0) is now the lower bound and the upper bound is determined by selecting the lower of the two cutoff frequencies (fci and fcf) divided by ten.

I selected the circuit’s component values to again have an operating frequency range between 500 Hz and 2 KHz and also changed the PULSE voltage source from a square wave to a triangle wave. This circuit will operate in reverse to the integrator, differentiating a triangle wave back into a square wave. The calculated values are as follows.

$$\large f_0 = \frac{1}{2 \pi R_f C_i} = \frac{1}{2 \pi (4.3M) (75p)} = 494 \; Hz$$

$$\large f_{ci} = \frac{1}{2 \pi R_i C_i} = \frac{1}{2 \pi (100K) (75p)} = 21.2 \; KHz$$

$$\large f_{cf} = \frac{1}{2 \pi R_f C_f} = \frac{1}{2 \pi (4.3M) (1.8p)} = 20.6 \; KHz$$

$$\large f_0 < f_s < \frac{min(f_{ci}, \, f_{cf})}{10} \\ \large => 494 < f_s < \frac{min(21.2K, \, 20.6K)}{10} \\ \large => 494 \; Hz < f_s < 2.06 \; KHz$$

$$\large R_c = R_i || R_f = \frac{(100K)(4.3M)}{100K + 4.3M} = 97.7 \; KΩ$$

$$\large V_{out} = -R_f C_i \frac{d}{dt} V_{in}(t) \\ \large = -(4.3M) (75p) \frac{2}{0.5m} = -1.29 \; V$$

The result of the differentiation without the scaling factor (-RfCi), is shown as 2/0.5m. Since differentiation is essentially the slope of a function at a given point in time, I calculated the slope as Δy/Δx with Δy being the change in voltage (2) and Δx being the change in time (0.5m) over a half period of the triangle wave input signal. The final result, with the scaling factor included, is -1.29 V. This means that the resulting inverted square wave will have an amplitude of 1.29 V.

Create the differentiator circuit, named op-amp_differentiating_amplifier.asc, with the specified components and values, run the simulation, and view the Vin and Vout waveforms.

You should see a triangle wave with an amplitude of 1 V and a frequency of 1 KHz for the input voltage (Vin) and a square wave with an amplitude of 1.29 V and a frequency of 1 KHz for the output voltage (Vout). There is a little bit of ringing at the leading edge of the square wave, but overall, it looks pretty good.

Again, I don’t believe that the SPICE measurement directives make much sense for the differentiator, so they were omitted.

This section described how the differentiating amplifier can be used to perform differentiation operations.

The following is a list of additional resources you may find helpful.

## Summary

In this tutorial, we learned how operational amplifiers can be used to perform mathematical operations. We created and simulated each of the following op-amp designs, with their associated mathematical operations, in LTspice and compared our expectations to simulated results.

• Non-Inverting Amplifier (scalar multiplication)
• Inverting Amplifier (scalar multiplication, scalar division, and negation)
• Inverting Summing Amplifier (addition, averaging, and weighted inputs)
• Differential Amplifier (subtraction)
• Integrating Amplifier (integration)
• Differentiating Amplifier (differentiation)

You should now have a toolkit of practical op-amp based circuits providing useful mathematical operations that you can use in your own projects.

The final LTspice schematics used in this tutorial are available on GitHub.

Thank you for joining me on this journey and I hope you enjoyed the experience. Please feel free to share your thoughts or questions in the comments section below. #### John Woolsey

John is an electrical engineer who loves science, math, and technology and teaching it to others even more.

He knew he wanted to work with electronics from an early age, building his first robot when he was in 8th grade. His first computer was a Timex/Sinclair 2068 followed by the Tandy 1000 TL (aka really old stuff).

He put himself through college (The University of Texas at Austin) by working at Motorola where he worked for many years afterward in the Semiconductor Products Sector in Research and Development.

John started developing mobile app software in 2010 for himself and for other companies. He has also taught programming to kids for summer school and enjoyed years of judging kids science projects at the Austin Energy Regional Science Festival.

Electronics, software, and teaching all culminate in his new venture to learn, make, and teach others via the Woolsey Workshop website.

• Dave Comer says:
• John Woolsey says: