Thursday, November 28, 2013

Using Latex To Add Page Numbers and a Binding Offset to PDF Files

I spent the weekend getting my tax documents in order (yeah I know, I live a pretty extreme life).  I like to assemble them into a single PDF file of about 100 pages and keep a digital copy and a produce a bound printed copy.  The bound version is easy to make notes on and show to others.  That's all well and good, but sometimes when binding the printed version, the holes are punched through important information on the side of the pages.  Adding a binding offset solves this problem.

A binding offset is an area on the side of the page that's kept clear for the holes to be punched through.  Offsetting the pages isn't enough though, they also need to be scaled to fit into the smaller area available.  In my case I print on both sides of the paper, this complicates things further as well.  It means the binding offset needs to alternate between the left and right sides of the page on consecutive pages.

I thought that this was pretty much impossible unless you used professional tools, but I soon found out it's ridiculously easy in LaTeX.  While I was at it, I decided to add page numbering to the output file as well.

Below I'll go though a quick exaggerated demonstration of what I mean.  I've started by looking at the 2nd and 3rd pages of a 3 page document.  The page on the left covers the extents of the page.

PDF file capture
Example document
After being processed the output file now contains an area in the middle of the page to allow binding.  The page numbers are also added to the bottom of the page opposite the binding area.
PDF file capture
Binding offset and page numbers added
The LaTeX file I used to generate the output is below.  It by no means covers all possible situations, PDF files can get complex, and this could break one of the more esoteric features of the standard, so I recommend testing it first.  It worked perfectly for me though.

The code below is also just a starting point, you could also include pages from other PDF files or put multiple pages on one page, it's a highly customisable tool.  This however should be enough of a framework to get you started.  I'm still learning all the tricks myself.

% pdfbind.tex

\documentclass[10pt,a4paper,twoside]{report}
\usepackage[final]{pdfpages}
\usepackage[left=2cm,right=2cm,top=2cm,bottom=2cm]{geometry}
\usepackage{fancyhdr}

\pagestyle{fancy}
\fancyhead{}                       % clear header
\fancyfoot{}                       % clear footer
\fancyfoot[LE,RO]{\Large\thepage}  % add page numbers to pages

% remove bars from top and bottom of page
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}

\begin{document}
\includepdfset{offset=42mm 0cm, pagecommand=\thispagestyle{fancy}}
\includepdf[fitpaper=true,scale=0.6,pages=-]{PDFInputFile.pdf}
\end{document}



Sunday, November 17, 2013

Doppler Radar Module

I've been doing a bit research into speed signs after I spotted one at the local university.  Initially it wasn't clear how they worked, but after a bit of googling it became clear they were radar based.  Normally I'd be happy and leave at that, but when you can get a simple Doppler radar module from eBay for under ten dollars, I had to have one to play around with.
Radar Module
Radar Module
They're nothing special, they have a limited range and aren't too accurate, so they can't be used to fine someone for speeding because it wouldn't stand up legally.  They can however be used for purposes like letting drivers know if they're doing over the speed limit, if you are under the speed limit you get a smiley face.  Something I initially thought wouldn't work, but it turns out to be rather effective.  Recently they've started appearing in our local area around schools where the speed limit drops from 60 km/h to 40 km/h between the hours of 2pm and 4pm on school days and they seem to be making a difference.
Brisbane Speed Sign
Image Source - http://www.couriermail.com.au/questnews/north/earn-a-smiley-face-by-slowing-down-as-you-drive-or-ride-past-newmarket-state-school/story-fn8m0rl4-1226647642745
If you're unaware of how Doppler radar speed measurement works it's pretty simple.  The device transmits a radio signal at a particular frequency, as that signal is reflected off a moving object its frequency is slightly changed.  The unit measures this reflected signal and uses the frequency difference between the transmitted and received signals to determine the speed of the object.  This usually occurs in the microwave region.  My module for instance operates at around 10 GHz.  The specs according to eBay are below.

Transmitter

Frequency : 10.525GHz
Frequency Setting Accuracy : 3MHz
Power Output (Min.) : 13dBm EIRP
Operating Voltage : +5V +/- 0.25V
Operating Current (CW): 60mA max, 37mA typical
Harmonic Emissions: -10dBm


Receiver

Sensitivity (10dB S/N ratio) in 3Hz to 80Hz band width : -86dBm
Noise in 3Hz to 80Hz band width : 10μV
Antenna Gain : 8dBi
E Plane 3dB Beamwidth : 36'.
H Plane 3dB Beamwidth: 72'


eBay specs
There wasn't a data sheet for this module, but this one I found on Google seems to be an exact match.

