## Introduction

Back in 2003, I used an approximation for the logarithm function in a hardware application. When originally implemented, the function only had to work for a limited range of input. Recently, a customer has requested that we expand the range of operation for this function. This post examines how I went about expanding this approximation's range of operation.

Engineers frequently have to approximate common mathematical functions. You might wonder why we still need to approximate these functions when there are excellent mathematical libraries available for all commonly used processors. There are two reasons:

- Speed

Library functions are coded for accuracy and they frequently take a long time to execute. There are applications where accuracy is less important than speed and approximations may accurate enough and fast enough to solve your problem. For example, I have had to use approximations to the square root function when computing the magnitude of vectors in real-time navigation applications. Library functions simply were too slow. - Cost

Library functions require lots of memory and may force you to buy a faster processor in order to execute them. My cost limitations are usually so tough that I have to use cheap processors like AVRs, which have limited memory and throughput. I need to find inexpensive ways to implement math functions on these brain-dead computers.

We will examine my original implementation and how I went about expanding its range of operation, which engineers refer to as the function's "dynamic range."

## Background

### Decibel Basics

Technically, I view decibels as a scaling rather than a unit. Decibels are always expressed relative to a unit, in this case milliwatts. Equation 1 defines the dBm, which means decibels relative to one mW.

Eq. 1 |

where

*P*is the measured power in milliwatts [mW],_{mW}*P*is the measured power expressed in decibels milliwatt (dBm)_{dBm}

I need to approximate Equation 1 over a range from - 6 dBm to 0 dBm with an accuracy of better than 0.5 dB. This post will use a 4^{th}-order polynomial to approximate the logarithm function. Equation 2 defines my polynomial model. I also include an equivalent matrix version.

Eq. 2 | |

Now that we know a bit about decibels, let's discuss how cable TV works, and if you want some more information, you can also click here to check out the updated cable TV statistics for this year.

### Some Cable TV Basics

Figure 1 illustrates the scenario I find myself in. In general, one laser will drive multiple stages of amplification. Thus, one laser and a "tree" of EDFAs can serve thousands of homes.

Video service providers use a laser to transmit their video signal to the homes they serve. The power of this signal is important because it determines how many homes can be served -- every home must receive a specified level of power to provide a high quality signal. However, more optical power requires more expensive transmission gear (e.g. devices called EDFAs). Service providers want to use exactly the amount of optical power that they need and no more. They set the power of this signal in decibels because that is how the equipment was designed (again, the decibel legacy). For all sorts of reasons, my gear at the home needs to measure the power of this signal. However, real components measure power in mW (or a similar unit) not decibels. Yet, I need to be able to provide an optical power measurement in decibels for system monitoring purposes. I do not want to raise my product costs by adding memory just to compute decibels. So I decided to use a polynomial approximation to the logarithm because it requires little memory and is very fast on an AVR processor.

## Analysis

### My Original Polynomial Approximation

Figure 2 shows my Mathcad implementation of a minimum-maximum error curve fit routine. I chose to use a dynamic range from -6 dBm to 0 dBm (to be truthful, the actual range I used was slightly different but for reasons that are unimportant here).

Figure 3 shows the "goodness of fit" for this approxmation.

### A Wider Dynamic Range Version

#### Approach

A service provider wants me to expand the range of operation of my optical power measurement to -12 dB to 6 dBm from -6 dBm to 0 dBm. As I thought about it, I made the following observations.

- I can break this range into three parts related by a factor of 4:
- -12 dBm to -6 dBm
- -6 dBm to 0 dBm
- 0 dm to 6 dBm
- You can see these ranges are related by a factor of 4 by noting that

Given these observations, I can state Equation 3.

Eq. 3 | |

So I can use my dB approximation over a wider dynamic range by using Equation 3 as shown in Equation 4.

Eq. 4 |

#### Results

Figure 4 shows the effectiveness of my approximation. This is not too bad and I can reuse software that has already been tested.

## Conclusion

This was a nice example of using a property of logarithms to solve a common engineering problem.