matlab编写2psk、qpsk、8psk调制解调的蒙特卡罗仿真
时间: 2024-01-02 10:03:10 浏览: 87
matlab实现的基于Trellis Code的8psk调制解调仿真,包括软-硬判决+源代码
以下是用 MATLAB 编写的 2PSK、QPSK 和 8PSK 调制解调的 Monte Carlo 仿真代码:
```matlab
clear all;
snr_dB = 0:2:12; % Signal to Noise Ratio (in dB)
N = 10^4; % Number of symbols in simulation
% Generating Gray code sequence for 2PSK modulation
seq_2psk = [0 1];
% Generating Gray code sequence for QPSK modulation
seq_qpsk = [0 1 3 2];
% Generating Gray code sequence for 8PSK modulation
seq_8psk = [0 1 3 2 6 7 5 4];
% Initializing error counters
err_2psk = zeros(1,length(snr_dB));
err_qpsk = zeros(1,length(snr_dB));
err_8psk = zeros(1,length(snr_dB));
for i = 1:length(snr_dB)
snr = 10^(snr_dB(i)/10); % Signal to Noise Ratio (in linear scale)
sigma = sqrt(1/(2*snr)); % Standard deviation of noise
% Generating random symbols for simulation
symbols_2psk = randi([0 1],1,N);
symbols_qpsk = randi([0 3],1,N/2);
symbols_8psk = randi([0 7],1,N/3);
% Modulation
mod_2psk = exp(1j*pi*seq_2psk(symbols_2psk+1));
mod_qpsk = exp(1j*pi/2*seq_qpsk(symbols_qpsk+1));
mod_8psk = exp(1j*pi/4*seq_8psk(symbols_8psk+1));
% Generating AWGN noise
noise = sigma*(randn(1,length(mod_2psk)) + 1j*randn(1,length(mod_2psk)));
% Adding noise to modulated signal
rx_2psk = mod_2psk + noise;
rx_qpsk = mod_qpsk + noise(1:length(mod_qpsk));
rx_8psk = mod_8psk + noise(1:length(mod_8psk));
% Demodulation
demod_2psk = real(rx_2psk) >= 0;
demod_qpsk = 2*(real(rx_qpsk) >= 0) + (imag(rx_qpsk) >= 0);
demod_8psk = 4*(real(rx_8psk) >= 0) + 2*(imag(rx_8psk) >= 0) + (real(rx_8psk) + imag(rx_8psk) >= 0);
% Counting errors
err_2psk(i) = sum(symbols_2psk ~= demod_2psk);
err_qpsk(i) = sum(symbols_qpsk ~= demod_qpsk);
err_8psk(i) = sum(symbols_8psk ~= demod_8psk);
end
% Plotting results
semilogy(snr_dB,err_2psk/N,'o-',snr_dB,err_qpsk/N,'*-',snr_dB,err_8psk/N,'+-');
legend('2PSK','QPSK','8PSK');
xlabel('SNR (dB)');
ylabel('Bit Error Rate');
title('Monte Carlo Simulation of PSK Modulation and Demodulation');
```
该代码会生成三条曲线,分别代表 2PSK、QPSK 和 8PSK 调制解调的比特误码率(BER)随信噪比(SNR)变化的情况。可以通过修改 `snr_dB` 和 `N` 的值来调整仿真的参数。
阅读全文