Skip to content
This repository has been archived by the owner on Feb 19, 2019. It is now read-only.

Commit

Permalink
Fully functional modulation/demodulation functions
Browse files Browse the repository at this point in the history
  • Loading branch information
JHertz5 committed Dec 23, 2016
1 parent 898da77 commit 2f7b74a
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 2 deletions.
59 changes: 58 additions & 1 deletion scripts/fDSQPSKDemodulator.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,61 @@
% bitsOut (Px1 Integers) = P demodulated bits of 1's and 0's
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function [bitsOut]=fDSQPSKDemodulator(symbolsIn,GoldSeq,phi)
function [ bitsOut ] = fDSQPSKDemodulator( symbolsIn, goldSeq, phi )

% chipPeriod and carrierPeriod are given nominal values
T_carrier = 5; % carrier period
T_chip = 1; % chip period
chipsPerBit = T_carrier/T_chip; % chipsPerBit must be an integer value

chipsLength = length(symbolsIn);
symbolLength = chipsLength/chipsPerBit;
seqLength = length(goldSeq);

%% De-Spread using Gold Sequence

% Change gold sequence form
goldSeq = 1 - 2*goldSeq; % 1 -> -1, 0 -> 1

symbolsModulated = complex(zeros(symbolLength, 1), zeros(symbolLength, 1));

for chipIndex = 1:chipsLength
seqIndex = mod(chipIndex-1, seqLength) + 1; % index for gold sequence
symbolIndex = ceil(chipIndex/chipsPerBit); % index for QPSK symbols

% Accumulate chips to be averaged later
symbolsModulated(symbolIndex) = symbolsModulated(symbolIndex) + goldSeq(seqIndex) * symbolsIn(chipIndex);

if mod(chipIndex, chipsPerBit) == 0
symbolsModulated(symbolIndex) = symbolsModulated(symbolIndex) / chipsPerBit;
end
end

%% Perform QPSK de-modulation

phi_rad = deg2rad(phi);
bitsOut = zeros(symbolLength*2, 1, 'uint8');

for symbolIndex = 1:symbolLength

% Find phase angle
theta = angle(symbolsModulated(symbolIndex));
if theta < 0
theta = theta + 2*pi;
end

% Convert phase angle to binary values
bitPair = uint8((theta - phi_rad)*2/pi);


% check for invalid values
if bitPair < 0 || bitPair > 3
fprintf('ERROR: bitsInPaired contained value outside range 0-3: %i @ index %i\n', symbolsPaired(symbolIndex), symbolIndex)
return
end

% Split bit pairs into bits
bitsOut(2*symbolIndex - 1 : 2*symbolIndex) = de2bi(bitPair, 2, 'left-msb');
end

end
54 changes: 53 additions & 1 deletion scripts/fDSQPSKModulator.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,56 @@
% symbolsOut (Rx1 Complex) = R channel symbol chips after DS-QPSK Modulation
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function [symbolsOut]=fDSQPSKModulator(bitsIn,goldseq,phi)
function [ symbolsOut ] = fDSQPSKModulator( bitsIn, goldSeq, phi )

% chipPeriod and carrierPeriod are given nominal values
T_carrier = 5; % carrier period
T_chip = 1; % chip period
chipsPerBit = T_carrier/T_chip; % chipsPerBit must be an integer value

symbolsLength = length(bitsIn)/2;
chipsLength = symbolsLength * chipsPerBit;
seqLength = length(goldSeq);

symbolsPaired = zeros(symbolsLength, 1, 'double');
symbolsModulated = complex( symbolsPaired, symbolsPaired ); % initialise as a symbolsBinary sized complex vector

%% Perform QPSK modulation

phi_rad = deg2rad(phi);

for symbolIndex = 1:symbolsLength

symbolsPaired(symbolIndex) = bitsIn(symbolIndex*2 - 1) * 2 + bitsIn(symbolIndex*2); % group bits into pairs

%check for invalid values
if symbolsPaired(symbolIndex) < 0 || symbolsPaired(symbolIndex) > 3
fprintf('ERROR: bitsInPaired contained value outside range 0-3: %i @ index %i\n', bitsInPaired(symbolIndex), symbolIndex)
return
end

% Find symbols in polar form
theta = phi_rad + symbolsPaired(symbolIndex)*pi/2; % NOTE: diagram was vague on what phi should be for n ~= 00
r = sqrt(2);

% Convert to a + bi form
symbolsModulated(symbolIndex) = complex(r*cos(theta), r*sin(theta));
end

%% Spread using Gold Sequence
% As specified in SomeNotes.1, "The messages are first modulated using QPSK,
% then spread by the gold sequences"

% Change gold sequence form
goldSeq = 1 - 2*goldSeq; % 1 -> -1, 0 -> 1

symbolsOut = complex(zeros(chipsLength, 1), zeros(chipsLength, 1));

for chipIndex = 1:chipsLength
seqIndex = mod(chipIndex-1, seqLength) + 1; % index for gold sequence
symbolIndex = ceil(chipIndex/chipsPerBit); % index for QPSK symbols

symbolsOut(chipIndex) = goldSeq(seqIndex) * symbolsModulated(symbolIndex);
end

end

0 comments on commit 2f7b74a

Please sign in to comment.