An Efficient Algorithm for Magnitude Operation

Magnitude operation is widely used in signal and data processing for signal detection and power estimation in systems such as real-time displays for sensors, radars, sonars, and scanners for medical-imaging systems.


April 01, 2000
URL:http://www.drdobbs.com/database/an-efficient-algorithm-for-magnitude-ope/184404064

Apr00: An Efficient Algorithm for Magnitude Operation

The author is a senior software engineer for Siemens Information Systems Limited in Bangalore, India. He can be contacted at [email protected].


In the field of signal processing, the signal contains information in amplitude and phase and is represented as complex variables. The energy of the signal is proportional to the magnitude of the signal. Magnitude operation is, therefore, widely used in signal and data processing for signal detection and power estimation. Systems where magnitude operations are frequently used include real-time displays for sensors, radars, sonars, scanners for medical imaging systems, and the like. These systems work with multiple modes, where the objects are displayed in circular and cartesian coordinate systems. Frequent operations in the display processor are:

Magnitude operation is generally implemented as lookup tables in real-time applications, with all possible combinations of input arguments as address locations. However, these lookup tables consume huge amounts of memory. While high-speed methods are available, the results they produce are only approximations for magnitude. In "A Baker's Dozen Magnitude Approximations and Their Detection Statistics," (IEEE Transactions on Aerospace and Electronic Systems, January 1976), A.E. Filip notes 13 approximations for magnitude in which the least accurate has a peak error of 27.3 percent and the most accurate has a peak error of 0.97 percent. In this article, I present an alternative approach to magnitude operation that is both fast and efficient in terms of memory. This approach, which is based on the Successive Approximation principle, predicts each bit of the result from MSB to LSB, and corrects it based on feedback. In addition, I've implemented this algorithm in C++ source code; see Listing One.

Algorithm Description

The algorithm itself is fairly straightforward. Note that in this article, I represent magnitude as M(a,b). If Z is a complex number given by Z=a+i×b where i is imaginary unity, and Z' is its conjugate given by Z=a-i×b, then Z×Z'=M×M. Furthermore, the magnitude of two numbers (a,b) is defined as SquareRoot(a×a+b×b).

For two integers (a,b), M(a,b) have the boundaries in Figure 1, where a is the larger number, see Figure 1(a), and b is the smaller number; see Figure 1(b). You define r such that r=M-a; as in Figure 1(c). This implies Figure 1(d). Therefore, if r is computed successively, such that b2=r2 +2×a×r, then (r+a) will give M(a,b). To compute r successively, in each iteration, 1 bit is added to it. Assume this increment is d. This yields Figure 1(e). Figure 2 provides a proof of the theorem in Figure 1(b).

The steps to compute M(a,b) are:

1. Compute b2 using shift and add operations. This takes N iterations, where N is the number of bits in b.

2. Set the initial value of increment d to 2n, where n=(N-1)th bit.

3. Set r=0.

4. Compute (r+d)2+2×a×(r+d), by computing (d2+2×r×d+2×a×d) and adding to previous value of (r2+2×a×r). Since d is always a power of 2, this can be computed using shift and add operations.

5. If (r+d)2+2×a×(r+d)<= b2, update r to r+d; or else do not update.

6. Reduce the increment d to next bit -- 2(n-1).

7. Repeat steps 4 to 6 until d becomes 1, (N-1 iterations).

8. Return M=r+a.

You can see that the number of iterations depends on the smallest number b, and the result is accurate to 1 LSB. Again, Listing One is C++ code that implements this. In this example, validation of input arguments are omitted.

DDJ

Listing One

unsigned long imag(unsigned int a, unsigned int b)
{
 unsigned int  l, r, d;
 unsigned long d2, b2, R, D;
 R = 0;
 l = 0;
 b2 = 0;
 r  = b;
 do
 {
  d = 1L << l;
  if(b & d)
  {
   b2 += (b << l);
  }
  l++;    // find the number of bits in b
 }while(r >>= 1);
 l--;
 while(l --)
 {
  d   = 1L << l;   // setting the increment
  d2  = d << l;
  D   = d2 + ((a + r) << (l + 1));// D = d**2 + 2*r*d + 2*a*d;
  if((R + D) <= b2)
  {
   r += d;   // updating value of r.
   R += D;
  }
 }
 return (r + a);
}

Back to Article

Apr00: An Efficient Algorithm for Magnitude Operation


(a)
M(a,b)>=a,

(b)
M(a,b)<a+0.5b

(c)
r^2=(M-a)^2=M^2+a^2-2*M*a=2*a^2+b^2-2*M*a;

(d)
b^2=r^2+2*a*r, (0<=r<0.5b)

(e)
(r+d)^2+2*a*(r+d)=(r^2+2*a*r)+(d^2+2*r*d+2*a*d)

Figure 1: Algorithm description.

Apr00: An Efficient Algorithm for Magnitude Operation


Since a>b,
a>(3/4)b
Multiply with b on both sides: ab>(3/4)b^2
  ab+(1/4)b^2>b^2
Add a^2 on both sides: a^2+ab+(1/4)b^2>a^2+b^2
  (a+0.5b)^2>M^2
for 'a', 'b' integers, M<(a+0.5b)

Figure 2: A proof of the theorem in Figure 1(b).

Terms of Service | Privacy Statement | Copyright © 2024 UBM Tech, All rights reserved.