An efficient way of finding sequential points near zero
4 次查看(过去 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 ) ;
更多回答(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!