Calculating phase delay for one signal to have the same zero crossings

10 views (last 30 days)
I need to fix the phase delay in the triangle signal to make sure all signals have the same zero crossings. I keep getting error messages (either not enough input arguments or an error within a specific signal that only shows up if I try to calculate). Is there a recommended formula to calculate and fix the phase delay?
% This script will generate a figure that plots a pulse waveform as a function of time.
% Set the signal parameters
Frequency_Hz = 100;
Amplitude = 1;
Phase_rad = 0;
DutyCycle = 90; % the percentage (0-100%) of the cycle the pulse train is "high" (positive)
Duration_sec = 0.03;
SamplingRate_Hz=20000;
% Generate the signal and corresponding time vector
[signal, time_sec] = pulse_542(Amplitude, Frequency_Hz, Phase_rad, DutyCycle, Duration_sec, SamplingRate_Hz);
% Create a plot showing the signal as a function of time
figure(1) % Creates figure number 1
hold on
plot(time_sec, signal, 'r-') % Plot the signal as a red solid line
ylim([-1.2 1.2]*Amplitude) % Allow better visualization of the pulse train
% This script will generate a figure that plots a triangle waveform as a function of time.
% Set the signal parameters
Frequency_Hz = 100;
Amplitude = 1;
Phase_rad = 0;
Duration_sec = 0.03;
SamplingRate_Hz=20000;
% Generate the signal and corresponding time vector
[signal, time_sec] = triangle_542(Amplitude, Frequency_Hz, Phase_rad, Duration_sec, SamplingRate_Hz);
% Create a plot showing the signal as a function of time
plot(time_sec, signal, 'g:') % Plot the signal as a dotted green line
% This script will generate a figure that plots a tone as a function of time.
% Set the tone parameters
Frequency_Hz = 100;
Amplitude = 1;
Phase_rad = 0;
Duration_sec = 0.03;
SamplingFreq_Hz=20000;
% Generate the tone and corresponding time vector
[tone_signal, tone_time_sec] = tone_542(Amplitude, Frequency_Hz, Phase_rad, Duration_sec, SamplingFreq_Hz);
tone_signal=tone_signal+1;
% Create a plot showing the tone as a function of time
plot(tone_time_sec, tone_signal, 'b--') % Plot the signal as a dashed blue line
hold off
title('Signals as a function of time') % Adds a title to the plot
ylabel('Signals') % Labels the y-axis
xlabel('Time (sec)') % Labels the x-axis
print -dtiff test

Answers (1)

Mathieu NOE
Mathieu NOE on 15 Sep 2023
hello
unfortunately you did not provide (attach) the functions you are sing to generate the waveforms
nevertheless, see below a code to show how to measure time lag between signals, that you can afterwards use to sync your signals
the result obtained here is 0.2 rad , which is what I defined in the signals at the beginning
all the best
x = 0:0.1:30;
y1 = sin(x+0.25).*exp(-x/10); % reference signal
y2 = 0.75*sin(x+0.45).*exp(-x/10); % 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

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!