Go Down

Topic: Sin Not Working On Due ? (Read 1 time) previous topic - next topic

DuaneB

Hi,
   Running the following code on Due does not produce the expected sinewave table, instead I get a lot of 0's and one 4096.

   Running the same code in processing with minimal adjustments for the different environment correctly draws a sinewave.

   Am I missing something or does the sin function have a problem on the Due ?

   Code -
Code: [Select]

// Create a table to hold the phase increments we need to generate midi note frequencies at our 44.1Khz sample rate
#define WAVE_SAMPLES 600
uint32_t nSineSamples[WAVE_SAMPLES];

void createSineTable()
{
  for(uint32_t nIndex = 0;nIndex < WAVE_SAMPLES;nIndex++)
  {
    // tried PI, also tried 22/7
    // 600 is the sample rate, 4096 is 1 in 12bit fixed point we want for DAC output
    // code needs to be rewritten to account for -1 to 1 output range of sine function
    // incorrectly assumes 0-1 at the moment, but sin function does not seem to be generating anything
    // at the moment
    nSineSamples[nIndex] = (uint32_t)sin(((2.0*(22.0/7.0))/600.0) * (float)nIndex)*4096.0;
    Serial.println(nSineSamples[nIndex]);
    Serial.println(sin((float)nIndex)*1000);
  }
}


Duane B

rcarduino.blogspot.com
Read this
http://rcarduino.blogspot.com/2012/04/servo-problems-with-arduino-part-1.html
then watch this
http://rcarduino.blogspot.com/2012/04/servo-problems-part-2-demonstration.html

Rcarduino.blogspot.com

westfw

Quote
nSineSamples[nIndex] = (uint32_t)sin(((2.0*(22.0/7.0))/600.0) * (float)nIndex)*4096.0;

Order of operations?  (uint32_t)sin(x) will be almost always zero.
Try an extra set of parens:

(uint32_t)  (sin(((2.0*(22.0/7.0))/600.0) * (float)nIndex)*4096.0);

DuaneB

And the answer is .....

'Thank you westfw'

Code: [Select]
(uint32_t)  (sin(((2.0*(22.0/7.0))/600.0) * (float)nIndex)*4096.0);

Works, thanks

Duane B

rcarduino.blogspot.com
Read this
http://rcarduino.blogspot.com/2012/04/servo-problems-with-arduino-part-1.html
then watch this
http://rcarduino.blogspot.com/2012/04/servo-problems-part-2-demonstration.html

Rcarduino.blogspot.com

westfw

"casting to a different type" is not one of the "mathematical operators" they teach you in grade school.  Nor are the logic operators (&&, ||, ==, etc) or bitwise operators (&, |, ^)
I've been bitten.  When in the SLIGHTEST doubt, use extra parenthesis!

retrolefty


"casting to a different type" is not one of the "mathematical operators" they teach you in grade school.  Nor are the logic operators (&&, ||, ==, etc) or bitwise operators (&, |, ^)
I've been bitten. 
Quote
When in the SLIGHTEST doubt, use extra parenthesis!




Sounds like the hardware equivalence of:

Quote
When in doubt add more bypass caps to it!


Lefty

DuaneB

It was not the only bug in my code, for anyone that stumbles across this in the future, a working sketch is here -

http://rcarduino.blogspot.com/2012/12/arduino-due-dds-part-1-sinewaves-and.html

Its a cross post, the same link is in the DAC post, but as the link contains a working version of the code and is relevant to the title I assume the admins will accept.

As in the DAC Post, the Due is really cruising, the 32bit operations and fast clock mean that it can crank out DDS in its sleep - Its too easy, I am going back to 8bits just for the challenge.

Duane B

rcarduino.blogspot.com
Read this
http://rcarduino.blogspot.com/2012/04/servo-problems-with-arduino-part-1.html
then watch this
http://rcarduino.blogspot.com/2012/04/servo-problems-part-2-demonstration.html

Rcarduino.blogspot.com

Go Up