- You need to convert your bits to symbols one way or another before doing the QPSK modulation.
- For the error rate calculation, you need to compare the received demodulated signal (i.e., received symbols) to the original transmitted symbols. Comparing qpskdemod_sig to qpskmod_sig is comparing the received symbols to the transmitted modulated signals.
Help with code running for each value of a vector
14 views (last 30 days)
Show older comments
Hello, I am writing as I am unsure as to how to get a function to run through all values of a vector to aid in my code for Bit Error Rate Analysis of QPSK, 16-QAM and 64-QAM. I am still currently writing the code for QPSK.
My code is as follows:
N = 10^6;
vec = -10:1:10;
col_vec = reshape(vec,21,1);
bit_stream = randi([0,1],[1,N]);
M = 4;
k = log2(M);
snr = convertSNR(vec,"ebno","snr");
qpskmod_sig = pskmod(bit_stream,M);
tx_sig = awgn(qpskmod_sig,snr);
qpskdemod_sig = pskdemod(tx_sig,M);
errorRate = comm.ErrorRate;
rx_sig_BER = errorRate(qpskmod_sig, qpskdemod_sig);
I am receiving the following error:
Error using awgn
Expected SNR input to be a scalar.
Error in awgn (line 73)
validateattributes(reqSNR, {'numeric'}, ...
Error in coursework_task_1 (line 9)
tx_sig = awgn(qpskmod_sig,snr);
0 Comments
Accepted Answer
Voss
on 19 Mar 2024
"how to get a function to run through all values of a vector"
Use a for loop with Nsnr iterations, where Nsnr is the number of elements in the snr vector. In each iteration, call awgn with the appropriate snr value, and then store each error rate result in an array (e.g., as an element of a vector or a column of a matrix).
However, there are a couple of problems with your code beyond that:
1. Converting bits to symbols:
N = 1e6;
M = 4;
k = log2(M);
bit_stream = randi([0,1],[1,N])
This is what you have now (calling pskmod on the bit stream):
qpskmod_sig = pskmod(bit_stream,M);
Look at a plot of the modulated signal points. There are only two points used, instead of the M=4 you would expect when using QPSK:
scatterplot(qpskmod_sig)
That's because pskmod is mapping bit 0 to 1+0j and bit 1 to 0+1j.
You need to call pskmod on symbols instead of bits. In this case each symbol is log2(M)=2 bits long, so you can reshape your bit stream and sum along the columns after multiplying by the appropriate powers of 2:
bits_to_transmit = reshape(bit_stream,k,[])
symbol_stream = sum(bits_to_transmit.*pow2(k-1:-1:0).',1)
Now the two-bit sequence [0 0] has become symbol 0; [0 1] has become 1; [1 0] has become 2; and [1 1] has become 3. (You could also have generated random integers between 0 and M-1 (i.e., randi([0,M-1],1,N/k)) at the start, instead of starting with bits.)
Now QPSK modulate the symbol stream:
qpskmod_sig = pskmod(symbol_stream,M);
(or doing the same thing with the reshaped bit stream:)
qpskmod_sig_1 = pskmod(bits_to_transmit,M,'InputType','bit');
isequal(qpskmod_sig,qpskmod_sig_1) % same result
And check the scatter plot of that:
scatterplot(qpskmod_sig)
Now there are 4 points. That's what you'd expect to see with QPSK.
2. Error rate calculation
snr = -10:10;
Nsnr = numel(snr);
error_rate = zeros(1,Nsnr); % pre-allocate error_rate vector
N_symbols = N/k;
for ii = 1:Nsnr
rx_sig = awgn(qpskmod_sig,snr(ii));
qpskdemod_symbols = pskdemod(rx_sig,M);
error_rate(ii) = nnz(symbol_stream ~= qpskdemod_symbols) / N_symbols;
end
Plot symbol error rate (SER) vs SNR:
figure()
semilogy(snr,error_rate(1,:),'.-')
xlabel('SNR [dB]')
ylabel('SER')
2 Comments
More Answers (0)
See Also
Categories
Find more on QPSK in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!