# Signals From Noise Easy Data Scaling Without Multiplication Or Division by Dave Van Ess, Principal Application Engineer

, MTS, Cypress Semiconductor

In many microcontroller-based systems I find it is necessary to scale the digitized ADC results, the digital value sent to a DAC, or data used by different algorithms. If you are lucky, the scaling is either a gain or an attenuation factor of 2n. If not, you will have to multiply or divide by some constant factor. In very small systems there may not be enough program area for a divide, or even a multiply, routine. This TechNote will show how data can be scaled with a series of shifts, additions, and subtractions. As shown in the equation below, any scaling factor can be represented as a series of binary values: A x = ai 2i x a = (1,0) B i To help determine the binary values for a particular scaling value, a spreadsheet has been included as a separate download on the site.

Setting the “A” value (B1) to 237 and “B” value (B2) to 64 results in a calculated scale factor of 3.703125 (D4). The spreadsheet automatically calculates the largest required binary value (B5). It also generates the binary values and calculates the error for each defined ai. For this example there are 6 terms required. They are shown as:

As

Fig. 1: Spreadsheet To Calculate 237/64 Scale Factor

Pu bl

is he d

on

EN

Suppose you have a system that requires that the data be scaled by a factor of 237/64. Open the data sheet (Fig. 1).

-G en iu s.

ne t

[+] Feedback

A x x x x = (G7 + G8 + G9 + G11 + G12) x = 2x + x + + + + B 2 8 16 64

This scale factor can be achieved with shifts and adds of the data. However this can be simplified if subtraction is also allowed. Notice that x/8 and x/16 are both set. Fig. 2 shows that they can be replaced with x/4 – x/16 (G10 – G12).

As

Pu bl

Fig. 3: Reducing Number Of Terms, Part 2

is he d

There are still six terms but now 2x, x, x/2, and x/4 are all set. As shown in Fig. 3, 2x + x + x/2 +x/4 can be simplified to 4x – x/4 (G6 – G10).

on

Fig. 2: Reducing Number Of Terms, Part 1

EN

-G en iu s.

ne t

[+] Feedback

There are now only four terms: A x x x x x x x x = 2x + x + + + + = 4x + B 2 8 16 64 4 16 64 Implementing this scale factor requires shifting the data along with one move of the data, two subtractions, and one addition. For the previous example, only four terms were needed. It is possible that exactly representing a scale factor may require an infinite number of terms. When multiplying by “A” and then dividing by “B” the accuracy is limited by the size of the data registers. However, the data itself often has limited accuracy. ADC data with a 1% accuracy surely isn’t going to need the scale factor to be much more accuracy that that, at least no more than a factor of ten. Knowing the acceptable accuracy of your data allows for only the minimum amount of calculation to scale the data.

Fig. 4: Determine Terms For SQRT(3) Scale Factor 0.01% Accuracy The original add only coefficients are shown in column J. Columns I, H, G, show the transform to get to most reduced terms in column F. The last line of the spreadsheet shows that the error is 0.0068% or 68 parts per million (ppm). The terms are: x x x x = F6 – F9 – F13 – F16 – F18 3 x = 2x 4 64 512 2048 Now accuracy better than 0.01% should be quite acceptable. But suppose you have some constraint that requires the error be less than 1ppm. This is easily be done by extending

As

Pu bl

is he d

on

EN

-G en iu s.

When dealing with three-phase power, a scale factor of the square root of three (1.732) is often required. The figure below shows that five terms are required for this scale factor: for an accuracy better than 0.01%.

ne t

[+] Feedback

About The Author Dave Van Ess is an application engineer and member of technical staff at Cypress Semiconductor. He is an electrical engineer with experience in hardware, software, and analog design. Dave joined Cypress in 2000. He has eight patents for medical systems, signal processing design, and PSoC digital block enhancements. He has written numerous User Modules, application notes, and articles. He graduated sigma cum barely with his BSEE from the University of California, Berkeley, 1977. He can be contacted at dwv@cypress.com

As

With the spreadsheet provided you can easily develop the terms required for your specific scaling factor so that you can easily scale your data without the use of multiplication or divide routines.

Pu bl

To implement the scale factor with the improved accuracy specification requires three extra terms as shown in the equation below. x x x x x x x 3 x = 2x + 4 64 512 2048 8192 262144 524288

is he d

on

Fig. 5: Determine Terms For SQRT(3) Scale Factor, Better Than 1ppm Accuracy

EN

-G en iu s.

ne t

[+] Feedback

An engineer by training, a poet by temperament, an outlaw in Nebraska, and a heck of a nice guy, Dave has worked in many different industries. His work experience includes test and measurement equipment, measurement and control systems for high-energy physics research, and underwater acoustical transmitters and receivers deployed in open sea and artic ice fields. Electrons fear him! Women revere him!

As

Pu bl

is he d

on

EN

-G en iu s.

ne t

[+] Feedback