Home Page or Table of Contents

A Scrapbook of Digital Signal Processing

Real-Time Log Power Computation

     For many real-time applications it is useful to be able to either process or display an array of time-domain
or frequency-domain vectors in compact numerical form.  One way of implementing this with an eye toward
rapid code execution is to convert the vectors to 8-bit log-power form.  The procedure consists of two
steps -- first converting each vector into unsigned magnitude and then converting this magnitude to log form.

     Although power can be computed by squaring and summing the vector components, the doubling in significant
bits required leads to a loss in dynamic range, as well as to doubling the number of shifts required for normalization.
On the other hand, using the magnitude requires computing a square root, a time-consuming process.  The relationship

        MAGNITUDE = SQRT(A2 + B2)

 can be approximated to within a dB using the relationships:

        MAGNITUDE = |A| + 1/2 |B| for A > B
        MAGNITUDE = |B| + 1/2 |A| for A < B 

thereby affording considerable computational savings by bypassing the square root operation.  The 
approximation is best when either A or B is zero, and worst when A=B, where SQRT(2) is taken as 1.5.

     Log power can be obtained either by normalizing a number or by utilizing its normalized floating-point
representation.  For an array scaled to fractions less than unity (other scalings are also possible)
the basic relationship is:

   if    x  = SQRT(A2 + B2) < 1,       then      log (x) = 8 x (31 + exp(x) )  +  man(x).

where exp(x) refers to the unbiased signed exponent of x, while man(x) refers to the uppermost bits of the
mantissa, with the most significant bit dropped (which is always 1).  The factor of 8 represents a shift of
3 places, while the bias of 31 is used to remove the negative exponent for the fractional form.  Thus, the
upper 5 bits of the 8-bit word are used for the exponent, while the 3 lower bits are used to interpolate
between successive 6-dB steps in the exponent (6-dB, not 3-dB, because the numbers represent magnitude,
not power).

          0 dB = 255       eeeee mmm = 11111 111  (255 used since 256 is out of range))
        - 6 dB = 248       eeeee mmm = 11110 000
        -12 dB = 240       eeeee mmm = 11100 000
       -192 dB =   0       eeeee mmm = 00000 000

The division of 6 dB into 8 parts (3 bits) gives slightly better than 1 dB resolution.  The maximum dynamic range
is 192 dB.  Notice that this operation is essentially one of masking and shifting to remove the MSB of the
mantissa (the DEC floating-point format automatically drops this redundant bit, referring to it as a "phantom" bit).

     For display purposes, using a bipolar DAC, the MSB can be used as a negative d.c. scope trigger by using
only 4 (instead of 5) bits for the 6-dB divisions

          0 dB = 127       eeeee mmm = 01111 111  (127 used to leave 128 free for triggering bipolar DAC)
        - 6 dB = 120       eeeee mmm = 01111 000
        -12 dB = 112       eeeee mmm = 01110 000
        -96 dB =   0       eeeee mmm = 00000 000

One result of this is a reduction in maximum possible dynamic range from 192 to 96 dB.  Other possibilities for
display purposes are to use only 2 bits for the mantissa and 5 bits for the exponent or to use a DAC with more
bits (10 or 12).  For a DAC with a +/-10-volt output, the scaling is close to 10 dB/volt (96 dB over 10 volts).
A simultaneous display of up to 8 channels can be accomplished by using a DAC with 12 bits and off-setting
each consecutive channel by about 1/2 volt.  Sequential or stacked displays can be generated from the same
output by setting the trigger of the first channel to minus full scale and the other channels to minus half scale
and adjusting the scope d.c. trigger level to trigger on the desired display, while adjusting the time base to
sweep more slowly for the sequential-channel display and faster for the stacked-channel display.

Home Page or Table of Contents