# Divide and plot signal parts

2 views (last 30 days)

Show older comments

Hello all,

I have a plot and I want to extract each of the marked parts of the curve, how to do it?

I tried findpeaks() but it detects the maxima part and I cannot get exactly that part of the curve.

thanks

##### 2 Comments

dpb
on 21 Aug 2022

### Accepted Answer

Mathieu NOE
on 23 Aug 2022

Edited: Mathieu NOE
on 23 Aug 2022

finally, decided to give it a try ....

your neg data in red (added for fun the positive segments as well in green)

%% dummy data

fs = 100;

samples = 6.5*fs;

dt = 1/fs;

t = (0:samples-1)*dt;

x = square(2*pi*0.5*t);

% high pass filter

fc = 0.25;

wn = 2*fc/fs;

[b,a] = butter(1,wn,'high');

xf = filter(b,a,x);

% apply a slope / shift

xf = xf + 3*(-1 + t./max(t));

%% main code

% first and second derivatives

[dxf, ddxf] = firstsecondderivatives(t,xf);

figure(1),

subplot(211),plot(t,xf,'+-');

subplot(212),plot(t,ddxf,'+-');

% start / stop points for negative data

all_points = find(ddxf>max(ddxf)/10); % adjust threshold according to your data

dd = diff(all_points);

ind_neg = find(dd<mean(dd));

start_point_neg = all_points(ind_neg);

stop_point_neg = all_points(ind_neg+1);

ll_neg = stop_point_neg - start_point_neg;

% start / stop points for positive data

ind_pos = find(dd>mean(dd));

start_point_pos = all_points(ind_pos)+1;

stop_point_pos = all_points(ind_pos+1)-1;

ll_pos = stop_point_pos - start_point_pos;

figure(2),

plot(t,xf,'+-');

hold on

for ci = 1:numel(ll_neg)

id_neg = start_point_neg(ci):stop_point_neg(ci);

plot(t(id_neg),xf(id_neg),'or');

end

for ci = 1:numel(ll_pos)

id_pos = start_point_pos(ci):stop_point_pos(ci);

plot(t(id_pos),xf(id_pos),'og');

end

hold off

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function [dy, ddy] = firstsecondderivatives(x,y)

% The function calculates the first & second derivative of a function that is given by a set

% of points. The first derivatives at the first and last points are calculated by

% the 3 point forward and 3 point backward finite difference scheme respectively.

% The first derivatives at all the other points are calculated by the 2 point

% central approach.

% The second derivatives at the first and last points are calculated by

% the 4 point forward and 4 point backward finite difference scheme respectively.

% The second derivatives at all the other points are calculated by the 3 point

% central approach.

n = length (x);

dy = zeros;

ddy = zeros;

% Input variables:

% x: vector with the x the data points.

% y: vector with the f(x) data points.

% Output variable:

% dy: Vector with first derivative at each point.

% ddy: Vector with second derivative at each point.

dy(1) = (-3*y(1) + 4*y(2) - y(3)) / (2*(x(2) - x(1))); % First derivative

ddy(1) = (2*y(1) - 5*y(2) + 4*y(3) - y(4)) / (x(2) - x(1))^2; % Second derivative

for i = 2:n-1

dy(i) = (y(i+1) - y(i-1)) / (x(i+1) - x(i-1));

ddy(i) = (y(i-1) - 2*y(i) + y(i+1)) / (x(i-1) - x(i))^2;

end

dy(n) = (y(n-2) - 4*y(n-1) + 3*y(n)) / (2*(x(n) - x(n-1)));

ddy(n) = (-y(n-3) + 4*y(n-2) - 5*y(n-1) + 2*y(n)) / (x(n) - x(n-1))^2;

end

##### 0 Comments

### More Answers (0)

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!