In short, I am reading a.wav file in MATLAB for the purposes of sending it to the ESP32 for an FFT analysis. The.wav file in question contains a recording of a Corona effect. My file has 96223 samples when inputted into MATLAB.
For now, I am trying to just get back a checksum so I can know that the data is sent correctly.
I have already tried using the code I've written for smaller sample sizes. For example, I get back the correct checksum when I send 200 samples although the code takes longer than I want it to take which is not good. More than that though, and I never get anything back because of timeouts.
This is my MATLAB code:
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.9f\n', corona(i,1));
pause(0.1);
end
output = fscanf(esp, '%f\n') %read the checksum
fclose(instrfind);
And this is my Arduino code:
#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;
}
}
I expected to get back a checksum but I get a timeout when using very large sample sizes. I should also add that even though the ESP32 should be able to handle my file, I can't just push the whole file into the serial port because I get a buffer overflow error. Is there a solution to this?
First %5.9f
doesn't make sense to me.
Thats minimum 5 characters with 9 digit precision. That 5 doesn't make sense as you'll always have at least 11 characters with 9 digit precision
Then let me do some maths for you:
96000 samples, 12 characters each (including \n
) is a total of 10368000 bits.
At 9600 baud that's 1080 seconds of transfer time. -> 18 minutes.
As you add 0.1s pause after each sample you add another 9600 seconds to that.
Which leaves you with a total of 178 minutes (3 hours) of transfer time.
What do you expect?
For 200 samples its still 22,25s.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.