四舍五入到最接近的十分之一
Rounding up to the nearest tenth
如果 cx_n
值与 yt
不匹配,我将尝试将 cx_n
向上舍入到最接近的数字。使用下面的代码,我可以四舍五入,但它一直到 1。例如,
cx_n
值之一是 0.58,我想四舍五入 0.6,而不是 1.0。我怎样才能做到这一点?
这是我的 MATLAB 代码:
Fn = 3; %Nyquist Frequency
Fnr = 6; %Nyquist Rate
Fs = Fnr*5; %Sampling frequency
Ts = 1 / Fs; %Sampling period
T = 1 / 3;
N = T / Ts; %Number of samples per period
start = 0;
stop =N-1;
c_increment = 1;
c_n = start:c_increment:stop;
c_nTs = c_n*Ts;
cx_n = sin(2*pi*3*c_nTs);
for m_d = 1:length(cx_n)
for quantisation = 1:length(yt)
if (m_d ~= quantisation )
p = round(cx_n);
stem(c_nTs, p);
end
end
end
只需将 p = round(cx_n);
替换为 p = round(cx_n,1);
参见 MATLAB 文档:round
Y = round(X,N) rounds to N digits:
- N > 0: round to N digits to the right of the decimal point.
- N = 0: round to the nearest integer.
- N < 0: round to N digits to the left of the decimal point.
如果 cx_n
值与 yt
不匹配,我将尝试将 cx_n
向上舍入到最接近的数字。使用下面的代码,我可以四舍五入,但它一直到 1。例如,
cx_n
值之一是 0.58,我想四舍五入 0.6,而不是 1.0。我怎样才能做到这一点?
这是我的 MATLAB 代码:
Fn = 3; %Nyquist Frequency
Fnr = 6; %Nyquist Rate
Fs = Fnr*5; %Sampling frequency
Ts = 1 / Fs; %Sampling period
T = 1 / 3;
N = T / Ts; %Number of samples per period
start = 0;
stop =N-1;
c_increment = 1;
c_n = start:c_increment:stop;
c_nTs = c_n*Ts;
cx_n = sin(2*pi*3*c_nTs);
for m_d = 1:length(cx_n)
for quantisation = 1:length(yt)
if (m_d ~= quantisation )
p = round(cx_n);
stem(c_nTs, p);
end
end
end
只需将 p = round(cx_n);
替换为 p = round(cx_n,1);
参见 MATLAB 文档:round
Y = round(X,N) rounds to N digits:
- N > 0: round to N digits to the right of the decimal point.
- N = 0: round to the nearest integer.
- N < 0: round to N digits to the left of the decimal point.