# relative-phase two oscillators

Hi everyone,

I'm trying to calculate the relative-phase between two oscillators. The first step to do that is applying the hilbert transform to the data. I got that using the hilbert function. But I couldn't continue the next steps.

I'll appreciate any help.

Thank you

Hi, and welcome!

Please see the FAQ: What's a reproducible example (`reprex`) and how do I do one?. You'll get more and better answers by sharing the actual code that you need help with.

Hello, technocrat,

This is a short part of the two oscillators:

oscill1 oscill2
1 898.0789 1533.986
2 897.7501 1535.044
3 897.4141 1536.068
4 897.0676 1537.054
5 896.7072 1537.999
6 896.3298 1538.902
7 895.9318 1539.759
8 895.5098 1540.568
9 895.3919 1541.329
10 895.4676 1542.042
11 895.4988 1542.706
12 895.4809 1543.323
13 895.4098 1543.894
14 895.2819 1544.421
15 895.0940 1544.905
16 894.8431 1545.348
17 894.5264 1545.754
18 894.1417 1546.124
19 893.6869 1546.463
20 893.1605 1546.774
21 892.5612 1547.062
22 891.8885 1547.330
23 891.1420 1547.583
24 890.3218 1547.826
25 889.4281 1548.061
26 888.4619 1548.293
27 887.4240 1548.524
28 886.3161 1549.025
29 885.1397 1549.763
30 883.8965 1550.495

I have tried 3 ways to calculate the relative-phase:
1 -I used hilbert() to tranform the data
hilbertoscill1<-hilbert(oscill1, f=30)
hilbertoscill2<-hilbert(oscill2, f=30)
But I don`t know how to continue

2- I found the code below and tried to adjust to R, but there was an error in the 2 step because the apperars as a list. I tried to convert to numeric, but it didn`t work

freq = 30; % Frequency in Hz
phase_deg_1 = 30; % Shift for signal 1 in Hz
phase_deg_2 = 30; % Shift for signal 2 in Hz

phase_deg_2/180;

signal_2 = sin(2

# 2: Take the FFT of each of the two signals

signal_1_fft = fft(signal_1);
signal_2_fft = fft(signal_2);

# Throw away DC component that should be at index 0 in a sane language

signal_1_fft = signal_1_fft(2:end);
signal_2_fft = signal_2_fft(2:end);

# 3: Find the phases of each signal

signal_1_phase = unwrap(angle(signal_1_fft));
signal_2_phase = unwrap(angle(signal_2_fft));

# 4: Now we can find the phase angle difference between the two signals

signal_phase_diff = signal_2_phase - signal_1_phase;

# 5: Next we can pull out the phase diff at the frequency of interest:

% Convert from bin number to frequency from 0 to Fs
binNum = ((length(signal_1_fft))/Fs)*freq;
binNum = round(binNum);

phase_diff = signal_phase_diff(binNum);

# Display result in degrees

sprintf('Phase difference between signals at %d Hz is %f degrees', freq, (phase_diff*180)/pi)

3- The other option was the function phase.sync() of the synchrony package, but I couldn't understand the outcome

phase <- phase.sync(oscill1, oscill2, nrands = 0, mod = 1, method = "fft", nbreaks = 10, mins = FALSE, quiet = FALSE)

Thank you!!

1 Like

Hi Fabián: Could you isolate a small step where an R command you used didn't behave as you expected? I'm not familiar with the subject area, but could help with R with if the question were focused on a single step were things go wrong.

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.