
Finding sequences representing a sin-function in a timeseries
12 views (last 30 days)
Show older comments
Hi,
I got a problem which I am not able to solve on my own and I hope for some help here. Thank you already in advance!
The Problem:
I got a timeseries which includes way data from a test rig (further information probably won't help) and I want a script which finds intervals with different sinus-like motions on it's own. I got Intervals without any motion, with motion but not sinus-like and the said intervals with sinus-like motion. The script should return the Interval Borders of the parts with sinus-like motion. If the sinus function changes, but it still stays a sinus function, it should create a new interval as well.
I hope the problem is understandable and someone has an idea how to tackle this problem. I would really appreciate your help!
Kind regards
0 Comments
Answers (1)
Mathieu NOE
on 23 Dec 2022
hello
maybe this ?
a simple approach based on signal envelope
here I make a comparison using the matlab built in envelope function vs a Fex submission, that better fills my need in this case
the diamond markers gives you the identified start and stop moments of sine signals , which are assumed to be of higher amplitude than the background noise (may need some filtering on real signals !)
each sine block can be extracted and saved if neede

t = 0:0.01:10;
x = 0.01*randn(size(t)); % some background noise
% create some sine periods
ind1 = 100:300;
f1 = 1.58;
x(ind1) = exp(-0.2*t(ind1)).*sin(2*pi*f1*t(ind1)+ t(ind1).^2);
ind1 = ind1+400;
f1 = 3.23;
x(ind1) = exp(0.03*t(ind1)).*sin(2*pi*f1*t(ind1)+ t(ind1).^2);
% Call function envelope to
% obtain the envelope data
%--------------------------------------------
[up,down] = envelope(x,200,'analytic'); % matlab built in function
[up2,down2] = envelope2(t,abs(x),'linear'); % Fex submission (bottom section)
figure(1)
plot(t,x,t,up,'k',t,up2,'r')
legend('signal','envelope','envelope2');
% now let's find the start / stop time index for the sine blocks
threshold = 0.2*max(abs(x)); % 20% of peak amplitude
[t0_pos1,s0_pos1,t0_neg1,s0_neg1]= crossing_V7(up2,t,threshold,'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"
figure(2)
plot(t,x,t,up2,'r',t0_pos1,s0_pos1,'dr',t0_neg1,s0_neg1,'dg','linewidth',2,'markersize',12);grid on
legend('signal','envelope2','sine start point','sine stop point');
% extract and save in cells sine portions
for ci = 1:numel(t0_pos1)
ind = (t>=t0_pos1(ci) & t<=t0_neg1(ci));
ti = t(ind);
xi = x(ind);
figure(2+ci)
plot(ti,xi);
out{ci} = [ti(:) xi(:)];
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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
error(nargchk(1,4,nargin));
% 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!');
end
% check the level input
if nargin < 3
% set standard value 0, if level is not given
level = 0;
end
% check interpolation method input
if nargin < 4
imeth = 'linear';
end
% 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;
end
end
end
% 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);
else
% empty output
ind_pos = [];
t0_pos = [];
s0_pos = [];
% extract the negative slope crossing points
ind_neg = [];
t0_neg = [];
s0_neg = [];
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%ù
function [up,down] = envelope2(x,y,interpMethod)
%ENVELOPE gets the data of upper and down envelope of the known input (x,y).
%
% Input parameters:
% x the abscissa of the given data
% y the ordinate of the given data
% interpMethod the interpolation method
%
% Output parameters:
% up the upper envelope, which has the same length as x.
% down the down envelope, which has the same length as x.
%
% See also DIFF INTERP1
% Designed by: Lei Wang, <WangLeiBox@hotmail.com>, 11-Mar-2003.
% Last Revision: 21-Mar-2003.
% Dept. Mechanical & Aerospace Engineering, NC State University.
% $Revision: 1.1 $ $Date: 3/21/2003 10:33 AM $
if length(x) ~= length(y)
error('Two input data should have the same length.');
end
if (nargin < 2)|(nargin > 3),
error('Please see help for INPUT DATA.');
elseif (nargin == 2)
interpMethod = 'linear';
end
% Find the extreme maxim values
% and the corresponding indexes
%----------------------------------------------------
extrMaxIndex = find(diff(sign(diff(y)))==-2)+1;
extrMaxValue = y(extrMaxIndex);
% Find the extreme minim values
% and the corresponding indexes
%----------------------------------------------------
extrMinIndex = find(diff(sign(diff(y)))==+2)+1;
extrMinValue = y(extrMinIndex);
up = extrMaxValue;
up_x = x(extrMaxIndex);
down = extrMinValue;
down_x = x(extrMinIndex);
% Interpolation of the upper/down envelope data
%----------------------------------------------------
up = interp1(up_x,up,x,interpMethod);
down = interp1(down_x,down,x,interpMethod);
end
See Also
Categories
Find more on Time-Frequency Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!