gpt-3.5-turbo-0613· openai
Suzhen/CodeChat
pam 2 ber and snr matlab plot
- User
pam 2 ber and snr matlab plot
- Assistant
To plot the BER (Bit Error Rate) vs. SNR (Signal-to-Noise Ratio) curve in MATLAB, you can use the following code:
% Generate SNR values snr_dB = -10:1:10; % SNR values in dB snr_lin = 10.^(snr_dB/10); % convert SNR to linear scale % Initialize BER vector ber = zeros(size(snr_dB)); % Perform BER calculation for each SNR value for i = 1:length(snr_dB) % Generate random bits data = randi([0 1], 1, 1000000); % Add Gaussian noise to the signal signal = data.*2 - 1; % BPSK modulation, mapping 0 to -1 and 1 to 1 noise = sqrt(1./(2*snr_lin(i))).*(randn(size(signal)) + 1j*randn(size(signal))); % Gaussian noise received = signal + noise; % received signal % Demodulate the received signal and calculate BER demod = real(received) < 0; % BPSK demodulation ber(i) = sum(demod ~= data)/length(data); % calculate BER end % Plot BER vs. SNR curve semilogy(snr_dB, ber, 'b-o'); xlabel('SNR (dB)'); ylabel('BER'); title('BER vs. SNR'); grid on;Make sure to adjust the range of SNR values (-10:1:10) and the number of bits (1000000) according to your requirements.