OSDN Git Service

First commit
authorsuikan <suikan@users.sourceforge.jp>
Sun, 21 Apr 2013 09:53:25 +0000 (18:53 +0900)
committersuikan <suikan@users.sourceforge.jp>
Sun, 21 Apr 2013 09:53:25 +0000 (18:53 +0900)
algorithm_nco/.cproject [new file with mode: 0644]
algorithm_nco/.gitignore [new file with mode: 0644]
algorithm_nco/.project [new file with mode: 0644]
algorithm_nco/Makefile [new file with mode: 0644]
algorithm_nco/dds.c [new file with mode: 0644]
algorithm_nco/fract_x86.h [new file with mode: 0644]
algorithm_nco/main.c [new file with mode: 0644]
algorithm_nco/sincostable.ods [new file with mode: 0644]

diff --git a/algorithm_nco/.cproject b/algorithm_nco/.cproject
new file mode 100644 (file)
index 0000000..55d7151
--- /dev/null
@@ -0,0 +1,51 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<?fileVersion 4.0.0?>
+
+<cproject storage_type_id="org.eclipse.cdt.core.XmlProjectDescriptionStorage">
+       <storageModule moduleId="org.eclipse.cdt.core.settings">
+               <cconfiguration id="0.1218010745">
+                       <storageModule buildSystemId="org.eclipse.cdt.managedbuilder.core.configurationDataProvider" id="0.1218010745" moduleId="org.eclipse.cdt.core.settings" name="Default">
+                               <externalSettings/>
+                               <extensions>
+                                       <extension id="org.eclipse.cdt.core.VCErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
+                                       <extension id="org.eclipse.cdt.core.GmakeErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
+                                       <extension id="org.eclipse.cdt.core.CWDLocator" point="org.eclipse.cdt.core.ErrorParser"/>
+                                       <extension id="org.eclipse.cdt.core.GCCErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
+                                       <extension id="org.eclipse.cdt.core.GASErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
+                                       <extension id="org.eclipse.cdt.core.GLDErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
+                               </extensions>
+                       </storageModule>
+                       <storageModule moduleId="cdtBuildSystem" version="4.0.0">
+                               <configuration artifactName="${ProjName}" buildProperties="" description="" id="0.1218010745" name="Default" parent="org.eclipse.cdt.build.core.prefbase.cfg">
+                                       <folderInfo id="0.1218010745." name="/" resourcePath="">
+                                               <toolChain id="org.eclipse.cdt.build.core.prefbase.toolchain.162844150" name="No ToolChain" resourceTypeBasedDiscovery="false" superClass="org.eclipse.cdt.build.core.prefbase.toolchain">
+                                                       <targetPlatform id="org.eclipse.cdt.build.core.prefbase.toolchain.162844150.769675636" name=""/>
+                                                       <builder id="org.eclipse.cdt.build.core.settings.default.builder.2116333506" keepEnvironmentInBuildfile="false" managedBuildOn="false" name="Gnu Make Builder" superClass="org.eclipse.cdt.build.core.settings.default.builder"/>
+                                                       <tool id="org.eclipse.cdt.build.core.settings.holder.libs.1556265373" name="holder for library settings" superClass="org.eclipse.cdt.build.core.settings.holder.libs"/>
+                                                       <tool id="org.eclipse.cdt.build.core.settings.holder.1519658776" name="Assembly" superClass="org.eclipse.cdt.build.core.settings.holder">
+                                                               <inputType id="org.eclipse.cdt.build.core.settings.holder.inType.1918225848" languageId="org.eclipse.cdt.core.assembly" languageName="Assembly" sourceContentType="org.eclipse.cdt.core.asmSource" superClass="org.eclipse.cdt.build.core.settings.holder.inType"/>
+                                                       </tool>
+                                                       <tool id="org.eclipse.cdt.build.core.settings.holder.448881169" name="GNU C++" superClass="org.eclipse.cdt.build.core.settings.holder">
+                                                               <inputType id="org.eclipse.cdt.build.core.settings.holder.inType.63221950" languageId="org.eclipse.cdt.core.g++" languageName="GNU C++" sourceContentType="org.eclipse.cdt.core.cxxSource,org.eclipse.cdt.core.cxxHeader" superClass="org.eclipse.cdt.build.core.settings.holder.inType"/>
+                                                       </tool>
+                                                       <tool id="org.eclipse.cdt.build.core.settings.holder.1193562534" name="GNU C" superClass="org.eclipse.cdt.build.core.settings.holder">
+                                                               <inputType id="org.eclipse.cdt.build.core.settings.holder.inType.870748494" languageId="org.eclipse.cdt.core.gcc" languageName="GNU C" sourceContentType="org.eclipse.cdt.core.cSource,org.eclipse.cdt.core.cHeader" superClass="org.eclipse.cdt.build.core.settings.holder.inType"/>
+                                                       </tool>
+                                               </toolChain>
+                                       </folderInfo>
+                               </configuration>
+                       </storageModule>
+                       <storageModule moduleId="org.eclipse.cdt.core.externalSettings"/>
+               </cconfiguration>
+       </storageModule>
+       <storageModule moduleId="cdtBuildSystem" version="4.0.0">
+               <project id="algorithm_dds.null.1186254974" name="algorithm_dds"/>
+       </storageModule>
+       <storageModule moduleId="scannerConfiguration">
+               <autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
+               <scannerConfigBuildInfo instanceId="0.1218010745">
+                       <autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
+               </scannerConfigBuildInfo>
+       </storageModule>
+       <storageModule moduleId="refreshScope"/>
+</cproject>
diff --git a/algorithm_nco/.gitignore b/algorithm_nco/.gitignore
new file mode 100644 (file)
index 0000000..7fc71cc
--- /dev/null
@@ -0,0 +1 @@
+/a.out
diff --git a/algorithm_nco/.project b/algorithm_nco/.project
new file mode 100644 (file)
index 0000000..214646e
--- /dev/null
@@ -0,0 +1,78 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+       <name>algorithm_nco</name>
+       <comment></comment>
+       <projects>
+       </projects>
+       <buildSpec>
+               <buildCommand>
+                       <name>org.eclipse.cdt.managedbuilder.core.genmakebuilder</name>
+                       <triggers>clean,full,incremental,</triggers>
+                       <arguments>
+                               <dictionary>
+                                       <key>?name?</key>
+                                       <value></value>
+                               </dictionary>
+                               <dictionary>
+                                       <key>org.eclipse.cdt.make.core.append_environment</key>
+                                       <value>true</value>
+                               </dictionary>
+                               <dictionary>
+                                       <key>org.eclipse.cdt.make.core.autoBuildTarget</key>
+                                       <value>all</value>
+                               </dictionary>
+                               <dictionary>
+                                       <key>org.eclipse.cdt.make.core.buildArguments</key>
+                                       <value></value>
+                               </dictionary>
+                               <dictionary>
+                                       <key>org.eclipse.cdt.make.core.buildCommand</key>
+                                       <value>make</value>
+                               </dictionary>
+                               <dictionary>
+                                       <key>org.eclipse.cdt.make.core.cleanBuildTarget</key>
+                                       <value>clean</value>
+                               </dictionary>
+                               <dictionary>
+                                       <key>org.eclipse.cdt.make.core.contents</key>
+                                       <value>org.eclipse.cdt.make.core.activeConfigSettings</value>
+                               </dictionary>
+                               <dictionary>
+                                       <key>org.eclipse.cdt.make.core.enableAutoBuild</key>
+                                       <value>false</value>
+                               </dictionary>
+                               <dictionary>
+                                       <key>org.eclipse.cdt.make.core.enableCleanBuild</key>
+                                       <value>true</value>
+                               </dictionary>
+                               <dictionary>
+                                       <key>org.eclipse.cdt.make.core.enableFullBuild</key>
+                                       <value>true</value>
+                               </dictionary>
+                               <dictionary>
+                                       <key>org.eclipse.cdt.make.core.fullBuildTarget</key>
+                                       <value>all</value>
+                               </dictionary>
+                               <dictionary>
+                                       <key>org.eclipse.cdt.make.core.stopOnError</key>
+                                       <value>true</value>
+                               </dictionary>
+                               <dictionary>
+                                       <key>org.eclipse.cdt.make.core.useDefaultBuildCmd</key>
+                                       <value>true</value>
+                               </dictionary>
+                       </arguments>
+               </buildCommand>
+               <buildCommand>
+                       <name>org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder</name>
+                       <triggers>full,incremental,</triggers>
+                       <arguments>
+                       </arguments>
+               </buildCommand>
+       </buildSpec>
+       <natures>
+               <nature>org.eclipse.cdt.core.cnature</nature>
+               <nature>org.eclipse.cdt.managedbuilder.core.managedBuildNature</nature>
+               <nature>org.eclipse.cdt.managedbuilder.core.ScannerConfigNature</nature>
+       </natures>
+</projectDescription>
diff --git a/algorithm_nco/Makefile b/algorithm_nco/Makefile
new file mode 100644 (file)
index 0000000..6857d9f
--- /dev/null
@@ -0,0 +1,18 @@
+#CC            = bfin-elf-gcc
+#LDFLAGS      = -msim
+CC            = gcc
+LDFLAGS                  = 
+CCFLAGS       =  -O0 -g
+LIBS             = -lm
+OBJS          = main.o dds.o
+
+all:   a.out
+
+a.out:     $(OBJS)
+       $(CC) $(LDFLAGS) $(OBJS) $(LIBS)
+
+clean:
+       rm -f *.o a.out
+
+.c.o:
+       $(CC) $(CCFLAGS) -c $<
diff --git a/algorithm_nco/dds.c b/algorithm_nco/dds.c
new file mode 100644 (file)
index 0000000..55765c7
--- /dev/null
@@ -0,0 +1,291 @@
+/*
+ * Error comparison vs double preceision sin() in x86 architecture by 4G points check (round) :
+ * ave cos err = 0.038109, ave sin err = 0.000000, max cos err : 4.109593, min cos err : -4.139676, max sin err : 4.139676, min sin err : -4.139676
+ * stddev cos err = 0.935760, stddev sin err = 0.936535,
+ *
+ * Error comparison vs double preceision sin() in x86 architecture by 4G points check (truncate):
+ * ave cos err = -1.272776, ave sin err = 0.000000, max cos err : 3.311013, min cos err : -6.006406, max sin err : 6.006406, min sin err : -6.006406
+ * stddev cos err = 1.324925, stddev sin err = 1.837222,
+ *
+ *
+ */
+
+#ifdef __i386__
+#include "fract_x86.h"
+#else
+#include <fract.h>
+#endif
+#include <limits.h>
+
+#include <stdio.h>
+
+fract32 ch[33] =
+{
+        2147483647  ,
+        2144896910  ,
+        2137142927  ,
+        2124240380  ,
+        2106220352  ,
+        2083126254  ,
+        2055013723  ,
+        2021950484  ,
+        1984016189  ,
+        1941302225  ,
+        1893911494  ,
+        1841958164  ,
+        1785567396  ,
+        1724875040  ,
+        1660027308  ,
+        1591180426  ,
+        1518500250  ,
+        1442161874  ,
+        1362349204  ,
+        1279254516  ,
+        1193077991  ,
+        1104027237  ,
+        1012316784  ,
+        918167572   ,
+        821806413   ,
+        723465451   ,
+        623381598   ,
+        521795963   ,
+        418953276   ,
+        315101295   ,
+        210490206   ,
+        105372028,
+        0
+
+};
+
+
+fract32 cm[64]=
+{
+        2147483647  ,
+        2147483016  ,
+        2147481121  ,
+        2147477963  ,
+        2147473542  ,
+        2147467857  ,
+        2147460908  ,
+        2147452697  ,
+        2147443222  ,
+        2147432484  ,
+        2147420483  ,
+        2147407218  ,
+        2147392690  ,
+        2147376899  ,
+        2147359845  ,
+        2147341527  ,
+        2147321946  ,
+        2147301102  ,
+        2147278995  ,
+        2147255625  ,
+        2147230991  ,
+        2147205094  ,
+        2147177934  ,
+        2147149511  ,
+        2147119825  ,
+        2147088876  ,
+        2147056664  ,
+        2147023188  ,
+        2146988450  ,
+        2146952448  ,
+        2146915184  ,
+        2146876656  ,
+        2146836866  ,
+        2146795813  ,
+        2146753497  ,
+        2146709917  ,
+        2146665076  ,
+        2146618971  ,
+        2146571603  ,
+        2146522973  ,
+        2146473080  ,
+        2146421924  ,
+        2146369505  ,
+        2146315824  ,
+        2146260881  ,
+        2146204674  ,
+        2146147205  ,
+        2146088474  ,
+        2146028480  ,
+        2145967224  ,
+        2145904705  ,
+        2145840924  ,
+        2145775880  ,
+        2145709574  ,
+        2145642006  ,
+        2145573176  ,
+        2145503083  ,
+        2145431729  ,
+        2145359112  ,
+        2145285233  ,
+        2145210092  ,
+        2145133690  ,
+        2145056025  ,
+        2144977098
+
+};
+
+fract32 sm[64]=
+{
+        0   ,
+        1647099 ,
+        3294197 ,
+        4941294 ,
+        6588387 ,
+        8235476 ,
+        9882561 ,
+        11529640    ,
+        13176712    ,
+        14823776    ,
+        16470832    ,
+        18117878    ,
+        19764913    ,
+        21411936    ,
+        23058947    ,
+        24705945    ,
+        26352928    ,
+        27999895    ,
+        29646846    ,
+        31293780    ,
+        32940695    ,
+        34587590    ,
+        36234466    ,
+        37881320    ,
+        39528151    ,
+        41174960    ,
+        42821744    ,
+        44468503    ,
+        46115236    ,
+        47761942    ,
+        49408620    ,
+        51055268    ,
+        52701887    ,
+        54348475    ,
+        55995030    ,
+        57641553    ,
+        59288042    ,
+        60934496    ,
+        62580914    ,
+        64227295    ,
+        65873638    ,
+        67519943    ,
+        69166208    ,
+        70812432    ,
+        72458615    ,
+        74104755    ,
+        75750851    ,
+        77396903    ,
+        79042909    ,
+        80688869    ,
+        82334782    ,
+        83980645    ,
+        85626460    ,
+        87272224    ,
+        88917937    ,
+        90563597    ,
+        92209205    ,
+        93854758    ,
+        95500255    ,
+        97145697    ,
+        98791081    ,
+        100436408   ,
+        102081675   ,
+        103726882
+
+};
+
+void fract32_sincos( fract32 angle, fract32 *c, fract32 *s )
+{
+    int index_h, index_m, index_l;
+    int minus, secondhalf;
+    fract32 cos, sin, sl,cl, temp;
+
+        // 角がちょうど -pi の時は特殊ケースとして扱う
+    if ( (int)angle == 0x80000000 )
+    {
+        *c = LONG_MIN;    // cos(-pi) = -1. 32bit for blackfin
+        *s = 0;             // sin( pi ) = 0;
+        return;
+    }
+
+        // 角が負の場合には正に直して計算する
+    if ( (int)angle < 0 )
+    {
+        angle = -angle;
+        minus = 1;
+    }
+    else
+        minus = 0;
+
+        // 角が第二象限の場合には第一象限に縮退させる
+    secondhalf =  angle > 0x3FFFFFFF;
+    angle &= 0x3FFFFFFF;
+
+    index_l = angle & 0x7FFFF;
+    index_m = ( angle >> 19 ) & 0x3F;
+    index_h = ( angle >> 25 ) & 0x1F;
+
+    cos =
+        sub_fr1x32(
+            mult_fr1x32x32(ch[index_h],cm[index_m]),
+            mult_fr1x32x32(ch[32-index_h],sm[index_m])
+        );
+    sin =
+        add_fr1x32(
+            mult_fr1x32x32(ch[32-index_h],cm[index_m]),
+            mult_fr1x32x32(ch[index_h],sm[index_m])
+        );
+
+    /*
+     * slは微小角index_lにpiをかけて得たsin(index_l)の近似値である
+     * 全周の1/2^13においては誤差は±0.5以下になる。
+     */
+    sl = mult_fr1x32x32(index_l*4,1686629713 );
+    /*
+     * slは微小角index_lにをもとにindex_l^2*612で求めたcos(index_l)の近似値である
+     */
+#if 1
+    cl = 0x7FFFFFFF - mult_fr1x32x32(
+            mult_fr1x32x32( index_l<<12, index_l<<12),
+            630
+            );
+
+    *c= sub_fr1x32(
+            mult_fr1x32x32(cos,cl),
+            mult_fr1x32x32(sin,sl)
+            );
+    *s = add_fr1x32(
+            mult_fr1x32x32(sin,cl),
+            mult_fr1x32x32(cos,sl)
+            );
+
+#else
+    cl = LONG_MIN + mult_fr1x32x32(
+            mult_fr1x32x32( index_l<<12, index_l<<12),
+            630
+            ); // 誤差を減らすため、あえて負の数を使っている
+
+    *c= -add_fr1x32(        // clの値が反転しているので、coscos-sinsinn を変形している。
+            mult_fr1x32x32(cos,cl),
+            mult_fr1x32x32(sin,sl)
+            );
+    *s = sub_fr1x32(        // clの値が反転しているので、sincos+cossinを変形している
+            mult_fr1x32x32(cos,sl),
+            mult_fr1x32x32(sin,cl)
+            );
+
+#endif
+    // 第二象限の場合には値の入れ替えを行う
+    if (secondhalf)
+    {
+        temp = *c;
+        *c = -*s;
+        *s = temp;
+    }
+        // 元々の角が負だった場合には、正弦の値を反転する(奇関数なので)
+    if ( minus )
+        *s = - *s;
+}
+
diff --git a/algorithm_nco/fract_x86.h b/algorithm_nco/fract_x86.h
new file mode 100644 (file)
index 0000000..c24fdc8
--- /dev/null
@@ -0,0 +1,15 @@
+/*
+ * Compatible typedef & macro to enable simulation in x86 env.
+ */
+
+
+typedef long long int fract32;
+
+#define SATP(x)  (((x)>2147483647LL)?(2147483647LL):x)
+#define SAT(x) (((x)>=-2147483648LL)?(SATP(x)):-2147483648LL)
+
+#define mult_fr1x32x32(x,y) SAT(((fract32)(x)*(fract32)(y)/*+0x40000000*/)>>31)
+
+#define sub_fr1x32(x,y) SAT((fract32)(x)-(y))
+
+#define add_fr1x32(x,y) SAT((fract32)(x)+(y))
diff --git a/algorithm_nco/main.c b/algorithm_nco/main.c
new file mode 100644 (file)
index 0000000..e948b84
--- /dev/null
@@ -0,0 +1,98 @@
+#include <stdio.h>
+#include <math.h>
+
+#ifdef __i386__
+#include "fract_x86.h"
+#else
+#include <fract.h>
+#endif
+#include <limits.h>
+
+extern void fract32_sincos( fract32 angle, fract32 *c, fract32 *s );
+#define NUM2P31 2147483648.0
+
+
+
+int main(void)
+{
+#if 1
+    fract32 c, s;
+    long long int i,j;
+    double theta;
+    double co, si;
+    double ecsum = 0, essum = 0;
+    double maxes = 0, mines = 0, maxec = 0, minec = 0;
+    long long int count = 0;
+
+#define STEP 1
+
+    for( j=LONG_MIN; j<LONG_MAX; j+=0x01000000 )
+    {
+        for ( i=0; i<0x01000000; i+=STEP)
+        {
+
+//            fract32_sincos( (i+j), &c, &s );
+            theta = (i+j)*M_PI/NUM2P31;
+#if 0
+            co = cos(theta)*NUM2P31 - c;
+            si = sin(theta)*NUM2P31 - s;
+#else
+            co = (cos(theta)-(float)cos(theta))*NUM2P31;
+            si = (sin(theta)-(float)sin(theta))*NUM2P31;
+#endif
+
+#if 0
+            ecsum += co;
+            essum += si;
+#else
+            ecsum += co*co;
+//            ecsum += (co+1.272776)*(co+1.272776);
+            essum += si*si;
+#endif
+            maxec = ( co > maxec )? co : maxec;
+            minec = ( co < minec )? co : minec;
+            maxes = ( si > maxes )? si : maxes;
+            mines = ( si < mines )? si : mines;
+            count++;
+
+        }
+#if 0
+        printf ( "%3d, theta=%f, cos=%lld:%f, sin=%lld:%f \n", (int)(j>>24), theta, c,cos(theta), s,sin(theta) );
+#endif
+#if 1
+        printf("%3d stddev cos err = %f, stddev sin err = %f,  \n",
+                (int)(j>>24),
+                sqrt(ecsum/count),
+                sqrt(essum/count)
+                );
+#else
+        printf("%3d ave cos err = %f, ave sin err = %f, max cos err : %f, min cos err : %f, max sin err : %f, min sin err : %f \n",
+                (int)(j>>24),
+                ecsum/count,
+                essum/count,
+                maxec,
+                minec,
+                maxes,
+                mines
+                );
+#endif
+    }
+
+#else
+
+    #define STEP 10103
+
+    fract32 c, s;
+    long long int i,j;
+    double theta;
+    double co, si;
+
+    for( j=LONG_MIN; j<LONG_MAX; j+=STEP )
+    {
+        fract32_sincos( j, &c, &s );
+        printf("%10d %10d\n", (int)c, (int)s);
+    }
+#endif
+       return 0;
+}
+
diff --git a/algorithm_nco/sincostable.ods b/algorithm_nco/sincostable.ods
new file mode 100644 (file)
index 0000000..04f2b70
Binary files /dev/null and b/algorithm_nco/sincostable.ods differ