-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbb2.py
137 lines (110 loc) · 3.15 KB
/
bb2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#! /usr/bin/env python
# bb2 Project
#
# License: see LICENSE.md
#
# Copyright (C) 2021 Neal Patwari
#
#
# Author: Neal Patwari, npatwari@wustl.edu
#
# Version History:
#
# Version 1.0: Initial Release. Feb 2021. For Python 3.6.10
# Version 1.1: For class Spring 2023.
#
import sys
import numpy as np
import scipy.spatial.distance as dist
import matplotlib.pyplot as plt
import matplotlib
import scipy.signal
import scipy.io
import digicomm
matplotlib.rc('xtick', labelsize=16)
matplotlib.rc('ytick', labelsize=16)
plt.ion()
#################################################
# Signal Generation
# INPUT: none
# OUTPUT: binary data
temp = 'ESE 471';
data = digicomm.text2bits(temp);
#################################################
# Modulation
# INPUT: data
# OUPUT: modulated values, x
symbolNumToValue = {
0: -1,
1: +1
}
x = np.array([symbolNumToValue[b] for b in data])
#################################################
# Upsample
# INPUT: modulated values, x
# OUTPUT: modulated values at sampling rate, x_s
T_s = 16
x_s = digicomm.oversample( x, T_s)
#################################################
# Pulse-shape filter
# INPUT: modulated values at sampling rate, x_s
# OUTPUT: baseband transmit signal s
pulse = np.sqrt(1./T_s)*np.ones(T_s)
s = np.convolve(x_s, pulse)
# Plot for project handout
plt.figure(1)
plt.plot(s, '-o', linewidth=2)
plt.ylim([-0.5, 0.5])
plt.xlim([0, len(s)])
plt.xlabel('Sample', fontsize=16)
plt.ylabel('Value', fontsize=16)
plt.grid('on')
# Load s if doing the receiver assignment from a saved mat file
#temp = scipy.io.loadmat('bb2.mat')
#s = temp['s'].flatten()
#################################################
#################################################
# Bipolar, baseband PAM receiver
#################################################
#################################################
#################################################
# Matched filter
# INPUT: baseband transmitted signal s
# OUTPUT: matched-filtered signal y
pulse = np.sqrt(1./T_s)*np.ones(T_s)
y = scipy.signal.lfilter(pulse, 1, s);
# Plot for project handout
plt.figure(2)
plt.plot(y, '-o', linewidth=2)
plt.ylim([-1.5, 1.5])
plt.xlim([0, len(y)])
plt.xlabel('Sample', fontsize=16)
plt.ylabel('Value', fontsize=16)
plt.grid('on')
#################################################
# Time Synch
# Input: Matched Filter output
# OUTPUT: Synched MF output with samples at US_Rate, 2*US_Rate, ...
# *** Note: User must set this by looking at y and seeing the
# first index at which the first symbol sample should be read.
y_s = y[15:]
# Plot eye-diagram
offset = 0
plt.figure(3)
digicomm.plot_eye_diagram(y_s, T_s, offset)
#################################################
# Downsample
# INPUT: Synched matched filter output
# OUTPUT: Symbol Samples (at n*T_s)
r_hat = y_s[range( T_s-1, len(y_s), T_s)]
#################################################
# Bit decisions
# INPUT: Symbol Samples
# OUTPUT: Bits
data_out = (r_hat>0).astype(int)
#################################################
# Translate to ascii text
# INPUT: Bits
# OUTPUT: Character vector, message_out
message_out = digicomm.binvector2str(data_out)
print(message_out)