Something went wrong. Try again.
A proof-of-concept proposal for turning standard Rubik's Cubes into smartcubes by embedding speakers into the cube's centercaps.
Something went wrong. Try again.
DIY-Smartcube Spectrogram.py
1.6 kB · 43 lines
Python
at commit b7ec24d3
1234567891011121314151617181920212223242526272829303132333435363738394041424344# Reference: https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.stft.html
import osimport matplotlib.pyplot as plt
from scipy import signalfrom scipy.io import wavfileimport numpy as np
from AuditorySupercube import AuditorySupercube
SAMPLES_PER_WINDOW = 1024 # Seems to be a good number to balance frequency precision with time precision.THRESHOLD = 1500 # The minimum value required for a frequency to be detected as present.
# Compute the Gabor transform on the affected audioaudio_path = "./tones.wav"sample_rate, audio_samples = wavfile.read(audio_path)freq, time, Zxx = signal.stft(audio_samples, fs=sample_rate, nperseg=SAMPLES_PER_WINDOW, noverlap=(SAMPLES_PER_WINDOW // 4) * 3)
# Determine the frequencies of interestspectrogram = np.abs(Zxx)face_mapping = AuditorySupercube()for time_idx in range(len(time)): important_freqs = [] for freq_idx in range(len(freq)): if spectrogram[freq_idx][time_idx] > THRESHOLD: important_freqs.append(freq[freq_idx]) if len(important_freqs) > 0: detected_states = face_mapping.get_state_from_freq(important_freqs) print(f"At time {time[time_idx]:.6f} the states {detected_states} were detected based on the {len(important_freqs)} frequencies {important_freqs} that surpassed the threshold.")
# Show off a spectrogram of the detected audioplt.pcolormesh(time, freq, np.abs(Zxx), shading='gouraud')plt.title('STFT Magnitude')plt.ylabel('Frequency [Hz]')plt.xlabel('Time [sec]')plt.show()
input("Press Enter to finish")