Find zeropoints with interpolation
11 次查看(过去 30 天)
显示 更早的评论
Hello i have a very long vector a = [2000000x1].
I want to find the indexes of all the zeropoints in this vektor, when the values go from negative to positive.
Below you can find a part of the graph where you can see what i mean.
Does anyone know how i can code this?
i just found this function but this does only give me the nearest points before the zeropoints and not the exact index of the zeropoint crossing and then theres still the problem that i dont know how to get only the zeropoints that go from negative to positive values. "https://de.mathworks.com/matlabcentral/answers/267222-easy-way-of-finding-zero-crossing-of-a-function"
Thank you.
0 个评论
采纳的回答
更多回答(2 个)
Sam Chak
2023-11-20
Check this out to see if this approach, using the zerocrossrate() function, is acceptable for finding the indices of the zero points.
x = linspace(0, 10, 101);
y = sin(2*pi/5*x);
plot(x, y), grid on, xlabel('x'), ylabel('y')
[~, count, indices] = zerocrossrate(y, Method="comparison")
%% Indices of the zero points
idx = find(indices == 1)
0 个评论
John D'Errico
2023-11-20
编辑:John D'Errico
2023-11-20
Almost trivial. LEARN TO USE MATLAB! I'll start with a long vector.
x = linspace(0,20,1e7)';
y = cos(x);
Yes, we know where the zero points should lie, at least in this trivial example. For reference, those locations will happen at
syms X
solve(cos(X) == 0,'returnconditions',true)
(Yes, I know that is also trivial to do, but it is nice to let MATLAB do the work in a maTLAB forum.) So we should expect to see zeros at:
format long g
k = (0:5)';
pi/2 + k*pi
First find where the crossing happens.
cloc = find(diff(y >= 0));
So cloc tells us the index of those points.
[x(cloc),y(cloc),y(cloc+1)]
Can you see that we have identified a zero crossing in each case? And of course, we are already quite close to the true zero in each case.
plot(x,y,'b-',x(cloc),y(cloc),'rx')
grid on
Next, just use linear interpolation between each pair of points. You could be more sophisticated, perhaps using a higher order interpolant. But a linear interpolant is trivial to use and inplement, and at this fine of an interval, a linear interpolant will be quite accurate.
And, yes, a linear interpolant is trivial to write. But again, this is a MATLAB forum! This is the point-slope form of a line therough two points. Just use solve!
syms a b ya yb
Y = ya + (yb - ya)/(b-a)*(X-a)
solve(Y == 0,X)
Having now gotten MATLAB to do the thinking for me, we can now do a linear interpolation.
ya = y(cloc);yb = y(cloc+1);
xlinear = (x(cloc).*yb - x(cloc+1).*ya)./(yb - ya);
How well did we do?
[pi/2 + k*pi,xlinear]
And we soo that to the accuracy of double precision arithmetic, in each case, we have a perfect prediction, merely using linear interpolation.
In the end, the result took me about 3 lines of code to write. (If you exclude the places where i used solve to do work that was just playing around with MATLAB.)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!