Increase buffer sizes to enable higher sample rates with less jitter

This commit is contained in:
Matt
2025-12-13 00:26:46 +00:00
parent f77e4ae39a
commit d3dd05edc5
3 changed files with 16 additions and 15 deletions

View File

@@ -137,7 +137,7 @@
<form id="configForm" class="controls">
<div>
<label>Sample Rate (Hz)</label>
<input type="number" id="sampleRate" value="10000" min="1" max="100000">
<input type="number" id="sampleRate" value="10000" min="1" max="83333">
</div>
<div>
<label>Bit Width</label>

View File

@@ -156,7 +156,8 @@ canvas.addEventListener('click', (event) => {
});
// Data buffer
const maxPoints = 1000;
const maxPoints = 4000;
/** @type Array<number> */
let dataBuffer = new Array(maxPoints).fill(0);
// WebSocket
@@ -201,7 +202,7 @@ function connect() {
};
}
function processData(newData) {
function processData(/** @type Uint16Array */newData) {
if (isFrozen) {
return; // Skip updating the buffer when frozen
}
@@ -219,30 +220,31 @@ function processData(newData) {
pushToBuffer(newData);
} else {
// Accumulation mode (Peak Detect)
let pointsToPush = [];
for (let val of newData) {
let pointsToPush = new Uint16Array(newData.length * 2); // Worst case
let idx = 0;
for (const val of newData) {
if (val < lowRateState.accMin) lowRateState.accMin = val;
if (val > lowRateState.accMax) lowRateState.accMax = val;
lowRateState.count++;
if (lowRateState.count >= lowRateState.targetCount) {
// Push min and max to draw a vertical line
pointsToPush.push(lowRateState.accMin);
pointsToPush.push(lowRateState.accMax);
pointsToPush[idx++] = lowRateState.accMin;
pointsToPush[idx++] = lowRateState.accMax;
// Reset
resetLowRateState();
}
}
if (pointsToPush.length > 0) {
pushToBuffer(pointsToPush);
if (idx > 0) {
pushToBuffer(pointsToPush.slice(0, idx));
}
}
}
function pushToBuffer(newItems) {
function pushToBuffer(/** @type Uint16Array */ newItems) {
if (newItems.length >= maxPoints) {
dataBuffer = newItems.slice(-maxPoints);
dataBuffer = Array.from(newItems.slice(-maxPoints));
} else {
dataBuffer.splice(0, newItems.length);
dataBuffer.push(...newItems);

View File

@@ -59,8 +59,7 @@ static void start_webserver(void);
#define ADC_GET_CHANNEL(p_data) ((p_data)->type2.channel)
#define ADC_GET_DATA(p_data) ((p_data)->type2.data)
#define ADC_READ_LEN 512
#define ADC_MAX_STORE_BUF_SIZE 1024
#define ADC_READ_LEN 1024
static adc_continuous_handle_t adc_handle = NULL;
static TaskHandle_t s_task_handle;
@@ -71,7 +70,7 @@ static int s_ws_client_fd = -1;
// Global configuration state
static volatile bool s_reconfig_needed = false;
static uint32_t s_sample_rate = 20000;
static uint32_t s_sample_rate = 10000;
static adc_atten_t s_atten = ADC_ATTEN_DB_12;
static adc_bitwidth_t s_bit_width = ADC_BIT_WIDTH;
static uint16_t s_test_hz = 100;
@@ -282,7 +281,7 @@ void app_main(void) {
ESP_LOGE(TAG, "Failed to create ring buffer");
}
xTaskCreate(adc_read_task, "adc_read_task", 4 * 1024, NULL, 5, NULL);
xTaskCreate(adc_read_task, "adc_read_task", 4096 + ADC_READ_LEN, NULL, 5, NULL);
// Wait for WiFi connection
EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,