INFO - FAQ - CC2-Forum - CCPro-Forum |
|
Hallo zusammen, ich habe verschiedene Alogrithmen ausprobiert, um den Sinus / Cosinus zu berechnen, z.B. mit Taylorreihen und dem bekannten Cordic Algorithmus. Das funktioniert ist aber extrem langsam insbesondere wenn man eine ausreichende Genauigkeit anstrebt. Die Lösung, was Basic angeht liegt in einer Nachschlagetabelle wie auch von Dietmar vorgeschlagen. Den Code hierzu hänge ich hier an. Die Berechnung eines Sinuswertes benötigt etwa 20ms. ' looktab für sinus, cosinus ' 157 Werte für sinus von 0 bis PI/2 = 1.57 ' die anderen Quadranten ergeben sich daraus und werden ' berechnet ' Skalierug Amplitude = 1000, input skalierung = 100 ' sinus (0.5) = (looktab sinus, 0.5*100 , value) / 1000 ' Wertebereich 0-2*PI=6.28 ' Fehler max. 0.3% ' Cosinus wird aus dem Sinus berechnet denn COS(a) = SIN(PI/2-a) ' Der Tangens kann natürlich auch aus dem Sin/Cos berechnet werden option ccbas 'include "orgcc.def" DEFINE value,rad,i,quadrant WORD 'Beispiel Ausgabe von Sinus im Bereich 0-2*PI FOR i = 0 to 628 PRINT "sinus["; i; " ]=" ; sin(i) next i 'Beispiel Ausgabe von Sinus im Bereich 0-2*PI FOR i = 0 to 628 PRINT "cosinus["; i; " ]=" ; cos(i) next i FUNCTION cos(rad) if rad >157 and rad <=314 then rad = 157 - (rad - 157) quadrant = -1 else if rad >314 and rad <=471 then rad = rad - 314 quadrant = -1 else if rad >471 and rad <=628 then rad = 157 - (rad - 471) quadrant = 1 else quadrant = 1 end if rad = 157-rad looktab sinus, rad, value RETURN value * quadrant FUNCTION sin(rad) if rad >157 and rad <=314 then rad = 157 - (rad - 157) quadrant = 1 else if rad >314 and rad <=471 then rad = rad - 314 quadrant = -1 else if rad >471 and rad <=628 then rad = 157 - (rad - 471) quadrant = -1 else quadrant = 1 end if looktab sinus, rad, value RETURN value * quadrant END TABlE sinus 0 9 19 29 39 49 59 69 79 89 99 109 119 129 139 149 159 169 179 188 198 208 218 227 237 247 257 266 276 285 295 305 314 324 333 342 352 361 370 380 389 398 407 416 425 434 443 452 461 470 479 488 496 505 514 522 531 539 548 556 564 572 581 589 597 605 613 620 628 636 644 651 659 666 674 681 688 696 703 710 717 724 731 737 744 751 757 764 770 777 783 789 795 801 807 813 819 824 830 836 841 846 852 857 862 867 872 877 881 886 891 895 900 904 908 912 916 920 924 928 932 935 939 942 945 948 952 955 958 960 963 966 968 971 973 975 977 979 981 983 985 987 988 990 991 992 993 994 995 996 997 998 998 999 999 999 999 1000 end table Gruss Ralf |
Antwort schreiben |