Floating-point code

main
Alex Wulff 2022-07-15 14:15:24 -04:00
rodzic e96c7b7417
commit 6289393253
4 zmienionych plików z 69 dodań i 10 usunięć

Wyświetl plik

@ -0,0 +1 @@
alex@Alexs-MBP.home.44315

Wyświetl plik

@ -14,15 +14,15 @@
// 0 = 500,000 Hz
// 960 = 50,000 Hz
// 9600 = 5,000 Hz
#define CLOCK_DIV 9600
#define CLOCK_DIV 12000
// Channel 0 is GPIO26
#define CAPTURE_CHANNEL 0
#define LED_PIN 25
#define NSAMP 20000
#define NSAMP 10000
uint16_t capture_buf[NSAMP];
uint16_t sending_buf[NSAMP];
float sending_buf[NSAMP];
int main() {
stdio_init_all();
@ -77,16 +77,26 @@ int main() {
gpio_put(LED_PIN, 1);
adc_run(true);
// first transmission will be garbage since we haven't filled the buffer yet
// first transmission will be garbage since we haven't filled buf
std::string encoded =
base64_encode((unsigned char const *)sending_buf, NSAMP*2);
base64_encode((unsigned char const *)sending_buf, NSAMP*4);
printf("%s", encoded.c_str());
gpio_put(LED_PIN, 0);
dma_channel_wait_for_finish_blocking(dma_chan);
memcpy(sending_buf, capture_buf, NSAMP*2);
uint16_t min = 32768;
uint16_t max = 0;
for (uint32_t i=0; i<NSAMP; i++) {
if (capture_buf[i] > max) max = capture_buf[i];
if (capture_buf[i] < min) min = capture_buf[i];
}
for (uint32_t i=0; i<NSAMP; i++) {
features[i] = ((float)capture_buf[i]-(float)min)/((float)max-(float)min)*2-1;
}
}
}

Wyświetl plik

@ -3,8 +3,8 @@ import numpy as np
import base64
if __name__=="__main__":
infile = "/Users/alex/Desktop/pico/other-3"
outfile = "/Users/alex/Desktop/pico/other.wav"
infile = "/Users/alex/Desktop/4khz/extinguish"
outfile = "/Users/alex/Desktop/4khz/extinguish.wav"
f = open(infile, "r")
@ -28,7 +28,7 @@ if __name__=="__main__":
audio = AudioSegment(
data.tobytes(),
sample_width=2,
frame_rate=5000,
frame_rate=4000,
channels=1
)

Wyświetl plik

@ -0,0 +1,48 @@
from pydub import AudioSegment
import scipy.io.wavfile
import numpy as np
import base64
import io
import matplotlib.pyplot as plt
if __name__=="__main__":
infile = "/Users/alex/Desktop/float-test/noise"
outfile = "/Users/alex/Desktop/float-test/noise.wav"
f = open(infile, "r")
byte_data = bytearray()
vals = f.read(1000)
# Reading all at once didn't work for some reason
while vals:
byte_data.extend(base64.b64decode(vals))
vals = f.read(1000)
if len(vals) != 1000: break
f.close()
# Enforce little endian
dt = np.dtype(np.float32)
dt = dt.newbyteorder('<')
data = np.zeros(int(len(byte_data)/4), dtype=dt)
num_idx = 0
byte_idx = 0
while byte_idx < len(byte_data)-4:
num = np.frombuffer(byte_data[byte_idx:byte_idx+4], dtype=dt)
if (num > 1 or num < -1):
print("Skipped one")
byte_idx = byte_idx + 3
else:
data[num_idx] = num[0]
num_idx = num_idx+1
byte_idx = byte_idx + 4
#plt.plot(data)
#plt.show()
wav_io = io.BytesIO()
scipy.io.wavfile.write(wav_io, 4000, data)
wav_io.seek(0)
sound = AudioSegment.from_wav(wav_io)
sound.export(outfile, format="wav")