Thursday, October 23, 2008

Photometric measurements of a laptop monitor

This post completes the path from the journey from light intensity, or exposure, to pixels on the screen. In the last one I looked at how my Nikon D300 translates intensities in the scene being photographed into values in the RAW and JPEG files. The response of the RAW file is very linear, doubling the exposure (or equivalently intensity) doubles the value in the pixel in the RAW file. The response in the JPEG file is more complex; over a 4-stop range the value in the file increases as the log of the exposure. The final part of the story is how do these values in the file correspond to intensity on a computer monitor. As seen last week, RAW files from a digital camera are a good method of measuring intensity.. this is what is called photometry (at least in the field of astronomy).

The first interesting measurement that can be made with a D300 is the colour temperature (CT) of the monitor. One way to do this is to just take a bunch of JPEG shots of a white window on your monitor, with the white balance set to various colour temperatures. The image in which the Red, Green and Blue channels look the best matched is the one that had the correct colour temperature setting. You can see this on the D300 colour graphs, or offline using a program like photoshop. Some measured curves are below:

The photo with 4350K CT looks too blue, showing that the monitor is hotter than that. Similarly the 7140K shot is dominated by the red channel, indicating the CT is less than 7140K. The best match between all channels is at a colour temperature between 5500K and 5880K. Interestingly, according to Wikipedia the sRGB standard demands a CT of 6500K, so my monitor is evidently a little below the standard.

The second thing that can be measured is the intensity of the light generated by the monitor for a know intensity in an image file. The testcard I used, which was drawn with GIMP (similar to photoshop) is shown immediately below, and the photograph of how it appeared on my monitor is below it. To save space on the blog, the JPEG version of the photo is shown, but I used the RAW file for the photometry, since its response is linear.


The relationship between value in the testcard and intensity on the monitor is plotted below.

The response is somewhat linear over an approximately 5-stop range. However, even when taken on a dark evening (with the lights off!), the monitor does not go completely dark, which presumably means the LCD display is only "so good" at blocking the fluorescent backlight from shining through.

The characteristic curves of the camera and display can be put together to give an overall response, as shown below. The results are rather depressing, they make me feel like I should get a better display!

I would be interested to see how other displays respond.. would a really expensive display be more linear and go darker -- presumably a CRT could go completely dark. It's my impression that my old laptop had a better display than my newer one, I'll have to test it some time.

Saturday, October 11, 2008

D300 characteristic curve

I don't have very much new to report this week. My first week at my new job went well, the commute on the RER-B train is pretty easy, and I have a nice walk through Parc Montsouris to get to the local train station, and a 10 minute hike up the side of a hill to get to the lab at the other end, so it seems I will be staying fit even despite myself.

Over the last few weeks I've been reading The Negative, the second of Ansel Adam's series on photography. It's a really well written book, that describes how he went about bringing the vision he had for his photographs into reality. It details how he meters the scene, decides on exposure for the shot and what processing he will do to during development to enhance or contract the contrast. I was amazed at the laborious care he put into each photograph, far more than I put into mine... but then of course, his photos are far far superior than any of mine :-)

The other thing that is obvious from the book is that he had a complete understanding and mastery of all the technical aspects of photography. His book is full of scientific explanations of how film works, the nature of light, and how intensities in a scene relate to density on the film (and ultimately on paper), which he describes as the characteristic curve of the film. He devotes a large part of a chapter and a full appendix explaining these curves and how he and his assistant determined them for each combination of film type and development regime he used. It made me realize that there are large gaps in my knowledge of how my camera responds to light intensities, and that I really have no idea how well the monitor on my laptop renders intensities, except that I think it is not very good when I compare it by eye to other laptops. This post explores the first part of the problem, determining the relationship between exposure and the mean value in the RAW and JPEG output of my D300.

Actually it's pretty easy to determine the characteristic curve with a digital camera, you just have to take a bunch of photos of a uniformly illuminated screen, ranging from severely under-exposed to severely over-exposed, and determine the mean of the values in the image file. The D300 makes it easy to take both a RAW and JPEG files at once, which means you can work out the characteristic curve for both types of files with one set of shots. Alternatively if you have a different workflow, converting from RAW to JPEG using external software, you can use that also. To remove any headaches, it is probably best to set the white-balance to PREdefined and take a WB measurement off the screen before starting.

