aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsotech117 <michael_foiani@brown.edu>2023-12-14 18:15:07 -0500
committersotech117 <michael_foiani@brown.edu>2023-12-14 18:15:10 -0500
commit3ca213d829b6c56beb764eae430a78736d73eeca (patch)
tree29a8e404e5f33ac4ac4a56f89cc81cf03bcfb1c2
parent25aa2dd624776f460c7424431b62958f9c524e3d (diff)
add functionality to restrict range and determine max 8 frequencies within that range
-rw-r--r--Sender.py5
-rw-r--r--visualize.py34
2 files changed, 25 insertions, 14 deletions
diff --git a/Sender.py b/Sender.py
index ca0183e..2a125e9 100644
--- a/Sender.py
+++ b/Sender.py
@@ -12,11 +12,9 @@ Play a single frequency.
:param samplingRate: Sampling rate in Hz.
"""
def play_frequency(freq, amplitude, duration=1.0, samplingRate=44100, p=None):
-
# Generate sample for the given frequency as a float32 array
samples = (amplitude * np.sin(2*np.pi*np.arange(samplingRate*duration)*freq/samplingRate)).astype(np.float32).tobytes()
-
# Open stream
stream = p.open(format=pyaudio.paFloat32,
channels=1,
@@ -29,7 +27,7 @@ def play_frequency(freq, amplitude, duration=1.0, samplingRate=44100, p=None):
stream.stop_stream()
stream.close()
- # p.terminate()
+ p.terminate()
# def play_frequency(freq, amplitude, duration=1.0, samplingRate=44100):
@@ -69,6 +67,7 @@ Use threads to play multiple frequencies simultaneously.
"""
def play_frequencies_separately(freq_map, duration=1.0, samplingRate=44100):
p = pyaudio.PyAudio()
+
threads = []
for freq, amplitude in freq_map.items():
thread = threading.Thread(target=play_frequency, args=(freq, amplitude, duration, samplingRate, p))
diff --git a/visualize.py b/visualize.py
index d1b2a08..7a86373 100644
--- a/visualize.py
+++ b/visualize.py
@@ -43,13 +43,27 @@ class Test(object):
scaled_spectrum = np.abs(spectrum)
scaled_spectrum = scaled_spectrum / (np.linalg.norm(scaled_spectrum) + 1e-16)
- # get the index of the max
- np_max = max(scaled_spectrum)
- max_index = np.where(scaled_spectrum == np_max)[0][0]
-
- # the index corresponds to the following linspace
- index_to_freq = np.linspace(0, self.RATE, self.CHUNK)
- return index_to_freq[max_index], scaled_spectrum
+ # 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
+ # 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]
+
+ # 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))]
+
+ # print(index_to_freq[max_index], max_index, max_index * self.RATE / (self.CHUNK - 1))
+ return top8_freqs[0], scaled_spectrum
def read_audio_stream(self):
data = self.stream.read(self.CHUNK)
@@ -61,10 +75,8 @@ class Test(object):
while not self.pause:
waveform = self.read_audio_stream()
freq_max, scaled_spectrum = self.get_fundamental_frequency(waveform)
- if 250 < freq_max < 550:
- if freq_max not in self.seenvalues:
- print(freq_max)
- self.seenvalues.add(freq_max)
+ if freq_max not in self.seenvalues:
+ print(freq_max)
# update figure canvas if wanted
if graphics: