Error while plotting zero crossing detector
조회 수: 2 (최근 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;
댓글 수: 0
채택된 답변
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
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.
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!