Storing, and averaging microphone readings

Hello all,

I have an electret microphone connected to my arduino and the readings are printing out to the serial monitor every 50ms.

I need to create an average of the readings every hour, then pop that average number in a mySQL database.

But a reading every 50ms = 72,000 readings per hour

How could i go about doing this?

How could i go about doing this?

Add each reading to a running total as you take it then calculate the average at the end of the hour. Make sure that you choose the right type of variable for the running total.

What "readings"? The average of an audio signal is always zero. The instantaneous value (one sample taken periodically) has little relationship to the audio waveform unless the Nyquist criterion is met (2*f samples/t).

These are max level readings.

This is a college project and i am new to it.

Max level readings are what is being serial printed (e.g 2.7, 2.1, 2.4, 2.4, 2.3, 2.7 (volts))

After one hour i need the calculated average level (e.g 2.5), like an SPL meter

what do you mean "Add each reading to a running total" and how can i go about this

what do you mean "Add each reading to a running total" and how can i go about this

At the start of the hour set the total to zero. Then as each reading is taken add it to the total. At the end of the hour divide the total by the number of readings. There is no need to store each reading.

mrears92:
These are max level readings.

This is a college project and i am new to it.

Max level readings are what is being serial printed (e.g 2.7, 2.1, 2.4, 2.4, 2.3, 2.7 (volts))

After one hour i need the calculated average level (e.g 2.5), like an SPL meter

what do you mean "Add each reading to a running total" and how can i go about this

Does your microphone module have a built in peak detector, or does it output audio? If it's audio, how do you know that the peak occurs at the time when you happen to sample it? Also, how do you handle the DC offset (A/D is 0-5V typically, while audio is AC, and has +/- swings)?

UKHeliBob:
At the start of the hour set the total to zero. Then as each reading is taken add it to the total. At the end of the hour divide the total by the number of readings.

Do i need to do this manually? Because I need the code to do all of that for me.

The idea is someone could press a button when they get in the studio and monitor their hearing exposure. (This is a prototype for a college project remember)

When the 'average number' is stored in the SQL database it will then be displayed on a localhost website via PHP

I need the code to do all of that for me.

If you want someone to write the code for you then ask a moderator to move this thread to Gigs and Collaborations or have a go yourself and get help here.

Do my questions confuse you? Do you perhaps doubt that the answers will affect your project? :slight_smile:

Reading a microphone != hearing exposure.

aarg:
Does your microphone module have a built in peak detector, or does it output audio? If it's audio, how do you know that the peak occurs at the time when you happen to sample it? Also, how do you handle the DC offset (A/D is 0-5V typically, while audio is AC, and has +/- swings)?

Ive just lifted the code from the adafruit website.

Aarg, It doesn't really matter about when it samples as its only a rubbish prototype. Your questions are slighlty confusing to me e.g. "Also, how do you handle the DC offset (A/D is 0-5V typically, while audio is AC, and has +/- swings)?". Im using an arduino that has a 0 - 5 v AD converter... haha

Also i dont want the code to be written for me, i just want to be pointed in the right direction of research

/****************************************
Example Sound Level Sketch for the 
Adafruit Microphone Amplifier
****************************************/

const int sampleWindow = 50; // Sample window width in mS (50 mS = 20Hz)
unsigned int sample;

void setup() 
{
   Serial.begin(9600);
}


void loop() 
{
   unsigned long startMillis= millis();  // Start of sample window
   unsigned int peakToPeak = 0;   // peak-to-peak level

   unsigned int signalMax = 0;
   unsigned int signalMin = 1024;

   // collect data for 50 mS
   while (millis() - startMillis < sampleWindow)
   {
      sample = analogRead(0);
      if (sample < 1024)  // toss out spurious readings
      {
         if (sample > signalMax)
         {
            signalMax = sample;  // save just the max levels
         }
         else if (sample < signalMin)
         {
            signalMin = sample;  // save just the min levels
         }
      }
   }
   peakToPeak = signalMax - signalMin;  // max - min = peak-peak amplitude
   double volts = (peakToPeak * 3.3) / 1024;  // convert to volts

   Serial.println(volts);
}

Ok, so how do the Adafruit module and your circuit compare?

Also please read this thread about sampling.

The exact same setup.

I need to know is how to calculate the overall average of 72,000 peak levels (from an hour of running) within software

Add up 72,000 readings and divide by 72,000. What is the prize?

aarg:
Add up 72,000 readings and divide by 72,000. What is the prize?

Surely thats too much for the arduino to do?

mrears92:
Surely thats too much for the arduino to do?

No it isn't. But if you are feeding values to SQL, I guess you have a PC so you could do it there instead if you like.

Any recommendations on doing it in the PC? What software?

new average := (old average * 71,999 + new value) / 72,000
old average := new average

Re-posted in Gigs and Collaborations.

I'm going to lock on of these threads. Which one should it be? If you want to pay someone to do it, I'll lock this one.

Hi,

I was wondering if there would be any problems sending data to a mySQL database every 50ms?

This would be 72,000 sensor readings per hour