In this blog post, step by step matlab code for QPSK modulation and demodulation is provided. At the end the spectrum at each stage from the transmitter to the receiver are shown in graph.

This may be useful for analyzing QPSK system and also useful for system designer.

Step 1 : Initializations

Fc = 5e6;      % carrier frequency

N = 10;        % no. of binary bits

Step 2: Generate random binary sequence of N bit

for i = 1:N
x(i) = round(rand);
end

Step 3: Perform Serial to Parallel conversion

for i = 1:N
    if rem(i,2) == 1
        if x(i) == 1
            I(i) = 1;
            I(i+1) = 1;
        else
            I(i) = -1;
            I(i+1) = -1;
        end
    else
        if x(i) == 1
            Q(i-1) = 1;
            Q(i) = 1;
        else
            Q(i-1) = -1;
            Q(i) = -1;
        end
    end
end

Step 4: Perform Zero Insertion or Upsampling

The reason for zero insertion or zero padding or upsampling is to increase the sampling frequency.

zero = 5; % sampling rate is 25MHZ

for i = 1 : zero*N
    if rem(i,zero) == 1
        Izero(i) = I(fix((i-1)/zero)+1);
        Qzero(i) = Q(fix((i-1)/zero)+1);
    else
        Izero(i) = 0;
        Qzero(i) = 0;
    end
end

Step 4: Apply Pulse Shaping Filter

NT = 50;
Nz = 2*zero*NT;

Fs = 25e6;
rf = 0.1;

psf = rcosfir(rf, NT, zero, Fs, 'sqrt');

Ipulse = conv(Izero, psf);
Qpulse = conv(Qzero, psf);


Step 5: QPSK Modulation

for i = 1: zero*N + Nz
    t(i) = (i-1)/(Fc*zero);
    Imod(i) = Ipulse(i).*sqrt(2)*cos(2*pi*Fc*t(i));
    Qmod(i) = Qpulse(i).*(-sqrt(2)*sin(2*pi*Fc*t(i)));
end

Q = Imod+Qmod;

Step 6: QPSK Demodulation

for i = 1: zero*N+Nz
    Idem(i) = Q(i).*sqrt(2)*cos(2*pi*Fc*t(i));
    Qdem(i) = Q(i).*(-sqrt(2)*sin(2*pi*Fc*t(i)));
end

Step 7: Apply Matched Filter

mtf = rcosfir(rf, NT, zero, Fs, 'sqrt');

Imat = conv(Idem, mtf);
Qmat = conv(Qdem, mtf);

Step 8: Data Selection

for i = 1: zero*N
    Isel(i) = Imat(i+Nz);
    Qsel(i) = Qmat(i+Nz);
end

Step 9: Sampling

for i = 1:N
    Isam(i) = Isel((i-1)*zero+1);
    Qsam(i) = Qsel((i-1)*zero+1);
end

Step 10: Apply Decision Device

threshold = 0.2;

 for i = 1:N
     if Isam(i) >= threshold
         Ifinal(i) = 1;
     else
         Ifinal(i) = -1;
     end
     if Qsam(i) >= threshold
         Qfinal(i) = 1;
     else
         Qfinal(i) = -1;
     end
 end

Step 11: Parallel to Serial Conversion

 for i = 1: N
     if rem(i,2) == 1
         if Ifinal(i) == 1
             final(i) = 1;
         else
             final(i) = 0;
         end
     else
         if Qfinal(i) == 1
             final(i) = 1;
         else
             final(i) = 0;
         end
     end
 end

Simulation Results

1. Spectrum of Input Binary Data

 figure(1)
 plot(20*log(abs(fft(x))))
 axis([0 N -40 100])
 grid
 title('Spectrum of Input Binary Data')

 
2. Spectrum of I & Q channel data

