You are on page 1of 3

def pulse_train(data,Nt): # data = bits; Nt = No.

of samples per bit


sig = []
for i in range(len(data)):
for j in range(Nt):
sig.append(data[i])
return sig

def butter_lowpass(cutoff, fs, order=5):


nyq = 0.5 * fs
normal_cutoff = cutoff / nyq
b, a = butter(order, normal_cutoff, btype='low', analog=False)
return b, a

def butter_lowpass_filter(data, cutoff, fs, order=5):


b, a = butter_lowpass(cutoff, fs, order=order)
y = lfilter(b, a, data)
return y

fc = 4;
fs = 16*fc;

N = 10 # No. of data bits


T = 1 # Bit duration = 1 second

data = []
for i in range(N):
data.append(randint(0,1))

t = np.linspace(0,N*T,fs*N*T)

c = np.cos(2*math.pi*fc*t)
# plt.figure(1)
# plt.subplot(3,1,1)
# plt.plot(t,c)
# plt.title('carrier signal')

m = pulse_train(data,int(fs*T))
m = [x*2-1 for x in m]
# plt.subplot(3,1,2)
# plt.plot(t,m)
# plt.title('message bits')

sig = np.multiply(c,m)
# plt.subplot(3,1,3)
# plt.plot(t,sig)
# plt.title('bpsk signal')

in_ph = []
quad = []

for i in range(N):
if(i%2 == 0):
in_ph.append(data[i])
else:
quad.append(data[i])

N1 = int(N/2)
T1 = 2*T
t1 = np.linspace(0,N1*T1,fs*N1*T1)
c1 = np.cos(2*math.pi*fc*t1)
s1 = np.sin(2*math.pi*fc*t1)

in_ph_m = pulse_train(in_ph,int(fs*T1))
in_ph_m = [x*2-1 for x in in_ph_m]

quad_m = pulse_train(quad,int(fs*T1))
quad_m = [x*2-1 for x in quad_m]

sig_in_ph = np.multiply(c1,in_ph_m)
sig_quad = np.multiply(s1,quad_m)

sig_qpsk = sig_in_ph - sig_quad

plt.figure(2)
plt.subplot(3,3,1)
plt.plot(t1,c1)
plt.subplot(3,3,4)
plt.plot(t1,in_ph_m)
plt.subplot(3,3,7)
plt.plot(t1,sig_in_ph)
plt.subplot(3,3,2)
plt.plot(t1,s1)
plt.subplot(3,3,5)
plt.plot(t1,quad_m)
plt.subplot(3,3,8)
plt.plot(t1,sig_quad)
plt.subplot(3,3,3)
plt.plot(t1,sig_in_ph)
plt.subplot(3,3,6)
plt.plot(t1,sig_quad)
plt.subplot(3,3,9)
plt.plot(t1,sig_qpsk)

plt.figure(3)
plt.subplot(3,1,1)
plt.plot(t,c)
plt.subplot(3,1,2)
plt.plot(t,m)
plt.subplot(3,1,3)
plt.plot(t,sig_qpsk)

plt.figure(4)
plt.scatter(in_ph_m,quad_m)

r_in_ph = np.multiply(sig_qpsk,c1)
r_quad = np.multiply(sig_qpsk,-s1)
plt.figure(5)
plt.subplot(2,1,1)
plt.plot(t,r_in_ph)
plt.subplot(2,1,2)
plt.plot(t,r_quad)

yf_in_ph = butter_lowpass_filter(r_in_ph,2,fs,5)
yf_quad = butter_lowpass_filter(r_quad,2,fs,5)

plt.figure(6)
plt.subplot(2,1,1)
plt.plot(yf_in_ph)
plt.subplot(2,1,2)
plt.plot(yf_quad)

ri = []
rq = []

sumi = 0
sumq = 0

for j in range(N1):
sumi = 0
sumq = 0
for i in range(0+j*fs*T1,fs*T1+j*fs*T1):
sumi = sumi + yf_in_ph[i]
sumq = sumq + yf_quad[i]

if (sumi>=0):
ri.append(1)
else:
ri.append(0)

if (sumq>=0):
rq.append(1)
else:
rq.append(0)

rdata = []
for i in range(N1):
rdata.append(ri[i])
rdata.append(rq[i])

You might also like