diff options
author | sotech117 <michael_foiani@brown.edu> | 2023-12-14 20:10:59 -0500 |
---|---|---|
committer | sotech117 <michael_foiani@brown.edu> | 2023-12-14 20:10:59 -0500 |
commit | 25c3b38b708bf467c40303083190409293452a33 (patch) | |
tree | 1ec8d1362e7e1858bebe15f1e1ce696709fd5b68 | |
parent | bf623056ce18cd94b17e7e62f05bd54ca0f2b194 (diff) |
better wave addition and reading freqs
-rw-r--r-- | Sender.py | 32 | ||||
-rw-r--r-- | __pycache__/utils.cpython-38.pyc | bin | 1778 -> 1782 bytes | |||
-rw-r--r-- | utils.py | 80 | ||||
-rw-r--r-- | visualize.py | 11 |
4 files changed, 37 insertions, 86 deletions
@@ -117,46 +117,18 @@ print(decoded_string) class LinkLayer: - def __init__(self, start_freq=18000): + def __init__(self, start_freq=19800): self.start_freq = start_freq - self.freq_range = 1000 + self.freq_range = 200 self.sampling_rate = 44100 self.p = pyaudio.PyAudio() self.isReceiving = False self.isEstablished = False self.bytes_per_transmit = 1 - self.freq_range = 1000 def transmit_string(self, data): data_list = string_to_binary(data) - - # for i in range(len(data_list)): - # freq_map = {} - # start_freq = 18000 - # for j in range(len(data_list[i])): - # if data_list[i][j] == "0": - # freq_map[start_freq + j * 250] = 0.0 - - # if data_list[i][j] == "1": - # freq_map[start_freq + j * 250] = 1.0 - - # # print(freq_map) - # play_frequencies_separately(freq_map, duration=0.5) play_data(data_list, self.start_freq, self.freq_range, self.bytes_per_transmit, self.p) - - - def receive_string(self, data): - binary = ['0'] * 8 - - for item in data: - freqPosition = (item - self.start_freq) // self.freq_step - if 0 <= freqPosition < 8: binary[freqPosition] = '1' - - binary_string = ''.join(binary) - try: - return chr(int(binary_string, 2)) - except ValueError: - return "Error: Invalid binary data" def send_data(self): while True: diff --git a/__pycache__/utils.cpython-38.pyc b/__pycache__/utils.cpython-38.pyc Binary files differindex 5a723ba..2511512 100644 --- a/__pycache__/utils.cpython-38.pyc +++ b/__pycache__/utils.cpython-38.pyc @@ -25,67 +25,39 @@ def frequencies_to_bytes(frequencies, expected_freqs): freq_interval = expected_freqs[1] - expected_freqs[0] plus_minus = freq_interval // 2 - byte_list = [0] * len(frequencies) + byte_list = ['0'] * len(expected_freqs) for freq in frequencies: - for i in range(len(frequencies)): + for i in range(len(expected_freqs)): # clamp the range around the frequency to the frequency if expected_freqs[i] - plus_minus <= freq < expected_freqs[i] + plus_minus: - byte_list[i] = 1 + byte_list[i] = '1' return byte_list - -def play_frequency(freq, p, duration=1.0, samplingRate=44100): - 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) -# print(freq_list, data) - -# def play_thread(freq): -# play_frequency(freq, p=p) - -# threads = [] -# for item in data: -# for i, bit in enumerate(item): -# if bit == '1': -# thread = threading.Thread(target=play_thread, args=(freq_list[i],)) -# threads.append(thread) -# thread.start() - -# for thread in threads: -# thread.join() - -# p.terminate() - 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): + for byte in data: + print(byte) + samples = None + for i, bit in enumerate(byte): if bit == '1': - 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) + print(freq_list[i]) + s = .125 * np.sin(2 * np.pi * np.arange(44100 * 10.0) * freq_list[i] / 44100) + if samples is None: + samples = s + else: + samples = np.add(samples, s) + if samples is not None: + print(samples) + stream = p.open(format=pyaudio.paFloat32, channels=1, rate=44100, output=True) + stream.write(samples.astype(np.float32).tobytes()) + stream.stop_stream() + stream.close() + +def receive_string(binary): + binary_string = ''.join(binary) + try: + print(chr(int(binary_string, 2))) + except ValueError: + print("Error: Invalid binary data")
\ No newline at end of file diff --git a/visualize.py b/visualize.py index 283358f..759cf34 100644 --- a/visualize.py +++ b/visualize.py @@ -9,6 +9,8 @@ import numpy as np import pyaudio import matplotlib.pyplot as plt +import utils as u + class Test(object): @@ -44,9 +46,9 @@ class Test(object): scaled_spectrum = scaled_spectrum / (np.linalg.norm(scaled_spectrum) + 1e-16) # FIXME: update to self values, given if ur a sender or receiver - starting_freq = 18000 + starting_freq = 19800 end_freq = 20000 - freq_to_index_ratio = (self.CHUNK) / 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) @@ -60,6 +62,11 @@ class Test(object): freqs = [int((indices[i] + starting_range_index) / freq_to_index_ratio) for i in range(len(indices))] print(freqs) + p = u.frequencies_to_bytes(freqs, u.calculate_send_frequencies(19800, 200, 1)) + data = p[:8] + print(data) + u.receive_string(data) + # get the n indices of the max peaks, within our confined spectrum # FIXME: update to self values bytes = 1 |