figure(2)
 subplot(221)
 plot(20*log(abs(fft(I))))
 axis([0 N -40 140])
 grid
 title('Spectrum of I channel data')

 subplot(222)
 plot(20*log(abs(fft(Q))))
 axis([0 N -40 140])
 grid
 title('Spectrum of Q channel data')

 subplot(223)
 plot(20*log(abs(fft(Izero))))
 axis([0 zero*N -20 140])
 grid
 title('Spectrum of I channel data after zero insertion')

 subplot(224)
 plot(20*log(abs(fft(Qzero))))
 axis([0 zero*N -20 140])
 grid
 title('Spectrum of Q channel data after zero insertion')


3. Time Domain Response and Transfer Function of Pulse Shaping Filter

figure(3)
 subplot(221)
 plot(psf)
 axis([200 300 -0.2 0.6])
 grid
 title('Time domain response of Pulse Shaping Filter')

 subplot(222)
 plot(20*log(abs(fft(psf))))
 axis([0 Nz -350 50])
 grid
 title('Transfer Function of Pulse Shaping Filter')

 subplot(223)
 plot(20*log(abs(fft(Ipulse))))
 axis([0 zero*N+Nz -250 150])
 grid
 title('Spectrum of I channel after Pulse Shaping Filter')

 subplot(224)
 plot(20*log(abs(fft(Qpulse))))
 axis([0 zero*N+Nz -250 150])
 grid
 title('Spectrum of Q channel after Pulse Shaping Filter')


4.Spectrum of I & Q channel after Modulation

 figure(4)
 subplot(211)
 plot(20*log(abs(fft(Imod))))
 axis([0 zero*N+Nz -250 150])
 grid
 title('Spectrum of I channel after modulation')

 subplot(212)
 plot(20*log(abs(fft(Qmod))))
 axis([0 zero*N+Nz -250 150])
 grid
 title('Spectrum of Q channel after modulation')


5. Spectrum of I & Q after Demodulation

figure(5)
 subplot(221)
 plot(20*log(abs(fft(Idem))))
 axis([0 zero*N+Nz -200 150])
 grid
 title('Spectrum of I channel after demodulation')

 subplot(222)
 plot(20*log(abs(fft(Qdem))))
 axis([0 zero*N+Nz -200 150])
 grid
 title('Spectrum of Q channel after demodulation')

 subplot(223)
 plot(20*log(abs(fft(Imat))))
 axis([0 zero*N -400 200])
 grid
 title('Spectrum of I channel after matched filter')

 subplot(224)
 plot(20*log(abs(fft(Qmat))))
 axis([0 zero*N -400 200])
 grid
 title('Spectrum of Q channel after matched filter')


6. Spectrum of I & Q channel signal after Sampling and Decision Device

figure(6)
 subplot(221)
 plot(20*log(abs(fft(Isam))))
 axis([0 N -40 150])
 grid
 title('Spectrum of I channel after sampler')

 subplot(222)
 plot(20*log(abs(fft(Qsam))))
 axis([0 N -40 150])
 grid
 title('Spectrum of Q channel after sampler')

 subplot(223)
 plot(20*log(abs(fft(Ifinal))))
 axis([0 N -40 150])
 grid
 title('Spectrum of I channel after Decision Device')

 subplot(224)
 plot(20*log(abs(fft(Qfinal))))
 axis([0 N -40 150])
 grid
 title('Spectrum of Q channel after Decision Device')


7. Constellation of Matched Filter Output

 figure(7)
 plot(Isel, Qsel)
 axis([-1.6 1.6 -1.6 1.6])
 grid
 title('Constellation of Matched Filter Output')


8. Constellation of Sampler

 figure(8)
 plot(Isam, Qsam, 'X')
 axis([-1.2 1.2 -1.2 1.2])
 grid
 title('Constellation of Sampler')

9. Spectrum of final received binary data

figure(9)
 plot(20*log(abs(fft(final))))
 axis([0 N 0 120])
 grid
 title('Spectrum of final received binary data')

In this tutorial we showed you how to do QPSK modulation in matlab using programming methods, that is by writing codes. In Quadrature Modulation Modelling using Matlab we show how you can model modulation/demodulation using matlab simulink. And in QPSK modulator design with Matlab-XilinxSystem Generator we show how to realize it in FPGA chips.

0 comments:

Post a Comment

 
Top