The plot shows the response of the RAW images (mean value in the image) over a 10-stop range. It is clear that the log of the mean signal in the RAW image increases linearly with the exposure in stops. A "zero" stop exposure, i.e. the exposure that the D300 metered as correct, and which should correspond to a middle gray exposure, creates a mean signal of approximately 300 in the RAW image. The sensor maxes out at 4096 (evidently a 14-bit sensor) about 4-stops over middle gray and reaches about 8 at 5-stops under middle gray.

The RAW data can be shown in a more useful form by plotting the y-axis as a base-2 logarithm relative to its "middle gray" value of 300, as above. In this form an increase in one unit on the y-axis corresponds to a doubling of the mean value in the RAW file, just as the exposure on the x-axis increases by a factor of two for each 1-stop increase. In this form it is clear that doubling (or halving) the exposure corresponds to a doubling (halving) of the mean value in the RAW file. The response is linear over an impressive 8-stop range, and possibly more.

The characteristic curve of the JPEGs produced by the camera is shown above. This curve has a 4-stop region around middle gray in which the response is roughly linear from an approximate value of 50 to 225. At lower values the response curves over, with the darker exposures being compressed into values below 50. At exposures 3-stops above middle gray, the curve saturates abruptly at a value of 255, unlike film which has a graceful rollover and some continued response even at high levels of over-exposures.

Another interesting thing that can easily be measured is the response of the camera to a middle gray exposure under different ISO settings. In the JPEG, the response is roughly constant, with the exposure being decreased as the ISO is increased, to maintain the middle gray level in the JPEG. The interesting thing (perhaps) is that it also remains roughly constant in the RAW file, as shown below, which means that the high ISO mode is implemented on the camera using a variable gain amplifier in the analog section of the CMOS sensor, rather than the camera digitally amplifying the value produced by the ADC.

Well that's it for now. Hopefully by next week I'll be able to measure the response of my laptop screen to fixed intensity images. That should be easy enough to do by photographing the screen of the laptop, since it's clear that the RAW file is very linear in its response to light.

Saturday, October 4, 2008

Debugging a serial (RS232) datastream with a digital storage oscilloscope

I'm finally in Paris, and loving it so far. It's a great city to walk around, there's so much to see. I spent most of my first day here out with my camera, with my home-made GPS switched on all day.. I got approximately 11 hours out of it before the battery died, and it wasn't even totally full to start with. Also the GPS seems to be very reliable now that I moved the wires from in front of its antenna.. it usually acquires its position a few 10s of seconds after it is switched on. After that it consistently re-acquires quickly as I come out of buildings and off of the Metro. I'm very happy with it now! The picture from Pont St Michel (above) was geotagged by my D300 with the following information:
  • Latitude: 40° 51.230' N
  • Longitude: 2° 20.683' E
  • Altitude: 40m
  • Time: 2008-10-04 13:25:50 UTC
If you build one of the interface circuits (either my design from the last post or one of the others from the web which use BJTs) you will probably want to test it before you connect it to your camera. An oscilloscope is the perfect tool for this. I tested my circuit initially using a 1970s vintage Tektronix 465 scope that I got on eBay for about $250 few years ago. An old analog scope like this does all you need to convince yourself that the voltage levels are right, i.e. between 0V and +5V and pegged at +5V when not transmitting. With an analog scope you can also check that the baud rate is right for the camera. At a baud rate of 4800 the duration of the smallest pulse should be 1/4800 = 0.208ms. You can also probably sell an old scope like the Tek'465 after you are finished with it for almost what you paid for it.

