Computing Multiplication and Division using CORDIC in VisualDSP++/VC++

Document Sample
Computing Multiplication and Division using CORDIC in VisualDSP++/VC++ Powered By Docstoc
					Computing Multiplication and Division using CORDIC
                 in VisualDSP++


                     Project Report
                        CSE-407

         Course on Digital Signal Processing
           Instructor: Engr. Maryam Mahsal Khan

                        Project By

   Mohammad Waqas     Roll#02 Reg No:2006-cise-457
   Arbab Waseem Abbas Roll#27 Reg No:2006-cise-454




       Department of Computer Systems Engineering,
      N-W.F.P. University of Engineering & Technology,
                   Peshawar, Pakistan.
                      January, 2009.
Computing Multiplication and Division using CORDIC


CONTENTS:                                       Page no.

 Abstract                                           3

 Introduction                                       4

 Methodology                                        5

 Conclusion                                         6

 Results/code                                       8

 References                                         15




                                                           2
Computing Multiplication and Division using CORDIC


ABSTRACT:

CORDIC stands for Coordinate Rotation Digital Computer. The methods were first used
in 1959 to solve trigonometric relationships that arose in navigation problems. In the
early 1980s, the methods were used by Hewlett-Packard for trigonometric function
evaluation on the HP-35 calculator. The functions that can be evaluated using CORDIC
methods are sine, cosine, tangent, inverse tangent, hyperbolic sine, hyperbolic cosine,
hyperbolic tangent, inverse hyperbolic tangent, natural logarithm, natural exponential,
square root, multiplication, division. In binary form the scheme consists of the iterative
equations but in our project we concentrate on multiplication & division using CORDIC.
In our code we illustrate the technique using linear method. As the linear mode can
perform multiply and divide, we only need add/subtract and shift to complete the
implementation. CORDIC algorithms are efficient in terms of both computation time and
hardware resources. The CORDIC is a hardware efficient method which can compute a
wide variety of trigonometric, hyperbolic , logarithmic functions , multiplication &
division and is useful for real-time signal and image processing. The CORDIC algorithm
makes use of only shifters and adder blocks to compute these functions.

The architecture for the implementation of the CORDIC algorithm is of a recursive set of
blocks and thus lends itself to a relatively easy manufacturing process. The CORDIC
architecture does not need a multiplier block for the computation of functions and this
makes it a clear winner among other architectures for functional computation.
TheCORDIC algorithm comprises an iterative series of steps and the accuracy of the final
result depends upon the number of iterations performed.




                                                                                         3
Computing Multiplication and Division using CORDIC


INTRODUCTION:

The CORDIC is a class of hardware-efficient algorithms for the computation of
trigonometric and other transcendental functions that use only shifts and adds to perform.
The CORDIC set of algorithms for the computation of trigonometric functions was
developed by Jack E. Volder in 1959 to help in building a real-time navigational system
for the B-58 supersonic bomber. Later, J. Walther in 1971 extended the CORDIC scheme
to other transcendental functions.
The CORDIC method of functional computation is used by most handheld calculators
,digital-systems, satellite & microcontrollers ,calculators such as the ones by Texas
Instruments and Hewlett-Packard) to approximate the standard transcendental functions
Calculators can only perform four operations inexpensively:
             Addition and Subtraction
             Storing in memory and Retrieving from memory
             Digit shift (multiplication/division by the base)
             Comparisons
The CORDIC Algorithm is a unified computational scheme to perform
     multiplication and division efficiently
 CORDIC algorithms are efficient in terms of both computation time and hardware
resources - and in most systems, these resources are normally a premium
This algorithm uses only minimal hardware (adder and shift) for computation of all
trigonometric and other function values. It consumes fewer resources than any other
techniques and so the performance is high. Thus, almost all scientific calculators use the
CORDIC algorithm in their calculations.




                                                                                         4
Computing Multiplication and Division using CORDIC


METHODOLOGY:

      Embedding of elementary function evaluation as a generalized rotation operation.
      Decompose rotation operation into successive basic rotations.
      Each basic rotation can be realized with shift and add arithmetic operations. shift-
       and-add arithmetic operations.

A CORDIC algorithm for multiplication can be derived using a series representation for x
as shown below




 From this, z is composed of shifted versions of y. The unknown value for z, may be
found by driving x to zero 1 bit at a time. If the ith bit of x is nonzero, yi is right shifted
by i bits and added to the current value of z. The ith bit is then removed from x by
subtracting 2-i from x. If x is negative, the ith bit in the twos complement format would
be removed by adding 2-i. In either case, when x has been driven to zero all bits have
been examined and z contains the signed product of x and y correct to B bits.
This algorithm is similar to the standard shift and add multiplication algorithm except for
two important features.
     Arithmetic right shifts are used instead of left shifts, allowing signed numbers to
        be used.
     Computing the product to B bits with the CORDIC algorithm is equivalent to
        rounding the result of the standard algorithm to the most significant B bits.
A CORDIC division algorithm is based on rewriting the equation z=x/y into the form x-
y*z=0. If z is expanded into its series representation, the second version of the equation
takes the form in Figure (a), which, after some manipulation, yields Figure (b).




                                                                                              5
Computing Multiplication and Division using CORDIC




This final form of the equation shows that the quotient z may be estimated 1 bit at a time
by driving x to zero using right-shifted versions of y. If the current residual is positive,
the ith bit in z is set. Likewise, if the residual is negative the ith bit in z is cleared.




                                 Cordic algo. Block diagram




                                                                                               6
Computing Multiplication and Division using CORDIC


CONCLUSION:

CORDIC algorithms are an efficient method to compute many different functions
• Low area, high speed
• Used in calculators, DSPs, math-coprocessors and supercomputers.
CORDIC algorithms have been around for some time. Volder’s original paper describing
the CORDIC technique for calculating trigonometric functions appeared in the 1959 IRE
transactions. However, the reasons for using CORDIC algorithms have not changed. The
algorithms are efficient in terms of both computation time and hardware resources.
In most systems, especially those performing control functions, these resources are
normally already at a premium.
Using CORDIC algorithms may allow a single chip solution where algorithms using the
look-up table method may require a large ROM size or where power series calculations
require a separate co-processor because of the
computation time required.
 For each algorithm in this core i-e multiplication & division , three areas have been
covered:
     theory of operation,
     determining the range of convergence for the algorithm
     and finally implementation of the algorithm on a typical systems.




                                                                                     7
Computing Multiplication and Division using CORDIC


RESULTS/CODE:

Loading: "C:\Documents and Settings\arbab waseem abbas\My Documents\VisualDSP
Projects\dsp_project\Debug\dsp_project.dxe"...
       Load complete.
       Breakpoint Hit at <ffa11ba0>

       MULTIPLY 36.000000 DIVIDE 9.000000
       Breakpoint Hit at <ffa11d36>

     Loading: "C:\Documents and Settings\arbab waseem abbas\My
Documents\VisualDSP Projects\dsp_project\Debug\dsp_project.dxe"...
     Load complete.
     Breakpoint Hit at <ffa11ba0>

       MULTIPLY 50.000000 DIVIDE 4.500000
       Breakpoint Hit at <ffa11d36>

CODE:
#include <stdio.h>
#include <math.h>
/* working with IEEE doubles, means there are 53 bits
 * of mantissa
 */
#define MAXBITS             53
/* define this to perform all (non 2power) multiplications
 * and divisions with the cordic linear method.
 */
