# finding zero crossings in a signal

139 views (last 30 days)
SSG_newbiecoder on 6 Jan 2018
Edited: Star Strider on 9 Jan 2018
I need to find out the zero crossing points in a signal. How can I achieve this in matlab? I tried the code below but it is not working for my signal.Can anybody help me out?
t = [1:0.01:5]; % Time Vector
y = -2+2.*sin(2*pi*t); % Signal
zci = @(v) find(v(:).*circshift(v(:), [-1 0]) <= 0); % Returns Zero-Crossing Indices Of Argument Vector
zx = zci(y); % Approximate Zero-Crossing Indices
figure(1)
plot(t, y, '-r')
hold on
plot(t(zx), y(zx), 'bp')
hold off
grid
legend('Signal', 'Approximate Zero-Crossings')

Star Strider on 6 Jan 2018
Edited: Star Strider on 9 Jan 2018
The code works correctly. The amplitude of your signal goes from -4 to 0, so the ‘zero-crossings’ in your signal are correctly detected at the highest value of each cycle.
If you want the indices where the signal crosses the -2 value, the ‘zci’ call should be:
zx = zci(y + 2); % Approximate Zero-Crossing Indices
EDIT #1 (08 Jan 2018 at 13:00 UCT)
‘My issue is that my signal need not have an array index with zero as a value so I'm supposed to take the value as close to zero as possible and consider the index of that value as the zero crossing point.’
My code does exactly that! EDIT #2 (09 Jan 2018 at 00:36 UCT)
If you want to find the midpoint crosses of your waveform, consider using the midcross (link) function.

Birdman on 6 Jan 2018
To find their time values, type
t=t(y==0)
To find their values in y vector, type
y=y(y==0)
##### 2 CommentsShowHide 1 older comment
Birdman on 8 Jan 2018
Ok, then you may change the condition as following
idx=find(y==0);
y(idx+1)
y(idx-1)
This will result in the closest value to zero in your y vector which is dependent on t vector. The result is(for both):
-0.0039 -0.0039 -0.0039 -0.0039
What if your t vector's increment change? For instance:
t=1:0.001:5;
y=-2+2.*sin(2*pi*t);
Then, when the same procedure applies,
idx=find(y==0);
y(idx+1)
y(idx-1)
The result is:
1.0e-04 *
-0.3948 -0.3948 -0.3948 -0.3948
which is different than before. Hope this helps.