Clear Filters
Clear Filters

Phase difference of two continuous signals.

85 views (last 30 days)
I have two signals, which has constanly changing frequencies. I want to be able to calculate the phase shift of these two signals at discreet time windows. I have been able to calculate the phase shift of the entire spectrum, however, I am unsure if this method applies to a continously changing frequency. Note, I realized after the fact, that I might need to use an external tigger source to keep the initial frequencies of both signals the same. Will this be an issue, or is it possible to calculate the localised phase shift under these conditions? I am using the Phase Difference Measurement add on to calculate the phase shift in mili seconds.
Below is the code I used:
file_path1 = '.csv';
file_path2 = '.csv';
ref = csvread(file_path1, 16, 0);
meas = csvread(file_path2, 16, 0);
signal_ref = ref(:,2);
signal_meas = meas(:,2);
% stored variables
dt = 1.6e-10;
fs = 1/dt;
n = length(signal1_new_meas);
fn = fs/(n-1);
ff = fs/2;
T = 0 : dt :(n-1)*dt;
T = T';
Freq = 0: fn: ff;
figure (1), clf;
subplot(211);
plot(T, signal_ref)
xlabel('Time (s)')
ylabel('Voltage (V)')
title('Refrence signal')
subplot(212)
plot(T, signal_meas)
xlabel('Time (s)')
ylabel('Voltage (V)')
title('Measurement signal')
FFT_ref = fft(signal_ref);
FFT_ref = FFT_ref(1:n/2);
FFT_ref = (abs(FFT_ref)).^2;
FFT_meas = fft(signal_meas);
FFT_meas = FFT_meas(1:n/2);
FFT_meas = (abs(FFT_meas)).^2;
figure (2), clf;
subplot(211);
plot(Freq, 10*log10(FFT_ref))
xlabel('Frequency (Hz)')
ylabel('PSD (dB/Hz)')
title('Refrence signal')
set(gca,'xlim',[0 200e3])
subplot(212)
plot(Freq, 10*log10(FFT_meas))
xlabel('Frequency (Hz)')
ylabel('PSD (dB/Hz)')
title('Measurement signal')
set(gca,'xlim',[0 200e3])
% time difference measurement
TimeDiff = phdiffmeasure(signal_meas,signal_ref, fs, 'corr');
TimeDiff = 1000*TimeDiff;
% display the result
disp(['Estimated time lag Y->X (Y w.r.t. X) = ' num2str(TimeDiff) ' ms'])
commandwindow
% plot the signals
figure(1)
plot(T, signal_meas, 'b', 'LineWidth', 1.5)
grid on
hold on
plot(T, signal_ref, 'r', 'LineWidth', 1.5)
xlabel('Time, s')
ylabel('Amplitude, a.u.')
title('Two signals with phase difference')
legend('Measurement signal X', 'Reference signal Y')
The the calculated TimeDiff output is calculated to be -0.54902ms.
Would the calculated phase shift be equal to the entire spectrum in this case? Apologies, the .csv file is rather large (about 1.4GB per .csv file), therefore I didnt include it.

Accepted Answer

Mathieu NOE
Mathieu NOE on 28 Feb 2024
hello
maybe you could use this code once you have splitted the large signal into smaller chuncks
x = 0:0.1:30;
y1 = sin(x+0.25).*exp(-x/100); % reference signal
y2 = 0.5*sin(x+0.45).*exp(-x/100); % signal shifted by 0.2 rad
figure(1),
plot(x,y1,'r',x,y2,'b');
% find crossing points at y threshold = 0
threshold = 0;
[xc1] = find_zc(x,y1,threshold);
[xc2] = find_zc(x,y2,threshold);
% phase shift = x distance divided by period and multiplied by 2pi
p1 = mean(diff(xc1)); % mean period of signal 1
p2 = mean(diff(xc2)); % mean period of signal 2
phase_shift = mean(xc1-xc2)/p1*2*pi % is the value used in the data generation
phase_shift = 0.2000
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [Zx] = find_zc(x,y,threshold)
% positive slope "zero" crossing detection, using linear interpolation
y = y - threshold;
zci = @(data) find(diff(sign(data))>0); %define function: returns indices of +ZCs
ix=zci(y); %find indices of + zero crossings of x
ZeroX = @(x0,y0,x1,y1) x0 - (y0.*(x0 - x1))./(y0 - y1); % Interpolated x value for Zero-Crossing
Zx = ZeroX(x(ix),y(ix),x(ix+1),y(ix+1));
end
  14 Comments
Mathieu NOE
Mathieu NOE on 6 Mar 2024
Judt FYI, I made a slight modification in the code to make the common time vector process a bit more robust
here tested for both initial csv files (ref_1.csv,meas_1.csv) and the 2 last ones (ref trigger start_000_ALL.csv,meas trigger start_000_ALL.csv)
in the last case , I got this result , but as I am at the very beginning of the learning curve regarding OFDR, I can't really interpret the results obtained here - hope you can do better than I !
% Results obtained with phdiffmeasure and 'dft' argument
% signals
% ref = csvread('ref_1.csv', 16, 0);
% meas = csvread('meas_1.csv', 16, 0);
ref = csvread('ref trigger start_000_ALL.csv', 16, 0);
meas = csvread('meas trigger start_000_ALL.csv', 16, 0);
time_ref = ref(:,1);
signal_ref = ref(:,2);
dt = mean(diff(time_ref));
time_meas = meas(:,1);
signal_meas = meas(:,2);
fs = 1/dt;
% define which data has time delay and interpolate the other data on
% correct time data
time_ref_min = time_ref(1);
time_meas_min = time_meas(1);
if time_ref_min>time_meas_min
time = time_ref;
signal_meas = interp1(time_meas,signal_meas,time);
else
time = time_meas;
signal_ref = interp1(time_ref,signal_ref,time);
end
clear time_ref time_meas
samples = length(signal_ref);
window_size = 32; % Amount of data point used to seperate data chunks.
overlap_percentage = 50; % Desired overlap percentage
overlap = window_size * overlap_percentage / 100;
shift = window_size-overlap; % nb of samples between 2 contiguous buffers
% Calculate number of windows
% num_windows = floor((m - window_size) / overlap) + 1;
num_windows = fix((samples-window_size)/shift +1);
% Iterate over the signal with sliding window
for ci = 1:num_windows
% Define start and end indices for the current window
start_index = 1+(ci-1)*shift;
end_index = min(start_index+ window_size-1,samples);
% Extract the current window
window_ref = signal_ref(start_index:end_index);
window_meas = signal_meas(start_index:end_index);
% phase difference code (with phdiffmeasure)
phase_shift_rad(ci) = phdiffmeasure(window_meas,window_ref, fs, 'dft'); % % phase difference estimation (rad)
time_index(ci) = round((start_index+end_index)/2); % time index expressed as sample unit (dt = 1 in this simulation)
end
timex = time(time_index); % new x axis
% phase_shift_rad = wrapToPi(phase_shift_rad);
figure(1),
subplot(3,1,1),plot(time,signal_ref)
xlim([min(time) max(time)]);
subplot(3,1,2),plot(time,signal_meas)
xlim([min(time) max(time)]);
subplot(3,1,3),plot(timex,phase_shift_rad,'r');
xlim([min(time) max(time)]);

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!