Wednesday, August 28, 2013

Testing Transmission Line Theory On A Roll Of Coaxial Cable

I recently bought a roll of coaxial cabling to do some work at my grandmother's place and thought I could use it to experimentally confirm transmission line theory.  It's one of those topics that you do a lot of reading about, but you may never actually encounter in the real world.  I can still remember when I was told about transmission lines and everything became clear.  Of course a voltage couldn't appear instantaneously at the end of a cable. Information can't travel faster than the speed of light.  The signal has to travel along the cable and to make things more interesting can get reflected depending on how the cable is terminated.  I've done the math, I've done the simulations, but I still want to do the experiment.

My experiment isn't anything fancy.  I terminated the roll of coaxial cable with an f connector and connected it to an oscilloscope via an adapter and a BNC tee.  The other side of the tee was then conencted to a function generator outputting a 10 volt pulse.  The other end of the coaxial cable wasn't terminated, so in theory the pulse should reach the end of the cable and be completely reflected.  The waveform on the scope was observed to see if this happened.  I'm not trying to take super accurate measurements, I only want to get a quick overview of the physics involved.

Test Equipment
Test Set-up
If the experiment is simplified it can be represented as the following schematic.  A function generator with a 50 ohm output impedance is connected to a coaxial cable with a 75 ohm characteristic impedance.

Schematic
Experiment Schematic
I'm not sure of the quality of my cable, from what I've heard it's average to good.  It's suitable for cable television installations, so I guess it's not designed for extremely long runs, but should give decent results.

Coaxial Cable Roll
305m Reel of Quad Shield RG6 Coaxial cable
Below is the output of the generator without the cable connected.  The peak voltage of the pulses is around 10 volts and they also have relatively short rise and fall times.

Oscilloscope Screen
Generator Output. No-load
After the cable was connected a second pulse appears, this is assumed to be the reflection of the initial pulse.  The magnitude of the first pulse is also reduced due to the loading of the cable.  As the signal is applied to the cable energy starts to flow down it.  The rate at which the energy flows down the cable can be replicated by replacing it with a resistor equal to it's characteristic impedance.  This forms a simple voltage divider with the function generator's output impedance.  The cable voltage would be equal to Vin*Zo/(Zo+Rg) = 10*75/(75+50) = 6 Volts.  This agrees with the observed voltage of the initial peak in the screen shot below.

The magnitude of the reflected pulse is a little bit beyond this experiment.  Theoretically it can be calculated, but I think it needs to be simulated for a real world application like this.  Simple calculations assume an ideal conductor, whereas in reality the conductor is non uniform an has imperfections.  This creates small impedance mismatches all along the cable causing transmission and and reflection at each point.  I think this is measured by the Structural Return Loss (SRL).  I suspect that this is the reason the pulses have rising tops.  As the pulse travels down the cable a portion of the signal is continually reflected back to the generator.  I can't be sure without some simulations but I think this is what is happening.

Oscilloscope Screen
Initial Pulse and Reflected pulse
There's another experiment that can be easily done with this arrangement.  The velocity factor of the coaxial cable can be calculated.  This is how fast the signal travels down the cable as a fraction of the speed of light.  From the waveform below it can be seen that the elapsed time between the rising edge of the initial pulse and the the rising edge of the reflected pulse is 2.4 us.  As the pulse had to do a round trip on a 305 m length of cable we know it travelled 610 m.  This gives a velocity of 250000 km/s.  As a fraction of the speed of light this is equal to 85%, closely agreeing with the 82% value from the cable's datasheet, also shown below.  This confirms that the second pulse is the reflection.

Oscilloscope Screen
Round Trip Time
Datasheet
Cable Data Sheet
Another phenomenon to observe is what happens when the transmitted pulse overlaps the reflected pulse. This is show in the waveform below.  As the duration of the pulse is increased, it will collide with the reflected pulse.  As the reflected and initial pulses collide they add constructively.  There are 3 distinct sections to the plot below.  The first section is the initial pulse, the second section is the sum of the initial and reflected pulse and the third section is only the reflected pulse.

Oscilloscope Screen
Reflected Pulse colliding with Transmitted pulse
I like doing small tests like this.  Experimentally confirming physics principles is useful as it shows the difference between ideal models and the real world.  An interesting quirk like the structural return loss of the cable is something I wouldn't have known about without actual taking measurements.

Saturday, August 17, 2013

Making Detecting the Angle of Rotated Text More Robust

In my last post I demonstrated how to find the angle of rotated printed text using image processing.  I also mentioned a couple of ways to make the process more robust.  In this post I'll expand on what I meant with a demonstration.

The code and image files associated with this post can be found here.

