Cannot run 16-PSK code in Switch case inside of another switch case
조회 수: 1 (최근 30일)
이전 댓글 표시
Here's the 16-PSK modulation code I tried:
kanal = get(handles.popupmenu1,'Value');
switch kanal
case 1
bitt=randi([0 1],12,1);
all_bit=sprintf('%d', bitt);
n_bit=length(all_bit);
sampling_bit=50;
bit=[];
for i=1:length(all_bit)
if all_bit(i)~=dec2bin(0) && all_bit(i)~=dec2bin(1)
set_ok=0;
break
else
set_ok=1;
end
end
for n=1:1:length(all_bit);
if all_bit(n)==dec2bin(0)
bit=[bit 1*zeros(1,sampling_bit)];
elseif all_bit(n)==dec2bin(1)
bit=[bit 1*ones(1,sampling_bit)];
end
end
plot(bit,'LineWidth',1.5);
xlabel('ms');
ylabel('amplitude');
grid on;
axis([0 n_bit*sampling_bit -2 2]);
xticks([0:sampling_bit:n_bit*sampling_bit]);
yticks([-2:1:2]);
ax=gca;
ax.XTickLabelRotation = -90;
freq=100;
amp=1;
cycle=2*pi;
ts = 0.0001:0.0002:0.01;
n_psk=16;
n_bit=length(all_bit);
nb_psk=log2(n_psk);
n_simbol=n_bit/nb_psk;
l_ts=length(ts);
ph=[];
simbol=[];
tempSymbol= [];
modulatedSignal=[];
stddev=1;
urutan_konstelasi_sudut=[0 1 3 2 7 6 4 5 15 14 12 13 8 9 11 10];
for i=1:n_psk/2
for j=i:i
urutan_konstelasi_sudut=[urutan_konstelasi_sudut j];
end
for k=n_psk-i+1:n_psk-i+1
urutan_konstelasi_sudut=[urutan_konstelasi_sudut mod(k,n_psk)];
end
end
for i=1:n_psk
ph=[ph urutan_konstelasi_sudut(i)/n_psk*cycle];
simbol=[simbol [amp*sin(cycle*freq*ts+ph(i))]];
end
for i = 1:nb_psk:n_bit-nb_psk+1
tempSymbol = [all_bit(i:i+nb_psk-1)];
for j=0:n_psk-1
if tempSymbol == dec2bin(j,nb_psk)%[000]
modulatedSignal = [modulatedSignal simbol(j*l_ts+1:(j+1)*l_ts)];
end
end
tempSymbol = []; %reset tempSymbol
end
axes(handles.axes2);
hold off;
plot(modulatedSignal,'LineWidth',1.5);grid on;
hold on;
legend('sinyal informasi')
end
the code above was executed successfully. but when i put the code in Switch case inside of another switch case like below:
kanal = get(handles.popupmenu1,'Value');
switch kanal
case 1
mod = get(handles.popupmenu2,'Value');
switch mod
case 1
bitt=randi([0 1],12,1);
all_bit=sprintf('%d', bitt);
n_bit=length(all_bit);
sampling_bit=50;
bit=[];
for i=1:length(all_bit)
if all_bit(i)~=dec2bin(0) && all_bit(i)~=dec2bin(1)
set_ok=0;
break
else
set_ok=1;
end
end
for n=1:1:length(all_bit);
if all_bit(n)==dec2bin(0)
bit=[bit 1*zeros(1,sampling_bit)];
elseif all_bit(n)==dec2bin(1)
bit=[bit 1*ones(1,sampling_bit)];
end
end
plot(bit,'LineWidth',1.5);
xlabel('ms');
ylabel('amplitude');
grid on;
axis([0 n_bit*sampling_bit -2 2]);
xticks([0:sampling_bit:n_bit*sampling_bit]);
yticks([-2:1:2]);
ax=gca;
ax.XTickLabelRotation = -90;
freq=100;
amp=1;
cycle=2*pi;
ts = 0.0001:0.0002:0.01;
n_psk=16;
n_bit=length(all_bit);
nb_psk=log2(n_psk);
n_simbol=n_bit/nb_psk;
l_ts=length(ts);
ph=[];
simbol=[];
tempSymbol= [];
modulatedSignal=[];
stddev=1;
urutan_konstelasi_sudut=[0 1 3 2 7 6 4 5 15 14 12 13 8 9 11 10];
for i=1:n_psk/2
for j=i:i
urutan_konstelasi_sudut=[urutan_konstelasi_sudut j];
end
for k=n_psk-i+1:n_psk-i+1
urutan_konstelasi_sudut=[urutan_konstelasi_sudut mod(k,n_psk)];
end
end
for i=1:n_psk
ph=[ph urutan_konstelasi_sudut(i)/n_psk*cycle];
simbol=[simbol [amp*sin(cycle*freq*ts+ph(i))]];
end
for i = 1:nb_psk:n_bit-nb_psk+1
tempSymbol = [all_bit(i:i+nb_psk-1)];
for j=0:n_psk-1
if tempSymbol == dec2bin(j,nb_psk)%[000]
modulatedSignal = [modulatedSignal simbol(j*l_ts+1:(j+1)*l_ts)];
end
end
tempSymbol = []; %reset tempSymbol
end
axes(handles.axes2);
hold off;
plot(modulatedSignal,'LineWidth',1.5);grid on;
hold on;
legend('sinyal informasi')
end
end
there is an error as follows:
Index in position 1 exceeds array bounds (must not exceed 1).
Error in mod16psk>pushbutton1_Callback (line 186)
urutan_konstelasi_sudut=[urutan_konstelasi_sudut mod(k,n_psk)];
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in mod16psk (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)mod16psk('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.
Did I miss something? Thanks before
댓글 수: 0
채택된 답변
Setsuna Yuuki.
2022년 10월 30일
Hi.
In the second code your use the variable:
mod = get(handles.popupmenu2,'Value');
but this name is a function name.
Change the variable name for other.
mod2 = get(handles.popupmenu2,'Value');
switch mod2
case 1
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!