Error while plotting zero crossing detector
7 views (last 30 days)
Show older comments
I am trying to plot zero crossing detector in matlab using interpolation for a sine wave
How to remove this red marker in code?

What am i doing wrong in code?
clc;
clear all;
close all;
% CONSTANTS
fs=2400;
fo=50;
t=0:200;
t2=0.5;
% Geneeration of sine wave
xp=sin(2*pi*(fo/fs)*t);
% figure(3)
% plot(t,xp,'b');
% grid on;
% legend('sine signal')
% upward zero crossing detector
UpZC = @(a) find(a(1:end-1) <= 0 & a(2:end) > 0);
% downward zero crossing detector
DownZC = @(a) find(a(1:end-1) >= 0 & a(2:end) < 0);
% interploator
ZeroX = @(x0,y0,x1,y1) x0 - (y0.*(x0 - x1))./(y0 - y1);
ZXi = sort([UpZC(xp),DownZC(xp)]);
% zerocrossing detector with interpolation
ZX = ZeroX(t(ZXi),xp(ZXi),t(ZXi+1),xp(ZXi+1));
if xp(end)==0
ZX(end+1) = t(end);
end
xx= zeros(1,length(ZX));
xx1= zeros(1,length(ZX));
for i=1:length(ZX)
if rem(i,2)==1
% xx(1,i)=ZX(i)+t2;
xx1(1,i)=ZX(i)-t2;
else
% xx(1,i)=ZX(i)-t2;
xx1(1,i)=ZX(i)+t2;
end
end
y1= sin(2*pi*(fo/fs)*t2);
% figure(2)
% plot(t,xp, '-b')
% hold on;
% plot(ZX,zeros(1,length(ZX)),'ro')
% grid on;
% legend('Signal', 'Interpolated Zero-Crossing')
figure(1)
plot(t,xp, '-b')
hold on;
% plot(xx,zeros(1,length(ZX))+y1,'r*')
hold on
plot(xx1,zeros(1,length(ZX))-y1,'r*')
hold on
plot(ZX,zeros(1,length(ZX)),'go')
grid on;
0 Comments
Accepted Answer
the cyclist
on 9 Jan 2023
To remove all the red stars, comment out this line:
plot(xx1,zeros(1,length(ZX))-y1,'r*')
To remove only the first red star, change that line to
plot(xx1(2:end),zeros(1,length(ZX)-1)-y1,'r*')
2 Comments
Walter Roberson
on 9 Jan 2023
Edited: Walter Roberson
on 9 Jan 2023
In newer versions of MATLAB, you can also control which points the marker gets plotted for using the MarkerIndices option. So in theory you could instead
plot(xx1, zeros(1,length(ZX))-y1, 'r*', 'MarkerIndices', 2:length(xx1))
....which will not make any visible difference when you are only plotting markers.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!