Skip to content

Instantly share code, notes, and snippets.

@j6s
Created March 2, 2016 19:57
Show Gist options
  • Select an option

  • Save j6s/1beb82cf9555546217aa to your computer and use it in GitHub Desktop.

Select an option

Save j6s/1beb82cf9555546217aa to your computer and use it in GitHub Desktop.
# Merges 2 signals, using one of the inputs as odd bits and the other one
# as even bits.
#
# arguments:
# - I: signal containing the odd bits
# - Q: signal containing the even bits
#
# returns: merged signal
function [signal] = mergeSignals (I, Q)
l = length(I) + length(Q);
signal = zeros(1,l);
for i = 1:l
if mod(i,2) == 1
# odd number
signal(i) = I(ceil(i/2));
else
# even number
signal(i) = Q(i/2);
end
end
endfunction
# pads a binary signal using {interpolation} samples per bit
#
# example:
# - signalIn: [1,1,0,1,0]
# - interpolation: 5
#
# output: [1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0]
function [signalOut] = padSignal (signalIn, interpolation)
l = length(signalIn);
# we have to add one value in the end, because we are using
# the 'previous' interpolation which looses the value of the
# last element
signalIn(l + 1) = 0;
signalOut = interp1(1:l+1, signalIn, 1:1/interpolation:l+1, 'previous');
endfunction
# decodes a QPSK ecoded message.
#
# agruments:
# - QPSK: The encoded message
# - f: the frequency to use (in Hz/bit)
# - bitlength: How many samples make up one bit?
#
# returns: original bit squence
#
function [signal] = QpskDecode (QPSK, f, bitlength)
# length of the incoming signal in samples
l = length(QPSK);
# number of bits of the bit stream
ll = round(l / bitlength);
# the time bounds that we want to work with
t = 0:1/bitlength:ll;
# the odd and even parts
de_I = QPSK .* cos(2 * pi * f * t);
de_Q = QPSK .* sin(2 * pi * f * t);
# getting the original bits back by using the mean value during the bit
I = zeros(1, ll);
for i = 1:ll
begin = (i-1) * bitlength + 1;
ending = i * bitlength + 1;
I(i) = round(mean(de_I(begin:ending)) + .5);
end
Q = zeros(1, ll);
for i = 1:ll
begin = (i-1) * bitlength + 1;
ending = i * bitlength + 1;
Q(i) = round(mean(de_Q(begin:ending)) + .5);
end
# merge the odd and even bits
signal = mergeSignals(I,Q);
endfunction
# Does the QPSK encoding.
# parameters:
# - signal: the bitsequence that should be encoded. Should be 1D array.
# - f: the frequency that should be used to encode
# - interpolation: how much should the sequence be interpolated (interpolation is
# necessary, because a sine is not descrete)
#
# return values:
# - QPSK: The final QPSK encoded signal
# - I_rc: The encoded odd bits of the signal
# - Q_rc: The encoded even bits of the signal
# - l: The length of the final sequence
# - t: The timeframe / x axis corresponding to l
function [QPSK, I_rc, Q_rc, l, t] = QpskEncode (signal, f = 2, interpolation)
[I,Q,l] = splitSignal(signal);
I = padSignal(I, interpolation);
Q = padSignal(Q, interpolation);
# using 1 and -1 instead of 1 and zero
I = I * 2 - 1;
Q = Q * 2 - 1;
# modulating onto the carrier
t = 0:1/interpolation:l;
f = 2;
I_rc = I .* cos(2 * pi * f * t);
Q_rc = Q .* sin(2 * pi * f * t);
# adding both streams together
QPSK = sqrt(1/2) * I_rc + sqrt(1/2) * Q_rc;
endfunction
#
# splits a signal into to halfs where one contains all even
# bits and the other one all odd bits
#
# arguments:
# - x1: incoming bitstream to split
#
# returns:
# - I: all of the odd bits
# - Q: all of the even bits
# - halflength: length of the two streams (half of the original length).
#
# Note: The length value might not be accurate for signals of odd length.
function [I,Q,halflength] = splitSignal (x1)
halflength = length(x1)/2;
I = zeros(1,halflength);
Q = zeros(1,halflength)
for i=1:halflength
#I(i) = x1(2*i);
#Q(i) = x1(2*i-1);
Q(i) = x1(2*i);
I(i) = x1(2*i-1);
end
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment