Skip to content

Instantly share code, notes, and snippets.

@edy555
Created September 3, 2016 03:18
Show Gist options
  • Save edy555/af54b4f3d5ca804541cb29f6a50708bb to your computer and use it in GitHub Desktop.
Save edy555/af54b4f3d5ca804541cb29f6a50708bb to your computer and use it in GitHub Desktop.
NanoVNAのテスト用コード。シリアル経由でデータを取ってきて、波形とスペクトラムをプロット。
#!/usr/bin/env python
import serial
import numpy as np
import pylab as pl
dev = '/dev/cu.usbmodem401'
ser = serial.Serial(dev)
def fetch_data():
ser.write("data 0\r")
ser.readline() # discard empty line
result = ''
line = ''
while True:
c = ser.read()
if c == chr(13):
next # ignore CR
line += c
if c == chr(10):
result += line
line = ''
next
if line.endswith('ch>'):
break
ser.close()
return result
def fetch_buffer():
data = fetch_data()
x = []
for line in data.split('\n'):
if line:
x.extend([int(d, 16) for d in line.strip().split(' ')])
return np.array(x, dtype=np.int16)
N = 256
fs = 48000
def plot_buffer():
buf = fetch_buffer()
samp = buf[1::2]
ref = buf[0::2]
pl.subplot(211)
pl.grid()
pl.plot(samp)
pl.plot(ref)
pl.subplot(212)
pl.grid()
pl.ylim((-50, 50))
pl.psd(samp, N, window = pl.blackman(N), Fs=fs)
pl.psd(ref, N, window = pl.blackman(N), Fs=fs)
plot_buffer()
pl.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment