Plotting points across the Sine Curve

4 ビュー (過去 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 Center および File ExchangeMATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by