![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/177924/image.jpeg)
An efficient way of finding sequential points near zero
조회 수: 3 (최근 30일)
이전 댓글 표시
I have an signal that oscillates. I am looking for an efficient way to find all values in it that are near to zero. I would only need to find one per cycle so nearest. I tried using a for loop but it wasn't a very neat way and I was wondering if there are any functions in MATLAB that will do this for me.
댓글 수: 0
채택된 답변
Cedric
2015년 8월 5일
What you want to achieve is not completely clear to me. Does that help?
t = 0 : 0.1 : 20 ;
x = sin( t ) + rand( size( t )) ;
figure() ; clf ; hold on ; grid on ;
set( gcf, 'Units', 'normalized', 'Position', [0.1,0.1,0.8,0.8] ) ;
plot( [min(t), max(t)], [0, 0], 'k', 'LineWidth', 2 ) ;
plot( t, x, 'b' ) ;
kerSize = 10 ;
x_smz = conv( x, ones(1, kerSize)/kerSize, 'same' ) ;
plot( t, x_smz, 'r' ) ;
s = sign( x_smz ) ;
tId = find( s(1:end-1) .* s(2:end) < 1 ) ;
tz = t(tId) - x_smz(tId) .* (t(tId+1)-t(tId))./(x_smz(tId+1)-x_smz(tId)) ;
plot( tz, 0, 'go', 'LineWidth', 2 ) ;
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/177924/image.jpeg)
추가 답변 (2개)
Matt J
2015년 8월 5일
편집: Matt J
2015년 8월 5일
If you're confident that the zeros are spaced apart by some minimum known amount Delta, you can divide your cycle into intervals of width Delta and apply fzero() in each.
If you have no prior knowledge of the spacing between the zeros, then there is no way to find them all non-analytically.
댓글 수: 2
Matt J
2015년 8월 5일
You can interpolate a curve, rather than fit. A simple choice would be
fzero( @(x) interp1(yoursignal,x) , initialGuess);
Star Strider
2015년 8월 5일
You have to use a for loop to iterate through the different starting estimates (or ranges). You can make this easier and more efficient by using built-in functions to define the approximate zero-crossings first. One possibility is here. See the documentation for circshift for details on using it with your data. I have sometimes found that using a two-index shift (changing -1 to -2) is superior to a one-index shift. It depends on your data, so you will have to experiment.
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!