Plotting points across the Sine Curve

6 次查看(过去 30 天)
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);
plot(t,y)
ylim([-1.05 1.05])
hold on
scatter(t(locup)',y(locup)',30,'r','*')
scatter(t(locdn)',y(locdn)',30,'g','d','filled')
% 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
grid

采纳的回答

Thorsten
Thorsten 2016-11-3
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 个评论
Osita Onyejekwe
Osita Onyejekwe 2016-11-3
is there a way to do this using the gradient and indices I already have in my plots, instead of the diff, thank you

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Simulink 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by