# The code explanation about QPSK modulation

This is the code about the QPSK modulation, and I don't understand the real part and the imaginary part code. Can anyone explain it to me if you know?

Why is 1:bit_amount_to_symbol:end for real part, and 2:bit_amount_to_symbol:end for imaginary part?

Why should it be multiplied by 2?

Why should they both subtract sqrt(SYMBOL_POWER/2)?

The code is MATLAB code:

transmit_data = [1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0];
SYMBOL_POWER = 1;
bit_amount_to_symbol = 2;

real_part = sqrt(SYMBOL_POWER/2) * (transmit_data(1:bit_amount_to_symbol:end)*2 - 1);

imaginary_part = sqrt(SYMBOL_POWER/2) * (transmit_data(2:bit_amount_to_symbol:end)*2 - 1);

modulation_data = real_part + imaginary_part * 1i;

• Comments are not for extended discussion; this conversation has been moved to chat. – Peter K. Jul 25 '18 at 11:28

Consider how to create BPSK from a sequence of zeros and ones first:

> % create some random data
> N = 20;
> transmit_data = (rand(N,1)>0.5);
>
> % map to BPSK where each data 'zero' maps to -1 and each data 'one' maps to +1
> bpsk_data = transmit_data*2 - 1;
>
> % The above is easier than creating a map and indexing into it
> % ...but that can also be done:
> mapping = [-1 ; 1];
> bpsk_data = mapping(transmit_data+1) % need to add 1 to transmit_data index for Matlab oddball indexing that starts at 1, not 0
>
> % observe the transmit_data and bpsk_data side-by-side:
> [transmit_data bpsk_data]


Now, extend this to QPSK by splitting a sequence of transmit_data into the data for the "real BPSK" and the data for the "imaginary BPSK" aspects of the constellation. This is done by selecting the odd indices for the reals: transmit_data(1:2:end) and the even indices for the imags: transmit_data(2:2:end). Follow above for BPSK creation and add real and imag to get complex QPSK constellation and then scale to get desired power.