Find zeropoints with interpolation
조회 수: 5 (최근 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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1544477/image.png)
댓글 수: 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에서 Function Creation에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!