The basis for this method is that we are only looking for large values in the Radon Transform, this indicates a feature like a line or row of text.  We are also looking for bright features in the gradient of the transform.  This indicates the transition point between a row of text and the white space below it.  By masking out lower values in these two steps, the data points that are more relevant should be highlighted.

The process starts out exactly the same as in my first demonstration, we need to generate the radon transform of the input image.

Radon Transform
Radon Transform from 70 to 110 degrees

As before, a gradient of the radon transform also needs to be calculated.

Vertical Image Gradient
Vertical Image Gradient

This is where things start to change.  A binary mask is generated from the radon transform.  This will only show bright points in the transform and remove other points that are most likely false positives.  The threshold has been set to 20 percent of the maximum value in the radon transform.  A permissive value but it still removes a lot of false positives.

Image Mask
Thresholded Radon Transform

The same process is applied to the gradient image using a a threshold of 10 percent of the maximum value of the gradient.

Image Mask
Thresholded Gradient Image

The two masks are then applied to the original Radon transform by multiplying the masks with the transform.

Radon Transform
Masked Radon Transform

I've created the false colour image below to help demonstrate the process a little better.  The red channel of the image is the mask from the original radon transform.  The Green channel is the mask generated from the gradient image.  The Blue channel is the original Radon Transform.  The only areas that will be visible in the final masked image are areas where the two masks align.  This means that the red and green channel will coincide and create a yellow pixel.  This means the only sections visible in the final image are those that are shades of yellow.  As the intensity of the blue Radon Transform becomes brighter it will turn the yellow pixel white.  (click to enlarge the image)

Radon Transform\
Coloured Transform

As before the images are vertically sumed to create an array of intensities.  The peak intensity will be the rotation angle of the text.  As a comparison, I've show the intensity arrays for the masked and unmasked versions of the process below.
Rotation Angle Graph
Gradient Intensity vs Text Rotation Angle - Unmasked


Rotation Angle Graph
Gradient Intensity vs Text Rotation Angle - Masked

The magnitude of the peaks in the graphs above doesn't matter, what's important is the ratio of the peak to the next largest feature.  In the unmasked version the peak is around 74000 and the next largest peak is around 34000, a ratio of about 2.2.  In the masked version the peak is around 29000 and the next largest peak is around 7000, a ratio of about 4.1.  This make picking the correct feature a lot easier and gives more confidence in the result, which as before comes out at 0.6 degrees.

This is by no means the best that could be done.  I picked the threshold values out of thin air.  They could be determined by trial and error for a particular type of document or they could be dynamic and adjust to the input image.  I'll leave that as an exercise for the reader :-)

Tuesday, August 6, 2013

Detecting the Rotation Angle of Printed Text

In my last post I described the basics of how to use the Radon Transform to detect the rotation angle of text.  In this post I'll automate the process slightly more, and try to make it more rugged.  As before, we'll start with the simulated test report.  This time however, a thresholding operation will be applied to the image to convert it to black and white before the radon transform is applied.   This will remove any variations due to the whiteness of the paper or darkness of the text.

The file used in testing the below process can be found here.
Report
Simulated Report
Previously when applying the Radon transform to the report, it was over the range of 0 to 180 degrees, this was more than what was needed.  I figure that the text will never be more than 20 degrees out of alignment in either direction.  For this reason the radon transform is only calculated over the range 70 to 110 degrees, this is 20 degrees either side of the 90 degree horizontal direction.

Radon Transform
Radon Transform from 70 to 110 degrees
After applying the radon transform, the features identifying the alignment of the text become evident in image.  To highlight these, a gradient filter was applied.  The vertical gradient filter of the Prewitt operator was convolved with the image.

[ 1  1  1]
[ 0  0  0]
[-1 -1 -1]

Convolving this matrix with the image has the effect of calculating a vertical gradient and then horizontally applying an averaging filter across three values.  This makes the features in the middle of the image more prominent.

Vertical Image Gradient
Vertical Image Gradient

The image is then summed vertically to produce an array.  The group of features in the middle of the image will cause a peak in this array.  This is evident in the plot of the array below.

Rotation Angle Graph
Gradient Intensity vs Text Rotation Angle
The location of the peak in this graph will indicate the amount the image is rotated.  By searching the array it can be found that the this peak is 0.6 degrees from the 90 degree mark.  This is how much the test image was rotated.

The above process represents the basis of an automated system to remove rotation distortion from scanned documents.  I haven't tested it on real documents, so there could be potential problems with how robust it is.  I've got a couple ideas to make the detection process stronger and less prone to error if that's a problem.  We're looking for two main things in the radon transform, bright spots that represent lines, and sharp transitions that represent the border between text and the white-space under it.  Applying a threshold to the radon transform will remove some of the background details that don't represent lines.  Thresholding the gradient image will also remove features that don't have sharp transitions.  It's just a matter of experimentation to see at what levels the thresholds need to be applied.