Error while plotting zero crossing detector

7 次查看(过去 30 天)
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;

采纳的回答

the cyclist
the cyclist 2023-1-9
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 个评论
Walter Roberson
Walter Roberson 2023-1-9
编辑:Walter Roberson 2023-1-9
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.
Shanmuka
Shanmuka 2023-1-9
Thank you!!!.
I am trying to remove to first red marker as signal starting from zero. Thank you again.

请先登录,再进行评论。

更多回答(0 个)

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by