Help with code running for each value of a vector

조회 수: 14 (최근 30일)
Liam
Liam 2024년 3월 19일
편집: Voss 2024년 3월 27일
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);

채택된 답변

Voss
Voss 2024년 3월 19일
"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. You need to convert your bits to symbols one way or another before doing the QPSK modulation.
  2. 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.
1. Converting bits to symbols:
N = 1e6;
M = 4;
k = log2(M);
bit_stream = randi([0,1],[1,N])
bit_stream = 1x1000000
0 1 1 1 1 0 0 0 0 0 0 1 1 0 1 0 0 1 1 1 0 0 0 0 1 1 1 1 0 0
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,[])
bits_to_transmit = 2x500000
0 1 1 0 0 0 1 1 0 1 0 0 1 1 0 0 1 0 0 0 1 0 1 1 1 0 1 0 1 1 1 1 0 0 0 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 0 1 0 0 0 0 1 0 0 0
symbol_stream = sum(bits_to_transmit.*pow2(k-1:-1:0).',1)
symbol_stream = 1x500000
1 3 2 0 0 1 2 2 1 3 0 0 3 3 0 0 3 1 0 0 2 1 2 2 2 0 3 0 2 2
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
ans = logical
1
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
Liam
Liam 2024년 3월 27일
Many thanks for this, if I needed to change the modulation technique does this code stay the same but have 3 different for statements?
Many Thanks in Advance
Voss
Voss 2024년 3월 27일
편집: Voss 2024년 3월 27일
You're welcome!
To change the modulation scheme, you would change pskmod and pskdemod to something else (or just change the value of M (and N, such that N is a multiple of log2(M)), in case you stick with PSK and just change the modulation order).

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 QPSK에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by