Software MCA for DSO and linux

For PC type software that runs under some PC opsys.

Software MCA for DSO and linux

Postby Doug Coulter » Wed Jul 24, 2013 12:06 am

This is just a teaser - I'll publish (GPL v2) the code once I've worked out a few more bugs, but this is fantastic. I'm using a GDS 1022 DSO scope (eg, the cheapest one on the market) to acquire data (in this shot, just from my signal generator) and make energy spectra from it - as good as those multi-thousand dollar units as far as I can tell.
A few more days or so of work on this and it'll be done and a handy tool in the lab. Most of us have scopes that talk this protocol (GW-Instek and tektronix) already, and a PC of some kind. Sorry, this is linux-only at present. You could run it in windows if you have perl, the right perl modules, and gnuplot - all available for windows, but if you're going to go to that much effort, let me reccomend getting Virtual Box and say ubuntu 12.04 or Mint 15 and just run it there - VB lets you have access to USB devices, and this doesn't hog a CPU all that badly...

What it does is periodically screen grab the scope - you set the scope up for a good looking signal from your detector, and you're good to go here. I give you a raw scope plot so you can get the numbers to set up the MCA parameters for your situation, and I do some "tricks" in signal processing to improve the resolution a normal cheap DSO has, the picture tells all. Those tiny peaks are bugs - I'm working on that one, but it's after 1 am...so another day. The other peaks are simply what happens if you change the scope gain during a run on the same signal, just so you can see how well this works otherwise. Heck, I'm impressed even if it was me doing it.
Screenshot-3.png
Screenshot of the two plots this makes, with the program GUI on top of one of them - simple, eh?
Posting as just me, not as the forum owner. Everything I say is "in my opinion" and YMMV -- which should go for everyone without saying.
User avatar
Doug Coulter
 
Posts: 3515
Joined: Wed Jul 14, 2010 7:05 pm
Location: Floyd county, VA, USA

Re: Software MCA for DSO and linux

Postby johnf » Wed Jul 24, 2013 4:15 am

Awesome

there had to be a better way!

keep at it Doug

watching!!!!!!!!!!!!!
johnf
 
Posts: 433
Joined: Sun Aug 08, 2010 3:51 pm
Location: Wellington New Zealand

Re: Software MCA for DSO and linux

Postby Doug Coulter » Wed Jul 24, 2013 10:02 pm