If you have access to a digital storage oscilloscope (DSO) or a USB oscilloscope, such as those made by Picoscope, you can also go one step further by decoding the serial datastream and making sure the information from the GPS is correct. If needs be you decode the RS232 stream directly from the GPS and the CMOS-level stream coming from the interface circuit. All the Picoscopes allow you to record the waveform and save it to your computer where the serial datastream can be decoded and examined. I used the Picoscope 3205 that my dad got me as a moving gift to save ~200,000 samples from the waveform generated by the interface circuit and wrote a small PERL program to convert it to ASCII. The figure below illustrates how first two characters in the serial data are encoded. The x-axis shows number of milliseconds since the time that the oscilloscope triggered at - it was set to trigger on the first transition from +5V to 0V. The y-axis shows the voltage at the output from the interface circuit.
The data is transmitted with an asynchronous serial start-stop format, in which the start of a character is indicated by the transmitter using a start bit, and the end of the character using one or more stop bits. In the figure above, the transmission is initially in the idle state, which has logic 1. The transmitter signals the start of a character with a logic 0 start bit, indicated by the first "s" in the figure. It then transmits the eight data bits, from least significant to most significant, indicated by 0-7. Finally it transmits two stop bits with logic 1, indicated by "S". The second character starts immediately, with the same sequence. The first two characters are:
  1. LSB-MSB: 00100100 = Decimal: 36 = ASCII: $
  2. LSB-MSB: 11100010 = Decimal: 71 = ASCII: G
which are the first two characters in the NMEA data messages. I wrote a small program in PERL which converts a set of voltage measurements into ASCII data. The data should be in a file with two columns, the first being a time measurement in milliseconds, the second a voltage. The script and two example files captured using my Picoscope are available on my website. For example, using the script on one of the example files (nmea2.txt) gives:

$GPGGA,152917.000,5317.5456,N,00614.3536,W,1,04,4.8,62.3,M,54.8,M,,0000*73
$GPGSA,A,3,16,06,23,31,,,,,,,,,6.3,4.8,4.1*3C
$GPGSV,3,1,11,16,56,290,36,24,46,077,20,21,45,147,13,31,45,203,33*77
$GPGSV,3,2,11,29,41,067,15,06,23,245,32,30,16,116,17,23,15,299,37*71
$GPGSV,3,3,11,13,13,328,20,10,12,043,11,03,09,249,22*42
$GPRMC,152917.000,A,5317.5456,N,00614.3536,W,0.37,77.42,220908,,*29

The specification for the NMEA messages are available on the Internet in many places, although they are apparently copyrighted by NMEA, so I won't post them here. For example, you can find the messages relevant to GPS at the end of many of USGlobalsat's manuals.

The PERL script can be used to decode either real RS232 or logic-level serial, at any baud rate. Just change the vales of the configuration variables in the script to reflect what you need.

That's all for now. I'm not sure what I'll have to write about next time as I don't have any of my electronics gear with me, it's stuck on a boat somewhere between L.A. and here.

Friday, September 26, 2008

Upgrades to the home-made GPS

I've been back in Ireland for just over a week and have had a chance to try out the GPS interface that I wrote about last time. Also, my dad gave me an amazing USB oscilloscope as a gift for starting my new job (and life!) in Paris. It is great for testing out the GPS interface circuit before it is ever connected to the camera, and I'll have some suggestions for using one to debug the datastream in the next post.

I used the GPS with my D300 at the weekend and I'm pretty happy with it. When I power up my camera with the GPS connected, it quickly starts to receive the NMEA datastream and the GPS light flashes. After about a minute the GPS locks on to its position and the GPS light turns solid. Photos then have the position and UTC time from the receiver embedded in their EXIF data. The photo sharing site Picasa recognises this information and put the photo onto a map automatically, which is nice.

I have a few reservations with the GPS engine and with the interface circuit:
  1. The combination of the D300 and GPS uses a lot of current, and runs down the battery very quickly. This happens because you really have to set the D300 so that it keeps the power on all the time when the GPS is connected, otherwise you will have to let the GPS power down between shots and it will take a long time to reacquire its position. When the power is on the D300's meter is also on, and between them they draw a lot of current. I measured the GPS at 80mA (at least during the period that it is acquiring the satellite lock). The D300 battery is 1500mAhr, which means that it should be able to power the GPS constantly for about 18 hours. It seems that you get more like 3-5 hours (I didn't measure it exactly), so I guess that having all of the camera electronics on must draw a lot of current also. You can obviously turn the camera off when you are not using it and save power, if you don't mind waiting for the GPS to reacquire its fix.
  2. The switch in the circuit doesn't allow the camera to power down as intended. Obviously it disconnects the +5V line from the interface board (by design) but somehow the circuit still keeps the camera awake even though there is no GPS data stream. Best that I can figure, the camera monitors the serial RX line for a transition from +5V to ground (i.e. it looks for the RS232 start bit).. and somehow it detects this when it is powering down and then comes back to life again. When you flip the switch on the GPS off you can see the meter turn off and immediately back on again over and over again.
  3. The GPS engine took a long time to lock on the first time it is turned on in a while... and occasionally won't lock on at all. Initially I though there was a problem with its backup battery.. but it turns out that it is not a battery at all but a 0.22F "supercap".
