Skip to content

Instantly share code, notes, and snippets.

@weibeld
Last active December 10, 2015 10:19
Show Gist options
  • Save weibeld/9ba5a35196143cfd6232 to your computer and use it in GitHub Desktop.
Save weibeld/9ba5a35196143cfd6232 to your computer and use it in GitHub Desktop.
A dBm to Milliwatts Conversion Table
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
# Create one or more LaTeX files each containing a dBm to milliwatts conversion
# table. The table is a LaTeX 'tabular' and has three columns:
# 1. dBm
# 2. Milliwatts (absolute notation)
# 3. Milliwatts (scientific notation)
#
# Prerequisites: R package 'xtable' installed
#
# Daniel Weibel <danielmweibel@gmail.com> December 2015
#------------------------------------------------------------------------------#
dBm2mW <- function(dbm) {
# Convert dBm to milliwatts
#----------------------------------------------------------------------------#
10^(dbm/10)
}
mW2dBm <- function(mw) {
# Convert milliwatts to dBm (not used in this file)
#----------------------------------------------------------------------------#
log10(mw) * 10
}
FormatSciLatex <- function(v, digits) {
# Format a number in scientific format for printing with LaTeX.
# Example: 0.063096 ==> "6.3906 x 10^{-2}"
#----------------------------------------------------------------------------#
sci <- formatC(v, digits=digits, format="e") # Format: "6.3096e-02"
n <- nchar(sci) # Length of each number
exp <- strtoi(substr(sci, n-1, n), base=10) # Exponent (last two chars)
sig <- substr(sci, n-2, n-2) # Exponent sign (3rd-last char)
sig <- ifelse(sig == "-", "-", "")
fac <- as.numeric(substr(sci, 1, n-4)) # Factor (chars up to "e")
paste0(fac, " x 10^{", sig, exp, "}")
}
XtableNoWarnings <- function(...) {
# Run xtable without warnings (e.g. due to non-standard 'align' specifiers)
#----------------------------------------------------------------------------#
options(warn=-1)
xt <- xtable(...)
options(warn=0)
xt
}
library(xtable)
# dBm values to include in each table (each list elt. will become a table)
dbm.groups <- list(0:-69, -70:-139)
# File name for each table
file.groups <- c("tab_1.tex", "tab_2.tex")
# Column alignment for each table (will be put in \begin{tabular}{...})
align.groups <- c("ld{3.0}d{10}x{6.4}", "ld{4.0}d{17}x{6.5}")
# Table header (same for all tables)
colnames <- c("\\multicolumn{1}{l}{dBm}",
"\\multicolumn{1}{l}{Milliwatts}",
"\\multicolumn{1}{l}{Milliwatts}")
# Number of digits after decimal point in 2nd column of tables
digits <- 5
i <- 1
for (dbm in dbm.groups) {
mw.raw <- dBm2mW(dbm)
mw.abs <- formatC(mw.raw, digits=digits, format="fg")
mw.sci <- FormatSciLatex(mw.raw, digits=digits-1)
df <- data.frame(dbm, mw.abs, mw.sci)
colnames(df) <- colnames
xt <- XtableNoWarnings(df, align=align.groups[i])
print(xt, include.rownames=FALSE,
floating=FALSE,
sanitize.text.function=identity,
file=file.groups[i])
i <- i + 1
}
% Place two dBm conversion tables, as produced by dbm_table.r, next to each
% other on an A4 page.
%
% Daniel Weibel <danielmweibel@gmail.com> December 2015
%------------------------------------------------------------------------------%
\documentclass{article}
\usepackage[a4paper,margin=1cm]{geometry}
\usepackage{dcolumn}
% Column type with alignment on decimal dot (or other char). The argument is
% the max. number of decimal places in the column (defines column width).
% Note: for the headers of these columns use e.g. \multicolumn{1}{|l|}{Text}
\newcolumntype{d}[1]{D{.}{.}{#1}} % Align on decimal dot
\newcolumntype{x}[1]{D{x}{\times}{#1}} % Align on 'x' and replace with '\times'
% Disable page numbers
\pagenumbering{gobble}
\begin{document}
\small
\begin{center}
\parbox{0.39\textwidth}{\centering\include{tab_1}}
\parbox{0.45\textwidth}{\centering\include{tab_2}}
\end{center}
\end{document}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment