Error while plotting zero crossing detector
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;
Accepted Answer
More Answers (0)
Categories
Find more on Signal Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!