Could someone please explain me this code?

조회 수: 7 (최근 30일)
Anitha Limann
Anitha Limann 2021년 10월 17일
댓글: Star Strider 2021년 10월 17일
x = linspace(min([x1,x2],[],2), max([x1,x2],[],2))
y1 = interp1(x1, y1, x, 'pchip','extrap')
y2 = interp1(x2, y2, x, 'pchip','extrap')
index = find(diff(sign(y1-y2)))
for k = 1:numel(index)
indexrange = max(1,index(k)-2) : min(numel(x),index(k)+2);
xi(k) = interp1(y1(indexrange)-y2(indexrange), x(indexrange), 0);
yi(k) = interp1(x(indexrange), y1(indexrange), xi(k));
end
Xc = [xi; yi]
Could you please explain me all the steps of this code?

채택된 답변

Star Strider
Star Strider 2021년 10월 17일
That looks like something I wrote!
It would be helpful to know the context (I don’t remember where I posted it), however it appears that ‘x1’ and ‘x2’ are vectors spanning different ranges. The ‘x’ assignment produces a vector that spans both ranges, The ‘y1’ and ‘y2’ assignments interpolate and extrapolate the original ‘y1’ and ‘y2’ to the new ‘x’, and the ‘index’ assignment returns the approximate intersection indices of ‘y1’ and ‘y2’. The loop then interpolates first to find the accurate ‘x’ value of the intersection (as ‘xi’), and then uses that value to interpolate the accurate ‘y’ value of the intersection (as ‘yi’). The ‘indexrange’ vector are the indices of both vectors for the interpolation, and is constructed so that the first value is never less than 1 and the last value is never greater than the length of the vector. The ‘Xc’ matrix concatenates them so that ‘xi’ is the first row and ‘yi’ is the second row. This simply makes it easier to view the result of the loop.
I usually comment-document my code, so my apologies if I forgot to in that instance.
.

추가 답변 (1개)

Anitha Limann
Anitha Limann 2021년 10월 17일
That is very true!
This is written by you. You had comments there but i was quite confused with the for loop here. and why we have to intrapolate and extrapolate x and y. Also i am not familiar with numel. That is why i wanted to get a clarification on the code.
I am really thankful for your help at the time and today again!
  댓글 수: 2
Anitha Limann
Anitha Limann 2021년 10월 17일
This is well explained here. Thank you so much!
Star Strider
Star Strider 2021년 10월 17일
As always, my pleasure!
.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Multirate Signal Processing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by