# Sine Wave - Phase Shift and Amplitude Measurement with MCU

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
27,137
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.

Joined Sep 22, 2016
13
Can i do the least-squares fit with a PIC18, doesn't require matrix multiplication ?

#### nsaspook

Joined Aug 27, 2009
10,426
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,066
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.

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 ?

#### crutschow

Joined Mar 14, 2008
30,801
What resolution do you need for the phase-shift measurement?

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

• 606.9 KB Views: 11
Last edited:

#### MrChips

Joined Oct 2, 2009
27,137
I would upload the sampled data to a PC and do the sine fit in MATLAB.

Joined Sep 22, 2016
13
My project has that limitation , i cannot do external processing.

#### MrChips

Joined Oct 2, 2009
27,137
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,066
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.

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
30,801
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.
Why do you want such a small resolution value?

#### upand_at_them

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