フィルターのクリア

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 Center および File ExchangeDiscrete 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