function bitstream = symbolDecision(Isig,Qsig,type)
% Symbol Decision / detection of symbols and output the bits.
% Usage: bitstream = symbolDecision(Isig,Qsig,type)
% Isig and Qsig: The in-phase and quadrature signals (should be
% down-converted and samplerate converted).
% type: modulation scheme
% bitstream: the detected bitstream
fs = 4e6; % sample frequency of input signal
Ts = 1/fs; % sample time
T = 1e-6; % symbol time
L = length(Isig);
Nsymbols = L*Ts/T; % number of symbols in input signal
bitstream = [];
%%%% QPSK %%%%
if type == 1
start = 1;
for idx=1:Nsymbols
final = start+T*fs-1;
% calculate angle in the middel of a sample period
cc = Isig(round(median(start:final))) + j*Qsig(round(median(start:final)));
phi = angle(cc);
start = final+1;
% which of the elementary angles is phi closest to?
phitest(1) = abs(pi/4 - phi);
phitest(2) = abs(3*pi/4 - phi);
phitest(3) = abs(-3*pi/4 - phi);
phitest(4) = abs(-pi/4 - phi);
[phimin, phiIdx] = min(phitest);
% append the bitstream
switch phiIdx
case 1, bitstream = [bitstream 0 0];
case 2, bitstream = [bitstream 0 1];
case 3, bitstream = [bitstream 1 1];
case 4, bitstream = [bitstream 1 0];
end
end
end % type == 1, QPSK
%%%% DQPSK %%%%
if type == 2
oldphi = 0;
start = 1;
for idx = 1:Nsymbols
final = start+T*fs-1;
% calculate angle in the middel of a sample period
cc = Isig(round(median(start:final))) + j*Qsig(round(median(start:final)));
phi = angle(cc);
deltaphi = phi - oldphi; % the angle change
% make sure that deltaphi lies between -pi and pi
if deltaphi > pi;
deltaphi = deltaphi - 2*pi;
elseif deltaphi < -pi
deltaphi = deltaphi + 2*pi;
end
% which of the elementary angles is deltaphi closest to?
phitest(1) = abs(pi/4 - deltaphi);
phitest(2) = abs(3*pi/4 - deltaphi);
phitest(3) = abs(-3*pi/4 - deltaphi);
phitest(4) = abs(-pi/4 - deltaphi);
[phimin, phiIdx] = min(phitest);
% append the bitstream
switch phiIdx
case 1, bitstream = [bitstream 0 0];
case 2, bitstream = [bitstream 0 1];
case 3, bitstream = [bitstream 1 1];
case 4, bitstream = [bitstream 1 0];
end
oldphi = phi; % store the old angle
start = final+1;
end
end % type == 2, pi/4-DQPSK
%%%% 8DPSK %%%%
if type == 3
oldphi = 0;
start = 1;
for idx = 1:Nsymbols
final = start+T*fs-1;
% calculate angle in the middel of a sample period
cc = Isig(round(median(start:final))) + j*Qsig(round(median(start:final)));
phi = angle(cc);
deltaphi = phi - oldphi; % the angle change
% make sure that deltaphi lies between -pi and pi
if deltaphi > pi;
deltaphi = deltaphi - 2*pi;
elseif deltaphi < -pi
deltaphi = deltaphi + 2*pi;
end
% which of the elementary angles is deltaphi closest to?
phitest(1) = abs(0 - deltaphi);
phitest(2) = abs(pi/4 - deltaphi);
phitest(3) = abs(pi/2 - deltaphi);
phitest(4) = abs(3*pi/4 - deltaphi);
phitest(5) = abs(min(pi - deltaphi,pi + deltaphi)); % extra check if deltaphi is exactly -pi or pi
phitest(6) = abs(-3*pi/4 - deltaphi);
phitest(7) = abs(-pi/2 - deltaphi);
phitest(8) = abs(-pi/4 - deltaphi);
[phimin, phiIdx] = min(phitest);
% append the bitstream
switch phiIdx
case 1, bitstream = [bitstream 0 0 0];
case 2, bitstream = [bitstream 0 0 1];
case 3, bitstream = [bitstream 0 1 1];
case 4, bitstream = [bitstream 0 1 0];
case 5, bitstream = [bitstream 1 1 0];
case 6, bitstream = [bitstream 1 1 1];
case 7, bitstream = [bitstream 1 0 1];
case 8, bitstream = [bitstream 1 0 0];
end
oldphi = phi; % store the old angle
start = final+1;
end
end % type == 3, 8DPSK
%%%% GFSK %%%%
if type == 4
start = 1;
for idx = 1:Nsymbols
final = start+T*fs-1;
for frek = 1:2 % for each frequency
inti = sum(Isig(frek,start:final)); % integrate in symbol period
intq = sum(Qsig(frek,start:final));
sumIQ(frek) = inti^2 + intq^2; % Squared and summed
end
compare = sumIQ(1) - sumIQ(2);
% append the bitstream
if compare >= 0
bitstream = [bitstream 1];
elseif compare < 0
bitstream = [bitstream 0];
end
start = final + 1;
end
end % type == 4, GFSK
- 1
- 2
前往页