"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:
- 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.
1. Converting bits to symbols:
bit_stream = randi([0,1],[1,N])
bit_stream =
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:
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 =
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 =
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)
And check the scatter plot of that:
Now there are 4 points. That's what you'd expect to see with QPSK.
2. Error rate calculation
error_rate = zeros(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;
Plot symbol error rate (SER) vs SNR:
semilogy(snr,error_rate(1,:),'.-')