Posterous theme by Cory Watilo

Presentation Quality Plots from Micro-Cap Data (Mathematica help for EEs)

by

Simulationplots

This is the first in a series of posts of things I do frequently in Mathematica 7 (used to use 6) as an Electrical Engineering student that makes my life easier. I am going to stay short on details as most of this should be like Oh I can just do that! But if you really have a question you can ask in the comments but no guarantee that I know why said piece of code worked. A lot of these I have been using for a couple of years now as once I figure out the "how" I just continue to use the solution as a template.

I might do another tutorial on how you export data from Micro-Cap but lets assume you have already exported the data to a text file. The text file is a giant list of data points and it doesn't use standard separators between columns nor does it use scientific notation like E-05 etc, instead it uses MEG, K, etc. So to be honest I didn't have the time to figure out how to parse all that non standard crap with Mathematica so instead I used sed and awk, but I called them right from my Mathematica notebook. Thats right you can send normal command line commands right from your notebook. Oh BTW I hate MS Windows and could care less about this working for Windows users, but if you are a windows user you can perhaps install sed and awk or use something else, all the same OS X and almost every Linux install come with sed and awk. (I run micro-cap from Crossover, essentially a paid version of Wine).
Example Data:

Waveform Values
===============
F abs(v(v3)/i(v3))
(Hz) 1.000 48.735K
1.019 48.692K
1.038 48.649K
1.057 48.606K

Mathematica Code:

outputz=Import["!sed -e '1,4d' -e 's/m/E-03/g' -e 's/u/E-06/g' -e 's/n/E-09/g' -e 's/p/E-12/g' -e 's/f/E-15/g' -e 's/K/E+03/g' -e 's/MEG/E+06/g' -e 's/G/E+09/g' -e 's/T/E+12/g' < ~/Dropbox/lab2/simulationdesign/outputz.txt > ~/Desktop/tmp.txt ; awk '$1=$1' ~/Desktop/tmp.txt | tr ' ' 't' ; rm ~/Desktop/tmp.txt","TSV"];

So the starting "!" tells Mathematica that it should run the command as an external operating system command (search the help for "Run" ). Then the '1,4d' deletes the first 4 lines of the file (you will need to modify this as this varies depending on the simulation / output data setup) as those lines are crap (but you need to remember which column is which, i.e. Frequency and Impedance). Next are a bunch of find and replace statements, these replace the prefixes with standard E+03 etc notation. Anyways I will let you figure out the rest, just remember to change your paths as necessary. Once the script is done Mathematica will import that data as TSV (tab separated values) and it can then be accessed by the name outputz. The code used to generate the plot is just:

ListLogLinearPlot[Thread[{outputz[[All,1]],outputz[[All,2]]}],Joined->{True},PlotLabel->"Output Impedance (Magnitude)",Frame->True,FrameLabel->{"Frequency (Hz)","Ohms"},GridLines->Automatic,Axes->False,PlotRange->{{1,10^8},Automatic},ImageSize->500]