Skip to main content


For the last few weeks I have been working on a Simulink / Labview / Ptolemy II like program for modelling... all sorts of stuff!! I am interested in modeling with different systems of computation, and of course some engineering problems, mechanical and electrical control etc. Ptolemey II is an open source java based system which a whole lot of people from Berkley have been working on for a number of years - I am not trying to match what they have - or get anywhere near the breadth that the commercial products Simulink and Labview have. But I do think something useful is very achievable - and of course leveraging what I can from Ptolemy II and basing mine on Python - heavily using SciPy, NumPy and MatPlotLib.

So far I have been working on the semantics - in essence creating a new language for defining models by using blocks (or Actors) that carry out a very specific function. An example actor might be a Ramp Source or a Sin Function. Each of these actors run in their own thread and communicate via dedicated Channels - which are based on the thread safe fifo queue implementation in the Python standard library. These base level actors can be composed together to create models, which are also actors in their own right - running in their own thread with all communication occurring through input and output channels.

The final goal is to have a visual interface - drag and drop blocks around, then connect up the model with channels. For now the whole thing is code based, so one of the more complicated models I have made is a simple pulse width modulator generator.

Here is the full code that defines and runs the model:
Created on 13/12/2009

@author: brian

from models.actors import Plotter, Ramp, Summer, Copier, Sin, GreaterThan, Subtractor, Constant, PassThrough
from models import CTSinGenerator
import matplotlib.pyplot as plt
from models.actors.Actor import MakeChans

import logging
logging.basicConfig(level=logging.INFO)"Logger enabled")

def run_multi_sum():
    This example connects 3 sources ( 2 ramps and a random) to a summer block
    The final output AND one of the ramps are dynamically plotted.
    The components are all generating the same sequence of tags so are always
    sim_time = 0.02
    sim_res = 10000
    wire_names = ('ramp',
    raw_wires = MakeChans(len(wire_names))
    wires = dict(zip( wire_names, raw_wires ))
    ramp_src = Ramp(wires['ramp_probe'], freq=500, simulation_time=sim_time, resolution=sim_res)
    sin_src = CTSinGenerator(wires['sin'], amplitude=0.5, freq=50.0, phi=0.0, timestep=1.0/sim_res, simulation_time=sim_time)
    const_src = Constant(wires['const_offset'], 0.5, resolution=sim_res, simulation_time=sim_time)
    offset_sin_sum = Summer([wires['sin'], wires['const_offset'] ], wires['offset_sin_probe'])
    ramp_cloning_probe = Copier(wires['ramp_probe'], [wires['ramp'], wires['ramp_plot']])
    ramp_plotter = Plotter(wires['ramp_plot'])
    sin_cloning_probe = Copier(wires['offset_sin_probe'], [wires['offset_sin'], wires['sin_plot']])
    sin_plotter = Plotter(wires['sin_plot'])

    # Output = sin - ramp
    subtractor = Subtractor(wires['offset_sin'], wires['ramp'], wires['diff'])

    # Want to see when that is > 0
    comparison = GreaterThan(wires['diff'], wires['pwm_bool'], threshold=0.0, boolean_output=True)
    if_device = PassThrough(wires['pwm_bool'], wires['on_value'], wires['pwm_value'], else_data_input=wires['off_value'])
    output_value_on = Constant(wires['on_value'], 1.0, resolution=sim_res, simulation_time=sim_time)
    output_value_off = Constant(wires['off_value'], 0.0, resolution=sim_res, simulation_time=sim_time)
    pwm_plotter = Plotter(wires['pwm_value'])
    components = [ramp_src, 
                  ]"Starting simulation")
    [component.start() for component in components]
    logging.debug("Finished starting actors")'The program will stay "running" while the plot is open')   

    [component.join() for component in components]

    logging.debug("Finished running simulation")

if __name__ == '__main__':

And here is the output:

This relatively simple model has 15 threads, and takes no time at all to run. There is still a long way to go, but if you are interested check out the project homepage on google code:
Or clone the source directly with: hg clone scipy-sim

Popular posts from this blog

Matplotlib in Django

The official django tutorial is very good, it stops short of displaying
data with matplotlib - which could be very handy for dsp or automated
testing. This is an extension to the tutorial. So first you must do the
official tutorial!
Complete the tutorial (as of writing this up to part 4).

Adding an image to a view

To start with we will take a static image from the hard drive and
display it on the polls index page.
Usually if it really is a static image this would be managed by the
webserver eg apache. For introduction purposes we will get django to
serve the static image. To do this we first need to change the

Change the template
At the moment poll_list.html probably looks something like this:

<h1>Django test app - Polls</h1> {% if object_list %} <ul> {% for object in object_list %} <li><a href="/polls/{{}}">{{ object.question }}</a></li> {% endfor %} </ul> {% else %} <p>No polls are available.</p> …

My setup for downloading & streaming movies and tv

I recently signed up for Netflix and am retiring my headless home media pc. This blog will have to serve as its obituary. The box spent about half of its life running FreeNAS, and half running Archlinux. I’ll briefly talk about my experience with FreeNAS, the migration, and then I’ll get to the robust setup I ended up with.

The machine itself cost around $1000 in 2014. Powered by an AMD A4-7300 3.8GHz cpu with 8GB of memory. A SilverStone DS380 case is both functional, quiet and looks great. The hard drives have been updated over the last two years until it had a full compliment of 6 WD Green 4TiB drives - all spinning bits of metal though.

Initially I had the BSD based FreeNAS operating system installed. I had a single hard drive in its own ZFS pool for TV and Movies, and a second ZFS pool comprised of 5 hard drives for documents and photos.

FreeNAS is straight forward to use and setup, provided you only want to do things supported out of the box or by plugins. Each plugin is install…

Python and Gmail with IMAP

Today I had to automatically access my Gmail inbox from Python. I needed the ability to get an unread email count, the subjects of those unread emails and then download them. I found a library on sourceforge, but it actually opened the normal gmail webpage and site scraped the info. I wanted something much faster, luckily gmail can now be accessed with both pop and imap.

After a tiny amount of research I decided imap was the better albiet slightly more difficult protocol. Enabling imap in gmail is straight forward, it was under labs.

The address for gmail's imap server is:

Python has a library module called imaplib, we will make heavy use of that to access our emails. I'm going to assume that we have already defined two globals - username and password. To connect and login to the gmail server and select the inbox we can do: