Skip to content

Instantly share code, notes, and snippets.

@kamiyaowl
Last active January 25, 2020 16:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kamiyaowl/6e6d289c66925441fb9a26c39fa5efa8 to your computer and use it in GitHub Desktop.
Save kamiyaowl/6e6d289c66925441fb9a26c39fa5efa8 to your computer and use it in GitHub Desktop.
PYNQ-Z2で音声入力をグラフに書く https://twitter.com/kamiya_owl/status/1221108838402674688
# %matplotlib inline
import matplotlib.pyplot as plt
from ipywidgets import *
plt.rcParams['figure.figsize'] = (15.0, 10.0)
from pynq.overlays.base import BaseOverlay
base = BaseOverlay("base.bit")
# 2の補数の扱い辛い
def twos_complement(hexstr,bits):
value = int(hexstr, 16)
if value & (1 << (bits-1)):
value -= 1 << bits
return value
# from audio_adau1761.h
I2S_DATA_RX_L_REG = 0x00
I2S_DATA_RX_R_REG = 0x04
I2S_DATA_TX_L_REG = 0x08
I2S_DATA_TX_R_REG = 0x0C
# キャプチャ数
capture_len = 256
pAudio = base.audio
pAudio.select_line_in() # line入力を使う
l_arr = []
r_arr = []
for i in range(capture_len):
# 24bit 2の補数表現なので適当に変換する
l_data = "{:06x}".format(pAudio.mmio.read_reg(offset=I2S_DATA_RX_L_REG))
r_data = "{:06x}".format(pAudio.mmio.read_reg(offset=I2S_DATA_RX_R_REG))
l_arr.append(twos_complement(l_data, 24))
r_arr.append(twos_complement(r_data, 24))
plt.title('ADAU1761 Audio RX Data')
plt.plot(l_arr, label='Lch')
plt.plot(r_arr, label='Rch')
plt.legend()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment