📄 matlab_project.m
字号:
% This MATLAB code is a basic simulator for a digital communication system
% It contains Pulse Position Modulation (PPM) and the students are required
% to include On-Off Keying (OOK), and Binary Phase Shift Keying (BPSK)
% The only thing the students are required to do (for the first part) is
% to fill under the statements ending with !!!!!!!!!!!!!!!!!!!!
% Then you should obtain the figures as described in the handouts and
% submit the project update and final report...
% Written by Ismail Guvenc for EEL4512: Introduction to Communication
% Systems, Spring 2006
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Initialize the variables first
clear all; close all;
no_of_bits = 10000;
delta = 0.8e-9; % This is the delay between two possible positions in PPM (in seconds)
Tm = 0.277e-9; % This parameter effects our pulse shape
fs = 5e10; % Sampling frequency (in Herz)
EbNo_vals = 5:12; % The SNR range (in dB) that we are going to investigate the Bit error rates (BERs)
total_error = zeros(1,length(EbNo_vals)); % Total number of bits received in error. Initialize it first to zero for all SNR
modul_type = 1; % 1: PPM, 2: BPSK, 3: OOK
% You need to change the modul_type to have different modulations. PPM simulation already works good.
% For BPSK and OOK, you should complete the required places indicated with exclamation marks
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Generate the pulse to be transmitted
t_pulse = -0.4e-9:1/fs:0.4e-9; % This is the time support of the pulse
y = (1-4*pi*(t_pulse/Tm).^2).*exp(-2*pi*(t_pulse/Tm).^2); % Generate the pulse using a certain expression
% The above pulse is a second derivative of a Gaussian pulse, and was
% a commonly pronounced pulse shape for Ultrawideband Systems until the announcement of the FCC spectral mask
y = y/sqrt(sum(y.^2)); % Normalize the pulse energy to 1
% If you remember, the energy of a signal is the sum of squares of all the
% samples of the signal (you may check that the energy of the above pulse 'y' is unitary)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Generates (no_of_bits) bits, which are equally likely to be 0 or 1
bits = randn(1,no_of_bits)>0;
% We are going to transmit these bits over an AWGN channel, and observe the
% bit error rates at different signal to noise ratios (SNRs)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for bit_no=1:no_of_bits
bit = bits(bit_no);
% Transmitter ------------------------------------------
% Switch to the modulation type initialized above, and map the bits onto the pulse using that modulation type
if modul_type == 1
% There are "41 samples" in my pulse, so I transmit the pulse within
% the first 41 samples for bit 0, and at the second 41 samples for bit 1
PPM_pulse_delay = 41*bit; % Pulse may be 1/0 delta away from the nominal position
sig = zeros(1,2*length(y));
sig((PPM_pulse_delay+1):(PPM_pulse_delay+41)) = y; % Note that here I use bit information to change the pulse position
% This was where we are "mapping" the information to the pulse. I
% can also change the polarity or amplitude of the pulse as should be done below...
elseif modul_type == 2
% FILL IN HERE TO IMPLEMENT TRANSMITTER FOR BPSK!!!!!!!!!!!!!!!!!
elseif modul_type == 3
% FILL IN HERE TO IMPLEMENT TRANSMITTER FOR OOK!!!!!!!!!!!!!!!!!!
end
% Note that for OOK and BPSK, as opposed to PPM, I send the pulse
% always at the first position, i.e. at the first 41 samples.
% Modulation for these two schemes occurs by multiplying the right handside of the above
% equation (e.g. bit * y or things like that ... ) as opposed to PPM's time-shift
% Additive White Gaussian Noise (AWGN) Channel ---------
noisy_sig = [];
% The noisy_sig will be a matrix, which contains the replicas of the noisy signal with different noise levels
% at different SNR values. First, we define a noise vector below. Then,
% depending on the SNR, we scale its amplitude in the FOR loop, and add
% to the transmitted signal. Note again that the transmitted signal
% energy is normalied to 1, so we calculate the corresponding noise
% variance according to the Eb/No by setting Eb to 1
noise = randn(1,length(sig)); % Power of the noise will be adjusted inside the loop
for EbNo = EbNo_vals % Investigate a certain EbNo range to evaluate the BER
% Note that for double-sided noise spectrum, Noise variance \sigma^2 = No/2
noise_var = 0.5 * 10^(-EbNo/10); % Calculate the noise power corresponding to a certain EbNo
Chan_Noise = noise * sqrt(noise_var); % Scale the noise amplitude according to the noise variance calculated
noisy_sig = [noisy_sig ; sig + Chan_Noise]; % Add noise to the signal (at a new row of the matrix)
end
% RECEIVER ----------------------------------------------
% At the receiver, we analyze different SNRs in a for loop, and
% see if the bit is received in error or not. If so, we increment the
% total error for the corresponding SNR.
for EbNo_ind = 0:(length(EbNo_vals)-1)
signal = noisy_sig((EbNo_ind+1),:);
if modul_type == 1 % PPM: Look at correlations at two different pulse positions
corr0 = sum(signal(1:41).*y); % Correlate the received signal at 0-position
corr1 = sum(signal((1:41)+41).*(y)); % Correlate the received signal at 1-position
elseif modul_type == 2 % BPSK
% FILL IN HERE TO IMPLEMENT THE RECEIVER FOR BPSK!!!!!!!!!!!!!!!!!
% corr0 = ... % Correlate the received signal at 0-position
% corr1 = 0; % Threshold...
elseif modul_type == 3 % OOK
% FILL IN HERE TO IMPLEMENT THE RECEIVER FOR OOK!!!!!!!!!!!!!!!!!!
% corr0 = ... % Correlate the received signal at 0-position
% corr1 = ... % Threshold...
end
% For OOK and BPSK, corr0 value should be the correlation at the first pulse position,
% and corr1 value should be the threshold that I should use (e.g., it is 0 for BPSK)
% To test a particular modulation, comment out all the rest, but
% the one that you would like to test at the transmitter and the receiver
% Compare the correlations at two different hypothesis (position, polarity etc.)
% Choose the bit (as the estimated received bit) corresponding to the one that has larger correlation
if corr0 > corr1
received_bits(EbNo_ind+1) = 0;
else
received_bits(EbNo_ind+1) = 1;
end
end
% Compare the received bits at different SNRs with the transmitted bit
% Increment the total_error for each SNR if the received bit is different than the transmitted bit
total_error = total_error + abs(received_bits-bit);
% Display the bit number at multiples of 100 on the screen (for simulation tracking purposes)
if mod(bit_no,100)== 0; bit_no, end
end
% Divide the number of bits received in error with the total number of bits transmitted
% This will be our bit error rate (BER)
sim_error = total_error/bit_no
% Now obtain the theoretical BERs using the Q-function
EbNo_non_dB = 10.^(EbNo_vals/10); % First convert the EbNo from dB to actual values
if modul_type == 1
theo_error = Q_fn (sqrt(EbNo_non_dB)); % Now calculate the BER (for PPM) using the Q-function
elseif modul_type == 2
% theo_error = Q_fn (???); % MUST BE MODIFIED ACCORDING TO BPSK BER!!!!!!!!!!!!!!!!!
elseif modul_type == 3
% theo_error = Q_fn (???); % MUST BE MODIFIED ACCORDING TO OOK BER!!!!!!!!!!!!!!!!!
end
% Plot and appropriately label the BER with respect to EbNo
semilogy(EbNo_vals,sim_error), grid on, hold on,
semilogy(EbNo_vals,theo_error,'o')
xlabel('E_b/N_0 (dB)'), ylabel('BER'), legend('PPM (simulation)','PPM (theory)')
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -