[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: detecting a sign change in a vector
From: |
Juan Pablo Carbajal |
Subject: |
Re: detecting a sign change in a vector |
Date: |
Sat, 2 Feb 2013 19:46:26 +0100 |
On Thu, Jan 31, 2013 at 7:41 PM, Jordi GutiƩrrez Hermoso
<address@hidden> wrote:
> On 31 January 2013 12:52, dirac <address@hidden> wrote:
>> This might be trivial but I am stuck on this.
>> I have a vector, V, which basically contains the values of a cos/sin
>> function. The signs of the elements in the vector change from + to -
>> occasionally.
>
> idx_changes = diff(sign(vector)) != 0)
>
> HTH,
> - Jordi G. H.
> _______________________________________________
> Help-octave mailing list
> address@hidden
> https://mailman.cae.wisc.edu/listinfo/help-octave
You can improve on Jordi's result by linear interpolation (or whatever
fancier interpolation). Check this out
t = linspace(0,2*pi,100);
x = sin (4*t + pi/5);
idx = find (diff (sign(x)) != 0);
% If we take the values given by the indexes we get
sin (4*t(idx) + pi/5)
0.2264968 -0.0697565 0.1642651 -0.0063466 0.1013720 -0.1954794
0.0380707 -0.1328855
% if we do simple linear interpolation
tzero = mean ([t(idx); t(idx+1)]);
sin (4*tzero + pi/5)
0.1013720 0.0570888 0.0380707 0.1202944 -0.0253839 -0.0697565
-0.0887363 -0.0063466
In one case you get a worse result, but in general the abscissa of the
zero is better estimated.
Also, note that all these methods work for functions without noise. If
you have noise you will better fit a model function to your data and
get the zeros of the fitted function.