MATLAB:
主程式
clear;clc;
close all;
N=10000;
EbN0dB = -[Link];
fc=100;
OF =8;
EbN0lin = 10.^ (EbN0dB/10);
BER = zeros (length (EbN0dB), 1);
a = rand (N, 1)>0.5;
[s,t] = qpsk_mod(a,fc,OF);
for i=1:length (EbN0dB), Eb=OF*sum(abs(s).^2)/(length(s));
N0= Eb/EbN0lin(i);
n = sqrt(N0 / 2) * (randn(1, length(s)));
r= s+ n;
a_cap=qpsk_demod(r,fc,OF);
BER (i) = sum(a~=a_cap.')/N;
end
theoreticalBER =0.5*erfc(sqrt(EbN0lin));
figure; semilogy (EbN0dB, BER, 'k*', 'LineWidth',1.5);
hold on; semilogy (EbN0dB, theoreticalBER, 'r-', 'LineWidth',1.5);
title('Probability of Bit Error for QPSK modulation');
xlabel('E_b/N_0 (dB)');ylabel('Probability of Bit Error - P_b');
legend('Simulated', 'Theoretical'); grid on;
子程式1:
function [s, t, I, Q] = qpsk_mod(a, fc, OF)
L = 2 * OF;
ak = 2 * a - 1;
I = ak([Link]nd);
Q = ak([Link]nd);
I = repmat(I, 1, L).';
Q = repmat(Q, 1, L).';
I = I(:).';
Q = Q(:).';
fs = OF * fc;
t = 0:1/fs:(length(I) - 1) / fs;
iChannel = I .* cos(2 * pi * fc * t);
qChannel = -Q .* sin(2 * pi * fc * t);
s = iChannel + qChannel;
doPlot = 1;
if doPlot == 1
figure;
subplot(3, 2, 1); plot(t, I);
xlabel('t'); ylabel('I(t) - baseband'); xlim([0, 10 * L / fs]);
subplot(3, 2, 2); plot(t, Q);
xlabel('t'); ylabel('Q(t) - baseband'); xlim([0, 10 * L / fs]);
subplot(3, 2, 3); plot(t, iChannel, 'r');
xlabel('t'); ylabel('I(t) - with carrier'); xlim([0, 10 * L / fs]);
subplot(3, 2, 4); plot(t, qChannel, 'r');
xlabel('t'); ylabel('Q(t) - with carrier'); xlim([0, 10 * L / fs]);
subplot(3, 1, 3); plot(t, s);
xlabel('t'); ylabel('s(t)'); xlim([0, 10 * L / fs]);
end
end
子程式2:
function [a_cap] = qpsk_demod(r, fc, OF)
fs = OF * fc;
L = 2 * OF;
t = 0:1/fs:(length(r) - 1) / fs;
x = r .* cos(2 * pi * fc * t);
y = -r .* sin(2 * pi * fc * t);
x = conv(x, ones(1, L));
y = conv(y, ones(1, L));
x = x(L:L:end);
y = y(L:L:end);
a_cap = zeros(1, 2 * length(x));
a_cap([Link]nd) = x.' > 0;
a_cap([Link]nd) = y.' > 0;
doPlot = 1;
if doPlot == 1
figure; plot(x(1:200), y(1:200), 'o');
end
end
結果圖:
Python
import numpy as np #for numerical computing
import [Link] as plt #for plotting functions
from passband_modulations import qpsk_mod,qpsk_demod
from channels import awgn
from [Link] import erfc
N=100000 # Number of symbols to transmit
EbN0dB = [Link](start=-4,stop = 11,step = 2) # Eb/N0 range in dB for simulation
fc=200 # carrier frequency in Hertz
OF =8 # oversampling factor, sampling frequency will be fs=OF*fc
BER = [Link](len(EbN0dB)) # For BER values for each Eb/N0
a = [Link](2, size=N) # uniform random symbols from 0's and 1's
result = qpsk_mod(a,fc,OF,enable_plot=True) #QPSK modulation, False or True
s = result['s(t)'] # get values from returned dictionary
for i,EbN0 in enumerate(EbN0dB):
# Compute and add AWGN noise
r = awgn(s,EbN0,OF) # refer Chapter section 4.1
a_hat = qpsk_demod(r,fc,OF) # QPSK demodulation
BER[i] = [Link](a!=a_hat)/N # Bit Error Rate Computation
#------Theoretical Bit Error Rate-------------
theoreticalBER = 0.5*erfc([Link](10**(EbN0dB/10)))
#-------------Plot performance curve------------------------
fig, axs = [Link](nrows=1,ncols = 1)
[Link](EbN0dB,BER,'k*',label='Simulated')
[Link](EbN0dB,theoreticalBER,'r-',label='Theoretical')
axs.set_title('Probability of Bit Error for QPSK modulation');
axs.set_xlabel(r'$E_b/N_0$ (dB)')
axs.set_ylabel(r'Probability of Bit Error - $P_b$');
[Link]();[Link]()
Discussion:In the QPSK modulation experiment, we generated the in-phase (I(t)I(t)) and quadrature-
phase (Q(t)Q(t)) components as baseband signals, representing the digital data to be transmitted.
These baseband signals were modulated using a carrier frequency; the I(t)I(t) component was
modulated with a cosine carrier, while the Q(t)Q(t) component was modulated with a sine carrier. The
combination of these modulated signals resulted in the QPSK signal s(t)s(t), which exhibited phase
changes corresponding to the digital input data. The bit error rate (BER) performance of the QPSK
modulation was evaluated by plotting the probability of bit error (Pb) against the energy per bit to
noise power spectral density ratio (Eb/N0). The BER curve, showing both simulated and theoretical
values, demonstrated a good agreement between the two. This alignment indicates that the
simulation accurately represents the theoretical performance of QPSK modulation. As expected, the
probability of bit error decreases as Eb/N0 increases, meaning that higher signal-to-noise ratios
(SNR) result in better performance and fewer errors. The results showed that QPSK modulation
maintains signal integrity even at relatively low SNRs, which is critical for reliable digital
communication. Overall, the experiment confirms the robustness and efficiency of QPSK modulation
in digital communication systems. The close match between the simulated and theoretical BER
curves validates the effectiveness of QPSK in reducing bit errors and maintaining data integrity,
making it a suitable choice for various communication applications where bandwidth efficiency and
signal reliability are essential.