I have "step" data and I want value only on the steps

8 views (last 30 days)
I have a data (that came from a misure of a displacement) and I need to extract the value of the data of every step. I tried to use "findpeaks" but it can't detecte every step (like you can see from the figure below) ((ignore Data fitting)).
The step should be 45 because there is a motor that change is position according to the angle.
In the file "DATI" there is the matrix from where the data came. First column is the displacement, second colomun is the time of each recording data (third and fourth column are irrilevant to the problem but they are the angles and respective times, as you can see, the two times aren't syncronized).
I tried to use this code but can't get any better:
mis = importdata("excel\DATI.csv");
x_mis = mis(:,1);
x_t_x = mis(:,2);
% The value that I need end at the 8022th row
x_mis_n = x_mis(1:8022,1);
x_t_x = x_t_x(1:8022,1);
% Trying to search for "plateau" value of each step
[pks,locs] = findpeaks(x_mis_n,x_t_x,'MinPeakDistance',0.3,'MinPeakWidth',0.01);

Accepted Answer

Star Strider
Star Strider on 31 Jan 2024
First, detrend the curve, second, find the peaks, last, return the values.
Try this —
mis = importdata("DATI.csv");
x_mis = mis(:,1);
x_t_x = mis(:,2);
% TF1 = islocalmax(x_mis, 'FlatSelection', 'first');
%
% figure
% plot(x_t_x, x_mis)
% hold on
% plot(x_t_x(TF1), x_mis(TF1), 'rs')
% hold off
% grid
% The value that I need end at the 8022th row
x_mis_n = x_mis(1:8022,1);
x_t_x = x_t_x(1:8022,1);
x_mis_n_dt = detrend(x_mis_n, 2);
TF2 = islocalmax(x_mis_n_dt, 'MinProminence',0.05); % Use: 'islocalmax'
[pks,locs] = findpeaks(x_mis_n_dt, 'MinPeakProminence',0.05); % Use: 'findpeaks'
figure
plot(x_t_x, x_mis_n)
hold on
plot(x_t_x(TF2), x_mis_n(TF2), 'rs')
hold off
grid
xlabel('x\_t\_x')
ylabel('x\_mis\_n')
title('Results: ‘islocalmax’')
Peak_Values = table(x_t_x(TF2), x_mis_n(TF2), 'VariableNames',{'x_t_x','x_mis_n'})
Peak_Values = 30×2 table
x_t_x x_mis_n ______ _______ 4.0622 0.1995 5.3659 0.35 6.0477 0.532 6.7004 0.6615 7.3363 0.8995 8.0061 1.071 8.6254 1.267 9.2743 1.463 9.9239 1.82 10.568 2.079 11.214 2.3415 11.863 2.625 12.516 3.024 13.252 3.185 13.841 3.5315 14.497 4.032
figure
plot(x_t_x, x_mis_n)
hold on
plot(x_t_x(locs), x_mis_n(locs), 'rs')
hold off
grid
xlabel('x\_t\_x')
ylabel('x\_mis\_n')
title('Results: ‘findpeaks’')
Peak_Values = table(x_t_x(locs), x_mis_n(locs), 'VariableNames',{'x_t_x','x_mis_n'})
Peak_Values = 30×2 table
x_t_x x_mis_n ______ _______ 4.0622 0.1995 5.3659 0.35 6.0477 0.532 6.7004 0.6615 7.3363 0.8995 8.0061 1.071 8.6254 1.267 9.2743 1.463 9.9239 1.82 10.568 2.079 11.214 2.3415 11.863 2.625 12.516 3.024 13.252 3.185 13.841 3.5315 14.497 4.032
% figure
% plot(x_t_x, x_mis_n)
% hold on
% plot(x_t_x(TF2), x_mis_n(TF2), 'rs')
% hold off
% grid
% axis([15 20 3 7])
I initially tried islocalmax with the 'FlatSelection','first' name-value pair, however it did not find all of them. My detrending approach seems to work.
.

More Answers (3)

the cyclist
the cyclist on 31 Jan 2024
I am not sure how you want to define the "plateau" value, since the displacement moves both up and down. Can you just use the unique values of the displacement?
dati = readtable("DATI.csv");
time = dati.Var2;
displacement = dati.Var1;
plot(time,displacement)
% Plateaus defined as unique displacement values
plateau = unique(displacement)
plateau = 1295×1
-0.0595 -0.0525 -0.0490 -0.0385 -0.0350 -0.0280 -0.0245 -0.0105 0 0.0035
The graph of all the data looks different from yours (including having some negative values for displacement). I don't know how you want to manage that.
  3 Comments

Sign in to comment.


Aaron
Aaron on 31 Jan 2024
Edited: Aaron on 31 Jan 2024
You might want to try locating the peaks on the difference of the data rather than the data itself.
I'm not familiar with the findpeaks function, but here is a small example of locating data at steps with diff:
(Note that whether you insert the NaN before or after the diff(y) affects whether you're looking at the data before or after the jump discontinuity.)
x = 1 : 100;
y = reshape((1 : 10).*ones(10,1), 1, []); % Step data
dy = [NaN,diff(y)];
peaks = dy > 0.5; % Locate jumps greater than 0.5
figure; hold on
plot(x,y)
plot(x(peaks),y(peaks),'or')

Austin M. Weber
Austin M. Weber on 31 Jan 2024
Edited: Austin M. Weber on 31 Jan 2024
If you want to count each step as being any increase from the previous value then this is what you want:
locs= [];
for i = 2:length(x_t_x)
if (x_mis_n(i) - x_mis_n(i-1)) > 0
% Identify position of step
locs = [locs i];
end
end
pks = x_mis_n(locs);
Or, without the loops:
d = diff(x_mis_n);
locs = find(d > 0)+1;
pks = x_mis_n(locs);

Categories

Find more on Signal Generation and Preprocessing in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!