Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Database

An Efficient Algorithm for Magnitude Operation


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:

  • Conversion of signals in time domain into spatial domain (for different coordinate systems).
  • Conversion of signals from one coordinate to another in spatial domain.

  • Conversion of spatial domain to rasters. These operations always involve high-speed magnitude computations.

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


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.