How to calculate phase difference between 2 periodic signals recorded from oscilloscope?

2 views (last 30 days)
I have recorded 2 signals from oscilloscope and stored it in a csv file.
I have ploted both the signals. But i need to calculate the phase difference between these 2 waves.
I have attached the csv file. consider the Channel 1 and 2.
I have plotted the code as below:
v=csvread("data.csv",2,0);
a=v(:,5); % the column you want to plot
a1=v(:,6);
ts=2e-9; % the sampling time is available in data (excel file)
z=length(a);
t=0:ts:ts*z-ts;
subplot(2,1,1)
plot(t,a)
xlim([0 5e-7])
subplot(2,1,2)
plot(t,a1)
xlim([0 5e-7])

Accepted Answer

Star Strider
Star Strider on 21 Dec 2021
One approach —
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/840620/data.CSV', 'VariableNamingRule','preserve')
T1 = 20482×6 table
Var1 Var2 Var3 Var4 Var5 Var6 ____________________ ___________________________________________ ____ ___________ _____ ____ {'Record Length' } {'20480' } NaN NaN NaN NaN {'Sample Interval' } {'CH1:0.0000000020000 CH2:0.0000000020000'} NaN NaN NaN NaN {'Vertical Units' } {'CH1:V CH2:V' } NaN -2.048e-05 -0.44 6.72 {'Vertical Scale' } {'CH1:1.00 CH2:2.00' } NaN -2.0478e-05 -0.48 6.72 {'Vertical Offset' } {'CH1:-0.04000 CH2:-0.08000' } NaN -2.0476e-05 -0.52 6.96 {'Horizontal Units'} {'s' } NaN -2.0474e-05 -0.44 6.8 {'Horizontal Scale'} {'0.0000000500' } NaN -2.0472e-05 -0.24 6.88 {'Model Number' } {'WaveAce1002' } NaN -2.047e-05 -0.16 6.96 {'Serial Number' } {'LCRY2150C00084' } NaN -2.0468e-05 -0.16 6.96 {'Software Version'} {'5.01.02.27' } NaN -2.0466e-05 -0.2 7.2 {0×0 char } {0×0 char } NaN -2.0464e-05 -0.24 6.96 {0×0 char } {0×0 char } NaN -2.0462e-05 -0.16 7.04 {0×0 char } {0×0 char } NaN -2.046e-05 -0.08 7.04 {0×0 char } {0×0 char } NaN -2.0458e-05 -0.04 7.04 {0×0 char } {0×0 char } NaN -2.0456e-05 0.08 7.2 {0×0 char } {0×0 char } NaN -2.0454e-05 0.12 7.04
% C1 = readcell('https://www.mathworks.com/matlabcentral/answers/uploaded_files/840620/data.CSV')
time = T1.Var4(3:end,:);
CH1 = T1.Var5(3:end,:);
CH2 = T1.Var6(3:end,:);
Ts = mean(diff(time)) % Sampling Interval
Ts = 2.0000e-09
Tsdc = Ts/std(diff(time)) % Check Regularity Of Sample Times
Tsdc = 2.1192e+12
Fs = 1/Ts % Sampling Frequency
Fs = 5.0000e+08
Fn = Fs/2; % Nyquist Frequency
L = numel(time) % Signals Length
L = 20480
[CH1a,CH2a,Dly] = alignsignals(CH1, CH2); % Align Signals, Determine Delay
fprintf('Delay = %3d Samples',Dly)
Delay = -64 Samples
[pks, locs] = findpeaks(CH1, 'MinPeakDistance',abs(Dly)*0.75); % Peaks & Locations (Indices)
FreqCH1 = 1/mean(diff(time(locs)))
FreqCH1 = 4.9998e+06
PhaseDiffSec = Dly*Ts % Samples * Seconds/Sample
PhaseDiffSec = -1.2800e-07
PhaseDiffCyc = PhaseDiffSec * FreqCH1 % Seconds * Cycles/Second
PhaseDiffCyc = -0.6400
PhaseDiffRad = PhaseDiffSec * 2*pi*FreqCH1 % Seconds * Radians/Second
PhaseDiffRad = -4.0210
figure
plot(time, CH1)
hold on
plot(time, CH2)
plot(time(locs), CH1(locs), '+r')
hold off
grid
xlim([[-1 1]*0.1E-5+1.2E-5])
xlabel('Time')
ylabel('Amplitude')
title('Original Signals With Located Peaks')
S1 = 0.5*sign(sin(2*pi*time*FreqCH1)) + 0.5;
S2 = sign(sin(2*pi*time*FreqCH1 - PhaseDiffRad)) + 1.1;
figure
plot(time, S1)
hold on
plot(time, S2)
hold off
grid
xlim([[-1 1]*0.1E-5+1.2E-5])
ylim([-0.1 2]*1.1)
xlabel('Time')
ylabel('Amplitude')
title('Approximate Signlal Simulation To Check Results')
.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!