Em có đoạn coed mô phỏng điều chế số sau nhưng khi mô phỏng thì báo lỗi, em đã sửa nhưng vẫn không được. Anh chị em nào biết lỗi chổ nào thì chỉnh sửa hay giải thích giúp em với. Đoạn code như sau:
% code final tinh ber cho bpsk, qpsk, 16qam, 64qam tren kenh fading + awgn
clc; clear all; close all;
tic;
N = 3e5;
t1=cputime;
Rb = 2e5;%
Tb = 1/Rb;
fc = 2.5e9; %0.9e9;%
fs = 16e8;
Ts = 1/fs;
M = 16; k = log2(M); L = 2^(k/2);
ks = fs/Rb;
kqpsk = log2(4);
% kenh truyen fading
v = 10;% toc do di chuyen
fd = (v*1e3/3600)*fc/(3e8);

% vong lap theo tung gia tri EbN0dB
EbN0dB = 1:2:45; % energy of bit per noise (dB)
EbN0 = 10.^(EbN0dB/10);
interr = 1;

% khoi tao du lieu
data_int = randint(1,N);

frame_len = 3e4; %mot frame co 30.000 bit
soframe = N/frame_len; % tong so frame
datafr = zeros(1,frame_len);

tx = [];
fadtx = [];

neqpsk = 0;
datarx = [];
ne = zeros(1,length(EbN0dB));
ne1 = zeros(1,length(EbN0dB));
ne2 = zeros(1,length(EbN0dB));
ber = zeros(1,length(EbN0dB));
ber1 = zeros(1,length(EbN0dB));
ber2 = zeros(1,length(EbN0dB));
fr_lentx = length(tx)/soframe;

for i=1:length(EbN0dB)
tic;


fprintf('EbN0dB : %d\n', EbN0dB(i));
% so lan lap cho mot gia tri EbN0
% khoi tao cac gia tri number of error cho qam

%%%%%%%%%%%%%%%%%
%% dieu che thich nghi QPSK
if(EbN0dB(i)> 27)
for inter = 1:interr
fprintf('\ninter :');fprintf('%d',inter);
chan1 = stdchan(1/Rb,fd,'cost207RAx4');
chan1.NormalizePathGains = 1;
chan1.ResetBeforeFiltering = 0;
chan1.StoreHistory = true;

fprintf('\n Frame:');
for fr =1:soframe

