How to mark step changes of various sizes

8 views (last 30 days)
This is my code for part of a project im doing:
time_run2 = transpose((1:numel(second_run_data(:, 1))') / 60);
figure(3);
plot(time_run2, second_run_data(:, 1))
whish is used to plot a section of data from a larger set. This data follows a step pattern however each step is brocken up into a bunch of little steps and then a section of flat followed by another step and more flat. some of the steps are slightly curved so i cant simply use the ischange() function to find them becasue it wil find multiple changes in each step. My task is too mark each step with a circle or an x whatever but im only allowed to have 1 mark per step. Is there any way of doing this becasue I have no idea

Accepted Answer

William Rose
William Rose on 10 May 2024
You may want to differentiate your signal with diff(), and then use findpeaks(). Differentiation amplifies high frequency noise, so it can produce spurious peaks. Therefore some smoothing may be required as well. You can use Matlab's smooth(), or some other lowpass filter. If the smoothing is by a linear filter (and smooth(), with the default method, is linear), then it does not matter whether you smooth then differentiate, or vice versa. I have used this approach to identify changes in heart rate in noisy data.
You say "some of the steps are curved" which makes me wonder if there is a bit of judgement involved in the definition of a step. It is OK if there is - that can be incorporated in code - but you have to give some examples for guidance. It would help if you would post the data for which you want to identify changes - maybe with a hand drawing of where you think the steps are.
Good luck.
  4 Comments
Justin
Justin on 12 May 2024
perfect thank you so much really appreciate it
William Rose
William Rose on 12 May 2024
@Justin, you are welcome.
d=importdata('FT02_run_2 (1).csv');
t=d.data(:,1); % time [min]
q=d.data(:,2); % flow [mL/min]
N=length(t); % number of samples
fs=(N-1)/(t(end)-t(1)); % sampling rate [1/min]
Find transition times
% Define constants
Tw=0.9; % segment width [min]
Ts=0.03; % separation between before and after segments [min]
nw=round(Tw*fs); % segment width [samples]
ns=round(Ts*fs); % segment separation [samples]
deltaQmin=0.1; % minimum flow change [L/min]
sdmax=0.05; % max st dev of flow within a segment [L/min]
Look for transitions
j=0; % number of transitions found so far
for i=ns+nw:N-nw
s1=std(q(i-ns-nw+1:i-ns)); % seg 1 SD
s2=std(q(i+1:i+nw)); % seg 2 SD
m1=mean(q(i-ns-nw+1:i-ns)); % seg 1 mean
m2=mean(q(i+1:i+nw)); % seg 2 mean
if s1<sdmax && s2<sdmax && abs(m1-m2)>deltaQmin
j=j+1;
tc(j)=i/fs;
end
end
ntrans=length(tc); % number of transitions found
Display and plot results
fprintf('Found %d transitions.\n',ntrans)
Found 26 transitions.
% plot the data
figure
plot(t,q,'-r.')
grid on; xlabel('Time (min)'); ylabel('Flow (mL/min'); ylim([9,14])
hold on
xline(tc,'b')
It is far from perfect, but it is an example of something you could build on.
Good luck.

Sign in to comment.

More Answers (0)

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!