Yup, there had to be a better way. A soundcard is just too slow - if you "shape" the signal enough for it to see it, there's just too much time-smear to handle any decent count rate - pileup kills you at even pretty low rates, and after all that low passing, it's hard to detect, it just smears everything out in the result. Not to mention the impulse reponse of a delta-sigma a/d is well...interesting (and usually over 40 samples long at a mere 48khz sample rate). Though you get fewer bits with the scope (it's about 9), you get a lot more samples, and you need no shaping at all for an NaI-class signal - you can adjust to get quite a few samples during each pulse to integrate the energy over.
While this particular scope won't quite do for a fast plastic scintillator on a fast phototube - well, those have horrible resolution anyway, since often much of the energy compton scatters out of the plastic.

Not having a nice cryo germanium detector, I have no clue what those signals look like, but if they are within what a scope can capture, this should do fine. You just need to set the sweep speed fast enough to capture a number of samples per pulse so the integration trick (which is really summing) doesn't have too many errors. Done right, you actually get more bits resolution than the native a/d gives.

Here, all I'm doing is detecting each pulse that starts below threshold, goes above it - integrate (sum) all the samples above threshold, and call that the "energy" which I use to decide which bin in a histogram to increment. The width minimum and maximum are designed to help with noise and pulse pileup situations. Evidently, there's an edge case I missed in the little state machine I coded to do all that; the capture from the scope itself now seems perfect - which turned into quite the chore on its own. It seems the GW-Instek imperfectly reverse-engineered the tektronix protocol, and the guys who wrote GDSH (a bash shell that also talks "scope") added another layer of not quite getting it. The upshot is that these scopes are quite picky in how they are talked to - too fast and they choke, too slow and something crashes - I got quite a bit of excercise getting up to power cycle the scope whilst working out that part of things. I don't bother to ask the scope about its timebase or vertical scaling, since that doesn't really matter here - you gather the pulse threshold, max and min width data simply by stopping the thing and looking at the raw scope plot, which is in 9 bit binary vertically, and sample number horizontally - gnuplot tells you the H and V numbers for where your cursor happens to be, so you can pretty quickly read that off a sample capture and set up the real MCA numbers.

I'm aware there are some theoretical issues with this approach. Pulses that just barely break theshold will add up to smaller numbers, since some of the pulse was below threshold - and it's a larger fraction for a small pulse than a large one, so the horizontal scale will be slightly "exponential" - small things will be off to the left, and large things off to the right, a little bit. I'll be testing with a real set of sources as soon as I get satisfied that I have that glitch handled on a "perfect" signal from my signal generator first - for example, the two main peaks from CS-137 are pretty far apart - one very low, one at 662kev. I might figure a way to linearize against that, we'll see - the code is actually pretty compact as of now, but then I do have a lot more freatures planned eventually.

I feel like I'm not losing much of anything here by only grabbing chunks of data - of course I have to ditch pulses already in progress at the beginning of each record, and those that don't end by the end, and I miss some data entirely during the time I'm analyzing the last record and waiting for the scope to send me the next at it's own sweet speed (not that fast). But since radiation is random as it gets, timing wise, as long as I get enough - I should get good spectra. If what I'm trying to measure has a periodic component - say 120hz ripple on a fusor power supply - I could get into issues with it "beating" so to speak, but we'll just have to see about that. I don't think it's going to be much of an issue in real practice.

I'm doing this because the last few MCA's we've bought off ebay were terrible or broken (notice no one selling one admits to being able to test it?), and I used up my loan time on the one good one I had for a bit before I did the experiment I wanted it for. Should have just used that money to buy a new one I suppose - it about added up to that, with nothing to show in return. And that money is now gone, so I'm doing this, which costs me nothing but time - I have the gear already. Seems most labs would have both a DSO and a PC, right?

The measurement I want is this - we know fusors make a bunch of X rays at the power supply energy level, at least. There should also be some from neutron capture in H in moderators at around 2.2 Mev, or 570kev if in boron. But not all fusion reactions make neutrons, some make fast protons (the one that also makes tritium). Those should also produce some gammas somewhere in the middle of that range, and provide a way to measure the relative ratios of the reactions vs conditions, something no one else seems to have done - all assume everything is thremal, which in my case, is not a good assumption. There's also that interesting 3rd DD reacion that goes straight to 4He and should be producing about a 16 megavolt gamma....I want to see that one! You know what happens in astronomy every time you give those guys a new capability, right? New discoveries flow.

And I wanted a tool that if it broke, I could fix it. Even the good one we had borrowed fried. Had that happened a few years down the road - just try and get an old MCA fixed, they are such a low volume thing the manufs drop them like hot potatoes.
Luckily in that case, it was the internal HV supply that let out the magic smoke - and I wasn't using it anyway. But you know what I mean. You get one of those old NIM things, and all the parts are proprietary hybrid chips no longer made, and no one will give you a schematic either, so you're hosed. This way all you need is a working DSO - almost all of them speak the same language - and a sensor (with its own power supply - trivial with a CCFL inverter and a voltage regulator) and PC. Well, for this the PC has to be running linux (or windows with a crap-ton of add-ons), but you could do this on a windows box with Virtual box - which does give the guest opsys access to USB. It really isn't taxing the PC very much at the speeds I'm currently grabbing scope screens - the scope is more the limit there - even though I'm writing this code in an interpreted language (perl). Which after all, is at least several times faster than any other interpreted language as things sit. Maybe half as fast as C on the metal, but a lot less wordy to get the job done.

For the curious, here's the code as it sits, error and all (but don't run this assuming I've fixed it - I'll fix it and post it on this thread later when I do).
mcas.zip
Code, complete with a bug or two
(7.01 KiB) Downloaded 341 times