fprintf(' %d', fr);
% dieu che
datafr(fr, = data_int((fr-1)*frame_len+1:fr*frame_len);
data = datafr(fr,;
tx = qamModul(data,4);
Eb = sum(abs(tx.*tx))./length(tx);

fad = abs(filter(chan1,ones(size(tx))));
txFade = fad.*tx;
% giai dieu che
rx = txFade;
var = sqrt(Eb./(2*(log2(4))*EbN0(i)));%(fs/2)*
noise = var*(randn(1,(frame_len/(log2(4)))) + 1i*randn(1,(frame_len/(log2(4)))));

% giai dieu che kenh awgn
rxSig = rx + noise; %awgn(rx,EbN0dB(i),'measured','db'); % rx;%;%a
datarx = qamDemodul(rxSig./fad,4);

% tinh tong so loi sau khi giai dieu che
err(fr) = biterr(datarx,data);
end % end for so frame
ne_inter(inter) = sum(abs(err));
end
%% dieu che thich nghi 16QAM neu EBN0dB = [21,27]
elseif (EbN0dB(i) >= 21 && EbN0dB(i) <= 27)
for inter = 1:interr
fprintf('\ninter :');fprintf('%d',inter);
chan1 = stdchan(1/Rb,fd,'cost207RAx4');
chan1.NormalizePathGains = 1;
chan1.ResetBeforeFiltering = 0;
chan1.StoreHistory = true;

fprintf('\n Frame:');
for fr =1:soframe

fprintf(' %d', fr);
% dieu che
datafr(fr, = data_int((fr-1)*frame_len+1:fr*frame_len);
data = datafr(fr,;
tx = qamModul(data,16);
Eb = sum(abs(tx.*tx))./length(tx);%)/log2(M3));%length(tx_qam16); %(frame_len/log2(M3));%

fad = abs(filter(chan1,ones(size(tx))));
txFade = fad.*tx;
% giai dieu che
rx = txFade;
var = sqrt(Eb./(2*(log2(16))*EbN0(i)));%(fs/2)*
noise = var*(randn(1,(frame_len/(log2(16)))) + 1i*randn(1,(frame_len/(log2(16)))));

% giai dieu che kenh awgn
rxSig = rx + noise; %awgn(rx,EbN0dB(i),'measured','db'); % rx;%;%a
datarx = qamDemodul(rxSig./fad,16);

% tinh tong so loi sau khi giai dieu che
err(fr) = biterr(datarx,data);
end % end for so frame
ne_inter(inter) = sum(abs(err));
end
%% dieu che 64QAM neu EbN0 <21
else (EbN0dB(i) < 21)
for inter = 1:interr
fprintf('\ninter :');fprintf('%d',inter);
chan1 = stdchan(1/Rb,fd,'cost207RAx4');
chan1.NormalizePathGains = 1;
chan1.ResetBeforeFiltering = 0;
chan1.StoreHistory = true;

fprintf('\n Frame:');
for fr =1:soframe

fprintf(' %d', fr);
% dieu che
datafr(fr, = data_int((fr-1)*frame_len+1:fr*frame_len);
data = datafr(fr,;
tx = qamModul(data,64);
Eb = sum(abs(tx.*tx))./length(tx);%)/log2(M3));%length(tx_qam16); %(frame_len/log2(M3));%

fad = abs(filter(chan1,ones(size(tx))));
txFade = fad.*tx;
% giai dieu che
rx = txFade;
var = sqrt(Eb./(2*(log2(64))*EbN0(i)));%(fs/2)*
noise = var*(randn(1,(frame_len/(log2(64)))) + 1i*randn(1,(frame_len/(log2(64)))));

% giai dieu che kenh awgn
rxSig = rx + noise; %awgn(rx,EbN0dB(i),'measured','db'); % rx;%;%a
datarx = qamDemodul(rxSig./fad,64);

% tinh tong so loi sau khi giai dieu che
err(fr) = biterr(datarx,data);
end % end for so frame
ne_inter(inter) = sum(abs(err));
end


end % end vong if - else
%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%
%% dieu che qpsk khong su dung thich nghi
for inter = 1:interr
fprintf('\ninter :');fprintf('%d',inter);
chan1 = stdchan(1/Rb,fd,'cost207RAx4');
chan1.NormalizePathGains = 1;
chan1.ResetBeforeFiltering = 0;
chan1.StoreHistory = true;

fprintf('\n Frame:');
for fr =1:soframe
fprintf(' %d', fr);
datafr(fr, = data_int((fr-1)*frame_len+1:fr*frame_len);
data = datafr(fr,;
tx1 = qamModul(data,4);
Eb1 = sum(abs(tx1.*tx1))./length(tx1);%)/log2(M3));%length(tx_qam16); %(frame_len/log2(M3));%
fad1 = abs(filter(chan1,ones(size(tx1))));
txFad1 = fad1.*tx1;
% giai dieu che
rx = txFad1;

var1 = sqrt(Eb1./(2*(log2(4))*EbN0(i)));%(fs/2)*
noise1 = var1*(randn(1,(frame_len/(log2(4)))) + 1i*randn(1,(frame_len/(log2(4)))));

% giai dieu che kenh awgn
rx1 = rx + noise1; %awgn(rx,EbN0dB(i),'measured','db'); % rx;%;%a
datarx1 = qamDemodul(rx1./fad1,4);

% tinh tong so loi sau khi giai dieu che
err1(fr) = biterr(datarx1,data);
end % end if - else
ne_inter1 = sum(abs(err1));
end % end for inter

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% dieu che 16 QAM khong thich nghi

for inter = 1:interr
fprintf('\ninter :');fprintf('%d',inter);
chan1 = stdchan(1/Rb,fd,'cost207RAx4');
chan1.NormalizePathGains = 1;
chan1.ResetBeforeFiltering = 0;
chan1.StoreHistory = true;

fprintf('\n Frame:');
for fr =1:soframe
fprintf(' %d', fr);
datafr(fr, = data_int((fr-1)*frame_len+1:fr*frame_len);
data = datafr(fr,;
tx2 = qamModul(data,16);
Eb2 = sum(abs(tx2.*tx2))./length(tx2);%)/log2(M3));%length(tx_qam16); %(frame_len/log2(M3));%
fad2 = abs(filter(chan1,ones(size(tx2))));
txFad2 = fad2.*tx2;
% giai dieu che
rx2 = txFad2;

var2 = sqrt(Eb2./(2*(log2(16))*EbN0(i)));%(fs/2)*
noise2 = var2*(randn(1,(frame_len/(log2(16)))) + 1i*randn(1,(frame_len/(log2(16)))));

% giai dieu che kenh awgn
rx2 = rx2 + noise2; %awgn(rx,EbN0dB(i),'measured','db'); % rx;%;%a
datarx2 = qamDemodul(rx2./fad2,16);

% tinh tong so loi sau khi giai dieu che
err2(fr) = biterr(datarx2,data);
end % end if - else
ne_inter2 = sum(abs(err2));
end % end for inter

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% dieu che 64 QAM khong thich nghi

for inter = 1:interr
fprintf('\ninter :');fprintf('%d',inter);
chan1 = stdchan(1/Rb,fd,'cost207RAx4');
chan1.NormalizePathGains = 1;
chan1.ResetBeforeFiltering = 0;
chan1.StoreHistory = true;

fprintf('\n Frame:');
for fr =1:soframe
fprintf(' %d', fr);
datafr(fr, = data_int((fr-1)*frame_len+1:fr*frame_len);
data = datafr(fr,;
tx3 = qamModul(data,64);
Eb3 = sum(abs(tx3.*tx3))./length(tx3);%)/log2(M3));%length(tx_qam16); %(frame_len/log2(M3));%
fad3 = abs(filter(chan1,ones(size(tx3))));
txFad3 = fad3.*tx3;
% giai dieu che
rx3 = txFad3;

var3 = sqrt(Eb3./(2*(log2(64))*EbN0(i)));%(fs/2)*
noise3 = var3*(randn(1,(frame_len/(log2(64)))) + 1i*randn(1,(frame_len/(log2(64)))));

% giai dieu che kenh awgn
rx3 = rx3 + noise3; %awgn(rx,EbN0dB(i),'measured','db'); % rx;%;%a
datarx3 = qamDemodul(rx3./fad3,64);

% tinh tong so loi sau khi giai dieu che
err3(fr) = biterr(datarx3,data);
end % end if - else
ne_inter3 = sum(abs(err3));
end % end for inter
%%%%%%%%%%%%%%%%%%%%%%%%%
%% tinh so bit loi
ne(i) = sum(abs(ne_inter))/interr;
ne1(i) = sum(abs(ne_inter1))/interr;
ne2(i) = sum(abs(ne_inter2))/interr;
ne3(i) = sum(abs(ne_inter3))/interr;

fprintf(' Ne(%d): %d\t',i,ne(i));
%disp(ne);disp(ne1);disp(ne2);
ber(i) = ne(i)/N;
ber1(i) = ne1(i)/N;
ber2(i) = ne2(i)/N;
ber3(i) = ne3(i)/N;
toc
end % end for EbN0

% tinh ber theo ly thuyet
qpskFad = berfading(EbN0dB,'qam',4,1);
qam16Fad = berfading(EbN0dB,'qam',16,1);
qam64Fad = berfading(EbN0dB,'qam',64,1);
%save('berqamAdap_1','N','M','Rb','fc','v','ne','n e1','ne2','ber','ber1','ber2','chan1','chan2');
%ve gian do ber - ebn0
%figure;
semilogy(EbN0dB,ber,'g-o','Linewidth',4); hold on;grid on;
semilogy(EbN0dB,qpskFad,'r-.'); hold on;
semilogy(EbN0dB,qam16Fad,'b-.'); hold on;
semilogy(EbN0dB,qam64Fad,'k-.'); hold on;
semilogy(EbN0dB,ber1,'r-x','Linewidth',2); hold on;
semilogy(EbN0dB,ber2,'b-x','Linewidth',2); hold on;
semilogy(EbN0dB,ber3,'k-x','Linewidth',2); hold on;
legend('Adaptive QAM','QPSK fading theory','QAM16 fading theory','QAM64 fading theory',...
'QPSK kenh Rural Area','16QAM kenh Rural Area','64QAM kenh Rural Area');
%axis([0 45 10^-4 1]);
title(['BER adaptive QAM tren kenh truyen fading Rural Area voi Rb ',int2str(Rb/1e3),'kbps, fc ',int2str(fc/1e6),'MHz, v ',int2str(v),'km/h']);
%grid on;

t2=cputime;
tongsophut1 = (t2-t1)/60;
disp(tongsophut1);

Chi tiết anh chị em liên lạc vơi em qua email: nguyenhuuan1008@gmail.com
Thank you very much!