Azzera filtri
Azzera filtri

Index exceeds the number of array elements (4)

2 visualizzazioni (ultimi 30 giorni)
Tai Phan
Tai Phan il 10 Giu 2019
Risposto: Walter Roberson il 26 Apr 2022
Hi all,
I try to run the code about Alamouti_scheme but I had an error "Index exceeds the number of array elements (4)", could you guys please help me find out my problem
Thank you very much
function [mod_symbols,sym_table,M] = modulator(bitseq,b)
%MIMO-OFDM Wireless Communications with MATLAB¢ç Yong Soo Cho, Jaekwon Kim, Won Young Yang and Chung G. Kang
%2010 John Wiley & Sons (Asia) Pte Ltd
N_bits = length(bitseq);
sq10=sqrt(10);
if b==1 % BPSK modulation
sym_table=exp(j*[0 -pi]); sym_table=sym_table([1 0]+1);
inp=bitseq; mod_symbols=sym_table(inp+1);
M=2;
elseif b==2 % QPSK modulation
sym_table = exp(j*pi/4*[-3 3 1 -1]); sym_table=sym_table([0 1 3 2]+1);
inp=reshape(bitseq,b,N_bits/b);
%mod_symbols=sym_table(565);
mod_symbols=sym_table([2 1]*inp+1);
M=4;
elseif b==3 % generates 8PSK symbols
sym_table=exp(j*pi/4*[0:7]); sym_table=sym_table([0 1 3 2 6 7 5 4]+1);
inp=reshape(bitseq,b,N_bits/b); mod_symbols=sym_table([4 2 1]*inp+1);
M=8;
elseif b==4 % 16-QAM modulation
m=0; sq10=sqrt(10);
for k=-3:2:3
for l=-3:2:3
m=m+1; sym_table(m) = (k+j*l)/sq10; % power normalization
end
end
sym_table = sym_table([0 1 3 2 4 5 7 6 12 13 15 14 8 9 11 10]+1); % Gray code mapping pattern for 8-PSK symbols
inp = reshape(bitseq,b,N_bits/b);
mod_symbols = sym_table([8 4 2 1]*inp+1); % maps transmitted bits into 16QAM symbols
M=16; %16 constellation points
else
error('Unimplemented modulation');
end
Alamouti_scheme.m
%MIMO-OFDM Wireless Communications with MATLAB¢ç Yong Soo Cho, Jaekwon Kim, Won Young Yang and Chung G. Kang
%2010 John Wiley & Sons (Asia) Pte Ltd
clear; clf;
L_frame=130; N_Packets=4000; % Number of frames/packet and Number of packets
NT=2; NR=1; b=2;
SNRdBs=[0:2:30]; sq_NT=sqrt(NT); sq2=sqrt(2);
for i_SNR=1:length(SNRdBs)
SNRdB=SNRdBs(i_SNR); sigma=sqrt(0.5/(10^(SNRdB/10)));
for i_packet=1:N_Packets
msg_symbol=randi(L_frame*b,NT);
tx_bits=msg_symbol.'; tmp=[]; tmp1=[];
for i=1:NT
[tmp1,sym_tab,P] = modulator(tx_bits(i,:),b); tmp=[tmp; tmp1];
end
X=tmp.'; X1=X; X2=[-conj(X(:,2)) conj(X(:,1))];
for n=1:NT
Hr(n,:,:)=(randn(L_frame,NT)+j*randn(L_frame,NT))/sq2;
end
H=reshape(Hr(n,:,:),L_frame,NT); Habs(:,n)=sum(abs(H).^2,2);
R1 = sum(H.*X1,2)/sq_NT + sigma*(randn(L_frame,1)+j*randn(L_frame,1));
R2 = sum(H.*X2,2)/sq_NT + sigma*(randn(L_frame,1)+j*randn(L_frame,1));
Z1 = R1.*conj(H(:,1)) + conj(R2).*H(:,2);
Z2 = R1.*conj(H(:,2)) - conj(R2).*H(:,1);
for m=1:P.
tmp = (-1+sum(Habs,2))*abs(sym_tab(m))^2;
d1(:,m) = abs(sum(Z1,2)-sym_tab(m)).^2 + tmp;
d2(:,m) = abs(sum(Z2,2)-sym_tab(m)).^2 + tmp;
end
[y1,i1]=min(d1,[],2); S1d=sym_tab(i1).'; clear d1
[y2,i2]=min(d2,[],2); S2d=sym_tab(i2).'; clear d2
Xd = [S1d S2d]; tmp1=X>0; tmp2=Xd>0;
noeb_p(i_packet) = sum(sum(tmp1~=tmp2));% for coded
end % End of FOR loop for i_packet
BER(i_SNR) = sum(noeb_p)/(N_Packets*L_frame*b);
end % End of FOR loop for i_SNR
semilogy(SNRdBs,BER), axis([SNRdBs([1 end]) 1e-6 1e0]);
grid on; xlabel('SNR[dB]'); ylabel('BER');
and the error is:
>> Alamouti_scheme
Index exceeds the number of array elements (4).
Error in modulator (line 16)
mod_symbols=sym_table([2 1]*inp+1);
Error in Alamouti_scheme (line 16)
[tmp1,sym_tab,P] = modulator(tx_bits(i,:),b); tmp=[tmp; tmp1];

Risposte (2)

Bob Thompson
Bob Thompson il 10 Giu 2019
The error is occuring because sym_table is a 1x4 complex double array, but in the particular line you are attempting to call element 357. I am not sure exactly what you're trying to do with this line, but perhaps indexing is not it? If you are actually trying to index here, you either need to change the value of bitseq, which is significantly greater than sym_table, or you need to make sym_table much much larger.

Walter Roberson
Walter Roberson il 26 Apr 2022
msg_symbol=randi(L_frame*b,NT);
The code was improperly converted from earlier code that was written using randint(). randint() had the size first and the range after. The correct conversion should probably be something like
msg_symbol = randi([0, NT-1], 1, L_frame*b);

Categorie

Scopri di più su Link-Level Simulation in Help Center e File Exchange

Tag

Prodotti


Release

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by