Zero Crossing of Signal - Misunderstanding the attached matlab code.

Hi guys!
Im trying to understand the zero crossing points that uses interpolation approximation which I found the code here in the threads of matlab.
the code that I found it is this:
x=1:length(y);
zci = @(v) find(v(:).*circshift(v(:), [-1 0]) <= 0);
% Returns Approximate Zero-Crossing Indices Of Argument Vector
dy = zci(y);
% Indices of Approximate Zero-Crossings
for k1 = 1:size(dy,1)-1
b = [[1;1] [x(dy(k1)); x(dy(k1)+1)]]\[y(dy(k1)); y(dy(k1)+1)];
% Linear Fit Near Zero-Crossings
x0(k1) = -b(1)/b(2);
% Interpolate ‘Exact’ Zero Crossing
mb(:,k1) = b;
% Store Parameter Estimates (Optional)
end
I almost understand the code but I didn't understand the statement of b = [[1;1] [x(dy(k1)); x(dy(k1)+1)]]\[y(dy(k1)); y(dy(k1)+1)]; what does it stand for?
what does this statement mean? I want to understand what this statement does exactly , any help? thanks alot.
does it take a row of matrix and devide it by another row matrix and the result is stored in b? I really just missunderstanding that row among others rows in the code.
all what I need is a detalied explanation what this row b = [[1;1] [x(dy(k1)); x(dy(k1)+1)]]\[y(dy(k1)); is about ? and what it does functionality (takes two rows of matrix and devide between each other?) ?
thanks alot.

 Accepted Answer

Thank you! I recognise my code!
The assignment:
b = [[1;1] [x(dy(k1)); x(dy(k1)+1)]]\[y(dy(k1)); y(dy(k1)+1)];
loops through the zero-crossings returned in ‘dy’ as indexed with ‘k1’ and calculates the linear regression slope and intercept terms for that small segment of the original waveform.
The next line calculates the interpolated value of the exact zero-crossing, and returns it as ‘x0’. The ‘mb’ matrix stores the slope and intercept terms.
For the record, I now use:
zci = @(v) find(diff(sign(v(:))));
since it is a bit more robust and does not have the wrap-around problems my original code for it does.

14 Comments

Thanks for your answer.
bear please with me, I didn't understand well what's the result that b variable/matrix store.
May you explain by an actualy example?
lets assume that x=[1 -1 1] ;
so the zero crossing points are at positions [2 3] ;
then what's the result that stored in b? could you please write the functionality that b does in my example above?
Appreciated and thanks alot.
what this statement [[1;1] [x(dy(k1)); x(dy(k1)+1)]] is about?
I didn't understand the logicaly functionality of this statement.
I didn't understand the functionaily of this statement, I mean after assigning the values of x(dy(k1)) and x(dy(k1)+1) what I get? a matrix 2x1 or what?
it would be appreciated at your free time fro your answer since the code written by you.
Please! I am not online here 24/7 (although it sometimes seems like it). I was sleeping! It is currently 05:35 here (12:35 UCT).
That line derives from calculating linear regressions using the mldivide,\ function (or operator).
This:
b = [[1;1] [x(dy(k1)); x(dy(k1)+1)]]\[y(dy(k1)); y(dy(k1)+1)];
creates the design matrix:
[[1;1] [x(dy(k1)); x(dy(k1)+1)]]
that is equivalent to:
[1 x(dy(k1))
1 x(dy(k1)+1)]
to create the intercept and slope terms for ‘b’ (in that order, so ‘b(1)’ is the intercept and ‘b(2)’ is the slope) where ‘dy’ are the indices returned by ‘zci’, so it calculates the linear regression between ‘x(dy(k1))’ and the next point in the vector, ‘x(dy(k1))+1)’. It then regresses that against the corresponding points in the dependent variable:
[y(dy(k1)); y(dy(k1)+1)]
Note that it is a column vector.
So, put that together with the information in the documentation link I provided here, and you will understand how that assignment calculation works.
Sorry for and yeah it sometimes look like you're always online :) , Appreciate your help / assistance.
I understand all, but there's something still confused about, what does exactly the operator \ give me?
lets assume I have A=
[1 2
3 4]
and B =
[5 6
6 6]
So what would be the outpit of A\B ? ofcourse I've read the attached doc of mldevide but I didn't understand well because it uses magic and other operators.
In addition, Im asking about the operator \ functionality and how it works because it sounds that I want to implement its functionality and not using any already built libary/operator.
thanks alot!
As always, my pleasure!
No worries!
The easiest way to determine that is to run it:
C = A\B
producing:
C =
-4.0000 -6.0000
4.5000 6.0000
It does a least-squares regression of ‘B’ as a function of ‘A’. If these were data, it would essentially be a multiple linear regression with 2 predictor variables and 2 response variables.
To understand the result, this multiplication:
D = A * C
returns ‘B’.
See the documentation for mldivide I linked to earlier for details.
Solved! , all good.
Much Appreciated.
Another question, I still confused why I need this row code:
dy = zci(y);
why wouldn't we use zci immediately? zci is the returned zero crossings points no?
why we need to assign the zero crossing points (zci) by y vector and then store the data to dy?
Appreciate!
I solved it, now I understand, you called to the function that finds the zero crossing points and then passed as parameter the variable y.
Appreciated all good
Another question, does the size of
b = [[1;1] [x(dy(k1)); x(dy(k1)+1)]]\[y(dy(k1)); y(dy(k1)+1)];
is constant and isn't related to the size of the given input? I mean always the size of [[1;1] [x(dy(k1)); x(dy(k1)+1)]] is 2x2 and the size of [y(dy(k1)); y(dy(k1)+1)] 1x2 ?
thanks for any help.
Another question, I still confused why I need this row code:
dy = zci(y);
My ‘zci’ function find the ap[proximate indices of the zero-crossings. I wrote it as a function to make it easier to use. Any vector will work for ‘v’, and here I used ‘y’, since only the dependent variable should have a significant number of zero-crossings.
Another question, does the size of
b = [[1;1] [x(dy(k1)); x(dy(k1)+1)]]\[y(dy(k1)); y(dy(k1)+1)];
is constant and isn't related to the size of the given input? I mean always the size of [[1;1] [x(dy(k1)); x(dy(k1)+1)]] is 2x2 and the size of [y(dy(k1)); y(dy(k1)+1)] 1x2 ?
Yes. It only calculates the intercept and slope for the two adjacent points, then calculates a much more precise estimate for the actual zero-crossings from them, assuming that ‘y’ in that region is acceptably linear. (I could have used other functions, such as polyfit for the slope and intercept, or interp1 aloone to find the zero crossings, however the approach using the linear regression using mldivide is faster and more efficient.)
I have absolutely no idea what you are doing.
I cannot comment further.

Sign in to comment.

More Answers (0)

Categories

Find more on Descriptive Statistics and Insights in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!