如何通过 MATLAB 将大数组(96000 个样本)发送到 ESP32 串口?



简而言之,我正在 MATLAB 中读取一个.wav文件,以便将其发送到 ESP32 进行 FFT 分析。有问题的.wav文件包含电晕效应的记录。我的文件在输入到 MATLAB 时有 96223 个样本。

现在,我正在尝试取回校验和,以便我知道数据已正确发送。

我已经尝试使用我为较小的样本量编写的代码。例如,当我发送 200 个样本时,我会得到正确的校验和,尽管代码花费的时间比我希望的要长,这并不好。不仅如此,由于超时,我再也得不到任何回报。

这是我的 MATLAB 代码:

esp = serial('COM3');
set(esp, 'DataBits' , 8);
set(esp, 'StopBits', 1);
set(esp, 'BaudRate', 9600);
set(esp, 'Parity', 'none');
set(esp, 'terminator', 'LF');
%filename = 'test100.wav';
%corona = audioread(filename);
load('corona')
fopen(esp);
pause(0.1)
for i = 1:200
fprintf(esp, '%5.9fn', corona(i,1)); 
pause(0.1);       
end
output = fscanf(esp, '%fn') %read the checksum
fclose(instrfind);

这是我的Arduino代码:

#include <Arduino.h>
float sentData[200]; //initialize data array
int i = 0;
const int ledPin = 26;
float checksum = 0;
int CNT = 0;
void printFloat(float value, int places);
void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
while (Serial.available() < 200)
{
digitalWrite(ledPin, HIGH); //keep the LED on while the data is being sent
}
while (Serial.available() != 0)
{
sentData[i] = Serial.parseFloat(); //parse the data to the array
i++;
}
Serial.flush();
delay(500);
digitalWrite(ledPin, LOW); //turn off the LED when data is fully parsed
for (size_t x = 0; x < 200; ++x)
{
checksum += sentData[x]; //calculate the sum of all elements in the sentData array
}
printFloat(checksum, 10); //send the checksum to the serial port for reading
}
void loop()
{
}
void printFloat(float value, int places)
{
// this is used to cast digits
int digit;
float tens = 0.1;
int tenscount = 0;
int i;
float tempfloat = value;
// if this rounding step isn't here, the value  54.321 prints as 54.3209
// calculate rounding term d:   0.5/pow(10,places)
float d = 0.5;
if (value < 0)
d *= -1.0;
// divide by ten for each decimal place
for (i = 0; i < places; i++)
d /= 10.0;
tempfloat += d;
// first get value tens to be the large power of ten less than value
if (value < 0)
tempfloat *= -1.0;
while ((tens * 10.0) <= tempfloat)
{
tens *= 10.0;
tenscount += 1;
}
// write out the negative if needed
if (value < 0)
Serial.print('-');
if (tenscount == 0)
Serial.print(0, DEC);
for (i = 0; i < tenscount; i++)
{
digit = (int)(tempfloat / tens);
Serial.print(digit, DEC);
tempfloat = tempfloat - ((float)digit * tens);
tens /= 10.0;
}
// if no places after decimal, stop now and return
if (places <= 0)
return;
// otherwise, write the point and continue on
Serial.print('.');
// now write out each decimal place by shifting digits one by one into the ones place and writing the truncated value
for (i = 0; i < places; i++)
{
tempfloat *= 10.0;
digit = (int)tempfloat;
Serial.print(digit, DEC);
// once written, subtract off that digit
tempfloat = tempfloat - (float)digit;
}
}

我希望得到一个校验和,但是当使用非常大的样本量时,我得到了超时。我还应该补充一点,即使 ESP32 应该能够处理我的文件,我也不能只是将整个文件推送到串行端口,因为我遇到了缓冲区溢出错误。有解决方案吗?

第一个%5.9f对我来说没有意义。

最少 5 个字符,精度为 9 位。这 5 没有意义,因为您将始终至少有 11 个字符和 9 位精度

那我来帮你做一些数学运算吧:

96000个样本,每个12个字符(含n(共10368000位。

在 9600 波特时,即 1080 秒的传输时间。 -> 18 分钟。

当您在每个样本后添加 0.1 秒暂停时,您又增加了 9600 秒。

这给您留下了总共 178 分钟(3 小时(的转移时间。

你期待什么?

对于 200 个样本,它仍然是 22,25 秒。

最新更新