Added Stdev class to until

This commit is contained in:
Philip Rosedale 2013-02-04 12:49:12 -08:00
parent a60d100b57
commit 3d1e249db7
3 changed files with 78 additions and 13 deletions

View file

@ -41,10 +41,7 @@ const int AUDIO_UDP_LISTEN_PORT = 55444;
int starve_counter = 0;
// Stuff used to compute the standard deviation of the sample
int stdev_counter = 0;
float stdev_variance[1000]; // Difference between when last two packets received
float stdev_value;
StDev stdev;
#define LOG_SAMPLE_DELAY 1
@ -226,14 +223,28 @@ void *receiveAudioViaUDP(void *args) {
while (true) {
if (sharedAudioData->audioSocket->receive((void *)receivedData, receivedBytes)) {
gettimeofday(&currentReceiveTime, NULL);
if (LOG_SAMPLE_DELAY) {
// write time difference (in microseconds) between packet receipts to file
double timeDiff = diffclock(previousReceiveTime, currentReceiveTime);
logFile << timeDiff << std::endl;
bool firstSample = (currentReceiveTime.tv_sec == 0);
gettimeofday(&currentReceiveTime, NULL);
if (LOG_SAMPLE_DELAY) {
if (!firstSample) {
// write time difference (in microseconds) between packet receipts to file
double timeDiff = diffclock(previousReceiveTime, currentReceiveTime);
logFile << timeDiff << std::endl;
}
}
// Compute standard deviation for jitter
if (firstSample) {
stdev.reset();
} else {
stdev.addValue(diffclock(previousReceiveTime, currentReceiveTime));
if (stdev.getSamples() > 300) {
printf("Avg: %4.2f, Stdev: %4.2f\n", stdev.getAverage(), stdev.getStDev());
stdev.reset();
}
}
AudioRingBuffer *ringBuffer = sharedAudioData->ringBuffer;

View file

@ -14,6 +14,47 @@
#include <iostream>
#include "world.h"
#include "glm.hpp"
#include "util.h"
const int MAX_STDEV_SAMPLES = 1000;
StDev::StDev() {
data = new float[MAX_STDEV_SAMPLES];
sampleCount = 0;
}
void StDev::reset() {
sampleCount = 0;
}
void StDev::addValue(float v) {
data[sampleCount++] = v;
if (sampleCount == MAX_STDEV_SAMPLES) sampleCount = 0;
}
float StDev::getAverage() {
float average = 0;
for (int i = 0; i < sampleCount; i++) {
average += data[i];
}
if (sampleCount > 0)
return average/(float)sampleCount;
else return 0;
}
float StDev::getStDev() {
float average = getAverage();
float stdev = 0;
for (int i = 0; i < sampleCount; i++) {
stdev += powf(data[i] - average, 2);
}
if (sampleCount > 0)
return sqrt(stdev/(float)sampleCount);
else
return 0;
}
float randFloat () {
return (rand()%10000)/10000.f;
@ -101,7 +142,7 @@ double diffclock(timeval clock1,timeval clock2)
}
void drawtext(int x, int y, float scale, float rotate, float thick, int mono, char *string,
float r=1.0, float g=1.0, float b=1.0)
float r, float g, float b)
{
//
// Draws text on screen as stroked so it can be resized
@ -126,7 +167,7 @@ void drawtext(int x, int y, float scale, float rotate, float thick, int mono, ch
void drawvec3(int x, int y, float scale, float rotate, float thick, int mono, glm::vec3 vec,
float r=1.0, float g=1.0, float b=1.0)
float r, float g, float b)
{
//
// Draws text on screen as stroked so it can be resized

View file

@ -24,4 +24,17 @@ void drawvec3(int x, int y, float scale, float rotate, float thick, int mono, gl
float r=1.0, float g=1.0, float b=1.0);
double diffclock(timeval clock1,timeval clock2);
class StDev {
public:
StDev();
void reset();
void addValue(float v);
float getAverage();
float getStDev();
int getSamples() {return sampleCount;};
private:
float * data;
int sampleCount = 0;
};
#endif