Now you know the principle behind it, back to the radar module.  The bottom of the board is mostly ground plane with two patch antennas, the one on the left is the transmitter an the one on the right is the receiver.  I think the two areas without solder mask in the middle of the ground plane are low inductance paths to ground for the transistor in the oscillator.
PCB
Radar Module Bottom Side
RF and microwave in general is something I don't know a lot about, but I think I can work out the general operation of the board.  Of course with the frequency of operation in the 10 GHz neighbourhood the board is covered with the obligatory microstrip stubs and filters.  I'd love to learn how that stuff works but for now it's akin to magic.  The transistor on the bottom right of the board appears to be the oscillator.  I can't find out much about the white cylindrical block, but according to http://www.shfmicro.com/gunn.htm it appears to be a Barium-titanate "puck" to stabilise the frequency.  Given the topography of the circuit, it looks to be part of the feed-back circuit of the oscillator even though it doesn't seem to be electrically connected to the circuit.  I'm going to put that down to some sort of RF coupling, or as I call it jiggery pokery.

EDIT:  A helpful commenter has informed me that the white object is a dielectric resonator.  Things make more sense now.

The output of the oscillator goes up to the middle on the left and off to the transmitting antenna.  It's also fed to what I believe to the mixer located near the centre.  The mixer also receives an input from the receiving antenna on the right.  The output is fed to IF signal at the top left.  This in theory should contain a signal close to DC and a frequency close to 20 GHz.  I doubt the 20 GHz will  make it out though, it's possibly already filtered out by the radial stub near the top of the board.  The PCB material will also play a part.  Given the price of the module it's like to have high dielectric losses at high frequencies.
PCB
Radar Module Top Side
The top of the board is covered with an aluminium shielding can that has what appears to be a tuning slug that is aligned with the white object on the board.
Shielding Can
Shielding Can with Tuning Slug
Transistor
Microwave Transistor
Frequency Stabilizer
Possible Frequency Stabilizer
Mixer
Mixer
Board hooked up for testing.  5 Volt DC applied, signal output is displayed on an oscilloscope.

Circuit Under Test
5 volt supply connected, output to oscilloscope
I initially started testing it by moving a piece of plastic toward and away from the PCB, I quickly realised that it wasn't going to reflect microwaves and wouldn't work.  (What was I thinking).  Luckily I had a sheet of aluminium checker plate lying around.
Checker Plate
Checker Plate
That did the job.  The signal below is the result as I moved the sheet toward the device.  The frequency will be proportional to the speed and the magnitude will be related to the size of the reflecting object and its distance from the module.  As a side note the module uses 40 mA in standard operation.
Oscilloscope Screen
Radar Module Output as Metal Plate Approaches it
Let's just take a quick estimate of the signal above.  Near the centre of the plot the period of the waveform is around 5ms, giving a frequency of 200 Hz.
Doppler Equations
Example Doppler Equations

3 m/s corresponds to what I would have estimated the speed to be.  It's not exact, but with a bit of work it could make something interesting.  I don't have anything in mind, but something may come to me.  One thing that is cool however is amateur radio operators modulating the current of the transmitter to vary the output frequency to transmit radio signals over 20 miles.  Have a look here for more information. http://www.shfmicro.com/gunn.htm

Wednesday, November 6, 2013

Most Efficient Way To Carry Exact Sub 5 Dollar Change in Australian Currency

A quick blog post today.  I wanted to know the most efficient way to carry change with me.  I'm a little OCD and I sometimes like to be able to pay with exact money.  It's easy to get to multiples of 5 dollars with notes, so all we need to know is the combinations of coins you would need to carry to give change up to 5 dollars.  The smallest denomination of Australian currency is the 5 cent coin.  This means that I need to be able to give any amount of change from 5c to 495c in 5c intervals with any combination of coins selected.

The code for today's post can be found here.

I defined efficiency using two metrics, least coins and least weight.  Finding the combination of coins was relatively easy.  I started by finding the combinations of minimum coins with an Octave script.  It took a while and I had to make some optimizations to speed it up, but it got there in the end.  I tried to do the same thing with the minimum weight calculations but it took too long.  So I ported the code over to C++ and made a "purse" class to track the weight of coins in the purse.  This sped up the process significantly.  The results are listed below.

Weight of Australian coins according to the reserve bank

$2    6.60 g
$1    9.00 g
50c  15.55 g
20c  11.30 g
10c   5.65 g
5c    2.83 g

Minimum coins needed to give exact sub $5 change

2x$2, 1x$1, 1x50c, 2x20c, 1x10c, 1x5c
2x$2, 1x$1, 1x50c, 1x20c, 2x10c, 1x5c
1x$2, 2x$1, 1x50c, 2x20c, 1x10c, 1x5c
1x$2, 2x$1, 1x50c, 1x20c, 2x10c, 1x5c

Minimum weight of coins to give exact sub $5 change

2x$2, 1x$1, 1x50c, 0x20c, 4x10c, 1x5c

2x$2, 1x$1, 1x50c, 1x20c, 2x10c, 1x5c

By a happy coincidence there is an intersection between the two groups.  The following combination satisfies the minimum weight and minimum coins condition.  8 coins and 63.18 g.

2x$2, 1x$1, 1x50c, 1x20c, 2x10c, 1x5c


Australian Coins
Most Efficient Way To Carry Exact Sub 5 Dollar Change in Australian Currency

It might only be a fraction of a gram but it's one of those things I just have to know even though it doesn't really matter.