This won't make much sense unless you open it with something that syntax-highlights perl (gedit, geany, padre, and many other editors). Then it's pretty obvious - I code perl like it was C with enforced bizarre hungarian naming rules (it's that flexible to let you do that). That's because C is really my native language. But for things with GUI's and more than one thread - perl's a lot easier if you don't really need much bit-banging at the systems level.
Posting as just me, not as the forum owner. Everything I say is "in my opinion" and YMMV -- which should go for everyone without saying.
User avatar
Doug Coulter
 
Posts: 3515
Joined: Wed Jul 14, 2010 7:05 pm
Location: Floyd county, VA, USA

Re: Software MCA for DSO and linux

Postby Jerry » Thu Jul 25, 2013 10:56 am

I wonder if it would be possible to do the same thing for a Tek scope like the TDS-340A with GPIB or serial. These scopes are 500MS/s and 100MHz bandwidth. Best thing is they sell for $200 or less.

The programming manual is here:

http://www.tek.com/manual/tds340a-tds36 ... mer-manual
Jerry
 
Posts: 573
Joined: Sun Jul 18, 2010 12:07 am
Location: Beaverton, OR

Re: Software MCA for DSO and linux

Postby Doug Coulter » Thu Jul 25, 2013 12:17 pm

Couldn't read that manual without joining (anyone want to download it and post a copy here?), but almost certainly.

I'm using a tiny subset of scope commands, and doing even this USB one through a software USB<>serial port emulator ( a perl module called Device::Serialport that makes it look like a normal com port to me). Might have to change a couple of constants - path to the scope in /dev, and the memory length I should expect when I ask for all the memory for a channel, and likely baud rate. The only commands I'm using right now are :stop, :run, and :acq1:mem?. Sadly, those are hard coded at the moment but not too hard to change if things work more or less the same; I believe they do from dox I've seen elsewhere. We're just using the scope as a burst fast a/d here. The only reason I'm even using stop and go commands is so the memory won't change while we're reading it. I'm not sure what resources are available to talk to GPIB these days...finding a PC adapter (and driver) for that is kind of difficult since ISA bus went away, as I found when I tried to find one for some other gear I have here.

Here's the manual for the GDS-1000 series. I believe, but haven't yet checked if the 2000 series would support the same stuff, but it's a good bet, since GDSH does both with no changes (I have a 1022 and the much more expensive/faster 2204). There's just more stuff you can do with the later scope series, but that doesn't matter if we don't need to do it - and we don't.
GDS-1000-U+Programming+Manual+12_16.pdf
Manual for GW-Instek 100o series scopes
(994.43 KiB) Downloaded 358 times


The protocol is pretty simple here. You send the scope one of the commands, with a linefeed at the end, and a ? just before that if it's a request to return data, then the scope spews out whatever - in its own sweet time (usually slow, and after a wait). I'd suspect the data header might be a little different in another scope, but maybe not. In this case, I'm just tossing it out anyway, as it's not useful for this job - and a mixed bag of ascii, binary integers with random-ending packing, and floats also with random-endian packing. Just need to know how many bytes to skip to get to the meat of the data - which is short integers packed big-endian (reverse of intel processors, so I have to flip the bytes).

The main issues I had revolved around how slow the scope's uP is. Most default to around 9600 baud on serial, and that's about the rate they can eat - so if a command is long, and USB is fast, you can swamp the scope's internal command buffer.
Then you have to prevent your read routine from timing out, since even though USB is quick - the scope takes awhile (half a second?) to get started sending data, and then sends it slowly. The same issues would apply whether using USB or a regular com port, most likely.

Currently, I'm only getting a screen-grab about 1/second, due to the slowness of the scope uP. I might double that with more tuning, but that's about it. Luckily for this, it doesn't matter much. Yes, we lose some data - it will take a little longer (has more deadtime) than the expensive pro MCAs, is all. But that should not matter other than how long it takes to get a spectrum of a lot of points. I'm assuming we have a big pulse rate here, since my intended use will have that, so it's not an issue. If you were trying to see something that only counted a couple times a second or less, other tricks - like that software Sesselman promotes that uses soundcard - might be better. But what I'm looking at with mine is hot sources and the fusor - the trouble there is they create counts far too fast for a soundcard to deal with - in fact, faster than even a super-pro digital studio soundcard can handle - max I've seen is not even up to the one sample per count rate speeds - 96khz sample rate, or even double that just does not cut it for this trick.

One nice thing about this - since I do no explicit polling (my waits suspend my process) - this code loads the PC so little I can't even see it on the performance monitor, even with nothing else running - it just sits flatlined at abouple % of CPU use, which is mostly the opsys and perf monitor loading - not this code.
Posting as just me, not as the forum owner. Everything I say is "in my opinion" and YMMV -- which should go for everyone without saying.
User avatar
Doug Coulter
 
Posts: 3515
Joined: Wed Jul 14, 2010 7:05 pm
Location: Floyd county, VA, USA

Re: Software MCA for DSO and linux

Postby Jerry » Thu Jul 25, 2013 1:30 pm

You can easily get GPIB cards in PCI and PCIe as well as others like PC/104 and even PCMCIA.

There are also GPIB to USB adapters on ebay for about $50.

The Tek 340 and its brothers will do up to 38400 on serial. Probably a lot better bandwidth.

You could probably run this thing on a RaspPi.

Programming manual here:

http://www.milton.arachsys.com/nj71/pdf ... Manual.pdf

If you can get it to work you could use the same setup with just about any tek scope with GPIB, they pretty much all use the same protocol.
Jerry
 
Posts: 573
Joined: Sun Jul 18, 2010 12:07 am
Location: Beaverton, OR

Re: Software MCA for DSO and linux

Postby Jerry » Thu Jul 25, 2013 1:38 pm

GPIB data rate on the scope is 200kB/s.
Jerry
 
Posts: 573
Joined: Sun Jul 18, 2010 12:07 am
Location: Beaverton, OR

Re: Software MCA for DSO and linux

Postby Doug Coulter » Thu Jul 25, 2013 6:34 pm

This looks really similar - but some differences. To make it support all that, I'd have to build up some arrays of commands etc and have you choose one to match your scope, it looks like. For example, the command I use to grab chnl1's memory is something like :acq1:mem?\n - which I didn't find in that manual (I only skimmed it really fast). There's probably a similar and equivalent command for those - I'd bet on it. Some of the other commands ARE the same, perhaps most of them.

I could never get the GPIB thing working with an adapter, maybe because there's no linux support for it? I was trying to remote a very fancy HP power supply that's used in my electrochemstry line. Never happened - I just set the thing up manually, which is rather a PITA, since it has a zillion paramters and is bad about correctly remembering setups (maybe it's not working by some definition - Bill found it in a junkyard with a bent faceplate).

We'll have to see where this goes. 38k baud is still on the slow side when you're sampling at mhz, and it may not reach theoretical speeds on either that or GPIB - I've seen things like that go to burst mode quite often - they'll do the rated rate for awhile, then pause for some time, then resume, so the net data flow isn't really what they say the baud (or other) rate is.

Luckily, it doesn't matter that much for this anyway - as long as I can get decently long records (they are 4000 numbers in the cheap GW scope - more than are shown on the scope screen) often enough - I don't care if it might take a little longer to build up a high-confidence spectrum of zillions of counts. The whole point of this is that counts are far too fast for the audio card thing - so even if I only get a fraction, it's still a lotta counts per, and I get spectra in seconds, not minutes or days.

And it's still a hell of a lot cheaper than a "real" new mca...factor 10 at least.

Thanks for the manual - I'll really read it soon and figure out how practical it would be to support more scopes.
Posting as just me, not as the forum owner. Everything I say is "in my opinion" and YMMV -- which should go for everyone without saying.
User avatar
Doug Coulter
 
Posts: 3515
Joined: Wed Jul 14, 2010 7:05 pm
Location: Floyd county, VA, USA


Return to PC

Who is online

Users browsing this forum: No registered users and 1 guest

cron