Created
September 24, 2017 09:25
-
-
Save nathanqthai/c125b34aff082469eea38bd9bef2ef2c to your computer and use it in GitHub Desktop.
idk im a dumpster fire atm
This file contains 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
void setup() { | |
Serial.begin(115200); // use the serial port | |
TIMSK0 = 0; // turn off timer0 for lower jitter - delay() and millis() killed | |
ADCSRA = 0xe5; // set the adc to free running mode | |
ADMUX = 0x40; // use adc0 | |
DIDR0 = 0x01; // turn off the digital input for adc0 | |
} | |
void loop() { | |
byte buf[256]; | |
while(1) { // reduces jitter | |
cli(); // UDRE interrupt slows this way down on arduino1.0 | |
for (int i = 0 ; i < 256 ; i += 2) { // save 256 samples | |
while(!(ADCSRA & 0x10)); // wait for adc to be ready | |
ADCSRA = 0xf5; // restart adc | |
byte m = ADCL; // fetch adc data | |
byte j = ADCH; | |
buf[i] = j; // put real data into even bins | |
buf[i+1] = m; // put real data into even bins | |
} | |
sei(); | |
Serial.write(0xff); | |
Serial.write(0xff); | |
for (int i = 0 ; i < 256 ; i++) { | |
Serial.write(buf[i]); // send out the data | |
} | |
} | |
} |
This file contains 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
load 38khz_adc.ino to arduino uno | |
pip3 install -r requirements.txt | |
python3 freq_finder.py | |
adjust fs in freq_finder.py as needed. |
This file contains 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
#!/usr/bin/env python3 | |
from numpy import mean, diff, zeros, sum | |
from matplotlib.mlab import find | |
import serial | |
def find_freq(sig, fs): | |
indices = find((sig[1:] >= 0) & (sig[:-1] < 0)) | |
crossings = [i - sig[i] / (sig[i+1] - sig[i]) for i in indices] | |
return fs / mean(diff(crossings)) | |
FRAME_SIZE = 128 | |
buf = zeros(FRAME_SIZE*10) | |
ser = serial.Serial('/dev/cu.usbmodem1421', 115200, timeout=5) | |
while True: | |
if ser.read(2) == b'\xff\xff': | |
frame = list(ser.read(256)) | |
h = [x * 256 for x in frame[::2]] | |
l = frame[1::2] | |
v = sum([h, l], axis=0) | |
v = [x - 512 for x in v] # arbitrary thresholding | |
buf[:-FRAME_SIZE] = buf[FRAME_SIZE:] | |
buf[-FRAME_SIZE:] = v | |
signal = buf | |
fs = 38000 | |
print('%f Hz' % find_freq(signal, fs)) |
This file contains 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
cycler==0.10.0 | |
matplotlib==2.0.2 | |
numpy==1.13.1 | |
pyparsing==2.2.0 | |
pyserial==3.4 | |
python-dateutil==2.6.1 | |
pytz==2017.2 | |
six==1.11.0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment