<div dir="ltr">I'm not sure it this was overlooked or if I should format it any other way...<div><br></div><div>Regards,</div><div>Nuno<br><br><div class="gmail_quote">---------- Forwarded message ----------<br>From: <b class="gmail_sendername">Nuno Gonçalves</b> <span dir="ltr"><<a href="mailto:nunojpg@gmail.com">nunojpg@gmail.com</a>></span><br>

Date: Sun, Apr 21, 2013 at 6:59 PM<br>Subject: rtl_test patch<br>To: <a href="mailto:osmocom-sdr@lists.osmocom.org">osmocom-sdr@lists.osmocom.org</a><br><br><br>Corrects bugs where:<br>
Cumulative PPM error is always "342" if execution stopped before first<br>
PPM_DURATION.<br>
PPM error alias to wrong values if over +-1045 (this happens quite a<br>
lot actually due to bug reported at [1]).<br>
<br>
Behavior changes:<br>
Cumulative PPM error value is now printed every PPM_DURATION so the<br>
user can check when it is settled.<br>
<br>
[1] <a href="http://lists.osmocom.org/pipermail/osmocom-sdr/2013-April/000584.html" target="_blank">http://lists.osmocom.org/pipermail/osmocom-sdr/2013-April/000584.html</a><br>
<br>
Thanks,<br>
Nuno<br>
<br>
Signed-off-by: Nuno Goncalves <<a href="mailto:nunojpg@gmail.com">nunojpg@gmail.com</a>><br>
<br>
diff --git a/src/rtl_test.c b/src/rtl_test.c<br>
index f5a56b8..c705e04 100644<br>
--- a/src/rtl_test.c<br>
+++ b/src/rtl_test.c<br>
@@ -55,6 +55,8 @@ static int ppm_benchmark = 0;<br>
 static int64_t ppm_count = 0L;<br>
 static int64_t ppm_total = 0L;<br>
<br>
+static uint32_t samp_rate = DEFAULT_SAMPLE_RATE;<br>
+<br>
 #ifndef _WIN32<br>
 static struct timespec ppm_start;<br>
 static struct timespec ppm_recent;<br>
@@ -108,6 +110,7 @@ static void rtlsdr_callback(unsigned char *buf,<br>
uint32_t len, void *ctx)<br>
 {<br>
  uint32_t i, lost = 0;<br>
  int64_t ns;<br>
+ int real_rate;<br>
<br>
  if (uninit) {<br>
  bcnt = buf[0];<br>
@@ -141,17 +144,23 @@ static void rtlsdr_callback(unsigned char *buf,<br>
uint32_t len, void *ctx)<br>
  if (ppm_now.tv_sec - ppm_recent.tv_sec > PPM_DURATION) {<br>
  ns = 1000000000L * (int64_t)(ppm_now.tv_sec - ppm_recent.tv_sec);<br>
  ns += (int64_t)(ppm_now.tv_nsec - ppm_recent.tv_nsec);<br>
- printf("real sample rate: %i\n",<br>
+ printf("real sample rate: %i",<br>
  (int)((1000000000L * ppm_count / 2L) / ns));<br>
- #ifndef __APPLE__<br>
- clock_gettime(CLOCK_REALTIME, &ppm_recent);<br>
- #else<br>
- gettimeofday(&tv, NULL);<br>
- ppm_recent.tv_sec = tv.tv_sec;<br>
- ppm_recent.tv_nsec = tv.tv_usec*1000;<br>
- #endif<br>
+<br>
  ppm_total += ppm_count / 2L;<br>
  ppm_count = 0L;<br>
+<br>
+ #ifndef _WIN32<br>
+ ns = 1000000000L * (int64_t)(ppm_now.tv_sec - ppm_start.tv_sec);<br>
+ ns += (int64_t)(ppm_now.tv_nsec - ppm_start.tv_nsec);<br>
+ real_rate = (int)(ppm_total * 1000000000L / ns);<br>
+ printf(" cumulative PPM error: %.1f\n",<br>
+ (1000000.0 * (real_rate - (int)samp_rate)) / samp_rate);<br>
+ #else<br>
+ printf("\n");<br>
+ #endif<br>
+<br>
+ ppm_recent = ppm_now;<br>
  }<br>
 #endif<br>
 }<br>
@@ -213,13 +222,10 @@ int main(int argc, char **argv)<br>
  int sync_mode = 0;<br>
  uint8_t *buffer;<br>
  uint32_t dev_index = 0;<br>
- uint32_t samp_rate = DEFAULT_SAMPLE_RATE;<br>
  uint32_t out_block_size = DEFAULT_BUF_LENGTH;<br>
  int device_count;<br>
  int count;<br>
  int gains[100];<br>
- int real_rate;<br>
- int64_t ns;<br>
<br>
  while ((opt = getopt(argc, argv, "d:s:b:tpS::")) != -1) {<br>
  switch (opt) {<br>
@@ -365,15 +371,6 @@ int main(int argc, char **argv)<br>
<br>
  if (do_exit) {<br>
  fprintf(stderr, "\nUser cancel, exiting...\n");<br>
- if (ppm_benchmark) {<br>
-#ifndef _WIN32<br>
- ns = 1000000000L * (int64_t)(ppm_recent.tv_sec - ppm_start.tv_sec);<br>
- ns += (int64_t)(ppm_recent.tv_nsec - ppm_start.tv_nsec);<br>
- real_rate = (int)(ppm_total * 1000000000L / ns);<br>
- printf("Cumulative PPM error: %i\n",<br>
- (int)round((double)(1000000 * (real_rate - (int)samp_rate)) /<br>
(double)samp_rate));<br>
-#endif<br>
- }<br>
  }<br>
  else<br>
  fprintf(stderr, "\nLibrary error %d, exiting...\n", r);<br>
</div><br></div></div>