Friday, December 13, 2024

Phase Difference calculation

Dear GNU Radio Community,

I want to extract the consistent and stable phase difference in degrees between the two signals, each of 30 Hz, from a demodulated signal from a VOR Signal RX through USRP N210. I am applying a Python block for this purpose and checking the Phase difference in degrees by giving its output to QT GUI Number sink, but I am not getting desired results, the values are continuously fluctuating, instead of the time domain, I am getting the accurate and stable two 30 Hz waves. 

Code using:
import numpy as np
from gnuradio import gr

class blk(gr.sync_block):  
    def __init__(self):  
        gr.sync_block.__init__(
            self,
            name="Phase Difference via IQ Demodulation",
            in_sig=[np.complex64, np.complex64],  # Two complex input signals
            out_sig=[np.float32]                  # Phase difference in degrees
        )

    def work(self, input_items, output_items):
        # Input complex signals
        signal1 = input_items[0]
        signal2 = input_items[1]
        # Ensure both signals have the same length
        if len(signal1) != len(signal2):
            raise ValueError("Input signals must have the same length.")
        # Demodulate signal1 to get I1 and Q1
        I1 = np.real(signal1)  # In-phase component of signal1
        Q1 = np.imag(signal1)  # Quadrature component of signal1
        # Demodulate signal2 to get I2 and Q2
        I2 = np.real(signal2)  # In-phase component of signal2
        Q2 = np.imag(signal2)  # Quadrature component of signal2
        # Calculate the phase for each signal
        phase1 = np.arctan2(Q1, I1)  # Phase of signal1 in radians
        phase2 = np.arctan2(Q2, I2)  # Phase of signal2 in radians
        # Compute the phase difference
        phase_diff_rad = phase1 - phase2
        # Normalize phase difference to the range [-π, π]
        phase_diff_rad = np.mod(phase_diff_rad + np.pi, 2 * np.pi) - np.pi
        # Convert phase difference to degrees
        phase_diff_deg = np.degrees(phase_diff_rad)
        # Output the phase difference
        output_items[0][:] = phase_diff_deg
        return len(output_items[0])

It could be possible to get stable/accurate values using IQ Demodulation technique or any other robust method? Kindly help, where I'm making mistakes.

Best regards
Muhammad Anas

No comments:

Post a Comment