Shop OBEX P1 Docs P2 Docs Learn Events
simple explanation please — Parallax Forums

simple explanation please

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.

I was looking at this code so as to attempt to out put three different pulsed sounds that increase and decrease in loudness. Need three different frequencies. Found this code but do not understand the twopi. Can any one reference me to some simple explanations please.
Thank you in advance.
Plus some relatively simple example code with good comments.
Thanks

Comments

  • cgraceycgracey Posts: 14,151

    Twopi is just the value that represents an entire circumference. If it is 4, then angles 0..3 would be 0, 90, 180, and 270 degrees.

  • JRoarkJRoark Posts: 1,215
    edited 2022-11-17 23:37

    @cgracey said:
    Twopi is just the value that represents an entire circumference. If it is 4, then angles 0..3 would be 0, 90, 180, and 270 degrees.

    Chip: Its actually a (badly named) function in this case instead of a value (per that PDF). The twopi function uses two sins 90 degrees out of phase in cartesian space to create a circle upon which the graph data is imposed. I think.

  • cgraceycgracey Posts: 14,151

    Oh, okay. I was just thinking about the QSIN and QCOS methods in Spin2, and their third argument called 'twopi'. I hadn't even looked at the pdf because I didn't think it was necessary.

  • AribaAriba Posts: 2,690

    Here is an example in Spin2 to generate an audio sine frequency:
    https://forums.parallax.com/discussion/comment/1496551/#Comment_1496551

    You can do this 3 times in the loop to generate 3 frequencies. To modulate the amplitude make the $7FFF a variable and change it from 0 to $7FFF and back to 0 to increase / decrease the loudness.

    Andy

  • AribaAriba Posts: 2,690

    The example modified for two frequencies and the amplitude modulated by a triangle waveform:

    CON
      _clkfreq = 180_000_000
    
      smprate  = 44100
      sinfreq1 = 440
      sinfreq2 = 734
      dacpin1  = 36
      dacpin2  = 37
      lforate  = 3
    
    VAR
      long  vol, lfo
    
    pub sine_out() | smp, phs1, phs2
      pinstart(dacpin1, P_DAC_DITHER_RND | P_DAC_600R_2V | P_OE, clkfreq/smprate, 0)  'smartpin DAC 16bits
      pinstart(dacpin2, P_DAC_DITHER_RND | P_DAC_600R_2V | P_OE, clkfreq/smprate, 0)
      repeat
         phs1 += sinfreq1 FRAC smprate             'rotate phase by one step angle for sine freq
         _,smp := polxy(vol, phs1)                 'cordic sine with rotating phase and modulated volume
         wypin(dacpin1, smp+$8000)                 'write sine sample to dac
    
         phs2 += sinfreq2 FRAC smprate
         _,smp := polxy(vol, phs2)                 'the same for the second frequency
         wypin(dacpin2, smp+$8000)
    
         repeat until pinread(dacpin1)             'wait for dac sample period
    
         lfo := (lfo + lforate) & $FFFF            'low freq. Oscillator (sawtooth)
         vol := abs(lfo - $8000)                   'saw to triangle = up/down ramp
    

    Andy

  • Just for clarity: the "twopi" variable mentioned in the QSIN and QCOS documentation has nothing at all to do with the "twopi" function described in your linked PDF file. They just happen to share a name.

  • pilot0315pilot0315 Posts: 910
    edited 2022-11-18 17:28

    Thanks to all.
    @ersmith
    Thanks for the explanation.
    @Ariba
    I will try your code.
    Thanks

Sign In or Register to comment.