2PSK,2DPSK波形的代码
时间: 2023-07-02 13:05:57 浏览: 97
序列10110010的2ASK/2FSK/2PSK波形代码
以下是2PSK和2DPSK的MATLAB代码示例:
2PSK:
```matlab
% Parameters
N_bits = 1000; % number of bits to transmit
EbN0_dB = 10; % Eb/N0 in dB
% Transmitter
bits = randi([0 1],1,N_bits); % generate random bits
symb = 2*bits-1; % map bits to symbols (-1 and 1)
tx_signal = symb; % transmitted signal is just the symbols
% Channel
noise = sqrt(0.5)*randn(1,N_bits); % AWGN with unit variance
EbN0 = 10^(EbN0_dB/10); % convert to linear scale
Es = mean(abs(symb).^2); % calculate symbol energy
N0 = Es/(2*EbN0); % calculate noise power spectral density
rx_signal = tx_signal + sqrt(N0)*noise; % received signal
% Receiver
est_symb = sign(rx_signal); % estimate symbols from received signal
est_bits = (est_symb+1)/2; % convert symbols back to bits
% BER calculation
ber = sum(est_bits~=bits)/N_bits; % calculate bit error rate
fprintf('Bit error rate = %g\n',ber);
```
2DPSK:
```matlab
% Parameters
N_bits = 1000; % number of bits to transmit
EbN0_dB = 10; % Eb/N0 in dB
% Transmitter
bits = randi([0 1],1,N_bits); % generate random bits
symb = zeros(1,N_bits/2); % initialize symbol vector
for ii = 1:2:N_bits
if bits(ii) == 0 && bits(ii+1) == 0
symb((ii+1)/2) = exp(1j*pi/4);
elseif bits(ii) == 0 && bits(ii+1) == 1
symb((ii+1)/2) = exp(1j*3*pi/4);
elseif bits(ii) == 1 && bits(ii+1) == 0
symb((ii+1)/2) = exp(-1j*3*pi/4);
else
symb((ii+1)/2) = exp(-1j*pi/4);
end
end
tx_signal = sqrt(2)*symb; % transmitted signal is root-2 times symbol vector
% Channel
noise = sqrt(0.5)*randn(1,N_bits/2); % AWGN with unit variance
EbN0 = 10^(EbN0_dB/10); % convert to linear scale
Es = mean(abs(symb).^2); % calculate symbol energy
N0 = Es/(2*EbN0); % calculate noise power spectral density
rx_signal = tx_signal + sqrt(N0)*noise; % received signal
% Receiver
est_symb = zeros(1,N_bits/2); % initialize estimated symbol vector
for ii = 1:N_bits/2
if real(rx_signal(ii)) >= 0 && imag(rx_signal(ii)) >= 0
est_symb(ii) = exp(1j*pi/4);
elseif real(rx_signal(ii)) >= 0 && imag(rx_signal(ii)) < 0
est_symb(ii) = exp(1j*3*pi/4);
elseif real(rx_signal(ii)) < 0 && imag(rx_signal(ii)) >= 0
est_symb(ii) = exp(-1j*3*pi/4);
else
est_symb(ii) = exp(-1j*pi/4);
end
end
est_bits = zeros(1,N_bits); % initialize estimated bit vector
for ii = 1:N_bits/2
if est_symb(ii) == exp(1j*pi/4)
est_bits(2*ii-1:2*ii) = [0 0];
elseif est_symb(ii) == exp(1j*3*pi/4)
est_bits(2*ii-1:2*ii) = [0 1];
elseif est_symb(ii) == exp(-1j*3*pi/4)
est_bits(2*ii-1:2*ii) = [1 0];
else
est_bits(2*ii-1:2*ii) = [1 1];
end
end
% BER calculation
ber = sum(est_bits~=bits)/N_bits; % calculate bit error rate
fprintf('Bit error rate = %g\n',ber);
```
阅读全文