The first two problems can be solved by changing the way that the GPS is powered, either by powering from the VBATT (brown) line from the camera or from an external battery pack. With this change there is no need to leave the camera meter on permanently, i.e. you can enable "Auto Meter Off" mode again and the camera will shut down its internal electronics after a short time of inactivity. Since the GPS engine (BR-355) specs call for a maximum of 6.5V, and the Li-Ion battery in the camera is 7.4V, a +5V regulator should be used to reduce the supply voltage, as shown below. I used a TS7805 which is all that I could find in Maplin in Dublin over the weekend. This 1A regulator is really overkill, and also requires a 2V clearance, meaning it will only deliver +5V while the battery voltage is >7V. A smaller (100mA) regulator would be better, but they were out of stock.
This circuit fixes the first two problems perfectly. The switch on the GPS now controls the power to the GPS engine. When it is on the engine draws power from the camera battery directly, irrespective of whether the camera switch is on. You can leave it on when out shooting and the GPS will stay locked on. The two interface transistors are powered from the camera +5V regulated supply and are only powered when the camera is on. When the camera meter comes on the D300 recognises the GPS stream within about a second, so there is very little shooting delay.

It also seems that the cause of the third problem may have been that I had routed the wires to the switch right over the antenna of the GPS and these were then interfering with the reception of the very weak satellite signals. Moving the wires to the side of the box seems to allow it to lock on much more quickly. I haven't had time to test this rigorously yet though, or to measure how long the GPS will run when powered from the camera battery.

Friday, September 19, 2008

Home-made GPS for Nikon D300

Please read the next blog entry before building this circuit, as it details some important upgrades.

I'm in Ireland for a small vacation, having left Los Angeles (and indeed the US) permanently last week. I start my new job in Paris next week, so I'm enjoying the opportunity do do absolutely no work this week!

One small Nikon project I worked on before I left L.A., inspired by some other blogs, is a home-made GPS which connects to my D300. The GPS is based on a Globalsat BR-355 engine, which can be purchased cheaply on eBay ($25 - Sept. 2008), with a simple interface using two FET transistors and a couple of capacitors (bypass and reservoir) to ensure the signal is nice and clean.

The BR-355 outputs an RS232 serial datastream which carries NMEA-formatted position and time information at 4800 baud. Logic levels from the BR-355 are -5V for logic 1 and +5V for logic 0 (i.e. standard RS232 at 5V). The camera expects +5V CMOS signals, +5V for logic 1 and 0V for logic 0. The interface circuit therefore needs to be an inverter operating between +5V and 0V, which can deal with negative voltages at its input. Others have suggested a simple NPN-transistor with a diode clamp and a couple of resistors, which should work fine. I opted for an N-channel and P-channel MOSFET configured as a CMOS-like inverter. The basic circuit is show below:

Like real CMOS output stages, one of the transistors is fully on and the other fully off when the input is held at a fixed logic level. When the GPS voltage is -5V (logic 1), Q1 is on and Q2 off, so the output is held at the +5V rail. When the GPS is at +5V (logic 0) then Q1 is off and Q2 on, and the output is held at ground. No clamping diode is needed as the N-channel FET (Q2) can handle negative voltages on its gate. Current only flows through this circuit when it is switching from one logic level to another. At some cross-over voltage (between 0V and +5V) both transistors conduct equally and current flows between the rails. This happens only very briefly as the GPS voltage slews between its two states, but can give rise to spikes on the output line. The bypass and reservoir capacitors in the circuit eliminate these spikes (actually, in a circuit this simple only one of the capacitors is really needed, but it's good practice to put them both in). The voltage at which this cross-over occurs is dependent on the characteristics of the MOSFETs used - in a real CMOS gate the two transistors are built so that the switch over is half the rail voltage (+2.5V). With the two transistors shown the switch over is +1.8V, and the circuit uses approximately 1 microamps when fed from the NMEA datastream (compared with the 40-80 milliamps that the GPS itself consumes!).

The circuit and GPS board all fit nicely into a small project box.

The final thing that needs to be discussed is the connections to the GPS engine and to the camera. The BR-355 consists of a GPS engine in a nice molded enclosure with a magnet to attach it to a metal surface. To use the engine in this project I suggest that you extract it from its enclosure - you have to do some cutting to get the cable free. The GPS engine has a small 5-pin connector and comes with cable that you can cut to any length you desire. You can see in the photo above that I left about 2 inches. The wires on the GPS cable have the following functions:
  • RED - +5V supply to GPS,
  • BLACK - Supply ground,
  • GREEN - Serial data (NMEA stream) out of GPS,
  • WHITE - Serial data in to GPS - can be used to send commands to GPS board to change its configuration,
  • SILVER braid - Cable ground.
The BLACK, GREEN and RED should be wired to the connector labelled GPS in the circuit diagram (as pins 1,2,3). I also connected the SILVER braid to the ground point. I left the white wire unconnected as I suspect it is pulled-up internally to the correct voltage.

On the camera side, the D300 has a 10-pin multi-function connector which is used by various Nikon products, such as the MC-30 shutter release and the MC-35 serial converter (Nikon's device for use with GPSs etc.) You can also get this connector on various non-Nikon products, such as the cheap shutter releases that you can get on eBay, but it does not seem to be available through the major electronics outlets, such as Digikey.

Initially, in my desire to be thrifty, I got a couple of these cheap shutter-releases from China with the hopes of using one of them, as described in epicblog. The two I bought both had 10-pin connectors but to save money they use 4 full size, active pins in the connector, to which wires can be soldered and 6 small stud pins, which do not penetrate the plastic molding at the back and so cannot have wires attached. This necessitates dismantling the cable and moving the active pins to different positions and re-soldering the cable. In the process of removing some of the stud pins by pushing them through the plastic molding they broke off. It seems that (at least) the stud-pins are quite brittle - I tested this on some of them that I successfully removed. I decided that, rather than risking having a pin break in the connector on the camera (which would be a disaster!), I would not use the cheap connector at all.

The next cheapest way to get one is to bid on any of the Nikon MC-?? accessories that has one, as it seems that all these Nikon products have connectors with 10 active pins and a high quality 10-wire cable. I got a Nikon MC-22 on eBay for about $35 and chopped off the banana clips that were on the end, leaving just the beautiful 10-wire cable and 10-pin connector.

A diagram of the pins on the connector and table of the function of each pin, and the colour of the wire that corresponds to it on the MC-22 I purchased is shown below.

  1. RED - RX - serial data in to camera
  2. BROWN - Battery voltage (~6V) - on all the time
  3. GRAY - Regulated voltage (5V) - this voltage is powered when the camera meter is also on. The D300 can be configured to keep this powered on whenever a GPS datastream is detected, so that the GPS stays on.
  4. PURPLE - Release shutter - the Nikon shutter release (MC-30) shorts this to ground when the button is pushed all the way in.
  5. ORANGE - unknown
  6. YELLOW - Signal ground (to regulated supply)
  7. GREEN - Battery ground
  8. BLUE - TX - serial data out from camera.. does the D300 use this line?
  9. WHITE - Enable meter - MC-30 shorts this to ground when the button is pushed half way.
  10. BLACK - unknown
I connected the YELLOW, RED and GRAY wires to the three pins on the CAM connector (again as pins 1,2 and 3 respectively) in the circuit above. I strongly advise you to verify the function of each of the wires on any cable you want to use, as I cannot accept responsibility for any damage you do to to your camera by using the information in this post, I am simply reporting on what I did.

With everything connected, turn on the camera and set the "Auto meter off" option to OFF (in the GPS menu), so that the power to the GPS remains on all the time that the camera itself is switched on. You should see the GPS indicator flash and eventually stay lit, showing that the camera is receiving the datastream and the GPS has locked on.

That's enough for now. Next time some thoughts on how well the circuit works in real life, further refinements and some options for debugging the data stream with a DSO (if you have one!).

Saturday, September 13, 2008

Summary of strobe project

I'll be moving from LA to Paris (via a 2 week holiday in Ireland), and I likely won't be doing anything more on my strobe measurements until all my electronics gear arrives in Paris, assuming it doesn't sink into the Atlantic. Here's a summary of where the project is:
  1. Understand the Nikon CLS protocol - not started yet.. I need to get a digital oscilloscope to work on this. I have my eye on a USB scope from the UK company Picoscope.
  2. Understand the response of the Nikon strobes to TTL control signals - some measurements made. Seems like it should be possible to calibrate a controller to generate fixed power settings (1/2, 1/4 etc..) to emulate a Nikon CLS wireless flash in manual mode. This calibration would need to be changable so that it could work with any Nikon strobe.
  3. Design and build a controller that can be connected to a Nikon strobe to make it compatible with the Nikon optical "wireless" protocol - not started.
  4. Design and build a radio frequency controller to do CLS over radio - a long way off!
Obviously there is still a lot to do. Hopefully I will get back to it in the new year.. or sooner if I can get a DSO before then, as point 1 may not really need all of my electronics gear.

Before I leave I hope to post something about interfacing a Nikon D300 with a serial GPS module.

Friday, September 5, 2008

Flash pulse vs. quench delay

Likely the best way to measure the amount of light emitted in the flash pulse is to use a flash meter. These are calibrated devices which measure the absolute amount of light in a flash pulse. They also cost a lot! Home-made solutions, based on a cheap photo-diode, are also possible. A photo-diode is a device that produces a current that depends on the intensity of light falling on it. Assuming that the flash pulses all have the same spectral profile, i.e. that all pulses have an equal mix of colours, but differ only by the overall amount of light produced, the photo-diode will produce an electrical pulse with a charge proportional to the amount of light in the flash pulse.

A nice method too read out the photo-diode would be to digitize the flash pulse intensity vs. time profile using a digital storage oscilloscope (DSO) and integrate the amount of light in software, but again, you need an expensive DSO to do this. A cheap solution is to integrate the amount of light in the flash pulse using an electronic integrator and then read out the result with a volt meter. An integrator is a small circuit using an operational amplifier chip, which stores charge in a capacitor, producing a voltage that is proportional to the total amount of charge that flows into it, ideal for this application. Pretty much any op-amp will work, but those with FET input transistors work best, since they draw almost no input current and have very low bias and offset currents, to which integrators are particularly sensitive. A typical integrator circuit is shown below.


I used the Texas Instruments TLC27M2A1 opamp in this circuit and was very happy with the results. The charge in the pulse is integrated while switch SW2 is held closed. The pulse size is read as a voltage at OUT. Switch SW1 clears the charge on the capacitor, zeroing the integrator for the next flash pulse. Ideally D1 is a photo-diode, but any regular LED will also work. To cover a large range of flash pulse sizes it is convenient to be able to change the value of C1 - an IC socket into which the capacitor is plugged makes this easy.



The plot shows the integrated charge from the photo-diode for a Nikon SB-25 and SB-600 strobe set in manual mode. The strobes were set to full, 1/2, 1/4, 1/8, 1/16, 1/32 and 1/64 power and the photo-charge measured at each setting. The charge is plotted in stops -- a change of one stop corresponds to a factor of two change in the detected charge. With the exception of the full power setting on each strobe the amount of charge is linearly related to the manual power setting to within a fifth of a stop. For each strobe the full power setting is almost a half a stop above the expected value. I have no explanation of why this is.. my guess is that it is difficult for the manufacturer to control the discharge of the capacitor at the full power setting, and this is the best the could do.



The results of using the a time delayed TTL quench with the SB-25 and SB-600 is shown in the second graph. The plots shows the amount of photo-charge measured as a function of the delay between the trigger and quench signals in milliseconds. The flash pulse size is again shown as a fraction of the full-power pulse in stops. A few things are immediately obvious:
  • The SB-600 can produce flash pulses over an amazing range of almost 14 stops. Fourteen stops is equivalent to ~1/16000! The lowest few points were hard to measures and there is considerable measurable error in the data.
  • The SB-25 seems to flatten out at small quench delays. Presumably the electronics in the -25 are not able to stop the current flow from the capacitor quickly enough to produce very dim flash pulses.
Well, that's enough for now. Next time a summary of where the project stands, and possibly also a small diversion into interfacing Nikon cameras with a GPS.