Radar data acquisition, analysis and visualization using reproducible research with Sweave Vladimir Skvortsov1,2 Keun Myoung Lee1

1CEWIT Korea

2Department of Science, SUNY Korea

The R User Conference 2013, July 10-12 2013 University of Castilla-La Mancha, Albacete, Spain

v.02f7 of 2013-07-11 . Outline

1. Outline

2. Introduction

3. Sweave Introduction Features

4. Radar project Problem Statement Concept Benefits and Goals Hardware

5. Data Acquisition Principles Measurement flow Measurement timings Measurement Setup using ++ Qt software Data acquisition module

6. Data Analysis and Visualization Workflow Data Analysis Examples

useR!2013: Radar DAQ, A&V using Sweave Outline 2 / 63 Outline

1. Outline

2. Introduction

3. Sweave Introduction Features

4. Radar project Problem Statement Concept Benefits and Goals Hardware

5. Data Acquisition Principles Measurement flow Measurement timings Measurement Setup using C++ Qt software Data acquisition module

6. Data Analysis and Visualization Workflow Data Analysis Examples

useR!2013: Radar DAQ, A&V using Sweave Introduction 3 / 63 Introduction

. Reproducible Research (RR) or reproducible data analysis is an approach aiming at complementing classical printed scientific articles with everything required to independently . reproduce the results they present.

Everything includes data, codes and description of how the code is applied to the data. The idea of RR is popular. The tools for RR implementation are important.

R + LATEX= Sweave is the most well-known tool.

useR!2013: Radar DAQ, A&V using Sweave Introduction 4 / 63 Outline

1. Outline

2. Introduction

3. Sweave Introduction Features

4. Radar project Problem Statement Concept Benefits and Goals Hardware

5. Data Acquisition Principles Measurement flow Measurement timings Measurement Setup using C++ Qt software Data acquisition module

6. Data Analysis and Visualization Workflow Data Analysis Examples

useR!2013: Radar DAQ, A&V using Sweave Sweave 5 / 63 Sweave R + LaTeX= Sweave

. ”Sweave provides a flexible framework for mixing text and S code for automatic document generation. A single source file contains both documentation text and S code, which are then woven into a final document containing the documentation text together with the S code and/or the . output of the code (text, graphs)” – Freidrich Leisch

Created in 2002. It is called ‘S-weave’ not ‘R-weave’ because R is an implementation of the S language.

useR!2013: Radar DAQ, A&V using Sweave Sweave 7 / 63 R + LaTeX= Sweave

⋆ Sweave is based on literate programming (LP) approach and has syntax that is similar to the noweb ⋆ A function in R ⋆ Allows mixing a code (R) with text written in (LaTeX) ⋆ Takes advantage of both LaTeX typesetting capabilities and R’s computational strengths ⋆ A Sweave is simple if you know both R and LTEX ⋆ A file is simple text one consisting of a sequence of code segments (called chunks) and documentation segments ⋆ Enables the generation of dynamic documents ⋆ R code is executed first and the output embedded when the document is generated ⋆ Enables a modularity of code using \SweaveInput{module.Rnw} command and code chunks reuse ⋆ Reproducible research tool

useR!2013: Radar DAQ, A&V using Sweave Sweave 8 / 63 Sweave Workflow

1. A Sweave file has the extension Rnw. This is a plain text file which has both LATEX code and R code all in one (also called a noweb file). For instance we start with

example.Rnw 2. Compile the Sweave file with R in order to produce a tex file:

R (using Sweave function) example.Rnw −−−−−−−−−−−−−−−−−−−→ example.tex 3. Compile the tex file with one of the most popular engines like pdfLATEX , XeLATEX or LuaLATEX in order to obtain your final document:

pdflatex example.tex −−−−−−−→ example.pdf 4. Display the example.pdf file.

. All the workflow is usually done with one command, e.g. in TeXmaker (Ubuntu/Linux) software: . R CMD Sweave %.Rnw|pdflatex -synctex=1 -interaction=nonstopmode -shell-escape %|pdflatex -synctex=1 -interaction=nonstopmode. -shell-escape %|evince %.pdf Make sure Sweave.sty is in your texmf path (or in your working directory)

useR!2013: Radar DAQ, A&V using Sweave Sweave 9 / 63 Our Development Workflow

1. Add code to R script for data analysis using RStudio, Rgedit or other IDE 2. Test the code 3. When the code debugging is complete, add the tested code to Sweave document

useR!2013: Radar DAQ, A&V using Sweave Sweave 10 / 63 Sweave Tools Comparison Software Tools Features TeXmaker (v3.4.1) RStudio (v0.96.316) Notes Sweave support yes yes LaTeX syntax high- yes yes lighting LaTeX custom build yes (full) yes (very limited) R syntax highlighting no, partial[1] yes (R chunk has 1R code different chunk has background) the same FG color LaTeX navigation yes no R code navigation yes, partial yes Own PDF viewer yes yes Dockable internal yes no (opens a PDF viewer separated window) synctex support yes yes not all view- ers Total score: yes (8), no (1) yes (7), no (2)

useR!2013: Radar DAQ, A&V using Sweave Sweave 11 / 63 R + LaTeX= Sweave

Sweave document is .Rnw text file which includes both R and LATEX code. \documentclass[a4paper]{article} \title{Sweave Example 1} \author{Vladimir Skvortsov} \begin{document} \maketitle Here is a linear regression example: <<>>= set.seed(123) # reproducibility x <- rnorm(50); y <- 5*x + rnorm(50) R code chunk model <- lm(y~x); summary(model) @ Sweave embeds all the results! \end{document}

useR!2013: Radar DAQ, A&V using Sweave Sweave 12 / 63 Sweave Options for R code chunks «...»= ⋆ name: user-specified – Specify name of chunk ⋆ eval: TRUE, FALSE – Evaluate chunk? ⋆ echo: TRUE, FALSE – If it is TRUE, in addition to output, R input is incorporated into the typeset document ⋆ term: TRUE, FALSE – Emulates R session? ⋆ results: verbatim, tex, hide – Output? Chunks with ’tex’ will be treated as LaTeX code ⋆ fig: TRUE, FALSE – Chunk produces figure/graphics? ⋆ include: TRUE, FALSE – Automatically generate \includegraphics statement

example:

<>= # empty @ .

useR!2013: Radar DAQ, A&V using Sweave Sweave 13 / 63 Sweave: Including Figures

⋆ One can generate one figure per chunk of code if fig=TRUE and include=TRUE a includegraphics LaTeX command is generated. ⋆ Sweave dump the figures into current document directory during the run. ⋆ To have more control over the figure including, we can create a figure environment after the chunk to put figures in. ⋆ To have a full control over inclusion of graphics, we have to create and include pdf or png files manually.

useR!2013: Radar DAQ, A&V using Sweave Sweave 14 / 63 Sweave: Including Figures - The Simplest Way

<>= library("graphics") x <- rnorm(50) y <- 5*x+rnorm(50) plot(x, y) @

useR!2013: Radar DAQ, A&V using Sweave Sweave 15 / 63 Sweave: Including Figures - More Control

<>= library("graphics") x <- rnorm(50) y <- 5*x+rnorm(50) plot(x, y) @ with some LaTeX include parameters ...

\begin{figure} \begin{center} \includegraphics[height=5cm,width=5cm] {filename-fig1plot2} \caption{Scatter plot} \label{fig:scatter2} \end{center} \end{figure}

useR!2013: Radar DAQ, A&V using Sweave Sweave 16 / 63 Sweave: Including Figures - Full Control

<>= require("grDevices") # pdf size (width and height) in inches! pdf(file="fig1plot3.pdf", width=6, height=5) x <- rnorm(50) y <- 5*x+rnorm(50) plot(x, y) dev.off() @ with some LaTeX include parameters ...

\begin{figure} \centering % width and height have to be in inches here too! \includegraphics[width=6in, height=5in]{fig1plot3} \caption{Scatter plot} \label{fig:scatter3} \end{figure}

useR!2013: Radar DAQ, A&V using Sweave Sweave 17 / 63 Sweave: Including Figures - Extreme Embedding

