aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsotech117 <michael_foiani@brown.edu>2023-12-14 19:34:57 -0500
committersotech117 <michael_foiani@brown.edu>2023-12-14 19:34:57 -0500
commitfb072a3df11567debb2fb7babb60a04677c167d9 (patch)
treecd484413cba075a6cdfb27b29a66a2415a2bcb5a
parent54d8ff9d8fbcc68e2fe424261b14ed2e1670e446 (diff)
add amplitude into reading ranges
-rw-r--r--Sender.py10
-rw-r--r--__pycache__/utils.cpython-38.pycbin1744 -> 1778 bytes
-rw-r--r--utils.py65
-rw-r--r--visualize.py19
4 files changed, 48 insertions, 46 deletions
diff --git a/Sender.py b/Sender.py
index 0eb97b3..34e0b6f 100644
--- a/Sender.py
+++ b/Sender.py
@@ -86,7 +86,7 @@ def transmit_string(data):
freq_map[start_freq + j * 250] = 1.0
# print(freq_map)
- play_frequencies_separately(freq_map, duration=0.5)
+ play_frequencies_separately(freq_map, duration=1000)
"""
:param data: A list of peak frequencies.
@@ -117,14 +117,14 @@ print(decoded_string)
class LinkLayer:
- def __init__(self, start_freq=18000, freq_step=250):
+ def __init__(self, start_freq=18000):
self.start_freq = start_freq
- self.freq_step = freq_step
+ self.freq_range = 1000
self.sampling_rate = 44100
self.p = pyaudio.PyAudio()
self.isReceiving = False
self.isEstablished = False
- self.byte_per_transmit = 8
+ self.bytes_per_transmit = 1
self.freq_range = 1000
def transmit_string(self, data):
@@ -142,7 +142,7 @@ class LinkLayer:
# # print(freq_map)
# play_frequencies_separately(freq_map, duration=0.5)
- play_data(data_list, self.start_freq, self.freq_range, self.byte_per_transmit, self.p)
+ play_data(data_list, self.start_freq, self.freq_range, self.bytes_per_transmit, self.p)
def receive_string(self, data):
diff --git a/__pycache__/utils.cpython-38.pyc b/__pycache__/utils.cpython-38.pyc
index b73d0a1..5a723ba 100644
--- a/__pycache__/utils.cpython-38.pyc
+++ b/__pycache__/utils.cpython-38.pyc
Binary files differ
diff --git a/utils.py b/utils.py
index f11adb3..5dbd98c 100644
--- a/utils.py
+++ b/utils.py
@@ -1,4 +1,3 @@
-
# given the cmdline arg, turns the byte sequencies into a list of frequencies, and vice versa
# 1875 1924 +24, -25, range/2, 1, flipping new info 2 sending or not
@@ -7,48 +6,51 @@ import pyaudio
import threading
-def make_frequencies_map(start_freq, freq_step, byte_per_transmit, is_sender=True):
- # start_freq += 1500
+def calculate_send_frequencies(start_freq, freq_range, bytes_per_transmit):
+ bits_to_send = 8 * bytes_per_transmit + 2 # 8 bits per byte, 2 bits for flags
+ freq_interval = freq_range / (bits_to_send + 1) # +1 to not include endpoints of range
+
freq_list = []
- sender_range = freq_step // 2
- plus_minus = sender_range // (byte_per_transmit + 3)
-
- for i in range(byte_per_transmit + 3):
- # print(start_freq + i * plus_minus, sender_range, plus_minus)
- freq_list.append(start_freq + i * plus_minus)
- if not is_sender:
- freq_list[i] += sender_range
+ for i in range(bits_to_send):
+ f = int(start_freq + (i + 1) * freq_interval)
+ freq_list.append(f)
- return freq_list
+ print(freq_list)
+ return freq_list
-def frequencies_to_bytes(frequencies, start_freq, freq_step, byte_per_transmit, is_sender=True):
- freq_list = make_frequencies_map(start_freq, freq_step, byte_per_transmit, is_sender)
- byte_list = []
- sender_range = freq_step // 2
- plus_minus = sender_range // (byte_per_transmit + 3)
+def frequencies_to_bytes(frequencies, expected_freqs):
+ # get the interval between frequencies, so we can clamp the range around them
+ freq_interval = expected_freqs[1] - expected_freqs[0]
+ plus_minus = freq_interval // 2
- for i in range(len(frequencies)):
- if frequencies[i] <= freq_list[i] < frequencies[i] + plus_minus - 1:
- byte_list.append("1")
- else:
- byte_list.append("0")
+ byte_list = [0] * len(frequencies)
+ for freq in frequencies:
+ for i in range(len(frequencies)):
+ # clamp the range around the frequency to the frequency
+ if expected_freqs[i] - plus_minus <= freq < expected_freqs[i] + plus_minus:
+ if byte_list[i] == 1:
+ return None # bad data, return nullish list
+ byte_list[i] = 1
return byte_list
+
def play_frequency(freq, p, duration=1.0, samplingRate=44100):
- amplitude = 1.0 # Maximum amplitude
- samples = (amplitude * np.sin(2 * np.pi * np.arange(samplingRate * duration) * freq / samplingRate)).astype(np.float32).tobytes()
+ amplitude = .1 # Maximum amplitude
+ print(freq)
+ samples = (amplitude * np.sin(2 * np.pi * np.arange(samplingRate * duration) * freq / samplingRate)).astype(
+ np.float32).tobytes()
stream = p.open(format=pyaudio.paFloat32, channels=1, rate=samplingRate, output=True)
stream.write(samples)
-
# thread for listening here
stream.stop_stream()
stream.close()
+
# def play_data(data, start_freq, freq_step, byte_per_transmit):
# p = pyaudio.PyAudio()
# freq_list = make_frequencies_map(start_freq, freq_step, byte_per_transmit)
@@ -69,26 +71,23 @@ def play_frequency(freq, p, duration=1.0, samplingRate=44100):
# thread.join()
# p.terminate()
-
-def play_data(data, start_freq, freq_step, byte_per_transmit, p):
- freq_list = make_frequencies_map(start_freq, freq_step, byte_per_transmit)
-
+
+def play_data(data, start_freq, freq_step, bytes_per_transmit, p):
+ freq_list = calculate_send_frequencies(start_freq, freq_step, bytes_per_transmit)
threads = []
for item in data:
for i, bit in enumerate(item):
if bit == '1':
- thread = threading.Thread(target=play_frequency, args=(freq_list[i], p, 1.0))
+ thread = threading.Thread(target=play_frequency, args=(freq_list[i], p, 10.0))
threads.append(thread)
thread.start()
for thread in threads:
thread.join()
-
# def listen_for_confirmation(stream):
# # Logic to listen and decode the confirmation data
# # This function should run in its own thread
-
-# recieved_data = stream.read(1024)
+# recieved_data = stream.read(1024)
diff --git a/visualize.py b/visualize.py
index 7a86373..283358f 100644
--- a/visualize.py
+++ b/visualize.py
@@ -46,24 +46,29 @@ class Test(object):
# FIXME: update to self values, given if ur a sender or receiver
starting_freq = 18000
end_freq = 20000
- freq_to_index_ratio = (self.CHUNK - 1) / self.RATE
+ freq_to_index_ratio = (self.CHUNK) / self.RATE
# only accept the scaled spectrum from our starting range to 20000 Hz
starting_range_index = int(starting_freq * freq_to_index_ratio)
ending_range_index = int(end_freq * freq_to_index_ratio)
print(starting_freq, end_freq, starting_range_index, ending_range_index)
restricted_spectrum = scaled_spectrum[starting_range_index:ending_range_index + 1]
+ # normalize the restricted spectrum
+ indices = np.argwhere(restricted_spectrum > .125)
+ print(indices)
+
+ freqs = [int((indices[i] + starting_range_index) / freq_to_index_ratio) for i in range(len(indices))]
+ print(freqs)
+
# get the n indices of the max peaks, within our confined spectrum
# FIXME: update to self values
bytes = 1
num_bits = bytes * 8 + 2
- top8_indices = np.argpartition(restricted_spectrum, -num_bits)[-num_bits:]
-
- # map the top 8 to indices to frequencies
- top8_freqs = [int((top8_indices[i] + starting_freq) / freq_to_index_ratio) for i in range(len(top8_indices))]
+ if num_bits > len(restricted_spectrum):
+ print("ERROR: num_bits > len(restricted_spectrum)")
# print(index_to_freq[max_index], max_index, max_index * self.RATE / (self.CHUNK - 1))
- return top8_freqs[0], scaled_spectrum
+ return freqs, scaled_spectrum
def read_audio_stream(self):
data = self.stream.read(self.CHUNK)
@@ -75,8 +80,6 @@ class Test(object):
while not self.pause:
waveform = self.read_audio_stream()
freq_max, scaled_spectrum = self.get_fundamental_frequency(waveform)
- if freq_max not in self.seenvalues:
- print(freq_max)
# update figure canvas if wanted
if graphics: