Plotting points across the Sine Curve

4 views (last 30 days)
I need help with my plot. I have points going across the sine function (zero-crossing). May someone please help me have different symbols for the symbols on the zero line. Say one symbol for the point on the zero-line on your way down. And another symbol for the point on your zero-line on your way up. Thank you! I am not interested in the peaks here. Thanks !
Here is the code. I know you will have to manipulate the code in some way to distinguish the negative direction going onto the zero line and the positive direction going onto the zero line.
x = 1:2000;
X = x;
J = 1;
Fs = 1999;
N = J*Fs;
t = 0: 1/Fs : J;
Fn = 5; % this control the number of cycles/periods
deltaJ = 0.0020;
deltax = 1;
y_sine_25HZ = sin(Fn*2*pi*t);
y = y_sine_25HZ ;
plot(x,y, 'k.')
drvY = gradient(y); % First Derivative Using Gradient
secondDrvY = gradient(drvY); % Second Derivative Using Gradient
indices = [find(secondDrvY(2:end).*secondDrvY(1:end-1)<0) find(abs(secondDrvY)<1e-15)]; % you are dealing with a discrete function here so the second derivative may not be exactly at zero
x_indices = t(indices);
dy=[0 sign(diff(y))]; % First derivative
locdn = find((diff(dy))== 2); % location down (second derivative)
locup = find((diff(dy))==-2);
ylim([-1.05 1.05])
hold on
% plot(t(zx), y(zx), 'x')
plot(t,y, 'k.', t(indices), zeros(1,length(indices)), 'd', 'MarkerSize', 10, 'MarkerFaceColor', 'r')
plot(t, secondDrvY, 'r')
plot(t, drvY, 'b')
legend('Signal', 'Approximate Zero-Crossings')
hold off

Accepted Answer

Thorsten on 3 Nov 2016
plot(t, y)
hold on
ind = find(diff(y>0)<0);
plot(t(ind), y(ind), 'ko')
ind = find(diff(y>0)>0);
plot(t(ind), y(ind), 'ro')
  1 Comment
Osita Onyejekwe
Osita Onyejekwe on 3 Nov 2016
is there a way to do this using the gradient and indices I already have in my plots, instead of the diff, thank you

Sign in to comment.

More Answers (0)


Find more on Simulink 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!