Pages: [1]   Go Down
Author Topic: Sin Not Working On Due ?  (Read 864 times)
0 Members and 1 Guest are viewing this topic.
Dubai, UAE
Offline Offline
Edison Member
*
Karma: 21
Posts: 1670
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
// 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
Logged


SF Bay Area (USA)
Online Online
Tesla Member
***
Karma: 106
Posts: 6367
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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);
Logged

Dubai, UAE
Offline Offline
Edison Member
*
Karma: 21
Posts: 1670
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

And the answer is .....

'Thank you westfw'

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

Works, thanks

Duane B

rcarduino.blogspot.com
Logged


SF Bay Area (USA)
Online Online
Tesla Member
***
Karma: 106
Posts: 6367
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

"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!
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 331
Posts: 16470
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

"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
Logged

Dubai, UAE
Offline Offline
Edison Member
*
Karma: 21
Posts: 1670
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged


Pages: [1]   Go Up
Jump to: