我正在开发一个程序,我必须在MATLAB中用16个量化级别量化正弦波
我开发了一个for循环,它应该正确地量化值,并且它对正弦波的正值起作用,但是然后为信号的负值显示全部为零
以下是我开发的代码:
sig1 = [0 0.6428 0.9848 0.8660 0.3420 -0.3420 -0.8660 -0.9848 -0.6428 -0.0000]
b = 4;
N = 10;
yMax = 1.4088;
yMin = -1.3660;
for i = 1:N
value = ((yMax - yMin)/(2^b));
halfstep = value / 2;
%Calculating quantization levels
value0 = value * 0;
value1 = value * 1;
value2 = value * 2;
value3 = value * 3;
value4 = value * 4;
value5 = value * 5;
value6 = value * 6;
value7 = value * 7;
value8 = value * -1;
value9 = value * -2;
value10 = value * -3;
value11 = value * -4;
value12 = value * -5;
value13 = value * -6;
value14 = value * -7;
value15 = value * -8;
%Quantizing signal1
if value15 < sig1(i) < value14
if sig1(i) < value15 + halfstep
qsig1(i) = -8;
else
qsig1(i) = -7;
end
elseif value14 < sig1(i) < value13
if sig1(i) < value14 + halfstep
qsig1(i) = -7;
else
qsig1(i) = -6;
end
elseif value13 < sig1(i) < value12
if sig1(i) < value13 + halfstep
qsig1(i) = -6;
else
qsig1(i) = -5;
end
elseif value12 < sig1(i) < value11
if sig1(i) < value12 + halfstep
qsig1(i) = -5;
else
qsig1(i) = -4;
end
elseif value11 < sig1(i) < value10
if sig1(i) < value11 + halfstep
qsig1(i) = -4;
else
qsig1(i) = -3;
end
elseif value10 < sig1(i) < value9
if sig1(i) < value10 + halfstep
qsig1(i) = -3;
else
qsig1(i) = -2;
end
elseif value9 < sig1(i) < value8
if sig1(i) < value9 + halfstep
qsig1(i) = -2;
else
qsig1(i) = -1;
end
elseif value8 < sig1(i) < value0
if sig1(i) < value8 + halfstep
qsig1(i) = -1;
else
qsig1(i) = 0;
end
elseif value0 < sig1(i) < value1
if sig1(i) < value0 + halfstep
qsig1(i) = 0;
else
qsig1(i) = 1;
end
elseif value1 < sig1(i) < value2
if sig1(i) < value1 + halfstep
qsig1(i) = 1;
else
qsig1(i) = 2;
end
elseif value2 < sig1(i) < value3
if sig1(i) < value2 + halfstep
qsig1(i) = 2;
else
qsig1(i) = 3;
end
elseif value3 < sig1(i) < value4
if sig1(i) < value3 + halfstep
qsig1(i) = 3;
else
qsig1(i) = 4;
end
elseif value4 < sig1(i) < value5
if sig1(i) < value4 + halfstep
qsig1(i) = 4;
else
qsig1(i) = 5;
end
elseif value5 < sig1(i) < value6
if sig1(i) < value5 + halfstep
qsig1(i) = 5;
else
qsig1(i) = 6;
end
elseif value6 < sig1(i) < value7
if sig1(i) < value6 + halfstep
qsig1(i) = 6;
else
qsig1(i) = 7;
end
elseif sig1(i) < value15
qsig1(i) = 0;
end
end
sig1
qsig1
如果有人能帮我弄清楚为什么sig1的负值在qsig1中全部为零,我会很感激!
谢谢!
答案 0 :(得分:0)
经过更多的工作,我发现MATLAB不能进行两次比较,我认为可以这样做,所以我使用和语句来达到预期的效果。
我使用的代码:
if sig1(i) < value15
qsig1(i) = -8;
elseif sig1(i) < value14 && sig1(i) > value15
if value15 < sig1(i) && sig1(i) < (value15 + halfstep)
qsig1(i) = -8;
else
qsig1(i) = -7;
end
elseif sig1(i) < value13 && sig1(i) > value14
if value14 < sig1(i) && sig1(i) < (value14 + halfstep)
qsig1(i) = -7;
else
qsig1(i) = -6;
end
elseif sig1(i) < value12 && sig1(i) > value13
if value13 < sig1(i) && sig1(i) < (value13 + halfstep)
qsig1(i) = -6;
else
qsig1(i) = -5;
end
elseif sig1(i) < value11 && sig1(i) > value12
if value12 < sig1(i) && sig1(i) < (value12 + halfstep)
qsig1(i) = -5;
else
qsig1(i) = -4;
end
elseif sig1(i) < value10 && sig1(i) > value11
if value11 < sig1(i) && sig1(i) < (value11 + halfstep)
qsig1(i) = -4;
else
qsig1(i) = -3;
end
elseif sig1(i) < value9 && sig1(i) > value10
if value10 < sig1(i) && sig1(i) < (value10 + halfstep)
qsig1(i) = -3;
else
qsig1(i) = -2;
end
elseif sig1(i) < value8 && sig1(i) > value9
if value9 < sig1(i) && sig1(i) < (value9 + halfstep)
qsig1(i) = -2;
else
qsig1(i) = -1;
end
elseif sig1(i) < value0 && sig1(i) > value8
if value8 < sig1(i) && sig1(i) < (value8 + halfstep)
qsig1(i) = -1;
else
qsig1(i) = 0;
end
elseif sig1(i) < value1 && sig1(i) > value0
if value0 < sig1(i) && sig1(i) < (value0 + halfstep)
qsig1(i) = 0;
else
qsig1(i) = 1;
end
elseif sig1(i) < value2 && sig1(i) > value1
if value1 < sig1(i) && sig1(i) < (value1 + halfstep)
qsig1(i) = 1;
else
qsig1(i) = 2;
end
elseif sig1(i) < value3 && sig1(i) > value2
if value2 < sig1(i) && sig1(i) < (value2 + halfstep)
qsig1(i) = 2;
else
qsig1(i) = 3;
end
elseif sig1(i) < value4 && sig1(i) > value3
if value3 < sig1(i) && sig1(i) < (value3 + halfstep)
qsig1(i) = 3;
else
qsig1(i) = 4;
end
elseif sig1(i) < value5 && sig1(i) > value4
if value4 < sig1(i) && sig1(i) < (value4 + halfstep)
qsig1(i) = 4;
else
qsig1(i) = 5;
end
elseif sig1(i) < value6 && sig1(i) > value5
if value5 < sig1(i) && sig1(i) < (value5 + halfstep)
qsig1(i) = 5;
else
qsig1(i) = 6;
end
elseif sig1(i) < value7 && sig1(i) > value6
if value6 < sig1(i) && sig1(i) < (value6 + halfstep)
qsig1(i) = 6;
else
qsig1(i) = 7;
end
elseif value7 < sig1(i)
qsig1(i) = 7;
end
答案 1 :(得分:0)
你的决定非常复杂。尝试使用 round 功能:
sig1 = [0 0.6428 0.9848 0.8660 0.3420 -0.3420 -0.8660 -0.9848 -0.6428 -0.0000]
b = 4;
N = 10;
yMax = 1.4088;
yMin = -1.3660;
value = ((yMax - yMin)/(2^b));
gsig1=round(sig1./value);