18 lines
1.1 KiB
Mathematica
18 lines
1.1 KiB
Mathematica
|
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
|