Created
August 17, 2019 16:57
-
-
Save en129/27b1bba13c9db696a9ebecd65f25d64d to your computer and use it in GitHub Desktop.
FFT spectrum demo for Maixduino
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
####################################### | |
# FFT spectrum demo for Maixduino | |
# original:https://github.com/sipeed/MaixPy_scripts/blob/master/hardware/demo_fft_spectrum.py | |
# Maixduino sch:http://dl.sipeed.com/MAIX/HDK/Maixduino/Maixduino-4.30/Maixduino-4.30%28schematic%29.pdf | |
# mic data sheet:http://dl.sipeed.com/MAIX/HDK/Chip_DS/%E9%BA%A6%E5%85%8B_MSM261S4030H0%28%E4%BD%BF%E7%94%A8%E7%9A%84%29.pdf | |
####################################### | |
from Maix import GPIO, I2S, FFT | |
import image, lcd, math | |
from board import board_info | |
from fpioa_manager import fm | |
sample_rate = 38640 | |
sample_points = 1024 | |
fft_points = 512 | |
hist_x_num = 100 | |
lcd.init(freq=15000000) | |
# close WiFi | |
fm.register(8, fm.fpioa.GPIO0) | |
wifi_en=GPIO(GPIO.GPIO0,GPIO.OUT) | |
wifi_en.value(0) | |
fm.register(20,fm.fpioa.I2S0_IN_D0) | |
fm.register(19,fm.fpioa.I2S0_WS) | |
fm.register(18,fm.fpioa.I2S0_SCLK) | |
rx = I2S(I2S.DEVICE_0) | |
rx.channel_config(rx.CHANNEL_0, rx.RECEIVER, align_mode = I2S.STANDARD_MODE) | |
rx.set_sample_rate(sample_rate) | |
img = image.Image() | |
if hist_x_num > 320: | |
hist_x_num = 320 | |
hist_width = int(320 / hist_x_num)#changeable | |
x_shift = 0 | |
while True: | |
audio = rx.record(sample_points) | |
fft_res = FFT.run(audio.to_bytes(),fft_points) | |
fft_amp = FFT.amplitude(fft_res) | |
img = img.clear() | |
x_shift = 0 | |
for i in range(hist_x_num): | |
if fft_amp[i] > 240: | |
hist_height = 240 | |
else: | |
hist_height = fft_amp[i] | |
img = img.draw_rectangle((x_shift,240-hist_height,hist_width,hist_height),[255,255,255],2,True) | |
x_shift = x_shift + hist_width | |
lcd.display(img) | |
fft_amp.clear() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment