indices for zero crossing of a Sine Function

4 ビュー (過去 30 日間)
Osita Onyejekwe
Osita Onyejekwe 2016 年 11 月 1 日
コメント済み: Osita Onyejekwe 2016 年 11 月 2 日
For the last 24 hours, I have been trying to find the indices of the zeros crossing of a simple Sine Function. I should get the indices to be from 0 to 1 and then 9 values inbetween. However I dont start at zero, however i do end up at 1 and I have an extra detected zero crossing in the middle. I just need help fixing my code. Please anyone. I am losing my mind. Here is my code
clear close all clc
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 = diff(y)/deltax; % one unit shorter than y
drvY = [drvY , drvY(end)]; % making up for the missing point
%
secondDrvY = diff(drvY)/deltax;
secondDrvY = [secondDrvY, secondDrvY(end)];
%% Indices where sign change occurs %sign_Location = sign(secondDrvY) % where sign changes occur for the second derivative indices = [find(secondDrvY(2:end).*secondDrvY(1:end-1)<0) find(abs(secondDrvY)<1e-15)]; t(indices)
this is the result I am getting::
0.0990 0.1991 0.2991 0.3992 0.4992 0.5993 0.6993 0.7994 0.8994 0.9995 1.0000
if you plot it you will clearly see that it should start at 0 and end at 1. However it doesnt start at 0, it ends at 1, but grabs an extra point. 0.9995 should not be there and there should be a zero in the beginning (first point)
Also once I find these zero crossings how do I plot them on the sine function plot to show them across the sine function. Thank you.
need help

採用された回答

Daniel kiracofe
Daniel kiracofe 2016 年 11 月 2 日
First question: why is there an extra one at end? Because of the way you handled the "missing" point at the end:
drvY = [drvY , drvY(end)];
with this, what you get is drvY(1999) = drvY(2000). And then what do you do for the second derivative? It's just drvY(1999) - drvY(2000), which is zero. And for the missing point, you are doing same thing again:
secondDrvY = [secondDrvY, secondDrvY(end)]
So you are guaranteed to get two zeros at the end no matter what your data. Handling the boundaries is always hard when doing finite differences. In a general problem, you may get slightly better results if you use gradient() instead of diff(). gradient() uses a second order (i.e. more accurate) central different for the majority of the points, and automatically handles the boundaries (i.e. the returned vector has the same length as the original vector).
However, if all you want to do is the find the zero crossings, then why are you looking at the second derivative to begin with? Just directly look for points that crossed zero:
indices = [find( y(2:end).*y(1:end-1)<0) find(abs(y)<1e-15)]
I think you'll find that works a lot better.
and to answer the other question, here is how I would plot it:
plot(t,y, 'k.', t(indices), zeros(1,length(indices)), 'd', 'MarkerSize', 10, 'MarkerFaceColor', 'r')
  1 件のコメント
Osita Onyejekwe
Osita Onyejekwe 2016 年 11 月 2 日
when you plot it, it misses the very last zero crossing...how do you fix it?

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by