# zero crossings with interpolation

51 views (last 30 days)
Mohamed Jamal on 13 Jul 2020
Commented: Sanders A. on 6 Jan 2023
Hi guys!
I have Ac singal as what Im attaching here(see photo down) , ofcourse , my AC signal ofcourse it's implicitly vector sized as 1X32000 samples. (vectors that its values are samples). Im trying to do zero crossing in matlab in order to detect where I have edges! , I's preferable to do that and find the approximated point on the edge that I have the zero crossings by interpolation(if there's another good way and it's optimal approximation to find the point at which there's the zero crossing would be accepted .. but to be assure Im dealing with AC signal this means analog "continues values" and not discrete .. so interpolation is very good for me to find zero crossings), once again it's right that my ac signal is a vector of samples but I take time (x-axis) into consideration .. so it's continues .. and it's suitable and better in my case to detect zero crossings by interpolation!
Any help please how can I do zero crossings by interpolation in matlab? really appreciated for any help.
lets assume that my ac signal (vector of samples) is variable b, also I want to plot the zero crossings as returned values and to show on the same ac signal(my input) the zero cossings like dots/marks when there's zero crossings. why I need to do interpolation method for zero crossings in my case? because my signal is about samples that I sampled .. attaching down a photo of what I mean : KSSV on 13 Jul 2020
Mohamed Jamal on 13 Jul 2020
Hi , he doesn't use interpolation method for zero crossings!!!!! your link is not helping me .. thanks anyway

neil jerome on 13 Jul 2020
LOTS of ways to do this!
i've 'over-commented' this code to show the logic - this is deliberately not the fastest or most compact way, but should be understandable... :)
good luck!
% commented find zero crossings
%% set up arbitrary signal (sum of 2 sines for demo)
x = 0:0.01:4;
sin1 = 10*sin(10*x); sin2 = 42*sin(6*x);
signal = sin1+sin2;
% plot to show signal
figure;
plot(x, signal, 'bo-');
hold on;
plot(x, zeros(1,legth(x)), 'k:');
%% find next point after zero crossing
sigPos = logical(signal>0); % find all positive points
cross = sigPos - circshift(sigPos,1); % find changeover points
plot(x(logical(cross)), signal(logical(cross)), 'ko');
%% assume straight line approximation between adjacent points
crossInd = find(cross); % x indices of cross points
nCross = length(crossInd); % number of cross points found
x0 = NaN(1,nCross); % vector of x-values for cross points
for aa = 1:nCross
thisCross = crossInd(aa);
% interpolate to get x coordinate of approx 0
x1 = thisCross-1; % before-crossing x-value
x2 = thisCross; % after-crossing x-value
y1 = signal(thisCross-1); % before-crossing y-value
y2 = signal(thisCross); % after-crossing y-value
ratio = (0-y1) / (y2-y1); % interpolate between to find 0
x0(aa) = x(x1) + (ratio*(x(x2)-x(x1))); % estimate of x-value
end
plot(x0, zeros(1,nCross), 'ro');
Sanders A. on 6 Jan 2023
I was able to fix the error mentioned by Mohamed and Ivy by making sure that there wasn't an erroneous zero crossing detected by having opposite signal(1) and signal (end) signs.
crossInd = find(cross); % x indices of cross points
if crossInd(1) == 1
crossInd(1) = [];
end
nCross = length(crossInd); % number of cross points found
For whomever may show up and find this very useful code later!