I have "step" data and I want value only on the steps
Show older comments
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
More Answers (3)
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
Farotto Luca
on 31 Jan 2024
Edited: Farotto Luca
on 31 Jan 2024
Sam Chak
on 31 Jan 2024
@Farotto Luca, Do you mean these 'plateau' values?

Farotto Luca
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
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 Get Started with Signal Processing Toolbox 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!


