Friday, December 13, 2013

HC-SR04: what is the distribution law for the frequencies of values? (Part 1)




In this small note we are going to study the distribution of the values returned by our preferred ultraso-nic sensor, namely, the HC-SR04. We suspect that it is a normal distribution. As suggested in [1, pag. 18 and ff], we use the following steps to verify this hypothesis:

1) generate sample data
2) compute the mean and standard deviation of sampled data
3) compute the kurtosis and skewness
4) compute standard error of kurtosis and skewness
5) compute the z-score for kurtosis and skewness
6) compare the above values with the corresponding ones of the normal distribution



Generate the sample data

The first operation is to obtain a large enough sample data. The idea to fix the obstacle at 1 meter from the sensor and then take ten thousand measurements (this is hopefully large enough). Since one cannot open files directly on a PC using Arduino, we are going to user the card reader (see THIS post) and store data on the SD card. The following short program will do the job.




/********************************************************************
GENERATING SAMPLE DATA:

Generate sample data from the HC-SR04 (ultrasonic sensor) and 
store it in a file of the SD card

Author: Enrico Formenti
Date: 10/10/2013
Revision: none

Most of the code has been taken from Arduino example sketches.

LICENCE:
MIT Licence


For the SD card reader:

+5, 3.3 and GND are clear
CS is also clear  (has to be connected to digital pin 10)
MOSI means DO (has to be connected to digital pin 11)
MISO means DI (has to be connected to digital pin 12)
SCK means CLK (has to be connected to digital pin 13)
*****************************************************************************/

#include <Serial.h>
#include <SD.h>

const uint8_t trigPin = 4; // trigger pin of HC-SR04
const uint8_t echoPin = 2; // echo pin of HC-SR04
const uint8_t CSPin = 10;

#define SAMPLESIZE 10000
#define BUFSIZE 100
const char filename[]="Sensor.dat";
File dataFile;
uint16_t buffer[BUFSIZE];

uint16_t readoutSensor() {
  // The sensor is triggered by a HIGH pulse of 10us or more.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(trigPin, OUTPUT);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(3);

  // Start trigger signal

  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
 
  // Read the signal from the sensor: a HIGH pulse whose
  // duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.

  pinMode(echoPin, INPUT);
 
  return pulseIn(echoPin, HIGH);
}

void printBuffer(File f, uint16_t len) {
  uint16_t i;
  for(i=0; i<len; i++)
    f.println(buffer[i]);
}

void setup() {
  
  uint16_t i;
  
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  // wait for serial port to connect. Needed for Leonardo only
  while (!Serial); 

Serial.print("Initializing SD card...");
  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  // Note that even if it's not used as the CS pin, the hardware SS pin
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output
  // or the SD library functions will not work.
   pinMode(CSPin, OUTPUT);
  
  if (!SD.begin(CSPin)) {
    Serial.println("Error: SD card initialization failed!");
    return;
  }
  Serial.println("done.");
 
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  Serial.print("Creating data file... ");
  dataFile = SD.open(filename, FILE_WRITE);
 
  // if the file opened okay, write to it:
  if (dataFile)
    Serial.println("done.");
  else { // if the file didn't open, print an error:
    Serial.println("Error: unable to create data file");
    return;
  }
  
  for(i=1; i<SAMPLESIZE;i++) {
    buffer[(i-1) % BUFSIZE]=(readoutSensor());
    if(!(i % BUFSIZE)) {
      printBuffer(dataFile, BUFSIZE);
      Serial.print(i);
      Serial.println(" done.");
    }
  }
  dataFile.close();   // close the file on the SD card

  Serial.println("Sample generation terminated.");
}

void loop() {
     // nothing happens after setup
}

/****************************************************************************/

The sample
After generating the sample using the above sketch, I run a C program for computing the frequencies of the sampled values and for sorting them. Here is a plot of what has been found:


We recall here the formulas that can be useful in our case [1]:

Compute the mean and the standard deviation



Compute the kurtosis K and skewness S

Compute standard error of kurtosis (SEK) and of skewness (SES)



Compute z-score for kurtosis and skewness


After a few calculations we found:


Num of record processed: 83
Total number of elements: 2017
Mean= 982.4402
St. Dev.= 253.7630
Kurtosis= 9.0216
Skewness= -2.8651
St. err. kurtosis= 0.1089
St. err. skewness= 0.0545
Z-score kurtosis= 82.81
Z-score skewness= -52.57

Conclusions
Therefore the frequencies of our sample are not normally distributed. We can remark three facts:

1- There are lots of zeroes (106, about 5%)
2- The sample seems to be the sum of two distinct distributions one with mean a bit less than 1000 and the other with mean a bit more than 1200.
3- Each of distribution seems to be formed by « spikes »; spikes seem to be regularly spaced.

Let us discuss each of these remarks. Concerning the first one recall that the sensor returns a zero essentially when there is a measurement error. Therefore, they can probably be ignored at this stage. 

The third remark seems to indicated that probably there are round-off errors since the sensor probably did not have enough time to re-establish an « ideal »  situation before the next measure signal starts. Indeed, if we look at our sample generation program, no care is taken to espace two measures to give enough time to the sensor to restart and for all possible echo phenomenon to stop. Recall the beat tones phenomenon.

Indeed, this additional echoes phenomenon is the explanation of the second distribution which appears to be similar to the first one but greatly scaled down wrt frequencies.

We therefore need to repeat the experiment taking all these remarks into account. I will do it this week. So, as usual, stay tuned!

See also:

No comments :

Post a Comment