I’m trying to process two digital signals with a micro. One of the things I want to do is to calculate the phase difference of both signals. I have tried multiple methods to no avail.
I have tried three methods to attempt to calculate the phase. The first one is to multiply both signals and extract the phase from it. I also tried peak detection and zero cross detection.
I had no luck so I thought there may be some issue with my acquisition system, so I decided to make sure by generating two signals directly on code and then making them go through my algorithms also to no avail.
int calculate_phase(float *buff_1, float *buff_2, float* phase, float mag_1, float mag_2, int decimation, float freq){
*phase = 0;
int peak_1 = 0;
int peak_2 = 0;
for (int i = 0; i<buff_size;++i){
// sin(2*pi*f*T_s*i)
buff_1[i] = 1*sin(2*M_PI*freq/125000000.0*decimation*i);
buff_2[i] = 1*sin(2*M_PI*freq/125000000.0*decimation*i+M_PI/4); //45 degree differential
}
// Signal Multiplication
/*
for (int i = 0;i<buff_size;++i){
buff_1[i] = buff_1[i]*buff_2[i]/mag_1/mag_2;
}
for (int i = 0;i<buff_size;++i){
*phase = *phase+ buff_1[i];
}
*phase = *phase/buff_size;
*phase = acos(*phase);
*phase = *phase/M_PI*360.0;*/
/*
// Peak detection
for (int i = 0; i< buff_size; ++i){
if(buff_1[i] == mag_1){
peak_1 = i;
break;
}
}
for (int i = 0; i< buff_size; ++i){
if(buff_2[i] == mag_2){
peak_2 = i;
break;
}
}
*phase = (float)(peak_1-peak_2)*decimation/125000000.0*360.0*freq;
*/
// 0 Cross detection
for (int i = 0; i< buff_size; ++i){
if(buff_1[i] > 0.0 && buff_1[i-1]< 0.0){
peak_1 = i;
break;
}
}
for (int i = 0; i< buff_size; ++i){
if(buff_2[i] > 0.0 && buff_2[i-1]< 0.0){
peak_2 = i;
break;
}
}
*phase = (float)(peak_1-peak_2)*decimation/125000000.0*360.0*freq;
printf("%fn",*phase);
}
This is my function. now. decimation is used to calculate the sampling frequency (which is 125MEG/decimation)
and freq is received as an input when calling the program. the magnitudes are also calculated but for the time being I am giving it a value of 1
However, when testing the 0 crossing method the computer returns 90 degrees of difference. When using the peak detection method the return is 0 degrees (I assume because due to discretization the value of the sine is never 1) and the product method returns a degree of 138.
Any help is appreciated. I think the 0 cross detection is the most promising of the three methods. Nevertheless, I cannot find the problem
Any help is appreciate it. If you have any further questions about the program do not hesitate to ask