2016-04-26 57 views
0

我想模擬CRC在瑞利衰落和AWGN中的影響。我的代碼如下:「用錯誤* .Matrix尺寸必須同意」關於通道噪聲添加的「錯誤維度必須一致」

clear 
NoBits =4; % number of bits 
noPacket=4; 

%-------------------------At the transmitter------------------------------- 

DataIn =randi([0,1],noPacket,NoBits); % generating 0,1 with equal probability 

%~~~~~~~~~~Cyclic Redundancy Check (CRC)~~~~~~~~~~ 
      div=[1 0 0 1];  % predetermined divisor 
      for i=1:noPacket 
       [q,r]=deconv(DataIn(i,:),div); 
       y(i,:)=[DataIn(i,:),zeros(1,3)]; 
       for k=1:NoBits 
         r(k)=mod(r(k),2); 
       end 
       fcs=[zeros(1,3),r];  % frame check sequence 
       DataOut(i,:)=bitxor(y(i,:),fcs); 
      end 

%~~~~~~~~~~BPSK Modulation~~~~~~~~~~ 

BPSK1 = 2*DataOut-1; % BPSK modulation 0 -> -1; 1 -> 0 

Eb_N0_dB = [-3:35]; % multiple Eb/N0 values 
%-------------------------Channel Modelling------------------------------- 

for ii = 1:length(Eb_N0_dB) 

    awgn = 1/sqrt(2)*[randn(1,NoBits) + j*randn(1,NoBits)]; % white gaussian noise, 0dB variance 
    Ray = 1/sqrt(2)*[randn(1,NoBits) + j*randn(1,NoBits)]; % Rayleigh channel 

% Channel and noise Noise addition 
y = Ray.*BPSK1 + 10^(-Eb_N0_dB(ii)/20)*awgn; 

%----------------------------At the Receiver------------------------------- 
     % equalization 
     yHat = y./Ray; 
    % receiver - hard decision decoding 
    recDat = real(yHat)>0; 

    % counting the errors 
    nErr(ii) = size(find([DataIn-recDat]),2); 

end 

simBer = nErr/NoBits; % simulated ber 
theoryBerAWGN = 0.5*erfc(sqrt(10.^(Eb_N0_dB/10))); % theoretical ber 
EbN0Lin = 10.^(Eb_N0_dB/10); 
theoryBer = 0.5.*(1-sqrt(EbN0Lin./(EbN0Lin+1))); 

% plot 
close all 
figure 
semilogy(Eb_N0_dB,theoryBerAWGN,'cd-','LineWidth',2); 
hold on 
semilogy(Eb_N0_dB,theoryBer,'bp-','LineWidth',2); 
semilogy(Eb_N0_dB,simBer,'mx-','LineWidth',2); 
axis([-3 35 10^-5 0.5]) 
grid on 
legend('AWGN-Theory','Rayleigh-Theory', 'Rayleigh-Simulation'); 
xlabel('Eb/No, dB'); 
ylabel('Bit Error Rate'); 
title('BER for BPSK modulation in Rayleigh channel'); 

我已經得到了錯誤的路線:

y = Ray.*BPSK1 + 10^(-Eb_N0_dB(ii)/20)*awgn; 

希望有人能幫助我解決這個。

+0

您試圖在1x4向量和4x7矩陣之間執行元素明智的乘法。 – ritualmagick

回答

2

我不認爲你完全理解element-wise vs matrix operations在MATLAB中。

第一個,其中每個操作前面都有一個點,例如.*,./,.^等表示您正在逐個操作元素。因此,在操作者的每一側上的矩陣必須具有確切相同的尺寸:

A = [1 2; 3 4]; B = [4 5; 6 7]; 
A .* B = [1*4 2*5; 3*6 4*7] = [4 10; 18 28] 

另一種類型,矩陣操作上的矩陣操作:

A * B = [1*4+2*6 1*5+2*7; 3*4+4*6 3*6+4*7] = [16 19; 36 46] 

矩陣操作可以在的基質來進行不同的大小,只要尺寸匹配:

A = [1 2; 3 4]; B = [4; 6]; 
A * B = [1*4 + 2*6; 3*4+4*6] = [16; 36] 

當你做Ray.*BPSK1那麼你就逐元素相乘,因而矩陣必須爲O f相同的尺寸。這是很難知道你要在這裏實現什麼,但也有避免尺寸不匹配的幾個備選方案:


Ray*BPSK1 

這是可行的,但會給你一個尺寸不匹配時,嘗試添加因爲上述表達式的結果將具有尺寸[1x7],並且awgn具有尺寸[1x4]


BPSK1.'*Ray.' 

這是可行的,但會給與尺寸[7x1]結果。因此,您將會遇到與上述相同的問題。


bsxfun(@times, BPSK1, Ray.') 

這工作,但會給你一個[7x4]陣列,從而給你一個新的不匹配問題。