I have a function that can find the phase difference between two signals, this function uses Hilbert transform to do so. Here is the python code for it

def phase_shift(carr_wave, rec_wave):              
     assert len(carr_wave)==len(rec_wave)
     carr_comp = hilbert(carr_wave)
     rec_comp = hilbert(rec_wave)
     c = np.inner( carr_comp, np.conjugate(rec_comp) ) / math.sqrt( np.inner(carr_comp,np.conjugate(carr_comp)) * np.inner(rec_comp,np.conjugate(rec_comp)) )
     phase_diff = np.angle(c)
     phase_diff = abs(phase_diff)
     return phase_diff

I am inserting a sample code of how this works and how many samples it need to be near accurate

f = 20000
offset = 0.25
samples_in_bit = 55
t = np.linspace(0,1,44000)
signal_1 = np.sin(2*math.pi*f*t)
signal_2 = np.sin(2*math.pi*f*t + offset)
phase_difference_1 = phase_shift(signal_1,signal_2)
phase_difference_2 = phase_shift(signal_1[:55],signal_2[:55])
print phase_difference_1,phase_difference_2

output :
0.249981325271 0.249951020276

I am doing BPSK at a frequency $= 20000\textrm{ Hz}$, duration of one bit $= 0.00125\textrm{ sec}$, with a frame rate of $44000$ per second. I am modulating 1800 bits which are all zeros.

Now I plot the phase difference of this modulated wave and actual carrier wave (Both will be same as I am sending all zeros) with perfect synchronization of both waves, the plot looks like this, which is obvious.

NOTE: I am finding phase difference at each bit (from start to end sample in one bit duration) and plotting against bit number on $x$-axis and phase on $y$-axis.

phase plot of perfect synced carrier and modulated waves

Now let's say if there is some $20$-$30$ samples of miss synchronization between carrier and modulated wave there will be a an offset phase where the plot looks like this. Which is okay that its phase difference is constant through out all bits.

plot where waves are not synced perfectly

After modulation, when I transmit the sound and record it (Using a good mic). I found that the phase difference plot between the transmitted wave and the recorded wave looks like this,almost a periodic function.

enter image description here

And this one is the same plot, but I have plotted absolute of phase differences.

enter image description here

To exactly know the correct sync between recorded and transmitted waves I am using cross correlation techniques which give a correct synchronization correct to $30$ frames(samples). So even if synchronization instant is wrong by $30$ frames (I am having $55$ frames in on bit duration) I should at least get a constant phase plot just like it did on my modulated wave (before transmitting).

  • My question is why is the phase difference between transmitted and recorded waves constantly changing ? And that too it is not a random change. It is kind of periodic.

  • Does this normally happen in all signal communications? or is there any problem in my transmission?

  • Or can I do any filtering after I receive the signal?


As the problem for this is found to be frequency offset between sent and recorded waves.

  • Is there a method or an idea to estimate the frequency from the recorded wave, which can be used to find the phase using Hilbert function.?

I am including the frequency spectrum of my sent and recorded wave as shown below. enter image description hereenter image description here enter image description here

  • 2
    $\begingroup$ The "periodic phase" only occurs because of the principal value of the phase, i.e., the phase value is always mapped to the interval $[-\pi,\pi]$ (by adding/subtracting multiples of $2\pi$). So you actually have a linearly increasing phase difference, which corresponds to a fixed frequency offset between the two waves. So you need to figure out where this frequency offset is coming from. $\endgroup$
    – Matt L.
    Jul 6, 2016 at 14:29
  • $\begingroup$ Yes, the jumping in phase is due to phase wrapping (ambiguity of the tangens). Look at the numpy "unwrap" function to solve that. Also, the phase does not change linear - If I had to take a guess, I'd say your modulator only advances the phase, but does not actually key it. $\endgroup$ Jul 6, 2016 at 14:41
  • $\begingroup$ @JanKrüger The OP says their data is all zero bits, so I wouldn't expect any keying at all with standard BPSK. $\endgroup$
    – Peter K.
    Jul 6, 2016 at 17:03
  • 1
    $\begingroup$ @PeterK. As far as I understood, everything is okay with sending all zeros. The problem occurs after modulating data (music) and sending it over the link: "After modulation, when I transmit the sound and record it (Using a good mic). I found that the phase difference plot between the transmitted wave and the recorded wave looks like this,almost a periodic function." $\endgroup$ Jul 6, 2016 at 17:18
  • $\begingroup$ @JanKrüger Ok! Yes, that seems to be the case. My mistake. $\endgroup$
    – Peter K.
    Jul 6, 2016 at 17:26

1 Answer 1


As Matt L said, This is due to the frequency offset between the sent (same as carrier wave as all zeros have been sent) and recorded wave. Which makes the phase difference to keep on increase with time. But as the principal values of phase lie in [-pi,pi] that we get from Hilbert function, we see this to be a periodically varying phase.

To prove that an error in frequency between sent and recorded waves does this, i have made a test on two signals with same phase but with different frequency ( an error of 0.01% ) and plotted the phase difference using Hilbert.

Python Code snippet for the test.

By increasing this frequency error( delta_f ) the periodicity of curve increases

freq = 20000
delta_f = 2                     # frquency error of 0.01%
t = np.linspace(0,1,44000)      #framerate of 44000 to match original case
sig1 = np.sin(2*math.pi*freq*t)
sig2 = np.sin(2*math.pi*(freq+delta_f)*t + math.pi/4)
phase_arr = []
for i in range (800):
    phase = phase_shift(sig1[i*55:(i+1)*55],sig2[i*55:(i+1)*55])
x= np.linspace(1,800,800)        
plt.xlabel('chunk number')   #chunk size is 55 i.e number of samples in one bit (original case) 
plt.ylabel('phase difference')

The output plot is as shown.

enter image description here


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.