Sine Wave - Phase Shift and Amplitude Measurement with MCU

Discussion in 'Embedded Systems and Microcontrollers' started by Rebentadevices, Sep 22, 2016.

  1. Rebentadevices

    Thread Starter New Member

    Sep 22, 2016
    6
    0
    Hello, i have two signals, sine waves, with known and same frequency (2 kHz) but different amplitudes and with a phase shift. In fact, I want to measure the phase shift between between these signals and amplitude of both using a MCU PIC18F28k22.

    My earlier approach was, first, to rectify each signal, sample with the MCU ADC, measure its amplitude(rms) and also convert each sine to a square wave and measure the time between the zero cross of each signal with a help of the external interrupt pin and a timer.

    But this technique bothers me, and it doesn't seem very smart, because on each measurement technique i'm just acquiring one parameter.

    Is there a better/simpler technique ?
     
  2. MrChips

    Moderator

    Oct 2, 2009
    12,449
    3,365
    Another method is to digitize the two sine waves. You need to over-sample, 10 to 100 times for best results.
    Then you do a least-squares fit to a sine wave. This will give you frequency, amplitude and phase information.
     
  3. Rebentadevices

    Thread Starter New Member

    Sep 22, 2016
    6
    0
    Can i do the least-squares fit with a PIC18, doesn't require matrix multiplication ?
     
  4. nsaspook

    AAC Fanatic!

    Aug 27, 2009
    2,912
    2,177
    Not smart? With a 8 bit MCU your approach of offloading what would normally be DSP functions to easily measured voltages and clock timing seems very smart if it provides the needed information.
     
  5. Sensacell

    Well-Known Member

    Jun 19, 2012
    1,132
    267
    Huh?

    Keep it simple!
    Sample the voltage with the ADC, measure the peak amplitude, find the zero-crossings by dividing the min and max values by 2.

    Use one of the internal timers to measure the periods, calculate the phase shift from the difference in zero crossings, relative to the period.

    No fancy math required.
     
  6. Rebentadevices

    Thread Starter New Member

    Sep 22, 2016
    6
    0
    I didn't fully get your approach, how can i find a zero-crossing by dividing the min and the max by two ?
     
  7. crutschow

    Expert

    Mar 14, 2008
    13,050
    3,244
    What resolution do you need for the phase-shift measurement?
     
  8. Rebentadevices

    Thread Starter New Member

    Sep 22, 2016
    6
    0
    Hello MrChips, i started searching about your hint on the least squares fit and i came across the IEEE Sine Fitting Algorithm (In Attachment ) but they indeed do matrix operations, do you know how can i apply this to a PIC18, is there another mathematical approach ?
     
    Last edited: Sep 22, 2016
  9. MrChips

    Moderator

    Oct 2, 2009
    12,449
    3,365
    I would upload the sampled data to a PC and do the sine fit in MATLAB.
     
  10. Rebentadevices

    Thread Starter New Member

    Sep 22, 2016
    6
    0
    My project has that limitation , i cannot do external processing.
     
  11. MrChips

    Moderator

    Oct 2, 2009
    12,449
    3,365
    As crutschow asks, what is your desired resolution on phase?
    You can do cross-correlation in the MCU.
    Or as Sensacell says, find the zero crossing. The phase is easy after that.
     
  12. Sensacell

    Well-Known Member

    Jun 19, 2012
    1,132
    267
    In real time, as the samples are coming in, latch and average the peaks of the sine wave.
    On the subsequent cycles, use the difference to establish a zero-crossing threshold, when the signal crosses the value, latch the timer value.

    Rinse and repeat... the difference is the period, and you got the peak amplitude as well.

    Calculate the phase by comparing the different capture times of the zero crossings.

    A meat-and-potatoes approach.
     
  13. Rebentadevices

    Thread Starter New Member

    Sep 22, 2016
    6
    0
    I was looking to a resolution of 0.1º.
    Can i do a cross-correlation when the two signals are not sampled at the same time?
     
  14. crutschow

    Expert

    Mar 14, 2008
    13,050
    3,244
    A resolution of 0.1º for a 2kHz signal requires a minimum sample rate of (2kHz * 3600) = 7.2 MS/s.
    The time resolution required is 1 / 7.2M = 139nS
    You may want to rethink your requirements. :rolleyes:
    Why do you want such a small resolution value?
     
    nsaspook likes this.
  15. upand_at_them

    Active Member

    May 15, 2010
    246
    29
    FYI, PIC-based TNC's do simple zero-crossing detection with a few discrete components and the signal triggering the PIC's interrupt.
     
Loading...