MATLAB Answers

lines are not continuous

2 views (last 30 days)
Ahmed Alalawi
Ahmed Alalawi on 11 Nov 2019
Commented: Ahmed Alalawi on 11 Nov 2019
Hello there
I have data which supposed to look like waves. But for some reasons, the lines are not continuous.
I have attached the data and also the figure. I marked the figure with red to show how the data supposed to look like.
Anyone has an idea of how to fix that?
Thank you

  1 Comment

Adam Danz
Adam Danz on 11 Nov 2019
I wonder why the data came out like that in the first place. Where did the data come from? Is it the result of a sensor hitting a bound or something like that?
I'd start with findpeak() to locate the indices where the sharp peaks form and then use those indices to isolate flipped segments.

Sign in to comment.

Accepted Answer

Daniel M
Daniel M on 11 Nov 2019
Edited: Daniel M on 11 Nov 2019
It's not perfect, but it might be sufficient. That's up to you. Play with the different values for dipreset. You might want to smooth the several samples around the 'entry' and 'exit' points.
clearvars
clc
close all
data = load('task.mat');
task = data.task;
% get the location of the peaks
[pks,locs] = findpeaks(task,'MinPeakHeight',15);
% show the data with the identified peaks
figure
plot(task)
hold on
plot(locs,pks,'v')
% this will fail if there are not an even number of peaks
locpairs = reshape(locs,2,[]);
task2 = task; % temporary duplicate
for k = 1:size(locpairs,2)
% get the locations between the first dip
diplocs = locpairs(1,k):locpairs(2,k);
dipval = task(diplocs); % get the values
% get reset value
% dipreset = min(dipval([1 end]));
% dipreset = mean(dipval([1 end]));
dipreset = dipval(1);
% reset dipval
dipval = abs(dipval - dipreset) + dipreset;
task2(diplocs) = dipval; % store result
end
hold on
plot(task2)
% view the output

  2 Comments

Daniel M
Daniel M on 11 Nov 2019
One smoothing attempt might be to add these lines in the loop at the bottom.
% smooth entry
dt = 4;
en = locpairs(1,k);
task2(en-dt:en+dt) = smooth(task2(en-dt:en+dt),2*dt+1,'sgolay',1);
% smooth exit
ex = locpairs(2,k);
task2(ex-dt:ex+dt) = smooth(task2(ex-dt:ex+dt),2*dt+1,'sgolay',1);
Ahmed Alalawi
Ahmed Alalawi on 11 Nov 2019
Thank you Daniel
You saved my day!

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by