Shop OBEX P1 Docs P2 Docs Learn Events
Qsin/Qcos examples? — Parallax Forums

Qsin/Qcos examples?

I wanted to try out some of the new Spin2 commands and was getting nowhere with qsin and qcos. Couldn't find any examples where someone has used them yet. I tried various versions of the following code but output returns 0. I think the problem is I'm not understanding the twopi value in the command.

PUB main() | x, y, length, angle, val, twopi
  angle := 45
  length := 15
  val := pi/180
  twopi := 2*pi
  x := qsin(length, angle, twopi)
  debug(udec(x))

This is the entry in the Spin2 manual:
QSIN(length, angle, twopi) : y
Rotate (length,0) by (angle / twopi) * 2Pi and return y. Use 0 for twopi = $1_0000_0000. Twopi is unsigned.
QCOS(length, angle, twopi) : x
Rotate (length,0) by (angle / twopi) * 2Pi and return x. Use 0 for twopi = $1_0000_0000. Twopi is unsigned.

Comments

  • RaymanRayman Posts: 14,632

    This is from @Wuerfel_21's VJET demo...

    PUB sin(angle) : s
                  'angle: 0..8192 = 360°
    
      return qsin($FFFF,angle,8192)
    
  • The parameters for QCOS and QSIN are a bit confusing:

    For "length" you want the value to be returned for cos(0). Basically this is a scaling factor -- all the "real" sin and cos values will be multiplied by this
    For "twopi" you want the angle that represents a full rotation. So for example if your angle is in degrees you would use "360" for twopi
    For "angle" use the angle as measured relative to twopi

    So for example to calculate when the input is a simple angle in degrees (no fractional parts) and with the result as a decimal with 3 decimal places use:

    length = 1000
    twopi = 360

    So qsin(1000, 45, 360) will return 707 (the actual sin value multiplied by 1000).

    If your angles are measured in tenths of a degree, use 3600 for twopi; if they're in radians multiplied by 100 use (314*2); and so on.

  • RaymanRayman Posts: 14,632

    Is the result signed? I'd assume so...

  • I've attached an example Spin2 file of using qsin() and qcos() and the DEBUG graphical Scope.

    It is a short program, I tried posting the code directly into this message, but unfortunately the tic-mark that Chip used to define the graphical DEBUG commands/routines is the same symbol that the forum's "code" format option uses, so the code won't display correctly. :-(

    Here is an example of the program's output:

    Hopefully this example will help you, I have some others too...

  • evanhevanh Posts: 15,910

    @ersmith said:
    So for example to calculate when the input is a simple angle in degrees (no fractional parts) and with the result as a decimal with 3 decimal places use:

    length = 1000
    twopi = 360

    So qsin(1000, 45, 360) will return 707 (the actual sin value multiplied by 1000).

    If your angles are measured in tenths of a degree, use 3600 for twopi; if they're in radians multiplied by 100 use (314*2); and so on.

    Thanks for that Eric. I had briefly looked at Chip's description and not got it either.

  • @"Francis Bauer" said:
    It is a short program, I tried posting the code directly into this message, but unfortunately the tic-mark that Chip used to define the graphical DEBUG commands/routines is the same symbol that the forum's "code" format option uses, so the code won't display correctly. :-(

    Works totally fine

    CON _clkfreq        = 100_000_000
    
    PUB go() | a, af, b, bf, c, d, e
    
      debug(`SCOPE MyScope SIZE 512 512)
      debug(`MyScope 'FreqA' -1000 1000 100 440 15 MAGENTA)
      debug(`MyScope 'FreqB' -1000 1000 100 330 15 ORANGE)
      debug(`MyScope 'FreqC' -1000 1000 100 220 15 BLUE)
      debug(`MyScope 'FreqD' -1000 1000 100 110 15 GREEN)
      debug(`MyScope 'FreqE' -1000 1000 100 0 15 YELLOW)
      debug(`MyScope TRIGGER 0 HOLDOFF 2)
    
      af~
      bf~
      repeat
        a := qsin(1000, af++, 200)      '  sine wave
        b := qcos(1000, bf++, 201)      'cosine wave
        c := (30 * b) / a           '     tan = sin/cos
        d := 10000 / a          'cosecant = 1/sin
        e := 10000 / b          '  secant = 1/cos
        debug(`MyScope `(a,b,c,d,e))
        waitms(1)
    
  • @Rayman said:
    This is from @Wuerfel_21's VJET demo...

    PUB sin(angle) : s
                  'angle: 0..8192 = 360°
    
      return qsin($FFFF,angle,8192)
    

    That ping didn't work BTW, always put a space after the username.

  • Thanks for all the replies. No wonder it didn’t work for me. Where is this information documented?

  • Francis BauerFrancis Bauer Posts: 364
    edited 2021-03-01 00:12

    @Wuerfel_21 said:

    @"Francis Bauer" said:
    It is a short program, I tried posting the code directly into this message, but unfortunately the tic-mark that Chip used to define the graphical DEBUG commands/routines is the same symbol that the forum's "code" format option uses, so the code won't display correctly. :-(

    Works totally fine

    CON _clkfreq        = 100_000_000
    
    PUB go() | a, af, b, bf, c, d, e
    
      debug(`SCOPE MyScope SIZE 512 512)
      debug(`MyScope 'FreqA' -1000 1000 100 440 15 MAGENTA)
      debug(`MyScope 'FreqB' -1000 1000 100 330 15 ORANGE)
      debug(`MyScope 'FreqC' -1000 1000 100 220 15 BLUE)
      debug(`MyScope 'FreqD' -1000 1000 100 110 15 GREEN)
      debug(`MyScope 'FreqE' -1000 1000 100 0 15 YELLOW)
      debug(`MyScope TRIGGER 0 HOLDOFF 2)
    
      af~
      bf~
      repeat
        a := qsin(1000, af++, 200)        '  sine wave
        b := qcos(1000, bf++, 201)        'cosine wave
        c := (30 * b) / a         '     tan = sin/cos
        d := 10000 / a            'cosecant = 1/sin
        e := 10000 / b            '  secant = 1/cos
        debug(`MyScope `(a,b,c,d,e))
        waitms(1)
    

    So what were the steps you used to post the code? I selected the 'code' entry in the pulldown and all it did was mangle the code when I put it in. I am using the latest version of Firefox when working with this forum.

  • cgraceycgracey Posts: 14,151

    I will improve the description. Sorry about this.

  • ersmithersmith Posts: 6,051
    edited 2021-03-01 00:20

    @Rayman said:
    Is the result signed? I'd assume so...

    Yes, the result is signed.

    @DiverBob said:
    Thanks for all the replies. No wonder it didn’t work for me. Where is this information documented?

    I had to figure it out by trial and error in order to implement QSIN and QCOS in flexspin, although it didn't take too long once I saw what was going on.

    It'd probably be a good idea to update the shared Spin document with more examples. Is that something you would be able to take on?
    EDIT: whoops, looks like Chip is on it already :). Thanks Chip.

  • @"Francis Bauer" said:

    @Wuerfel_21 said:

    @"Francis Bauer" said:
    It is a short program, I tried posting the code directly into this message, but unfortunately the tic-mark that Chip used to define the graphical DEBUG commands/routines is the same symbol that the forum's "code" format option uses, so the code won't display correctly. :-(

    Works totally fine

    CON _clkfreq        = 100_000_000
    
    PUB go() | a, af, b, bf, c, d, e
    
      debug(`SCOPE MyScope SIZE 512 512)
      debug(`MyScope 'FreqA' -1000 1000 100 440 15 MAGENTA)
      debug(`MyScope 'FreqB' -1000 1000 100 330 15 ORANGE)
      debug(`MyScope 'FreqC' -1000 1000 100 220 15 BLUE)
      debug(`MyScope 'FreqD' -1000 1000 100 110 15 GREEN)
      debug(`MyScope 'FreqE' -1000 1000 100 0 15 YELLOW)
      debug(`MyScope TRIGGER 0 HOLDOFF 2)
    
      af~
      bf~
      repeat
        a := qsin(1000, af++, 200)      '  sine wave
        b := qcos(1000, bf++, 201)      'cosine wave
        c := (30 * b) / a           '     tan = sin/cos
        d := 10000 / a          'cosecant = 1/sin
        e := 10000 / b          '  secant = 1/cos
        debug(`MyScope `(a,b,c,d,e))
        waitms(1)
    

    So what were the steps you used to post the code? I selected the 'code' entry in the pulldown and all it did was mangle the code when I put it in? I am using the latest version of Firefox when working with this forum.

    You need to triple-backtick it. The code block button defaults to single backticks, which are for inline code.

    Needs to look like this:

    ```
    PUB doStuff()
    send("Code goes here")
    send("Also why is the Spin highlighter still not in here?")
    ```

    which turns into

    PUB doStuff()
      send("Code goes here")
      send("Also why is the Spin highlighter still not in here?")
    
  • @Wuerfel_21 said:

    @"Francis Bauer" said:

    @Wuerfel_21 said:

    @"Francis Bauer" said:
    It is a short program, I tried posting the code directly into this message, but unfortunately the tic-mark that Chip used to define the graphical DEBUG commands/routines is the same symbol that the forum's "code" format option uses, so the code won't display correctly. :-(

    Works totally fine

    CON _clkfreq        = 100_000_000
    
    PUB go() | a, af, b, bf, c, d, e
    
      debug(`SCOPE MyScope SIZE 512 512)
      debug(`MyScope 'FreqA' -1000 1000 100 440 15 MAGENTA)
      debug(`MyScope 'FreqB' -1000 1000 100 330 15 ORANGE)
      debug(`MyScope 'FreqC' -1000 1000 100 220 15 BLUE)
      debug(`MyScope 'FreqD' -1000 1000 100 110 15 GREEN)
      debug(`MyScope 'FreqE' -1000 1000 100 0 15 YELLOW)
      debug(`MyScope TRIGGER 0 HOLDOFF 2)
    
      af~
      bf~
      repeat
        a := qsin(1000, af++, 200)        '  sine wave
        b := qcos(1000, bf++, 201)        'cosine wave
        c := (30 * b) / a         '     tan = sin/cos
        d := 10000 / a            'cosecant = 1/sin
        e := 10000 / b            '  secant = 1/cos
        debug(`MyScope `(a,b,c,d,e))
        waitms(1)
    

    So what were the steps you used to post the code? I selected the 'code' entry in the pulldown and all it did was mangle the code when I put it in? I am using the latest version of Firefox when working with this forum.

    You need to triple-backtick it. The code block button defaults to single backticks, which are for inline code.

    Needs to look like this:

    ```
    PUB doStuff()
    send("Code goes here")
    send("Also why is the Spin highlighter still not in here?")
    ```

    which turns into

    PUB doStuff()
      send("Code goes here")
      send("Also why is the Spin highlighter still not in here?")
    

    OK, Thank you. Live and learn...

  • @ersmith said:
    The parameters for QCOS and QSIN are a bit confusing:

    For "length" you want the value to be returned for cos(0). Basically this is a scaling factor -- all the "real" sin and cos values will be multiplied by this
    For "twopi" you want the angle that represents a full rotation. So for example if your angle is in degrees you would use "360" for twopi
    For "angle" use the angle as measured relative to twopi

    So for example to calculate when the input is a simple angle in degrees (no fractional parts) and with the result as a decimal with 3 decimal places use:

    length = 1000
    twopi = 360

    So qsin(1000, 45, 360) will return 707 (the actual sin value multiplied by 1000).

    If your angles are measured in tenths of a degree, use 3600 for twopi; if they're in radians multiplied by 100 use (314*2); and so on.

    That is a great explanation, ran your example so now time to play! May be time to get back to trying rotxy and polxy again....

  • Wuerfel_21Wuerfel_21 Posts: 5,050
    edited 2021-03-01 00:30

    @"Francis Bauer" said:
    OK, Thank you. Live and learn...

    ... Hanging on the edge of tomoooorrow!

    No?

    Ok I'll see myself out then.

Sign In or Register to comment.