⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 runmeqam16.m

📁 Qam 16 in rayleigh channel
💻 M
字号:
clear
clc
disp('This program calculates the Bit Error Rate for 16QAM')
disp('BER is plotted in a jpeg file: BER.jpg')
disp('  ')

%================================================
%================================================
% Select the range of SNR per bit to simulate
startSNRperBitdB = 0;
endSNRperBitdB = 20;
stepSNRdB = 2;
snrArray=(startSNRperBitdB : stepSNRdB: endSNRperBitdB);
numSNRsteps=length(snrArray);
%================================================
%================================================
% To turn ON/OFF fading
isFadingOn=3;
%================================================
%================================================

% for 16-QAM 4 bits make one symbol
numBitsPerSymbol = 4;  

% Number of 16-QAM symbols per frame
numSymbolsPerFrame = 100;

% Number of channel bits per frame
numChannelBits=numSymbolsPerFrame*numBitsPerSymbol;

% Maximum number of frames to simulate per SNR. This number shuld be large
% enough otherwise the BER curve will not be smooth
numIterations = 10000;                              

% Each SNR, stop iterations after getting this number of frame errors 
enoughFrameErrors=400 ; 

%=============================
%Loop over SNR per symbol
%=============================
errorArray=zeros(numSNRsteps, 2); % first column is SNR, 2'nd column is BER
for snrCounter = 1:numSNRsteps
    
    SNRperBitdB=snrArray(snrCounter)
    SNRperBit=10^(SNRperBitdB/10)  ;
    
    % SNR per symbol is 4 times SNR per bit (16-QAM symbol carries 4 bits)
    SNRperSymbol=SNRperBit * numBitsPerSymbol  ;

    %=============================
    %Loop over frames of bits
    %=============================
    sumBitError = 0; % reset number of bit errors
    sumFrameError=0; % reset number of frame errors
    numSimulatedSymbols = 0;  % reset number of simulated symbols
    for frameCounter = 1 : numIterations

       %*********************************
       % We need to stop simulating this SNR if we already got enough
       % frame errors. This saves simulation time at low SNR values
       if sumFrameError >= enoughFrameErrors
         break
       end
       %*********************************

       % generate 1/0 channel bits for this frame randomly
       TxFrameBits(1:numChannelBits)= round(rand(1,numChannelBits));

       % generate the corresponding QPSK Symbols using an m function
       TxFrameSymbols = QAM16_modulate( TxFrameBits) ;

       %====================================
       %====================================
       
%%
%Fading chnnels choose (1)for rayleigh (2)for rician & insert LOS comp
%(3)Nakagmi and insert par. m other for AWGN case
       % generate fading gain: Rayleigh, non-selective
       % We need to make sure that each elemnts of the complex gain
       % has E(|fadingGain|^2)=1
       if isFadingOn==1
           %Ryleigh fading gain
            fadingGain= sqrt(0.5)*(randn(1,numSymbolsPerFrame) + j*randn(1,numSymbolsPerFrame));
       elseif isFadingOn==2
           % Rician fading gain
           S=3; %LOS component
           fadingGain= sqrt(0.5)*(randn(1,numSymbolsPerFrame) + j*randn(1,numSymbolsPerFrame))+S;
       elseif isFadingOn==3
           %Nakagmi fading gain
           m=10;
           P_avg=1; %normalization of average power
           Y = random('gam',m,P_avg/m,[1 numSymbolsPerFrame]); %generation of two parmeters gamma RV
           nakagami=sqrt(Y); %Nakagami RV is root of gamma distributed RV.
           phase=random('unif',-pi,pi,[1 numSymbolsPerFrame]); %generation of uniform distributed phase
           fadingGain=nakagami.*exp(i*phase);
       else
           % No fading
           fadingGain= 1;
       end
       
%%
       
       % Apply fading to the transmitted symbols 
       fadedSymbols= fadingGain .* TxFrameSymbols ;
       %====================================
       %====================================

        % generate AWGN samples for every channel Symbol
        AWGN=sqrt(1/SNRperSymbol/2)*(randn(1,numSymbolsPerFrame) + j*randn(1,numSymbolsPerFrame));

        % add channel symbols to AWGN
        RxChannelSymbols =  fadedSymbols + AWGN ;

        % Remove channel effect (assuming perfect channel estiamtion)
        % Apply the channel equalizer to equalize the fading channel effect
        equalizedSymbols=RxChannelSymbols ./ fadingGain ;

        % Apply QAM thresholds
        [Real16, Imag16] = QAM16_thresholds(equalizedSymbols) ;
        %  decide the received channel bits
        RxFrameBits = QAM16_demodulate(Real16, Imag16) ;

      % Decide the number of bit erros in this frame
        numBitErrors= sum(TxFrameBits ~= RxFrameBits);

        if numBitErrors>0
            frameError=1;
        else
            frameError=0;
        end

      % accumulate bit error counter
        sumBitError = sumBitError + numBitErrors ;

        % Accumulated frame erros
        sumFrameError = sumFrameError + frameError ;

      % accumulate the number of simulated symbols till now
        numSimulatedSymbols = numSimulatedSymbols + numSymbolsPerFrame;

    end
%=============================
%End looping over frames of bits
%=============================
BitErrorRate = sumBitError/numSimulatedSymbols/numBitsPerSymbol ;

errorArray(snrCounter,1:2) = [SNRperBitdB BitErrorRate];

end
%=============================
%End looping over SNR per symbol
%=============================

semilogy(errorArray(:,1) , errorArray(:,2),'-s', 'LineWidth',2,'color',[1 1 0])
ha=gca ;
set(ha,'fontsize',16)


hold on
grid on
ha2=xlabel('E_b/N_o in dB');
ha3=ylabel('Bit Error Rate');
set(ha2, 'FontSize', 14);
set(ha3, 'FontSize', 14);

if isFadingOn==3
    ha1=title('Bit Error Rate for 16QAM in Nakagami Fading Channel');
    fileName=('16QAMfading');
else
    ha1=title('Bit Error Rate for 16QAM in AWGN Channel');
    fileName=('16QAMawgn');
end
set(ha1, 'FontSize', 14);

%text(5, 0.1,'text here')

saveas(gcf, fileName, 'fig')
saveas(gcf, fileName, 'jpg')



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -