indices for zero crossing of a Sine Function

4 visualizaciones (últimos 30 días)
Osita Onyejekwe
Osita Onyejekwe el 1 de Nov. de 2016
Comentada: Osita Onyejekwe el 2 de Nov. de 2016
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

Respuesta aceptada

Daniel kiracofe
Daniel kiracofe el 2 de Nov. de 2016
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 comentario
Osita Onyejekwe
Osita Onyejekwe el 2 de Nov. de 2016
when you plot it, it misses the very last zero crossing...how do you fix it?

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by