MIMO(matlab)

MIMO(matlab)

第一种方法
代码:

clear all
datasize = 10000000; %仿真的符号数
EbN0 = 0:30; %信噪比
M = 2; %BPSK调制
x = randsrc(2,datasize/2,[0:1]); %数据源符号 2 x 500000
s = randsrc(1,datasize/2,[0:1]); % 1 x 500000
x1 = pskmod(x,M,pi);%BPSK编码
s1 = pskmod(s,M,pi); 

h = randn(2,datasize/2) + j*randn(2,datasize/2); %Rayleigh衰落信道
h = h./sqrt(2); %能量归一化
 
for index=1:length(EbN0)
    
   
 %1发1收
    sigma1 = sqrt(1/(2*10.^(EbN0(index)/10))); %SISO信道高斯白噪声标准差
    n0 = sigma1*(randn(2,datasize/2)+ j*randn(2,datasize/2));
    y1 = x1 + n0./h; %通过SISO瑞利衰落信道后的判决变量
    x2 = pskdemod(y1,M,pi);
    
    %2发1收
    sigma2 = sqrt(1/(10.^(EbN0(index)/10))); %Alamouti方案每个子信道高斯白噪声标准差
    n = sigma2*(randn(2,datasize/2)+j*randn(2,datasize/2));
    n1(1,:) = (conj(h(1,:)).*n(1,:)+h(2,:).*conj(n(2,:)))./(sum(abs(h).^2)); 
    n1(2,:) = (conj(h(2,:)).*n(1,:)-h(1,:).*conj(n(2,:)))./(sum(abs(h).^2)); 
    y2 = x1 + n1;   %Alamouti方案的判决变量
    x3 = pskdemod(y2,M,pi);
    
    %1发2收
    n2=(conj(h(1,:)).*n0(1,:)+conj(h(2,:)).*n0(2,:))./(sum(abs(h).^2));
    y3=s1+n2;
    x4 = pskdemod(y3,M,pi);
    
    [temp,ber2(index)] = biterr(x,x2,log2(M));
    [temp,ber3(index)] = biterr(x,x3,log2(M));
    [temp,ber4(index)] = biterr(s,x4,log2(M));
end
 
semilogy(EbN0,ber2,'-ko',EbN0,ber3,'-kdiamond',EbN0,ber4,'-kv')
grid on
legend(' no diversity(1Tx,1Rx)', 'new scheme(2Tx,1Rx)','MRRC(1Tx,2Rx)')
xlabel('SNR')
ylabel('BER')
axis([0 30 1e-4 1]);
title('MIMO')


最终结果
MIMO(matlab)_第1张图片

小结

1.仿真符号数会影响图像
如果符号数少,会导致图像下部出现失真(将代码中符号数改为1000000即可看出差别)。

第二种方法(以matlab文档为基础)
代码:

frmLen = 100;       % 数据长度
numPackets = 1000;  % 个数
EbNo = 0:2:20;      % 信噪比
N = 2;              % 最大发射天线数
M = 2;              % 最大接收天线数

% BPSK
P = 2;			
hMod   = comm.BPSKModulator;
hDemod = comm.BPSKDemodulator('OutputDataType','double');

% 利用alzmouti方法
hAlamoutiEnc = comm.OSTBCEncoder;
hAlamoutiDec = comm.OSTBCCombiner;

hAWGN1Rx = comm.AWGNChannel('NoiseMethod', 'Signal to noise ratio (Eb/No)',...
                            'SignalPower', 1);
hAWGN2Rx = clone(hAWGN1Rx);

% 误码率计算
hErrorCalc1 = comm.ErrorRate;
hErrorCalc2 = comm.ErrorRate;
hErrorCalc3 = comm.ErrorRate;

% 为提高速度预置
H = zeros(frmLen, N, M);
ber_noDiver  = zeros(3,length(EbNo));
ber_Alamouti = zeros(3,length(EbNo));
ber_MaxRatio = zeros(3,length(EbNo));

grid on;
hold on;
ax = gca;
ax.YScale = 'log';
xlim([EbNo(1), EbNo(end)]);
ylim([1e-4 1]);
xlabel('Eb/No (dB)');
ylabel('BER');
title('1x1,1x2,1x3');

for idx = 1:length(EbNo)
    reset(hErrorCalc1);
    reset(hErrorCalc2);
    reset(hErrorCalc3);
    
    hAWGN1Rx.EbNo = EbNo(idx);
    hAWGN2Rx.EbNo = EbNo(idx);
  
    for packetIdx = 1:numPackets
        % 生成每一帧的数据
        data = randi([0 P-1], frmLen, 1);

        % 调制数据
        modData = step(hMod, data);

        % Alamouti空时编码
        encData = step(hAlamoutiEnc, modData);

        %为发射天线和接收天线构造瑞利衰落响应矩阵
        H(1:N:end, :, :) = (randn(frmLen/2, N, M) + ...
                         1i*randn(frmLen/2, N, M))/sqrt(2);
        % 假设保持两个符号周期不变
        H(2:N:end, :, :) = H(1:N:end, :, :);

        %  H代表1x1, 2x1, 1x2信道
        H11 = H(:,1,1);
        H21 = H(:,:,1)/sqrt(2);
        H12 = squeeze(H(:,1,:));

        % 经过信道传输
        chanOut11 = H11 .* modData;
        chanOut21 = sum(H21.* encData, 2);
        chanOut12 = H12 .* repmat(modData, 1, 2);

        % 添加AWGN(加性高斯白噪声信道)影响
        rxSig11 = step(hAWGN1Rx, chanOut11);
        rxSig21 = step(hAWGN1Rx, chanOut21);
        rxSig12 = step(hAWGN2Rx, chanOut12);

        % Alamouti空时编码合成
        decData = step(hAlamoutiDec, rxSig21, H21);

        % 最大距离检测
        demod11 = step(hDemod, rxSig11.*conj(H11));
        demod21 = step(hDemod, decData);
        demod12 = step(hDemod, sum(rxSig12.*conj(H12), 2));

        ber_noDiver(:,idx)  = step(hErrorCalc1, data, demod11);
        ber_Alamouti(:,idx) = step(hErrorCalc2, data, demod21);
        ber_MaxRatio(:,idx) = step(hErrorCalc3, data, demod12);

    end 

    % 画图
    semilogy(EbNo(1:idx), ber_noDiver(1,1:idx), 'r*', ...
             EbNo(1:idx), ber_Alamouti(1,1:idx), 'go', ...
             EbNo(1:idx), ber_MaxRatio(1,1:idx), 'bs')
           
    legend('1x1', '2x1','1x2');

    drawnow;
end 

fitBER11 = berfit(EbNo, ber_noDiver(1,:));
fitBER21 = berfit(EbNo, ber_Alamouti(1,:));
fitBER12 = berfit(EbNo, ber_MaxRatio(1,:));
semilogy(EbNo, fitBER11, 'r', EbNo, fitBER21, 'g', EbNo, fitBER12, 'b');
hold off;

结果
MIMO(matlab)_第2张图片

你可能感兴趣的:(MIMO,matlab)