signal wave sorting ?

1 view (last 30 days)
Hassan Abdelazeem
Hassan Abdelazeem on 25 Jan 2022
Commented: Mathieu NOE on 2 Feb 2022
%Dear All
% I have data of two signals but the data does not start from zero I need
% to make these data start from zero for both signals but without using
% sort function in matlab becuase I need to make the lag angle between them as it
% is before sorting
plot(thetaBm);hold on

Answers (1)

Mathieu NOE
Mathieu NOE on 25 Jan 2022
this would be my suggestion : first , unwrap the angle data , then find the x coordinates for a given y (common) value for both data sets. Then it's easy to do the difference of the two x data sets and you can compute the average of that array (delta_x)
the picyure shows the points used for that x distance computation. they have exactly the same y coordinate for both curves
% raw data display
samples = numel(thHm);
x = 1:samples;
% unwrap angles
thHm = 180/pi*unwrap(thHm*pi/180);
thBm = 180/pi*unwrap(thBm*pi/180);
% now lets compute the x distance between the two curves
% using the "crssing" function to get both curves x coordinates for given y
% value (defind in threshold value below);
npoints = 50;
threshold = linspace(min(thHm(1),thBm(1))-1,max(thHm(samples),thBm(samples))+1,npoints);
for ck = 1:npoints
[~,s0_pos,t0_neg1(ck),s0_neg]= crossing_V7(thHm,x,threshold(ck),'linear'); % positive (pos) and negative (neg) slope crossing points
[~,s0_pos,t0_neg2(ck),s0_neg]= crossing_V7(thBm,x,threshold(ck),'linear'); % positive (pos) and negative (neg) slope crossing points
% ind => time index (samples)
% t0 => corresponding time (x) values
% s0 => corresponding function (y) values , obviously they must be equal to "threshold"
% result averaged
delta_x = mean(t0_neg1 - t0_neg2);
plot(x,thHm,'r',x,thBm,'b',t0_neg1,threshold,'dr',t0_neg2,threshold,'db','linewidth',2,'markersize',12);grid on
legend('thHm','thBm','negative slope crossing points thHm','negative slope crossing points thBm');
function [t0_pos,s0_pos,t0_neg,s0_neg] = crossing_V7(S,t,level,imeth)
% [ind,t0,s0,t0close,s0close] = crossing_V6(S,t,level,imeth,slope_sign) % older format
% CROSSING find the crossings of a given level of a signal
% ind = CROSSING(S) returns an index vector ind, the signal
% S crosses zero at ind or at between ind and ind+1
% [ind,t0] = CROSSING(S,t) additionally returns a time
% vector t0 of the zero crossings of the signal S. The crossing
% times are linearly interpolated between the given times t
% [ind,t0] = CROSSING(S,t,level) returns the crossings of the
% given level instead of the zero crossings
% ind = CROSSING(S,[],level) as above but without time interpolation
% [ind,t0] = CROSSING(S,t,level,par) allows additional parameters
% par = {'none'|'linear'}.
% With interpolation turned off (par = 'none') this function always
% returns the value left of the zero (the data point thats nearest
% to the zero AND smaller than the zero crossing).
% check the number of input arguments
% check the time vector input for consistency
if nargin < 2 | isempty(t)
% if no time vector is given, use the index vector as time
t = 1:length(S);
elseif length(t) ~= length(S)
% if S and t are not of the same length, throw an error
error('t and S must be of identical length!');
% check the level input
if nargin < 3
% set standard value 0, if level is not given
level = 0;
% check interpolation method input
if nargin < 4
imeth = 'linear';
% make row vectors
t = t(:)';
S = S(:)';
% always search for zeros. So if we want the crossing of
% any other threshold value "level", we subtract it from
% the values and search for zeros.
S = S - level;
% first look for exact zeros
ind0 = find( S == 0 );
% then look for zero crossings between data points
S1 = S(1:end-1) .* S(2:end);
ind1 = find( S1 < 0 );
% bring exact zeros and "in-between" zeros together
ind = sort([ind0 ind1]);
% and pick the associated time values
t0 = t(ind);
s0 = S(ind);
if ~isempty(ind)
if strcmp(imeth,'linear')
% linear interpolation of crossing
for ii=1:length(t0)
%if abs(S(ind(ii))) >= eps(S(ind(ii))) % MATLAB V7 et +
if abs(S(ind(ii))) >= eps*abs(S(ind(ii))) % MATLAB V6 et - EPS * ABS(X)
% interpolate only when data point is not already zero
NUM = (t(ind(ii)+1) - t(ind(ii)));
DEN = (S(ind(ii)+1) - S(ind(ii)));
slope = NUM / DEN;
slope_sign(ii) = sign(slope);
t0(ii) = t0(ii) - S(ind(ii)) * slope;
s0(ii) = level;
% extract the positive slope crossing points
ind_pos = find(sign(slope_sign)>0);
t0_pos = t0(ind_pos);
s0_pos = s0(ind_pos);
% extract the negative slope crossing points
ind_neg = find(sign(slope_sign)<0);
t0_neg = t0(ind_neg);
s0_neg = s0(ind_neg);
% empty output
ind_pos = [];
t0_pos = [];
s0_pos = [];
% extract the negative slope crossing points
ind_neg = [];
t0_neg = [];
s0_neg = [];
Mathieu NOE
Mathieu NOE on 2 Feb 2022
problem solved ?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!