Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 4 of 4

Thread: Slow sinewave code

  1. #1
    Senior Member
    Join Date
    Oct 2013
    Location
    Rogersville MO
    Posts
    253

    Slow sinewave code

    Any thought on why this is so slow? Can only get 514 Hz out of it with the .1 phase inc.

    #define ARM_MATH_CM4
    #include <arm_math.h>

    float phase = 0.0;
    float twopi = 3.14159 * 2;
    float val = 0.0;

    //elapsedMicros usec = 0;

    void setup() {
    analogWriteResolution(12);
    }

    void loop() {
    val= arm_sin_f32(phase)*2000.0+2050.0;
    analogWrite(A14, (int) val);
    phase = phase + 0.1;
    if (phase >= twopi) phase = 0.0;

  2. #2
    Senior Member
    Join Date
    Oct 2013
    Location
    Rogersville MO
    Posts
    253
    19.7 Khz with some jitter


    #define ARM_MATH_CM4
    #include <arm_math.h>

    float phase = 0.0;
    float twopi = 3.14159 * 2;
    float val = 0.0;
    float inc = 0.0;
    int valtable[65];
    int i=0;

    //elapsedMicros usec = 0;

    void setup() {
    analogWriteResolution(12);
    for(inc=0.0; inc<=twopi; inc=inc+.1){
    valtable[i]=(int)(arm_sin_f32(inc)*2000.0+2050.0);
    i++;
    }
    }
    void loop() {
    for(i=0; i<62; i++) {
    analogWrite(A14, valtable[i]);
    }


    }

  3. #3
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,419
    I don't know how fast the DAC is but 62 samples per cycle and 19.7kHz means that you are writing just over 1 million samples per second. That might be the limit of the DAC?

    The jitter is because you write one cycle and then exit from the loop function. Try this:
    Code:
    void loop() {
      while(1) {
        analogWrite(A14, valtable[i++]);
        if(i >= 62)i = 0;
      }
    }
    Pete

  4. #4
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    22,504
    The DAC's typical full-scale settling time is 15 us, according to the datasheet. That's approx 33 kHz full-scale bandwidth. The worst case (over temperature, supply voltage, etc) is about half that speed. It very likely might be able to reproduce higher frequency signals at a reduced signal amplitude. There's no specs in the datasheet regarding small signal bandwidth vs slew rate limiting.

    Of course, you can write to the DAC at extreme speed. Just know the actual analog hardware is speed limited to audio band or slightly ultrasonic range. At least the good news is it will (probably) act like a nice low-pass filter, rather than horrible aliasing as if you'd decimated the data rate is software without properly low pass filtering.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •