Thank you for quoting my code!
I am not certain what you intend by ‘median’ zero-crossings. It is straightforward to calculate the more precise zero-crossings using simple linear interpolation. (You can use interp1 for this, but the calculations in my code are likely more efficient for this simple problem.)
The Code —
t = linspace(0, 20*pi, 5000);
y = sin(2*pi*t);
zci = @(v) find(v(:).*circshift(v(:), [1 0]) <= 0);
zx = zci(y);
for k1 = 1:numel(zx)-1
tv = t(zx(k1):zx(k1)+1);
yv = y(zx(k1):zx(k1)+1);
b = [[1;1], tv(:)]\yv(:);
ti(k1) = -b(1)/b(2);
end
figure(1)
plot(t, y)
hold on
plot(t(zx), y(zx), 'pg')
plot(ti, zeros(size(ti)), '+r')
hold off
figure(2)
plot(t, y)
hold on
plot(t(zx), y(zx), 'pg')
plot(ti, zeros(size(ti)), '+r')
hold off
axis([60 63 ylim])
Best Answer