Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unorthodox sine generation in two lines of code without trigonometry and without lookup table... #2

Open
rin67630 opened this issue May 28, 2021 · 0 comments

Comments

@rin67630
Copy link

Just an information:
you can issue a pair of sinus-cosinus signals very easily without any complex math and look up table.

Try that:

// Digital sinus generator for Arduino Due.
int8_t x ; 
int8_t y ;
void setup() {
//pinMode(5,OUTPUT);
//pinMode(6,OUTPUT);
y= 114; // put less to change amplitude but not less than 64, else it will deteriorate the sine quality
        // 114 gives 2v PP and 0.70V RMS which is 10dB
//Serial.begin (115200);        // to test show on serial
}
void loop() {
x += (y / 4);
y -= (x / 4);           // Divisor value: according to table below:
//printf ("%i  %i \n", x , y ); // to test show on serial
delayMicroseconds(2);  // Delay value according to table below:
analogWrite(DAC1, y + 127);
}
/*
Frequ    divisor delay   divisor delay
3 Khz     8       0       n/a    n/a
2 Khz     8       4       n/a    n/a
1 Khz     8       13      16     4 (0,945 Khz), 3(1.054 KHz) right value would have been between.
500Hz     8       33      16     13
250Hz     8       71      16     30
125Hz     8       149     16     68
63Hz      8       299     16     140
32Hz      8       599     16     282
Frequ    divisor delay   divisor delay  divisor delay 
50 Hz     8       378     16     179     32      76
5Khz      4       2       (fastest run at a round frequency)
*/

This is a simple demonstration example using delays and 8 bit integer math just to show how fast this unorthodox sine generation can be.
Getting 50Hz in a PLL should be easy to be done by using 16bit values and PLLing the divisor and replacing the delay with a periodic processing.

Why does these two lines issue a sinus without trigonometry?
Pure magic?
Not really:
x += (y / 4); //is in a loop the expression of: "x is the integral of y"
y -= (x / 4); //is in a loop the expression of: "-y is the integral of x"
The only solution of this pair of equations is x=cos (y) and -y=sin(x)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant