Shop OBEX P1 Docs P2 Docs Learn Events
Control Phase on NCO — Parallax Forums

Control Phase on NCO

MrPittsMrPitts Posts: 2
edited 2006-07-20 17:39 in Propeller 1
Hi all!

I'm wanting to find more information on controlling the phase of a sinewave using the Propeller NCO.· I have a need to·generate two sinewaves operating at identical·frequencies but·90 degs out of phase from one another.··Can someone provide me a code·snippet that demonstrates doing·this in SPIN, or a good explanation of using the phsa and frqa functions?

Thanks so much!

Ron
«1

Comments

  • El PaisaEl Paisa Posts: 375
    edited 2006-07-18 02:05
    Are the Sine and Cosine are not the same thing?
  • El PaisaEl Paisa Posts: 375
    edited 2006-07-18 02:23
    ·By definition:

    ·····························Cos(theta)=sqr(1-sin(theta)²)
  • BuddhaBuddha Posts: 25
    edited 2006-07-18 02:38
    cos(x) = sin(x + 90)

    Edit: the +90 assumes angles in degrees, of course. [noparse]:)[/noparse]
  • LarryLarry Posts: 212
    edited 2006-07-18 02:45
    Almost--

    Both functions are trigonometric cofunctions of each other, in that function of the complementary angle, which is the "cofunction," is equal to the other function:

    sin(x) = cos(90°-x) and
    cos(x) = sin(90°-x).

    www.math2.org/math/algebra/functions/sincos/overview.htm

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
  • pjvpjv Posts: 1,903
    edited 2006-07-18 03:05
    Hi Mr Pitts;

    Take a peek at my entry for the Parallax SX contest to see how to generate 2 same frequency (or different frequency for that matter) sine waves, separated by a phase angle of your chosing.

    As one experiment I summed the two (same freq) waves together to simulate varable amplitude...... the sum of two same freq sinewaves distanced by some phase, is itself a sine wave, the amplitude being a function of the phase difference.

    I know you asked about a Propeller, but similar concepts to my SX example can be applied.

    Hope it is of some interest.

    Cheers,

    Peter (pjv)
  • El PaisaEl Paisa Posts: 375
    edited 2006-07-18 12:13
    pjv said,
    ...... the sum of two same freq sinewaves distanced by some phase, is itself a sine wave, the amplitude being a function of the phase difference. said...
    I beg to disagreed.
    the sum of two identical sinewaves with a different phase between them is not itself a sine wave, is a complex waveform.
    Look the definition of fft.
  • SteveWSteveW Posts: 246
    edited 2006-07-18 12:18
    I'm with pjv - if they're both sinewaves of the same frequency and amplitude, the output is a sinewave, same frequency, different phase & amplitude.

    Steve
  • El PaisaEl Paisa Posts: 375
    edited 2006-07-18 13:44
    There are not.
    Look the definition of fft.
    If you plot in a piece of paper each sinewave with proper phase and added them up, you will see the result is not a sinewave.
  • Ym2413aYm2413a Posts: 630
    edited 2006-07-18 13:54
    El Paisa said...
    There are not.
    They are so.
    El Paisa, I think your confusing Harmonics and Phase.
  • El PaisaEl Paisa Posts: 375
    edited 2006-07-18 14:23
    A distorted sinewave and harmonics have the same meaning?
  • Ym2413aYm2413a Posts: 630
    edited 2006-07-18 14:40
    The wave would be distorted because it has harmonics.

    csunix1.lvc.edu/~snyder/1ch5.html
  • SteveWSteveW Posts: 246
    edited 2006-07-18 14:42
    There is confusion here. El Paisa, you might like to try adding 2 identical, differing only by phase sinewaves together, on a spreadsheet, graph paper, spectrum analyser, however you like. I suspect you will see no harmonics, no new frequencies, just a new sine wave of the original frequency, with a different phase.

    Adding harmonics, different frequencies, distortion all will include new frequencies. Adding 2 sine waves of the same frequency - no new frequencies.

    Steve
  • Paul BakerPaul Baker Posts: 6,351
    edited 2006-07-18 16:17
    Sorry El Paisa in this instance you are incorrect, you need different frequencies to arive at a complex waveform. Heres the notable points in a sine wave

    ····· 0
    90º··· 1
    180º· 0
    270º· -1
    360º· 0

    it is easy to see that adding two sine wave of same phase and frequency results in a sine wave of the same frequency with double the amplitude (1+1=2, -1+-1=-2). Likewize adding two sine waves of phase difference 180º results in a sine wave of 0 amplitude (1+-1=0). Adding a sine wave of phase difference 90º results in a sine wave of amplitude 1.5 whose peak is at 45º

    you can graph it here: http://www.coolmath.com/graphit/index.html

    · attachment.php?attachmentid=42520

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ...

    Post Edited (Paul Baker) : 7/18/2006 4:25:20 PM GMT
    666 x 405 - 55K
  • El PaisaEl Paisa Posts: 375
    edited 2006-07-18 16:44
    Paul Baker said
    it is easy to see that adding two sine wave of same phase and frequency said...
    That is not correct.
    I said adding two sinewaves with the same frequency and DIFFERENT phase between them does not produce a sinewave.
    It is a distorted sinewave with harmonic content.
  • SteveWSteveW Posts: 246
    edited 2006-07-18 16:55
    >I said adding two sinewaves with the same frequency and DIFFERENT phase between them does not produce a sinewave.
    >It is a distorted sinewave with harmonic content.

    You are wrong.
    It is an undistorted sinewave, with no harmonic content.
    Please, check your maths, one way or another, before continuing this.

    Steve
  • Paul BakerPaul Baker Posts: 6,351
    edited 2006-07-18 17:41
    El Paisa, look at the graph I showed you, it is sin(x) + cos(x), this is the same as sin(x) + sin(x + 90º),· these are two different phases of the same frequency. The result is a sine wave y=1.5*sin(x + 45º).

    Here is the graph of 1.5*sin(x + 45º):


    attachment.php?attachmentid=42527

    Now, compare that with the previous graph, they are identical.



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ...

    Post Edited (Paul Baker) : 7/18/2006 5:49:41 PM GMT
    662 x 364 - 43K
  • El PaisaEl Paisa Posts: 375
    edited 2006-07-18 19:06
    I have··to be sure·myself.
    I proved myself, see data from MCAD.
    case closed.



    ·
  • El PaisaEl Paisa Posts: 375
    edited 2006-07-18 19:14
    Oops,
    This is the MCAD file
  • Kaos KiddKaos Kidd Posts: 614
    edited 2006-07-18 19:58
    El Paisa: It's not close just yet... I'v my two bits worth, and I know you will read them, thus proving my point; the case is not closed.
    Paul, Steve have explaned and proven the issue at hand. I even went as far as to research and derived the same answer as Paul did.
    If you have any proof, you should post it here and now, saving any chance of 'grace' within the forums.
    ................................

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Just tossing my two bits worth into the bit bucket


    KK
    ·
  • SteveWSteveW Posts: 246
    edited 2006-07-18 20:36
    >This is the MCAD file

    Err... [noparse]:)[/noparse]

    However, even when you do get it to show up, lots of people (including me) can't read MCAD, so you may need to resort to real mathematics, too, or pictures.

    Steve
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2006-07-18 21:03
    Guys,

    Here's a trig identity that should lay this discussion to rest. It comes from Abramowitz & Stegun's Handbook of Mathematical Functions:

    ····sin(z1) + sin(z2) = 2 * sin((z1 + z2) / 2) * cos((z1 - z2) / 2)

    Substituting x + k for z1 and x for z2, we get:

    ····sin(x + k) + sin(x) = 2 * sin((2x + k) / 2) * cos(k / 2)

    Now, substituting

    ····a = 2 * cos(k / 2) (a constant), and
    ····n = k / 2 (another constant),

    we get

    ····sin(x + k) + sin(x) = a * sin(x + n)

    which is just another sine function of the same frequency, but with different phase and amplitude from the two original sine functions.

    Q.E.D.

    -Phil
  • rokickirokicki Posts: 1,000
    edited 2006-07-18 21:12
    You don't even need the trig. A simple thought experiment suffices.

    The sine function is the height of a point on a rotating wheel.

    A phase offset is just another point on that same wheel.

    The sum of the two (they are vectors from the origin, or center of
    the wheel) is yet another point on that same wheel, rotating at the
    same speed.

    Thus, guaranteed to be a sine wave, no harmonics, no distortion.
  • SteveWSteveW Posts: 246
    edited 2006-07-18 21:31
    However, none of this helps our original poster, who wanted to generate, using propeller NCOs, 2 sine waves 90 degrees out of phase. Any hints, chaps?

    (It's ridiculously hot in here in Cambridge, and I've cooled down with beer - wading through propeller documentation isn't really an option...)

    Steve
  • rokickirokicki Posts: 1,000
    edited 2006-07-18 21:41
    Well, my understanding is the NCO's generate square waves. It would take some pretty aggressive filtering to get
    acceptable sine waves from these. (And we won't even mention that the square waves are not nice pretty square
    waves with each phase always having the same length.)

    That said, you can write to the phase registers; shouldn't this be all that is needed? Just write 0x00000000 to one
    phase register, and 0x40000000 to the other, and you should get square waves 90 degrees out of sync.
  • SteveWSteveW Posts: 246
    edited 2006-07-18 22:11
    Ah, sounds like some LC filtering is called for if our poster is looking for fast stuff, or maybe DDS or other synthesis if slower. (Come to think of it, if he's after fixed frequency, then a single output and some judicious filtering / analogue cunning should get him both phases using only one pin...)

    Steve
  • pjvpjv Posts: 1,903
    edited 2006-07-19 00:14
    Hi All;

    I hadn't meant to cause such a furore with my suggestion......, and yes El Paisa, I did build this so I DO know that it generates sine waves.

    Two R/C DACs plus a few lines of code is all that is required to generate the two sine waves.

    The SX example in the contest shows the code, hence the concept, and that can be readily transported to the propeller. It would be a lot nicer than filtering the signal, especially if frequency variations are required.

    Dead simple, really.

    Cheers,

    Peter (pjv)
  • cgraceycgracey Posts: 14,133
    edited 2006-07-19 01:33
    Here is a program that generates dual sine waves of the same frequency with a variable phase offset. It plays on pins 10 and 11 using the CTRs in DUTY mode. It updates its samples at 250KHz, so it should be usefull to 100KHz, or so. The mouse is used to alter frequency (in Hz) and phase (in degrees). It starts out at 1KHz with 0-degree offset. It runs nicely on the Propeller Demo Board. You can hook your scope up to the stereo integrators on the demo board and see the waveforms. Moving the mouse left and right lowers and raises the frequency. Moving it up and down shifts the phase offset. One could set their scope to A+B mode and observe the sum of the two waves while altering the phase shift. X-Y mode might be interesting, too. I hope you guys try this out. I spent some time this afternoon getting this together.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


    Chip Gracey
    Parallax, Inc.
  • El PaisaEl Paisa Posts: 375
    edited 2006-07-19 02:16
    Sorry guys.

    You are write, the waveform is sinewave with no distortion but a different frequency.
    The new frequency is function of the phase angle.

    Apparently the attachment manager does not like *.bmp files.
    This is a screen captured (*.jpg format) of the mcad file.
    1280 x 1024 - 159K
  • El PaisaEl Paisa Posts: 375
    edited 2006-07-19 03:09
    Sorry again.
    I meant to say the sinewave sum amplitude (not frequency) changes as function of the phase between the two sinewaves.
  • Beau SchwabeBeau Schwabe Posts: 6,559
    edited 2006-07-19 03:15
    El Paisa,

    As long as BOTH sinewaves have the same frequency, then the product is also going to be the same frequency.


    If you have QuickBasic4.5 or QBASIC then you can run the program below to illustrate this. ...Or you can play the
    attached WMV file. The "GREEN" wave is fixed Zero phase, while the "RED" wave changes phase from 0 to 360. The
    "YELLOW" wave is the sum of both waves.

    SCREEN 12
    
    Pi# = ATN(1) * 4
    
    x = 640
    y = 480
    
    Size = 100
    
    Periods = 5
    
    WHILE 1
    
          FOR PHASE = 0 TO 350 STEP 10
              CLS
              FOR DEG = 0 TO 360 * Periods STEP 1
       
                  RAD1# = (DEG / 180) * Pi#
                  RAD2# = ((DEG - PHASE) / 180) * Pi#
    
                  Y1 = y / 2 + SIN(RAD1#) * Size
                  Y2 = y / 2 + SIN(RAD2#) * Size
                  Y3 = (Y1 + Y2) - y / 2
    
                  XX = (DEG / (360 * Periods)) * x
           
                  PSET (XX, Y1), 10
                  PSET (XX, Y2), 12
                  PSET (XX, Y3), 14
    
              NEXT DEG
    
          NEXT PHASE
    WEND
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
Sign In or Register to comment.