New functions to calculate the distance of a point from a line and find all zero crossings from a vector/1-D array.
This commit is contained in:
parent
1ef2cf4ae0
commit
4f28af7e10
@ -0,0 +1,10 @@
|
|||||||
|
function ret = calculateDistanceFromPointToLine(p0 , p1, p2)
|
||||||
|
p01 = p0 - p1;
|
||||||
|
p12 = p2 - p1;
|
||||||
|
CrossProduct = [p01(2)*p12(3) - p01(3)*p12(2), p01(3)*p12(1) - p01(1)*p12(3), p01(1)*p12(2) - p01(2)*p12(1)];
|
||||||
|
ret = rssq(CrossProduct) / rssq(p12);
|
||||||
|
|
||||||
|
%Height of parallelogram (Distance between point and line) = Area of parallelogram / Base
|
||||||
|
%Area = One side of parallelogram X Base
|
||||||
|
%ret = norm(cross(one side, base))./ norm(base);
|
||||||
|
end
|
@ -0,0 +1,18 @@
|
|||||||
|
function ret = findAllZeroCrossings(x,y)
|
||||||
|
% Finds all Zero-crossing of the function y = f(x)
|
||||||
|
zci = @(v) find(v(:).*circshift(v(:), [-1 0]) <= 0); % Returns Approximate Zero-Crossing Indices Of Argument Vector
|
||||||
|
zxidx = zci(y);
|
||||||
|
if ~isempty(zxidx)
|
||||||
|
for k1 = 1:numel(zxidx)
|
||||||
|
idxrng = max([1 zxidx(k1)-1]):min([zxidx(k1)+1 numel(y)]);
|
||||||
|
xrng = x(idxrng);
|
||||||
|
yrng = y(idxrng);
|
||||||
|
[yrng2, ~, jyrng] = unique(yrng); %yrng is a new array containing the unique values of yrng. jyrng contains the indices in yrng that correspond to the original vector. yrng = yrng2(jyrng)
|
||||||
|
xrng2 = accumarray(jyrng, xrng, [], @mean); %This function creates a new array "xrng2" by applying the function "@mean" to all elements in "xrng" that have identical indices in "jyrng". Any elements with identical X values will have identical indices in jyrng. Thus, this function creates a new array by averaging values with identical X values in the original array.
|
||||||
|
ret(k1) = interp1( yrng2(:), xrng2(:), 0, 'linear', 'extrap' );
|
||||||
|
end
|
||||||
|
else
|
||||||
|
warning('No zero crossings found!')
|
||||||
|
ret = nan;
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue
Block a user