k210实现麦克风阵列声源定位

import sensor

import image

import lcd

import time

import KPU as kpu

from fpioa_manager import *

from Maix import GPIO

# 设置麦克风阵列

fm.register(board_info.MIC_ARRAY_DATA, fm.fpioa.I2S0_IN_D0, force=True)

fm.register(board_info.MIC_ARRAY_WS, fm.fpioa.I2S0_WS, force=True)

fm.register(board_info.MIC_ARRAY_BCK, fm.fpioa.I2S0_SCLK, force=True)

# 初始化LCD

lcd.init()

lcd.rotation(2)

# 初始化麦克风阵列

mic_dev = I2S(I2S.DEVICE_0)

# 设置麦克风阵列参数

sample_rate = 32000

record_time = 5000

samples_per_record = sample_rate * record_time // 1000

mic_dev.sample_rate(sample_rate)

# 初始化KPU

lcd.draw_string(100, 100, "Initializing KPU...")

sensor.reset()

sensor.set_pixformat(sensor.RGB565)

sensor.set_framesize(sensor.QVGA)

sensor.set_vflip(1)

sensor.run(1)

sensor.skip_frames(30)

lcd.draw_string(100, 100, "Loading Model...")

task = kpu.load(0x300000)

# 循环读取麦克风数据并进行声源定位

while True:

    try:

        lcd.draw_string(100, 150, "Recording...")

        mic_dev.rec(samples_per_record)

        lcd.draw_string(100, 150, "Processing...")

        pcm_data = mic_dev.record_read(samples_per_record)

        pcm_data = bytes([int(i / 256) for i in pcm_data])

        a = kpu.forward(task, pcm_data)

        output = kpu.get_output(task, 0)

        max_index = output.index(max(output))

        lcd.draw_string(100, 200, "Sound from direction: %d" % max_index)

    except Exception as e:

        print(e)

        lcd.draw_string(100, 200, "Error occurred!")

        continue

# 释放资源

mic_dev.deinit()

kpu.deinit(task)

你可能感兴趣的:(单片机)