#define CORDIC_LINEARx
/* these are the constants needed */
static double invGain1;
static double invGain2;
static double atanTable[MAXBITS];
static double atanhTable[MAXBITS];
static double gain1Cordic();
static double gain2Cordic();
void initCordic()
{
   /* must call this first to initialise the constants.
    * of course, here i use the maths library, but the
    * values would be precomputed.
    */
   double t = 1;
   int i;
   for (i = 0; i < MAXBITS; ++i) {


                                                                                8
Computing Multiplication and Division using CORDIC

    atanTable[i] = atan(t);
    t /= 2;
    atanhTable[i] = 0.5*log((1+t)/(1-t));
  }
  /* set constants */
  invGain1 = 1/gain1Cordic();
  invGain2 = 1/gain2Cordic();
}
void cordit1(double* x0, double* y0, double* z0, double vecmode)
{
  /* this is the circular method.
   * one slight change from the other methods is the y < vecmode
   * test. this is to implement arcsin, otherwise it can be
   * y < 0 and you can compute arcsin from arctan using
   * trig identities, so its not essential.
   */
  double t;
  double x, y, z;
  int i;
  t = 1;
  x = *x0; y = *y0; z = *z0;
  for (i = 0; i < MAXBITS; ++i) {
     double x1;
     if (vecmode >= 0 && y < vecmode || vecmode<0 && z >= 0) {
         x1 = x - y*t;
         y = y + x*t;
         z = z - atanTable[i];
     }
     else {
         x1 = x + y*t;
         y = y - x*t;
         z = z + atanTable[i];
     }
     x = x1;
     t /= 2;
  }
  *x0 = x;
  *y0 = y;
  *z0 = z;
}
void cordit2(double* x0, double* y0, double* z0, double vecmode)
{
  /* here's the hyperbolic methods. its very similar to
   * the circular methods, but with some small differences.
   *
   * the `x' iteration have the opposite sign.



                                                                   9
Computing Multiplication and Division using CORDIC

   * iterations 4, 7, .. 3k+1 are repeated.
   * iteration 0 is not performed.
   */
  double t;
  double x, y, z;
  int i;
  t = 0.5;
  x = *x0; y = *y0; z = *z0;
  int k = 3;
  for (i = 0; i < MAXBITS; ++i) {
     double x1;
     int j;
     for (j = 0; j < 2; ++j) {
         if (vecmode >= 0 && y < 0 || vecmode<0 && z >= 0) {
            x1 = x + y*t;
            y = y + x*t;
            z = z - atanhTable[i];
         }
         else {
            x1 = x - y*t;
            y = y - x*t;
            z = z + atanhTable[i];
         }
         x = x1;
         if (k) {
            --k;
            break;
         }
         else k = 3;
     }
     t /= 2;
  }
  *x0 = x;
  *y0 = y;
  *z0 = z;
}
void cordit0(double* x0, double* y0, double* z0, double vecmode)
{
  /* the linear methods is the same as the circular but
   * ive simplified out the x iteration as it doesnt change.
   */
  double t;
  double x, y, z;
  int i;
  t = 1;
  x = *x0; y = *y0; z = *z0;



                                                                   10
Computing Multiplication and Division using CORDIC

  for (i = 0; i < MAXBITS; ++i) {
     if (vecmode >= 0 && y < 0 || vecmode<0 && z >= 0) {
        y = y + x*t;
        z = z - t;
     }
     else {
        y = y - x*t;
        z = z + t;
     }
     t /= 2;
  }
  *x0 = x;
  *y0 = y;
  *z0 = z;
}
/** Linear features ***********************************************/
double mulCordic(double a, double b)
{
   double x, y, z;
   x = a;
   y = 0;
   z = b;
   cordit0(&x, &y, &z, -1);
   return y;
}
double divCordic(double a, double b)
{
   double x, y, z;
   x = b;
   y = a;
   z = 0;
   cordit0(&x, &y, &z, 0);
   return z;
}
#ifdef CORDIC_LINEAR
#define MULT(_a, _b) mulCordic(_a, _b)
#define DIVD(_a, _b) divCordic(_a, _b)
#else
#define MULT(_a, _b) (_a)*(_b)
#define DIVD(_a, _b) (_a)/(_b)
#endif
/** circular features ***********************************************/
static double gain1Cordic()
{
   /* compute gain by evaluating cos(0) without inv gain */
   double x, y, z;



                                                                         11
Computing Multiplication and Division using CORDIC

  x = 1;
  y = 0;
  z = 0;
  cordit1(&x, &y, &z, -1);
  return x;
}
double atanCordic(double a)
{
  /* domain: all a */
  double x = 1;
  double z = 0;
  cordit1(&x, &a, &z, 0);
  return z;
}
double sincosCordic(double a, double* cosp)
{
  /* |a| < 1.74 */
  double sinp = 0;
  *cosp = invGain1;
  cordit1(cosp, &sinp, &a, -1);
  return sinp;
}
double tanCordic(double a)
{
  /* |a| < 1.74 */
  double sinp = 0;
  double cosp = invGain1;
  cordit1(&cosp, &sinp, &a, -1);
  return DIVD(sinp, cosp);
}
double asinCordic(double a)
{
  /* |a| < 0.98 */
  double x, y, z;

  x = invGain1;
  y = 0;
  z = 0;
  int neg = 1;
  if (a < 0) {
     a = -a;
     neg = 0;
  }

  cordit1(&x, &y, &z, a);
  if (neg) z = -z;



                                                     12
Computing Multiplication and Division using CORDIC

  return z;
}
/** hyperbolic features ********************************************/
double gain2Cordic()
{
   /* calculate hyperbolic gain */
   double x, y, z;
   x = 1;
   y = 0;
   z = 0;
   cordit2(&x, &y, &z, -1);
   return x;
}
double sinhcoshCordic(double a, double* coshp)
{
   /* |a| < 1.13 */
   double y;
   *coshp = invGain2;
   y = 0;
   cordit2(coshp, &y, &a, -1);
   return y;
}
double tanhCordic(double a)
{
   /* |a| < 1.13 */
  double sinhp, coshp;
  coshp = invGain2;
  sinhp = 0;
  cordit2(&coshp, &sinhp, &a, -1);
  return DIVD(sinhp,coshp);
}
double atanhCordic(double a)
{
   /* |a| < 1.13 */
   double x, z;
   x = 1;
   z = 0;
   cordit2(&x, &a, &z, 0);
   return z;
}
double logCordic(double a)
{
   /* 0.1 < a < 9.58 */
   double x, y, z;
   x = a + 1;
   y = a - 1;



                                                                        13
Computing Multiplication and Division using CORDIC

    z = 0;
    cordit2(&x, &y, &z, 0);
    return 2*z;
}
double sqrtCordic(double a)
{
  /* 0.03 < a < 2 */
  double x, y, z;
  x = a + 0.25;
  y = a - 0.25;
  z = 0;
  cordit2(&x, &y, &z, 0);
  return MULT(x, invGain2);
}
double expCordic(double a)
{
  double sinhp, coshp;
  coshp = invGain2;
  sinhp = 0;
  cordit2(&coshp, &sinhp, &a, -1);
  return sinhp + coshp;
}


void main()
{
  initCordic();
   printf("MULTIPLY %f ",MULT(25.0000, 2.0000));
  printf("DIVIDE %f",DIVD(9.0000, 2.0000));

}




                                                     14
Computing Multiplication and Division using CORDIC



REFRENCES:

1. Volder, Jack E., “The CORDIC Trignometric Computing Technique”, IRE
Transactions Electronic Computers, vol. EC-8, pp. 330-334, September
1959.
2. Specker W. H., “A Class of Algorithms for Ln x, Exp x, Sin x, Cos x,
Tan-1x and Cot-1x”, IEEE Transactions Electronic Computers, vol. EC-14,
pp.
85-86, 1965.
3. Walther, J. S., “A Unified Algorithm For Elementary Functions”, 1971
Proceedings of the Joint Spring Computer Conference, pp. 379-385, 1971.
4. Jarvis, Pitts, “Implementing CORDIC Algorithms”, Dr. Dobb’s Journal,
#169`, pp. 152-156, October 1990.




                                                                     15

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:29
posted:9/12/2012
language:Latin
pages:15