Created
April 3, 2020 10:31
-
-
Save benevpi/ea8281b7fa7e4c166a44f51cf6732c55 to your computer and use it in GitHub Desktop.
Final Circuit Python frequency locator for Adafruit Clue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import array | |
import board | |
import audiobusio | |
import ulab | |
import ulab.extras | |
import ulab.vector | |
fft_size = 2048 | |
# instantiate board mic | |
mic = audiobusio.PDMIn(board.MICROPHONE_CLOCK, board.MICROPHONE_DATA, | |
sample_rate=16000, bit_depth=16) | |
#use some extra sample to account for the mic startup | |
samples_bit = array.array('H', [0] * (fft_size+3)) | |
# Main Loop | |
def main(): | |
max_all = 10 | |
while True: | |
mic.record(samples_bit, len(samples_bit)) | |
samples = ulab.array(samples_bit[3:]) | |
#print(len(samples)) | |
spectrogram1 = ulab.extras.spectrogram(samples) | |
# spectrum() is always nonnegative, but add a tiny value | |
# to change any zeros to nonzero numbers | |
spectrogram1 = ulab.vector.log(spectrogram1 + 1e-7) | |
#print(len(spectrogram1)) | |
spectrogram1 = spectrogram1[1:(fft_size//2)-1] | |
max_curr = ulab.numerical.max(spectrogram1)[0] | |
index=0 | |
for item in spectrogram1: | |
if item == max_curr: | |
print((index+1)*(16000/fft_size)) | |
index=index+1 | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment