Plotting points across the Sine Curve

조회 수: 7 (최근 30일)
Osita Onyejekwe
Osita Onyejekwe 2016년 11월 2일
댓글: Osita Onyejekwe 2016년 11월 3일
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에서 Clocks and Timers에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by