1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ScipyでCORDIC

Posted at

加算,減算,シフト演算だけで三角関数を計算出来るというアレです.
詳細はここのページが非常に解りやすかった.
実際に書いてみたコードは浮動小数点数で演算してるんで,シフト演算の代わりに乗算を使用.
それにしても,よくこんなアルゴリズムを思いつくなぁ.

以下,コードと実行結果.

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

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?