[PYTHON] CORDIC with Scipy

It is that trigonometric functions can be calculated only by addition, subtraction, and shift operations. For details, this page was very easy to understand. The code I actually wrote is calculated with floating point numbers, so multiplication is used instead of shift operation. Even so, I often come up with such an algorithm.

Below is the code and execution results.

cordic.py


#!/usr/bin/env python
from __future__ import division
import scipy as sp
import numpy as np
import matplotlib.pyplot as plt
import math

term_num = 17
thetas =  map( lambda x : math.atan( 1.0 / math.pow( 2.0,x ) ) , range( term_num  + 1) )
hypot_length =  1.0 / reduce( lambda x,y:  x * ( 1.0 / math.cos(y) ) ,thetas,1.0 )

def cos_cordic( angle ):
        x,y = (1.0,1.0)
        acc_theta = thetas[0]
        scale_ratio = 1.0
        for i,theta in enumerate( thetas[1:] ):
                x1,y1 = x,y
                scale_ratio *= 0.5
                if acc_theta < angle :
                        acc_theta += theta
                        x -= scale_ratio * y1
                        y += scale_ratio * x1
                else:
                        acc_theta -= theta
                        x += scale_ratio * y1
                        y -= scale_ratio * x1
        return ( x * hypot_length )


if __name__ == '__main__':
        t = [ x * ( math.pi / 200.0 ) for x in range( 100 ) ]
        result = map( cos_cordic,t )

        plt.plot( result )
        plt.show()

cordic.png

Recommended Posts

CORDIC with Scipy
LPC with Scipy
ICA with Scipy
Normarize data with Scipy
Use OpenBLAS with numpy, scipy
Extract peak values with scipy
Harmonic mean with Python Harmonic mean (using SciPy)
Calculate sample distribution with Scipy (discrete distribution)
Generate a normal distribution with SciPy
scipy Voronoi
Install Scipy
Use multithreaded BLAS / LAPACK with numpy / scipy
Using Intel MKL with NumPy / SciPy (November 2019 version)
Create 3D scatter plot with SciPy + matplotlib (Python)