Find that particular start index at which the atleast 6 points are consecutively increasing
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Shamala B K
el 1 de Sept. de 2022
Comentada: Shamala B K
el 6 de Sept. de 2022
I have attached excel sheet which has 100 values(points) of acceleration. When I plot the graph I obtain various increasing, decreasing and constant values in the graph. I want to find out that particular index at which the graph is continously increasing for atleast 6 consecutive points. Through this I have to get the output as 'Fall is detected at %d index' and when there is no points which are increasing conitnously for minimum of 6 points it should display 'No fall is detected'. This is a data obtained from an hardware model and hence the code shoud work during real time as well. This can be done through for loop or while loop or if elseif statement or any other simpler way which should work in real time as well.
Please help me out
8 comentarios
Respuesta aceptada
dpb
el 3 de Sept. de 2022
Editada: dpb
el 3 de Sept. de 2022
OK, since we have real data, I'll just use it and illustrate the way the previous code works with it...
a=readmatrix('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1116060/acceleration%20values%20a.xlsx');
a=a(isfinite(a)); % remove the extraneous variable name -- it would be better if you wrote the file as column, not row
% the (original) engine
nMatch=6; ptn=repmat('2',1,nMatch); % how many in a row, a pattern of that many 2's
d=sprintf('%d',sign(diff([a(1) a]))+1); % build search vector of -,0,+ accel as 0,1,2 respectively
isBad=strfind(d,ptn); % locate where the pattern is in the search string
Now, let's see how that works...
disp(isBad) % look at what we got...
NB: the sequence of 63 thru 67 indicates there are more than 6 in a row -- this search finds all such sequences; you may want to only consider the first overall or each group as separate? Didn't say; only said to find that many in a row which we've done.
plot(a,'x') % the a points
hold on
plot(a,'-') % add a line between to visualize
plot(isBad,a(isBad),'or'); % show where the points are on the graph, too...
legend('Accel Points','Acceleration','MatchPoints','location','Northwest')
What this shows is that just looking at sequential points may not be enough -- the really sharp spike isn't found because there are only three points on the rising edge ot that excursion; it's not until the later longer period that the criteriion kicks into play. That lets the overall acceleration be greater in the unfound region than before it gets flagged in the found section. That may (or may not) be ok; we don't know what the purpose is and if it is postprocessing data as you said it's too late to do anything about it, anyway.
But, it illustrates the algorithm works as said; just that it probably is not sophisticated enough for use alone -- it may be that you really need to look at the actual magnitude of the derivative of the acceleration?
You can limit the found locations to just the beginning locations with a "greedy" regexp expression -- if instead we write
ptn=sprintf('2{%d,}',nMatch);
isBad=regexp(d,ptn);
disp(isBad)
you observe we only now get only the one result instead of multiple matches for the ROI; regexp matched all 8 '2's from position 63 thru to the peak in the one result.
This again works for any length a vector or any number of points to match simply by changing the nMatch value and setting the input length of what a is passed.
If, OTOH, you really are trying to do something dynamically, then you would need to start a counter inside the acquisition loop to test the current value against the previous and increment if larger. If reaches the magic number, then alarm, if a new point in non-increasing then reset the counter.
5 comentarios
dpb
el 5 de Sept. de 2022
That's an entirely different problem -- and pretty-much intractable a priori if you're collecting data in real time rather than post-processing...there's really no way to predict just how many will be increasing from counting only whether is is/not higher/lower than previous -- and certainly not before even started to set the trigger number. That's why I suggested before that you probably would need to consider actual values and magnitude as well.
Since we have absolutely no idea what this is or what is/is not important, not much can say about what limits might ought to be other than the observation that simply counting isn't likely to be enough...
Más respuestas (1)
dpb
el 1 de Sept. de 2022
Editada: dpb
el 3 de Sept. de 2022
For small sample size such as this, the string conversion route is probably as good as any...
ERRATUM:
Forgot that "-111" would also match three ones even though the first is a decrease -- fix is to convert from [-1,0,1] to [0,1,2] for [<0,0,>0], respectively, so the values are unique numerically, not just absolutely.
nMatch=10; % number repeats to find
d=sprintf('%d',sign(diff(a)))+1; % convert acceleration to [0,1,2] as char() vector
isBad=strfind(d,repmat('2',1,nMatch)); % find match, +1 correction for vector length of d re: a
For really long sequences/records, download @Jan's FEX submittal RunLength that does run-length encoding very quickly (it's a mex file function).
I made a crude test case that matches the description but illustrates issues with the idea if the data aren't smooth -- I added random noise to a constant, then a slope from the min to max positive ranges given (which are quite small in comparison to the negative), and then a first negative section and applied the above. One can see it does what was asked but that may not be what is needed...may need to smooth data first or somesuch.
a=[rand(20,1)/100+0.05; linspace(0.06,0.5,40).'+rand(40,1)/10; linspace(0.5,-9.6,20).'+rand(20,1)/10 ];
d=sprintf('%d',sign(diff(a))+1);
nMatch=3;ptn=repmat('2',1,nMatch);
isBad=strfind(d,ptn);
9 comentarios
dpb
el 2 de Sept. de 2022
Editada: dpb
el 2 de Sept. de 2022
isBad is the location where the number nMatch of consecutive increasing acceleration begins, what your original post asked for.
If there are more than one, then there are multiple matches in the original array; each beginning at the subsequent location.
The above would indicate a(152:154) is a positive group with a zero or negative difference following, then another rising group at 157, etc.,...
Attach a dataset to compare those to original and see that that is so...
You may need some other more sophisticated search algorithm to satisfy some other criteria than just counting consecutive values, but we've no klew what you're trying to do, specifically, nor have we seen any representative datasets to look at...
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!