Embedding images with an encoding algorithm ⋆ Encode your image (e.g. myfigure.png) using Base64 encoding algorithm ⋆ Put the text based Base64 code in an filecontents environment (filecontents package) which writes it to a file ⋆ Add to preamble (-shell-escape is active and use an external script) \newcommand{\generateimage}[2]{% \immediate\write18{convert.cmd #1 > #2}} ⋆ Add to document \generateimage{myfigure.txt}{myfigure.png} \includegraphics{myfigure.png}

source: http://tex.stackexchange.com/questions/21541/ embedding-images-with-an-encoding-algorithm

useR!2013: Radar DAQ, A&V using Sweave Sweave 18 / 63 Sweave: Including Figures - Full Control w. Font Embed.

<>= require("grDevices") # pdf size (width and height) in inches, check if the "NimbusRom" font family exists! pdf(file="fig1plot4.pdf", width=6, height=5, family="NimbusRom", useDingbats=FALSE) x <- rnorm(50) y <- 5*x+rnorm(50) plot(x, y) dev.off() embedFonts("fig1plot4.pdf", outfile = "fig1plot4-ef.pdf") # outfile is the name of the new file (with fonts embedded) @ with some LaTeX include parameters ...

\begin{figure} \centering % width and height have to be in inches here too! \includegraphics[width=6in, height=5in]{fig1plot4-ef} \caption{Scatter plot} \label{fig:scatter3} \end{figure}

useR!2013: Radar DAQ, A&V using Sweave Sweave 19 / 63 R - checking for available fonts

Check a list of installed fonts in R (Ubuntu 12.04):

> require("grDevices") > names(pdfFonts()) [1] "serif" "sans" "mono" [4] "AvantGarde" "Bookman" "Courier" [7] "Helvetica" "Helvetica-Narrow" "NewCenturySchoolbook" [10] "Palatino" "Times" "URWGothic" [13] "URWBookman" "NimbusMon" "NimbusSan" [16] "URWHelvetica" "NimbusSanCond" "CenturySch" [19] "URWPalladio" "NimbusRom" "URWTimes" [22] "Japan1" "Japan1HeiMin" "Japan1GothicBBB" [25] "Japan1Ryumin" "Korea1" "Korea1deb" [28] "CNS1" "GB1"

useR!2013: Radar DAQ, A&V using Sweave Sweave 20 / 63 R - access to all system fonts A way to have access to all system fonts is to install and use extrafont package: install.packages("extrafont")

library(extrafont) font_import() Check a list of fonts: fonts() ... ## [11] "Courier New" "Georgia" ... Create a pdf file with embedded font: require("extrafont") loadfonts() # In Windows, in each R session where you embed fonts, # you will need to tell R where Ghostscript is installed. if (.Platform$OS.type == "windows") { Sys.setenv(R_GSCMD="C:/gs/gs9.06/bin/gswin64c.exe") } pdf(file="fig1plot4.pdf", width=6, height=5, family="Georgia", useDingbats=FALSE) x <- rnorm(50) y <- 5*x+rnorm(50) plot(x, y) dev.off() embed_fonts("fig1plot4.pdf", outfile = "fig1plot4-ef.pdf")

source: http://blog.revolutionanalytics.com/2012/09/how-to-use-your-favorite-fonts-in-r-charts.html

useR!2013: Radar DAQ, A&V using Sweave Sweave 21 / 63 Outline

1. Outline

2. Introduction

3. Sweave Introduction Features

4. Radar project Problem Statement Concept Benefits and Goals Hardware

5. Data Acquisition Principles Measurement flow Measurement timings Measurement Setup using C++ Qt software Data acquisition module

6. Data Analysis and Visualization Workflow Data Analysis Examples

useR!2013: Radar DAQ, A&V using Sweave Radar project 22 / 63 “Virtual Fence” radar project Problem Statement

⋆ To date, the art of protecting areas has been via the installation of perimeter fencing, multiple perimeter cameras, and security personnel watching monitors and walking the perimeter fencing ⋆ Relying on human beings and visible weather conditions are not sufficient solutions to fully protect property from vandalism, theft in today’s environment ⋆ Environment conditions may vary a lot, as day-night time, making any only-one-detector not practical so that without additional sensors and sensor fusion (e.g. infrared or radar ones) the system is not reliable ⋆ Wired solutions require a building of more serious infrastructure ⋆ Some sensors can only detect motion - not stationary objects

useR!2013: Radar DAQ, A&V using Sweave Radar project 24 / 63 Concept

useR!2013: Radar DAQ, A&V using Sweave Radar project 25 / 63 Benefits and Goals

Benefits Goals ⋆ Creation of virtual fence concept to ⋆ The system replaces the need for building reduce investments into building a fence, multiple perimeter cameras and infrastructure security guards watching monitors or ⋆ Development of system node prototype walking perimeters with continuous working cycle, making it ⋆ Radar technologies have made a big easy configurable progress and it is continued, a cost and ⋆ Use a mass-product (automotive) radar to size are day-by-day reduced (silicon reduce a system cost and simplify a microwave radars, due to a mass system implementation; use a standard production) gain horn antenna ⋆ Radars work in various environments ⋆ Evaluation of radar performance (e.g. ⋆ Various radar post-processing and range measurement and its resolution) imaging algorithms can help in system ⋆ Investigation of post-processing tuning and improvement algorithms ⋆ The system will have a compact size ⋆ Evaluation of processing times of various ⋆ Application area is wide measurement procedures ⋆ Experimental studies

useR!2013: Radar DAQ, A&V using Sweave Radar project 26 / 63 Hardware (FMCW radar)

useR!2013: Radar DAQ, A&V using Sweave Radar project 27 / 63 Target Ranging Experiment Setup

useR!2013: Radar DAQ, A&V using Sweave Radar project 28 / 63 FMCW measurement principle

⋆ FMCW (frequency modulated continuous wave) radar generates an RF signal (using linear frequency sweep). ⋆ The received signal is then mixed with the emitted signal and due to the delay there will be a frequency difference that can be detected as a signal in the low frequency range.

⋆ Due to the delay ∆t caused by the distance traveled by the emitted signal to the reflector and back to the receiver, there will be a small difference in signal frequency between the two RF signals that is an IF(Intermediate Frequency)-signal with frequency ∆f or fIF.

useR!2013: Radar DAQ, A&V using Sweave Radar project 29 / 63 Basic Measurement Setup

useR!2013: Radar DAQ, A&V using Sweave Radar project 30 / 63 Intermediate frequency fIF

For linear (in time) output RF signal, a simplified expression of the IF signal with the frequency fIF is:

BW d f = · 2 , IF T c where ⋆ d is the distance between the radar antenna and the reflector and ⋆ c is the speed of light.

Typical values for the RS3400 radar would be a frequency sweep of BW = 1500 MHz in T = 75 ms corresponding to a sweep rate of kf = BW/T = 20000 MHz/s. A distance between the radar and a reflector of d = 15 m gives a delay of ∆t = 0.1 µs and then the IF signal frequency is fIF = 2001 Hz.

⋆ If several reflectors are appearing in the measurement, the resulting IF signal will contain a superposition of the individual IF-signals from the echoes. ⋆ The different echoes are distinguished by their unique IF signal frequency. ⋆ Fourier Transform (FT) of the sampled signal can be used to extract the distances to the different echoes.

useR!2013: Radar DAQ, A&V using Sweave Radar project 31 / 63 Actual range measurement resolution

The actual range resolution (∆da) is dependent on sampling frequency, a number of points and bandwidth, finally it gives the expression

f c ∆d = s · , a L 2 · µ

where µ = BW/Tmod is the modulation coefficient, Tmod - the modulation time; fs - the sampling frequency, L - the number of points (signal length).

9 ⋆ If we consider that L = 1501, fs = 20000 Hz, BW = 1.5 × 10 Hz and −4 Tmod = 7.5 × 10 s, then ∆da ≈ 0.1 m (frequency resolution ∆f = 13.3 Hz) and

⋆ if L = 301 ⇒ ∆da ≈ 0.5 m (∆f = 66.4 Hz).

useR!2013: Radar DAQ, A&V using Sweave Radar project 32 / 63 Signal processing in time domain

A signal h(t) in the time domain may be represented by its spectrum H(f) in the frequency domain. The relations between h(t) and H(f) are given by the Fourier transform analysis and synthesis (inverse transform) equations: ∫ ∞ H(f) = h(t)e2πift dt, −∞ ∫ ∞ − h(t) = H(f)e 2πift dt. −∞ If the signal h(t) is sampled at evenly spaced intervals, there is an equivalent transform for sampled signals called the discrete time Fourier transform (DTFT), then the transform is given by:

N∑−1 N∑−1 1 − H = h(k)e2πift/Ndt, h = H(f)e 2πift/Ndt. n k N k=0 n=0

useR!2013: Radar DAQ, A&V using Sweave Radar project 33 / 63 Post-processing algorithm

. Algorithm . Require: Select x array size of radar data using power-of-two rule, e.g. 29 = 512 points (fs = 20 kHz gives a maximum detectable frequency fIF = 10 kHz and 75 m range) procedure PerceptualHash(x) 1. Apply low-pass filter to reduce high frequency noise. 2. Compute the FFT (or high resolution FT). The FFT separates the mixer output into a collection of frequencies and scalars. 3. Take all x values or divide frequencies into bands (a number of the bands depends on the required sensitivity) then compute the mean FFT value for all values or separately within the bands (excluding the first DC term) 4. Set the hash bits to 0 or 1 depending on whether each of the FFT values is above or below the average value. The result will not vary as long as the overall structure of the radar data remains the same. 5. Construct the hash ph(x)* (e.g. set the n bits into a n-bit integer. return ph(x) end procedure .

(*) The perceptual hash values can be compared using the Hamming distance algorithm (comparing each bit position and counting the number of differences).

useR!2013: Radar DAQ, A&V using Sweave Radar project 34 / 63 Outline

1. Outline

2. Introduction

3. Sweave Introduction Features

4. Radar project Problem Statement Concept Benefits and Goals Hardware

5. Data Acquisition Principles Measurement flow Measurement timings Measurement Setup using C++ Qt software Data acquisition module

6. Data Analysis and Visualization Workflow Data Analysis Examples

useR!2013: Radar DAQ, A&V using Sweave Data Acquisition 35 / 63 Data Acquisition Principles ⋆ The data acquisition is essential part of any experimental study ⋆ The software application for the radar is built around serial terminal application ⋆ The software has two versions: C++ with Qt library and R ⋆ The R version is used as a high level test lab for all algorithms which, in case of successful result, then are ported to C++ for higher execution speed on embedded hardware ⋆ There are several choices for serial data acquisition in R

useR!2013: Radar DAQ, A&V using Sweave Data Acquisition 37 / 63 Measurement Flow with Continuous Mode

useR!2013: Radar DAQ, A&V using Sweave Data Acquisition 38 / 63 Meas. Timings (desktop, C++ Qt, Ubuntu 11.04)

No. Procedure Duration (ms)

Initialization (one time run only, it is not part 105 of measurement cycle), sum for 4 commands

1 Measurement start and OK confirmation 38

2 Data receiving and OK confirmation (for 301 537 points)

3 Data post-processing (FFT) and visualization 38

Sum (measurement cycle only): 613

useR!2013: Radar DAQ, A&V using Sweave Data Acquisition 39 / 63 Measurement Setup using C++ Qt software

useR!2013: Radar DAQ, A&V using Sweave Data Acquisition 40 / 63 Software (C++ Qt): Linux desktop and embedded

Qt Creator C++ IDE and QSerialPortTerminal (radar I/O SW)

useR!2013: Radar DAQ, A&V using Sweave Data Acquisition 41 / 63 Software (C++ Qt): Demo app

useR!2013: Radar DAQ, A&V using Sweave Data Acquisition 42 / 63 Measurement Setup with High Level Interface using R

useR!2013: Radar DAQ, A&V using Sweave Data Acquisition 43 / 63 Data acquisition module 1

There are several choices for serial data acquisition in R: ⋆ The scan function is typically the first choice if we have only requirements however some external operation is still required to set characteristics. ⋆ There is another way using a patch as described in [Shotwell2011] to add a tty connection to R within any POSIX compliant OS. However the patching is required. ⋆ In addition, there is an access to the serial port from within the Tcl interpreter which is integrated in R with package tcltk so that data can be read into R in real time. ⋆ In this work, due to some software/hardware requirements, we decided to proceed with more sophisticated method using low-level interface to Java VM with R package rJava and RXTX native library providing serial communication for the JDK as shown in [Jean-Robert2012]. The development was done using Eclipse/Java as IDE.

useR!2013: Radar DAQ, A&V using Sweave Data Acquisition 44 / 63 Data acquisition module 2 ⋆ The Java interface class was built to write commands and read the data making use of the events to avoid polling the radar. ⋆ The Java class is exposed to R with help of rJava package ⋆ The R script has 2 modes of data import: ▶ file import (data saved earlier) ▶ radar data acquisition (new data) ⋆ The R version is used as a high level test lab for all algorithms which, in case of successful result, then are ported to C++ for higher execution speed on embedded hardware RXTX library source: http://rxtx.qbang.org/wiki/index.php/Main_Page

useR!2013: Radar DAQ, A&V using Sweave Data Acquisition 45 / 63 SerialRadar - RXTX based Java class

• Fields ▶ OutputStream out; ▶ SerialReader input; ▶ SerialPort serialPort; ▶ private static String command; ▶ private static String radar_buffer; ▶ static int counter = 0; ▶ static String port_name; • Methods ▶ public SerialRadar() ▶ void connect ( String portName ) throws Exception ▶ public static void setCommand(String s) ▶ public static String getCommand() ▶ public static void setRadarBuffer(String s) ▶ public static String getRadarBuffer() ▶ public synchronized void close() ▶ public float[] radarData() ▶ static void listPorts() ▶ static String getPortTypeName( int portType )

useR!2013: Radar DAQ, A&V using Sweave Data Acquisition 46 / 63 SerialReader - RXTX based Java class

• Fields ▶ String buffer_string; ▶ String radar_buffer_string = ””; ▶ private BufferedReader inStream;

• Methods ▶ public SerialReader(InputStream in) ▶ public void serialEvent(SerialPortEvent arg0) ▶ public String writeAndGet(String command)

useR!2013: Radar DAQ, A&V using Sweave Data Acquisition 47 / 63 Radar commands to get data

INIT SWEEP:MEASURE ON SWEEP:NUMBERS 1 SWEEP:NUMBERS ? (0 is default; continuous)

FREQUENCY:POINTS 301 FREQUENCY:POINTS 1501 (default) FREQUENCY:POINTS ? SWEEP:IDLE ? (default: 5.000000e-05)

TRIG:ARM TRACE:DATA ?

useR!2013: Radar DAQ, A&V using Sweave Data Acquisition 48 / 63 Sweave - rJava based Serial Interface

1 require(rJava) 2 # .jinit initializes the Java Virtual Machine (JVM). 3 # This function must be called before any rJava functions can be used. 4 .jinit(classpath= ’SerialRadar.jar’, parameters= ”−Xmx512m”) 5 radarJava <− .jnew(’SerialRadar’, check=TRUE) 6 .jmethods(radarJava, ”SerialRadar”) 7 # [Ubuntu] using USB−Serial converter port 8 .jcall(radarJava, returnSig=’V’, method=”connect”, ”/dev/ttyUSB0”)

9 ret <− .jcall(radarJava, returnSig=’S’, method=”writeAndGet”, ”INIT\r”) 10 ret <− .jcall(radarJava, returnSig=’S’, method=”writeAndGet”, ”SWEEP:MEASURE ON\r”) 11 ret <− .jcall(radarJava, returnSig=’S’, method=”writeAndGet”, ”SWEEP: NUMBERS 1\r”) 12 # * set a number of points (default: 1501) 13 ret <− .jcall(radarJava, returnSig=’S’, method=”writeAndGet”, ”FREQUENCY: POINTS 1501\r”)

14 ret <− .jcall(radarJava, returnSig=’S’, method=”writeAndGet”, ”TRIG:ARM\r”) 15 ret <− .jcall(radarJava, returnSig=’S’, method=”writeAndGet”, ”TRACE:DATA ?\r”) 16 rdata <− .jcall(radarJava, returnSig=”[F”,”radarData”)

useR!2013: Radar DAQ, A&V using Sweave Data Acquisition 49 / 63 Sweave - rJava based Serial Interface

1 require(rJava) 2 # .jinit initializes the Java Virtual Machine (JVM). 3 # This function must be called before any rJava functions can be used. 4 .jinit(classpath= ’SerialRadar.jar’, parameters= ”−Xmx512m”) 5 radarJava <− .jnew(’SerialRadar’, check=TRUE) 6 .jmethods(radarJava, ”SerialRadar”) 7 # [Ubuntu] using USB−Serial converter port 8 .jcall(radarJava, returnSig=’V’, method=”connect”, ”/dev/ttyUSB0”)

9 ret <− .jcall(radarJava, returnSig=’S’, method=”writeAndGet”, ”INIT\r”) 10 ret <− .jcall(radarJava, returnSig=’S’, method=”writeAndGet”, ”SWEEP:MEASURE ON\r”) 11 ret <− .jcall(radarJava, returnSig=’S’, method=”writeAndGet”, ”SWEEP: NUMBERS 1\r”) 12 # * set a number of points (default: 1501) 13 ret <− .jcall(radarJava, returnSig=’S’, method=”writeAndGet”, ”FREQUENCY: POINTS 1501\r”)

14 ret <− .jcall(radarJava, returnSig=’S’, method=”writeAndGet”, ”TRIG:ARM\r”) 15 ret <− .jcall(radarJava, returnSig=’S’, method=”writeAndGet”, ”TRACE:DATA ?\r”) 16 rdata <− .jcall(radarJava, returnSig=”[F”,”radarData”)

useR!2013: Radar DAQ, A&V using Sweave Data Acquisition 49 / 63 Sweave - rJava based Serial Interface

1 require(rJava) 2 # .jinit initializes the Java Virtual Machine (JVM). 3 # This function must be called before any rJava functions can be used. 4 .jinit(classpath= ’SerialRadar.jar’, parameters= ”−Xmx512m”) 5 radarJava <− .jnew(’SerialRadar’, check=TRUE) 6 .jmethods(radarJava, ”SerialRadar”) 7 # [Ubuntu] using USB−Serial converter port 8 .jcall(radarJava, returnSig=’V’, method=”connect”, ”/dev/ttyUSB0”)

9 ret <− .jcall(radarJava, returnSig=’S’, method=”writeAndGet”, ”INIT\r”) 10 ret <− .jcall(radarJava, returnSig=’S’, method=”writeAndGet”, ”SWEEP:MEASURE ON\r”) 11 ret <− .jcall(radarJava, returnSig=’S’, method=”writeAndGet”, ”SWEEP: NUMBERS 1\r”) 12 # * set a number of points (default: 1501) 13 ret <− .jcall(radarJava, returnSig=’S’, method=”writeAndGet”, ”FREQUENCY: POINTS 1501\r”)

14 ret <− .jcall(radarJava, returnSig=’S’, method=”writeAndGet”, ”TRIG:ARM\r”) 15 ret <− .jcall(radarJava, returnSig=’S’, method=”writeAndGet”, ”TRACE:DATA ?\r”) 16 rdata <− .jcall(radarJava, returnSig=”[F”,”radarData”)

useR!2013: Radar DAQ, A&V using Sweave Data Acquisition 49 / 63 Outline

1. Outline

2. Introduction

3. Sweave Introduction Features

4. Radar project Problem Statement Concept Benefits and Goals Hardware

5. Data Acquisition Principles Measurement flow Measurement timings Measurement Setup using C++ Qt software Data acquisition module

6. Data Analysis and Visualization Workflow Data Analysis Examples

useR!2013: Radar DAQ, A&V using Sweave Data Analysis and Visualization 50 / 63 Data Analysis and Visualization Data Processing Workflow

1. Make a measurement 2. Acquire data from radar (specify a number of points to return from radar) 3. Apply FFT analysis to the data using fft function of stats package 4. Calculate a distance to the target(s) (optional: and/or speed of the target(s))

useR!2013: Radar DAQ, A&V using Sweave Data Analysis and Visualization 52 / 63 Running R with gedit R plugin (Rgedit)

useR!2013: Radar DAQ, A&V using Sweave Data Analysis and Visualization 53 / 63 Sweave Example: 3 distances to target

1 (10 cm) 2 (100 cm) 3 (200 cm) Mixer output Mixer 40000 45000 50000 55000 0 500 1000 1500 sample #

useR!2013: Radar DAQ, A&V using Sweave Data Analysis and Visualization 54 / 63 Sweave FFT analysis for 3 distances to target

4, 130.26 15, 125.05 25, 123.12 1 (10 cm) 2 (100 cm) . 3 (200 cm) FFT analysis of

the experimental Magnitude, dB data using

frequency and 80 90 100 120 phase response 0 50 100 150 with variation of sample # distance to target: • 1, d = 10cm • 2, d = 100cm • 3, d = 200cm

d - distance to Phase .target −2 −1 0 1 2

0 50 100 150 sample #

useR!2013: Radar DAQ, A&V using Sweave Data Analysis and Visualization 55 / 63 Sweave Example: radar, 3 Algorithms Signal Analysis . Lab experiments with the target at 4, 39.947, 137.753 FFT CZT HRFT 10 cm using 3 different algorithms for signal analysis:

• FFT Magnitude, dB • CZT (Chirp Z 60 80 100 120 140 Transform, 4 times res. 0 100 200 300 400 increase comp. frequency, Hz to FFT) • HRFT (High Resolution Fourier Transform),

shown in Phase frequency range of 13.3-186.4 Hz, 5 times res. −3 −2 −1 0 1 2 increase comp. 0 100 200 300 400 . to FFT) frequency, Hz

useR!2013: Radar DAQ, A&V using Sweave Data Analysis and Visualization 56 / 63 Sweave Example: radar, hash bits of 6 tests with 512 points

6

5 hash bits 4 0 3 1 2

1 1 64 512 192 128 256 320 384 448

(2nd bit map shows the biggest difference - the intruder is presented)

useR!2013: Radar DAQ, A&V using Sweave Data Analysis and Visualization 57 / 63 Demo: Dynamic Report Generation - Radar Test PDF source:figure-radar-test-20130329-144628-ef.pdf

This report has been generated by Sweave on 2013−03−29 14:46:46 raw data Mixer output Mixer

35000 50000 0 500 1000 1500 sample # [FFT] First target has been found at d = 0.17 m 6, 6, 138.218 Magnitude, dB 60 100 140

0 50 100 150 frequency, Hz . useR!2013: Radar DAQ, A&V using Sweave Data Analysis and Visualization 58 / 63 Sweave Example: raw, FFT; target ranging (42.4 m) Mixer output Mixer 44000 48000 52000 0 500 1000 1500 sample #

[Analysis] First target has been found at d = 42.4 m

signal clutter SCR+100

426, 5659.1, 114.9 Magnitude, dB 60 80 100 120 140

sample # 1 50 100 150 200 250 300 350 400 450 500 550 600 650 700 750

f, Hz 0 1000 2000 3000 4000 5000 6000 7000 8000 9000 10000

d, m 0 2 4 6 8 10 13 16 19 22 25 28 31 34 37 40 43 46 49 52 55 58 61 64 67 70 73

useR!2013: Radar DAQ, A&V using Sweave Data Analysis and Visualization 59 / 63 Conclusions

⋆ Sweave tools are not perfect yet, so that still requires a lot of manual coding (however there are good separate LaTeX and R tools) ⋆ To take advantage of the Sweave (literate programming) approach, you have to know both R and LaTeX ⋆ Relating to rJava use, there is a problem, when each Java update overwrites the installed RXTX libraries; you have to set several paths to avoid ”[name] not found” message ⋆ You can keep track of results, output and codes in one place. ⋆ You can redirect your text and code to various forms of output and dynamically (automatically) generate or update various documents: presentations, reports, and books. ⋆ You can reproduce your results. The chances are very high!

useR!2013: Radar DAQ, A&V using Sweave Conclusions 60 / 63 Conclusions

⋆ Sweave tools are not perfect yet, so that still requires a lot of manual coding (however there are good separate LaTeX and R tools) ⋆ To take advantage of the Sweave (literate programming) approach, you have to know both R and LaTeX ⋆ Relating to rJava use, there is a problem, when each Java update overwrites the installed RXTX libraries; you have to set several paths to avoid ”[name] not found” message ⋆ You can keep track of results, output and codes in one place. ⋆ You can redirect your text and code to various forms of output and dynamically (automatically) generate or update various documents: presentations, reports, and books. ⋆ You can reproduce your results. The chances are very high!

useR!2013: Radar DAQ, A&V using Sweave Conclusions 60 / 63 Conclusions

⋆ Sweave tools are not perfect yet, so that still requires a lot of manual coding (however there are good separate LaTeX and R tools) ⋆ To take advantage of the Sweave (literate programming) approach, you have to know both R and LaTeX ⋆ Relating to rJava use, there is a problem, when each Java update overwrites the installed RXTX libraries; you have to set several paths to avoid ”[name] not found” message ⋆ You can keep track of results, output and codes in one place. ⋆ You can redirect your text and code to various forms of output and dynamically (automatically) generate or update various documents: presentations, reports, and books. ⋆ You can reproduce your results. The chances are very high!

useR!2013: Radar DAQ, A&V using Sweave Conclusions 60 / 63 Conclusions

⋆ Sweave tools are not perfect yet, so that still requires a lot of manual coding (however there are good separate LaTeX and R tools) ⋆ To take advantage of the Sweave (literate programming) approach, you have to know both R and LaTeX ⋆ Relating to rJava use, there is a problem, when each Java update overwrites the installed RXTX libraries; you have to set several paths to avoid ”[name] not found” message ⋆ You can keep track of results, output and codes in one place. ⋆ You can redirect your text and code to various forms of output and dynamically (automatically) generate or update various documents: presentations, reports, and books. ⋆ You can reproduce your results. The chances are very high!

useR!2013: Radar DAQ, A&V using Sweave Conclusions 60 / 63 Conclusions

⋆ Sweave tools are not perfect yet, so that still requires a lot of manual coding (however there are good separate LaTeX and R tools) ⋆ To take advantage of the Sweave (literate programming) approach, you have to know both R and LaTeX ⋆ Relating to rJava use, there is a problem, when each Java update overwrites the installed RXTX libraries; you have to set several paths to avoid ”[name] not found” message ⋆ You can keep track of results, output and codes in one place. ⋆ You can redirect your text and code to various forms of output and dynamically (automatically) generate or update various documents: presentations, reports, and books. ⋆ You can reproduce your results. The chances are very high!

useR!2013: Radar DAQ, A&V using Sweave Conclusions 60 / 63 Conclusions

⋆ Sweave tools are not perfect yet, so that still requires a lot of manual coding (however there are good separate LaTeX and R tools) ⋆ To take advantage of the Sweave (literate programming) approach, you have to know both R and LaTeX ⋆ Relating to rJava use, there is a problem, when each Java update overwrites the installed RXTX libraries; you have to set several paths to avoid ”[name] not found” message ⋆ You can keep track of results, output and codes in one place. ⋆ You can redirect your text and code to various forms of output and dynamically (automatically) generate or update various documents: presentations, reports, and books. ⋆ You can reproduce your results. The chances are very high!

useR!2013: Radar DAQ, A&V using Sweave Conclusions 60 / 63 For Further Reading

Zahn, I. Learning to Sweave in APA style. The PracTeX Journal, No. 1, 2008. URL: http://tug.org/pracjourn/2008-1/zahn/ Shotwell, M. Experimenting with a tty connection for R. In useR! 2011 The R User Conference, Coventry, UK, 2011. Jean-Robert. A thermometer in R using Arduino and Java. Personal blog, 2012. URL: http://jean-robert.github.com/2012/11/11/thermometer-R-using-Arduino-Java.html Skvortsov, V., K. M. Lee, and S. E. Yang. Inexpensive Radar-Based Surveillance: Experimental Study. In 9th International Conference & Expo on Emerging Technologies for a Smarter World - CEWIT2012, Songdo, Incheon, Korea, 2012.

useR!2013: Radar DAQ, A&V using Sweave Bibliography 61 / 63 Final

Thank you! This presentation has been created using Sweave (XeLATEX and R) and is based on Incheon LaTeX Beamer style template! Link

http://bit.ly/1dofwtn