I have "step" data and I want value only on the steps
7 views (last 30 days)
Show older comments
Farotto Luca
on 31 Jan 2024
Commented: Star Strider
on 31 Jan 2024
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)).![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1605081/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1605081/image.png)
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);
0 Comments
Accepted Answer
Star Strider
on 31 Jan 2024
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'})
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'})
% 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.
.
2 Comments
More Answers (3)
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)
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
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')
0 Comments
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);
0 Comments
See Also
Categories
Find more on Signal Generation and Preprocessing 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!