Print RMS of 15 samples

Fri, 15 May 2009 22:12:22 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Fri, 15 May 2009 22:12:22 +0100
changeset 1
cfb60868c745
parent 0
2f2165f8c8ea
child 2
05b2fd8fe5aa

Print RMS of 15 samples

main.c file | annotate | diff | comparison | revisions
--- a/main.c	Fri May 15 21:03:40 2009 +0100
+++ b/main.c	Fri May 15 22:12:22 2009 +0100
@@ -1,4 +1,5 @@
 #include <string.h>
+#include <math.h>
 #include <stdlib.h>
 #include <time.h>
 
@@ -7,10 +8,12 @@
 
 xmpp_conn_t *conn1, *conn2;
 
-unsigned long mincount, maxcount, count;
+unsigned long mincount, maxcount;
+unsigned long count = 0;
 
-long samples; /* Current number of samples taken */
-long max_samples; /* The number of samples to do, if specified by user */
+unsigned long sum_squares = 0;
+unsigned long samples = 0; /* Current number of samples taken */
+unsigned long max_samples = 15; /* The number of samples to do, if specified by user */
 
 time_t lasttime; /* Time of the last sample we took */
 
@@ -33,11 +36,22 @@
 		count++;
 		if(time(NULL) != lasttime)
 		{
-			printf("%d stanzas/sec\n", count);
-			if(count > maxcount)
+
+			if(lasttime > 0)
 			{
-				printf("%d beats old maximum of %d\n", count, maxcount);
-				maxcount = count;
+				//printf("%d stanzas/sec\n", count);
+				if(count > maxcount && lasttime > 0)
+				{
+					//printf("%d beats old maximum of %d\n", count, maxcount);
+					maxcount = count;
+				}
+				sum_squares += count*count;
+				samples++;
+				if(samples >= max_samples)
+				{
+					printf("%Lf\n", sqrtl(sum_squares/samples));
+					exit(0);
+				}
 			}
 			count = 0;
 			lasttime = time(NULL);
@@ -57,7 +71,7 @@
 
     if (status == XMPP_CONN_CONNECT) {
 	xmpp_stanza_t* pres;
-	fprintf(stderr, "DEBUG: connected\n");
+	//fprintf(stderr, "DEBUG: connected\n");
 	xmpp_handler_add(conn,message_handler, NULL, "message", NULL, ctx);
 	
 	/* Send initial <presence/> so that we appear online to contacts */
@@ -69,13 +83,13 @@
 	if(conn == conn2) 
 	{
 		count = 0;
-		lasttime = time(NULL);
+		lasttime = 0;
 		
 		xmpp_send(conn1, bstanza);
 	}
     }
     else {
-	fprintf(stderr, "DEBUG: disconnected\n");
+	//fprintf(stderr, "DEBUG: disconnected\n");
 	xmpp_stop(ctx);
     }
 }
@@ -86,7 +100,7 @@
     xmpp_log_t *log = NULL;
     xmpp_stanza_t *body, *text;
 
-    printf("Loading...\n");
+    //printf("Loading...\n");
     /* init library */
     xmpp_initialize();
 
@@ -128,7 +142,7 @@
     xmpp_connect_client(conn1, "localhost", 5222, conn_handler, ctx);
     xmpp_connect_client(conn2, "localhost", 5222, conn_handler, ctx);
 
-    printf("Connecting...\n");
+    //printf("Connecting...\n");
     /* enter the event loop - 
        our connect handler will trigger an exit */
     xmpp_run(ctx);

mercurial