Data Receive Rate Delay in Sresnse

Asked 1 years ago, Updated 1 years ago, 261 views

Using Spresense, we collect FFT results of voice in the form of a csv file on the set microSD, and the data reception rate is increasing day by day.Is there any possible cause?

Also, the board is run by Arduino IDE, and the sketch I am using is exactly the same as Chapter 8's ft_datacollection.ino on page 159 of the book "Low Power Edge AI Starting with Spresense."
At first, it was taken at intervals of about 1 second, but now it is about 6 seconds apart.(I don't know how to change both hardware and sketches.)

#include<Audio.h>
# include <FFT.h>
# include <SDHCI.h>
SDClass SD;

#define FFT_LEN1024

// Initialize FFT with monaural, 1024 samples

FFTCclass<AS_CHANNEL_MONO, FFT_LEN> FFT;

AudioClass* theAudio=AudioClass::getInstance();


void avgFilter(float dst [FFT_LEN]) {
  static const int avg_filter_num = 8;
  static floatpAvg [avg_filter_num] [FFT_LEN/2];
  static intg_counter = 0;
  if(g_counter==avg_filter_num)g_counter=0;
  for(inti=0;i<FFT_LEN/2;++i){
    pAvg[g_counter][i]=dst[i];
    float sum = 0;
    for(int j=0;j<avg_filter_num;++j){
      sum+=pAvg[j][i];
    }
    dst[i] = sum/avg_filter_num;
  }
  ++g_counter;
}

void setup() {
  Serial.begin (115200);
  
  // Wait for SD card to be inserted
  while(!SD.begin()) {Serial.println("Insert SD card";};

  // Humming window, monaural, overlap 50%
  FFT.begin(WindowHamming, AS_CHANNEL_MONO, (FFT_LEN/2));

  Serial.println ("Init Audio Recorder");
  theAudio->begin();
  // Set Input to Microphone
  theAudio->setRecorderMode(AS_SETRECDR_STS_INPUTDEVICE_MIC);
  // Recording settings: PCM (16-bit RAW data) format,
  // Specify the location of the DSP codec (BIN directory on the SD card),
  // Sampling rate 48000Hz, monaural input
  interr=theAudio->initRecorder(AS_CODECTYPE_PCM) 
    , "/mnt/sd0/BIN", AS_SAMPLINGRATE_48000, AS_CHANNEL_MONO);                             
  if(err!=AUDIOLIB_ECODE_OK){
    Serial.println("Recorder initialize error");
    while(1);
  }

  Serial.println("Start Recorder");
  theAudio->startRecorder();// Start Recording
}


void loop() {
  static constuint32_t buffering_time= 
      FFT_LEN*1000/AS_SAMPLINGRATE_48000;
  static constuint32_t buffer_size=FFT_LEN*sizeof(int16_t);
  static const contact_index=AS_CHANNEL_MONO-1;
  static char buffer [buffer_size];
  static floatpDst [FFT_LEN];
  uint32_tread_size;

  // Store the requested data in buffer_size in buffer
  // The amount of data that can be read is set to read_size.
  intret=theAudio->readFrames(buff, buffer_size, & read_size);
  if(ret!=AUDIOLIB_ECODE_OK&& 
      ret!=AUDIOLIB_ECODE_INSUFFICIENT_BUFFER_AREA) {
    Serial.println("Error="+String(ret)));
    theAudio->stopRecorder();
    while(1);
  }

  if(read_size<buffer_size){
    delay(buffering_time);
    return;
  }
  
  FFT.put(q15_t*)buff, FFT_LEN); // Run FFT
  FFT.get(pDst,0);// Get FFT results
  avgFilter(pDst); // Smooth with past FFT results

  static uint32_t last_capture_time = 0;
  uint32_t capture_interval = millis()-last_capture_time;
  // record after one second
  if(capture_interval>1000){
    theAudio->stopRecorder();// Stop Recording
    // saveData function:recording data to SD card  
    //   Pointer to data (pDst)
    //   Size of recorded data (FFT_LEN/8)
    //   Number of Data Stored (100)
    saveData(pDst, FFT_LEN/8,100); 
    theAudio->startRecorder();//Recording Resume
    last_capture_time = millis();
  } 
}

// Record data on SD card
void saveData(float*pDst, int dsize, intquantity){
  static intgCounter = 0;// file name followed by
  char filename [16] = {};

  // Return without doing anything when more than the specified number of saves is reached
  if(gCounter>quantity){
    Serial.println("Data accumulated";
    return;
  }

  // open a data storage file
  sprintf(filename, "data%03d.csv", gCounter++);
  // delete a file if it already exists
  if(SD.exists(filename))SD.remove(filename);
  // Open File
  File myFile=SD.open(filename, FILE_WRITE);
  // Write Data
  for(inti=0;i<dsize;++i){
    myFile.println(String(pDst[i], 6));
  }
  myFile.close();//Close file
  Serial.println("Data saved as"+String(filename)));
}

sense

2022-12-12 09:20

1 Answers

The built-in implementation of the FAT file system slows access as the number of files increases.Isn't that why?In this case, you can improve by dividing the file into multiple folders.

Paste the stack overflow exchange in English.For your information.

https://stackoverflow.com/questions/52821728/fatfs-significant-slow-down-in-directories-with-many-files


2022-12-12 09:24

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.