Sine Wave - Phase Shift and Amplitude Measurement with MCU

Thread Starter

Rebentadevices

Joined Sep 22, 2016
13
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 ?
 

MrChips

Joined Oct 2, 2009
30,714
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.
 

nsaspook

Joined Aug 27, 2009
13,086
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.
 

Sensacell

Joined Jun 19, 2012
3,432
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.
 

Thread Starter

Rebentadevices

Joined Sep 22, 2016
13
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.
I didn't fully get your approach, how can i find a zero-crossing by dividing the min and the max by two ?
 

Thread Starter

Rebentadevices

Joined Sep 22, 2016
13
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.
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 ?
 

Attachments

Last edited:

MrChips

Joined Oct 2, 2009
30,714
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.
 

Sensacell

Joined Jun 19, 2012
3,432
I didn't fully get your approach, how can i find a zero-crossing by dividing the min and the max by two ?
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.
 

Thread Starter

Rebentadevices

Joined Sep 22, 2016
13
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.
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?
 

crutschow

Joined Mar 14, 2008
34,285
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?
 
Top