From 8a4ba6b57ac93fa80e81b32ff7fe910d78d2cd05 Mon Sep 17 00:00:00 2001 From: Philip Rosedale Date: Mon, 4 Feb 2013 10:55:38 -0800 Subject: [PATCH 1/3] adding stdev stuff --- Source/Audio.cpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/Source/Audio.cpp b/Source/Audio.cpp index 48076d9d87..9c7c6b78a3 100644 --- a/Source/Audio.cpp +++ b/Source/Audio.cpp @@ -27,7 +27,7 @@ const float AMPLITUDE_RATIO_AT_90 = 0.5; const short RING_BUFFER_FRAMES = 5; const short RING_BUFFER_SIZE_SAMPLES = RING_BUFFER_FRAMES * BUFFER_LENGTH_SAMPLES; -const short JITTER_BUFFER_LENGTH_MSECS = 1; +const short JITTER_BUFFER_LENGTH_MSECS = 26; const int SAMPLE_RATE = 22050; const short NUM_AUDIO_SOURCES = 2; @@ -38,6 +38,12 @@ char EC2_WEST_AUDIO_SERVER[] = "54.241.92.53"; 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; #define LOG_SAMPLE_DELAY 1 @@ -121,7 +127,8 @@ int audioCallback (const void *inputBuffer, } if (ringBuffer->diffLastWriteNextOutput() < BUFFER_LENGTH_SAMPLES) { - std::cout << "Starved\n"; + starve_counter++; + printf("Starved #%d\n", starve_counter); ringBuffer->endOfLastWrite = NULL; } } @@ -214,9 +221,11 @@ void *receiveAudioViaUDP(void *args) { 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; } From 3d1e249db7ce9e052dae385be864f1ea18ddcb54 Mon Sep 17 00:00:00 2001 From: Philip Rosedale Date: Mon, 4 Feb 2013 12:49:12 -0800 Subject: [PATCH 2/3] 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 From 765cded31acb7df10db461e20db98149990a68f3 Mon Sep 17 00:00:00 2001 From: Philip Rosedale Date: Mon, 4 Feb 2013 13:38:54 -0800 Subject: [PATCH 3/3] Added test for standard deviation calcs at startup. --- Source/Audio.cpp | 6 +++--- Source/Util.cpp | 10 +++++++--- Source/main.cpp | 31 +++++++++++++++++++++++++++++-- 3 files changed, 39 insertions(+), 8 deletions(-) diff --git a/Source/Audio.cpp b/Source/Audio.cpp index 37c2e9b69a..29b05dda40 100644 --- a/Source/Audio.cpp +++ b/Source/Audio.cpp @@ -84,7 +84,7 @@ int audioCallback (const void *inputBuffer, // int16_t *inputRight = ((int16_t **) inputBuffer)[1]; if (inputLeft != NULL) { - data->audioSocket->send((char *) EC2_WEST_AUDIO_SERVER, 55443, (void *)inputLeft, BUFFER_LENGTH_BYTES); + data->audioSocket->send((char *) WORKCLUB_AUDIO_SERVER, 55443, (void *)inputLeft, BUFFER_LENGTH_BYTES); } int16_t *outputLeft = ((int16_t **) outputBuffer)[0]; @@ -236,12 +236,12 @@ void *receiveAudioViaUDP(void *args) { } } - // Compute standard deviation for jitter + // Compute and report standard deviation for jitter calculation if (firstSample) { stdev.reset(); } else { stdev.addValue(diffclock(previousReceiveTime, currentReceiveTime)); - if (stdev.getSamples() > 300) { + if (stdev.getSamples() > 500) { printf("Avg: %4.2f, Stdev: %4.2f\n", stdev.getAverage(), stdev.getStDev()); stdev.reset(); } diff --git a/Source/Util.cpp b/Source/Util.cpp index 6eedd863b8..2fb5ad14eb 100644 --- a/Source/Util.cpp +++ b/Source/Util.cpp @@ -17,7 +17,11 @@ #include "util.h" -const int MAX_STDEV_SAMPLES = 1000; +// +// Standard Deviation Object +// + +const int MAX_STDEV_SAMPLES = 1000; // Don't add more than this number of samples. StDev::StDev() { data = new float[MAX_STDEV_SAMPLES]; @@ -49,8 +53,8 @@ float StDev::getStDev() { for (int i = 0; i < sampleCount; i++) { stdev += powf(data[i] - average, 2); } - if (sampleCount > 0) - return sqrt(stdev/(float)sampleCount); + if (sampleCount > 1) + return sqrt(stdev/(float)(sampleCount - 1.0)); else return 0; } diff --git a/Source/main.cpp b/Source/main.cpp index 49e9bc6829..1dc89ffeee 100644 --- a/Source/main.cpp +++ b/Source/main.cpp @@ -903,8 +903,35 @@ int main(int argc, char** argv) int test_recv = network_receive(UDP_socket, &from_addr, incoming_packet, delay); printf("Received %i bytes\n", test_recv); - // Load textures - //Img.Load("/Users/philip/Downloads/galaxy1.tga"); + // + printf("Testing math... standard deviation.\n"); + StDev stdevtest; + stdevtest.reset(); + stdevtest.addValue(1345); + stdevtest.addValue(1301); + stdevtest.addValue(1368); + stdevtest.addValue(1322); + stdevtest.addValue(1310); + stdevtest.addValue(1370); + stdevtest.addValue(1318); + stdevtest.addValue(1350); + stdevtest.addValue(1303); + stdevtest.addValue(1299); + + if (stdevtest.getSamples() == 10) + printf("Samples=PASS "); + else + printf("Samples=FAIL "); + + if (floor(stdevtest.getAverage()*100.0) == 132859.0) + printf("Average=PASS "); + else + printf("Average=FAIL, avg reported = %5.3f ", floor(stdevtest.getAverage()*100.0)); + + if (floor(stdevtest.getStDev()*100.0) == 2746.0) + printf("Stdev=PASS \n"); + else + printf("Stdev=FAIL \n"); // // Try to connect the serial port I/O