Looking for a way to copy an IGOR Pro function in MatLab
3 views (last 30 days)
In Igor pro there is a function called findLevel. Essentially what it does is locate the x coordinate at which the data either passes through or reaches a given y value. In my case my y axis is acceleration and my x axis is time in seconds and I am trying to find all time points wherein the acceleration reaches or passes through the value y=150. However, I haven't found a way to replicate this in MatLab. I've tried using the Interp1 function but this didn't work (possibly because none of the acceleration values are exactly 150?). I was hoping someone might be aware of a way to attack this. Any input would be appreciated.
Star Strider on 29 Jul 2022
You can do something similar with something like this (that I have turned into a funciton) —
x = linspace(0, 10*pi, 250);
y = sin(x) + linspace(-2, 2, numel(x));
level = 1.5;
xv = FindLevel(x, y, level);
plot(xv, ones(size(xv))*level, 'rs')
function xv = FindLevel(x,y,level)
cxi = find(diff(sign(y-level)));
for k = 1:numel(cxi)
idxrng = max(1,cxi(k)-1) : min(numel(x), cxi(k)+1);
xv(k) = interp1(y(idxrng), x(idxrng), level);
The ‘xv’ output of ‘FindLevel’ are the interpolated x-coordinates of the points where the curve equals ‘level’, so they may not be exact points in the ‘x’ vector. Those approximate points (actually indices) are returned by the ‘cxi’ assignment in my function.