From daccb8378485bed273db268cc08160be17164338 Mon Sep 17 00:00:00 2001 From: Philip Rosedale Date: Mon, 4 Feb 2013 12:49:12 -0800 Subject: [PATCH] Added Stdev class to until --- Source/Audio.cpp | 33 ++++++++++++++++++++++----------- Source/Util.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++-- Source/Util.h | 13 +++++++++++++ 3 files changed, 78 insertions(+), 13 deletions(-) diff --git a/Source/Audio.cpp b/Source/Audio.cpp index 700ed438b4..37c2e9b69a 100644 --- a/Source/Audio.cpp +++ b/Source/Audio.cpp @@ -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(¤tReceiveTime, 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(¤tReceiveTime, 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; diff --git a/Source/Util.cpp b/Source/Util.cpp index 74cfb4b41d..6eedd863b8 100644 --- a/Source/Util.cpp +++ b/Source/Util.cpp @@ -14,6 +14,47 @@ #include #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 diff --git a/Source/Util.h b/Source/Util.h index 0074ff9994..594983972b 100644 --- a/Source/Util.h +++ b/Source/Util.h @@ -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