필터 지우기
필터 지우기

rounding issues in matlab, need to force values to 0

조회 수: 1 (최근 30일)
bob
bob 2022년 2월 2일
답변: Benjamin Thompson 2022년 2월 2일
How do force matlab to set specfic function values at 0 instead of 1.34e-18 or something small. the if and else statements dont work and the treshold code (X(X<treshold))=0; doesnt work as that defaults some of my Xn (y1.....y4) values to be null/0 and phase becomes 0.
%y2 = 100e-3*1/5*sinc(n*1/5);
%y3 = 100e-3*1/7*sinc(n*1/7);
%y4 = 100e-3*1/2*( sinc (n*1/2) ).^2;
n = -15:1:15;
y1 = 100e-3*1/2*sinc(n*1/2);
if abs(y1)< 1e-6
y1=0;
end
figure(1)
stem(n,abs(y1),'r','LineWidth',1.4)
ylabel('Xn (Volts)'); xlabel('n'); title(' Pulse Amplitude Spectrum [DC=1/2]'); grid on
figure(2)
stem(n,angle(y1)*180/pi,'b','LineWidth',1.4)
ylabel('Phase (Degrees)'); xlabel('n'); title('Pulse Phase Spectrum [DC=1/2]'); grid on
%{
figure(3)
stem(n,abs(y2),'r','LineWidth',1.4)
ylabel('Xn (Volts)'); xlabel('n'); title('Pulse Amplitude Spectrum [DC=1/5]'); grid on
figure(4)
stem(n,angle(y2)*180/pi,'b','LineWidth',1.4)
ylabel('Phase (Degrees)'); xlabel('n'); title(' Pulse Phase Spectrum [DC=1/5]'); grid on
i dont want to recreate my code and use loops, any ideas how to force those super small values to 0, so that they dont affect my phase diagram
  댓글 수: 1
Benjamin Thompson
Benjamin Thompson 2022년 2월 2일
You can use an index vector with ones in the spots that you want to replace with zero, similar to:
>> X
X =
1.0e-06 *
0.6555
0.1712
0.7060
0.0318
0.2769
>> I = (abs(X) < 1e-7)
I =
5×1 logical array
0
0
0
1
0
>> X(I) = 0
X =
1.0e-06 *
0.6555
0.1712
0.7060
0
0.2769

댓글을 달려면 로그인하십시오.

채택된 답변

James Tursa
James Tursa 2022년 2월 2일
편집: James Tursa 2022년 2월 2일
Instead of
if abs(y1)< 1e-6
y1=0;
end
try
y1(abs(y1)<1e-6) = 0;

추가 답변 (1개)

Benjamin Thompson
Benjamin Thompson 2022년 2월 2일
You can use an index vector with ones in the spots that you want to replace with zero, similar to:
>> X
X =
1.0e-06 *
0.6555
0.1712
0.7060
0.0318
0.2769
>> I = (abs(X) < 1e-7)
I =
5×1 logical array
0
0
0
1
0
>> X(I) = 0
X =
1.0e-06 *
0.6555
0.1712
0.7060
0
0.2769

카테고리

Help CenterFile Exchange에서 Discrete Fourier and Cosine Transforms에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by