-
Notifications
You must be signed in to change notification settings - Fork 0
/
Receiver_uSonic_NFC.py
219 lines (198 loc) · 6.84 KB
/
Receiver_uSonic_NFC.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import pyaudio
import wave
import numpy as np
import matplotlib.pyplot as plt
#'''
CHUNK = 1024 #1024 #change to 2048 ?
FORMAT = pyaudio.paInt16 #paInt8
CHANNELS = 1 #2
RATE = 44100 #sample rate
RECORD_SECONDS = 26
WAVE_OUTPUT_FILENAME = "output.wav"
p = pyaudio.PyAudio()
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK) #buffer
print("* recording")
frames = []
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
frames.append(data) # 2 bytes(16 bits) per channel
print("* done recording")
stream.stop_stream()
stream.close()
p.terminate()
wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()
#'''
change = str(raw_input("Do you want to change the default frequency settings(Y)? "))
if change == 'Y':
f0 = str(raw_input("Enter frequency of bit 0 transmission: "))
for i in range(len(f0)):
if (ord(f0[i]) >= 48 and ord(f0[i]) <= 57):
bool_valid0 = True
else:
bool_valid0 = False
print 'Invalid Input: Enter numeric value only'
break
if bool_valid0 == True:
freq0 = int(f0)
else:
print ""
#exit program
f1 = str(raw_input("Enter frequency of bit 1 transmission: "))
for i in range(len(f1)):
if (ord(f1[i]) >= 48 and ord(f1[i]) <= 57):
bool_valid1 = True
else:
bool_valid1 = False
print 'Invalid Input: Enter numeric value only'
break
if bool_valid1 == True:
freq1 = int(f1)
else:
print ''#exit program
else:
freq0 = 1000
freq1 = 5000
chunk = 22050
num_samples = 0
delta = 10
#freq0 = 1000
#freq1 = 5000
bits = ''
bit_count = 0
ascii = 0
file_content = ''
# open up a wave
wf = wave.open('output.wav', 'rb')
swidth = wf.getsampwidth()
RATE = wf.getframerate()
# use a Blackman window
window = np.blackman(chunk)
# open stream
p = pyaudio.PyAudio()
stream = p.open(format =
p.get_format_from_width(wf.getsampwidth()),
channels = wf.getnchannels(),
rate = RATE,
output = True)
# read some data
#print("2")
data = wf.readframes(chunk)
print len(data)
print (chunk*swidth)
bit_list = []
num_values = 50
amplitude = 0.5
message_table = []
# play stream and find the frequency of each chunk
while len(data) == chunk*swidth:
# write data out to the audio stream
stream.write(data)
#print("1")
# unpack the data and times by the hamming window
indata = np.array(wave.struct.unpack("%dh"%(len(data)/swidth),\
data))*window
# Take the fft and square each value
fftData=abs(np.fft.rfft(indata))**2
# find the maximum
which = fftData[1:].argmax() + 1
# use quadratic interpolation around the max
if which != len(fftData)-1:
y0,y1,y2 = np.log(fftData[which-1:which+2:])
x1 = (y2 - y0) * .5 / (2 * y1 - y2 - y0)
# find the frequency and output it
thefreq = (which+x1)*RATE/chunk
if ((thefreq >= (freq1-delta)) and (thefreq <= (freq1+delta))):
bits = bits + str(1)
bit_list.append(1)
print "The freq is %f Hz." % (thefreq)
for i in range(num_values):
message_table.append(amplitude)
elif ((thefreq >= (freq0-delta)) and (thefreq <= (freq0+delta))):
bits = bits + str(0)
bit_list.append(0)
print "The freq is %f Hz." % (thefreq)
for i in range(num_values):
message_table.append(-1*amplitude)
bit_count = len(bits)
if bit_count != 0:
ascii = ascii + int(bits[bit_count-1])*2**(8-bit_count)
num_samples += 1
if bit_count == 8:
#print ascii
file_content += str(unichr(ascii))
bits = ''
ascii = 0
#if ((thefreq >= (10000-delta)) and (thefreq <= (10000+delta))):
# break
else:
thefreq = which*RATE/chunk
if ((thefreq >= (freq1-delta)) and (thefreq <= (freq1+delta))):
bits = bits + str(1)
bit_list.append(1)
print "The freq is %f Hz." % (thefreq)
for i in range(num_values):
message_table.append(amplitude)
print "The freq is %f Hz." % (thefreq)
elif ((thefreq >= (freq0-delta)) and (thefreq <= (freq0+delta))):
bits = bits + str(0)
bit_list.append(0)
print "The freq is %f Hz." % (thefreq)
for i in range(num_values):
message_table.append(-1*amplitude)
print "The freq is %f Hz." % (thefreq)
bit_count = len(bits)
if bit_count != 0:
ascii = ascii + int(bits[bit_count-1])*2**(8-bit_count)
if bit_count == 8:
#print ascii
file_content += str(unichr(ascii))
bits = ''
ascii = 0
#num_samples += 1
#if ((thefreq >= (10000-delta)) and (thefreq <= (10000+delta))):
# break
# read some more data
data = wf.readframes(chunk)
if data:
stream.write(data)
#print (num_samples*2048)
#print swidth
stream.close()
p.terminate()
print file_content
time_message = []
freq_message = RATE/chunk
time_period_message = 1.0/freq_message
start = 0.0
step = time_period_message/num_values
for i in range(len(message_table)):
time_message.append(start)
start = start + step
plt.subplot(111,axisbg='black')
plt.axis([0,max(time_message),(-(amplitude)-0.5),(amplitude+0.5)])
plt.plot(time_message, message_table, 'go')
plt.plot(time_message, message_table, 'g')
plt.ylabel('Amplitude');
plt.xlabel('Time');
plt.title('Message Waveform');
plt.text((time_period_message), amplitude+0.3, 'Frequency = '+str(freq_message) + 'Hz', color = 'red')
plt.text((time_period_message), amplitude+0.1, 'Amplitude = '+str(amplitude) + 'units', color = 'red')
plt.text((2.0*max(time_message)/3), amplitude+0.3, 'Time period = '+str(time_period_message) + 's', color = 'red')
plt.show()
'''
if ((frequency >= (freq1-delta)) and (frequency <= (freq1+delta))):
bits = bits + str(1)
elif ((frequency >= (freq0-delta)) and (frequency <= (freq0+delta))):
bits = bits + str(0)
bit_count = len(bits)
ascii = ascii + int(bits[bit_count-1])*2**(8-bit_count)
'''