Skip to main content

Posts

Showing posts with the label numpy

Analysis of a 3 degree of freedom building in an earthquake with scipy

This is my building, I want to know how much each floor moves in an earthquake... Also it would be nice to know the acceleration experienced by each floor. First off we need some data. I have an earthquake data file containing ground acceleration data from the Kobe earthquake  of 1995. It is a matlab file, so the data will have to be extracted into a numpy format. Scipy has an io module which contains a matlab submodule. Since we want to visualize this data somehow, the pylab package will also be used. Firstly I'll make a small helper function that enforces complex symmetry, useful for after the frequency domain analysis: from scipy.io import matlab as mio from pylab import plot , show , ylabel , xlabel , title , figure , legend , annotate import numpy as np def enforce_complex_sym ( array , nyquist ): '''Enforce complex symmetry''' array [:, nyquist + 1 :] = np . conj ( array [:, nyquist - 1 : 0 : - 1 ]) Next I'l...

SciPy-Simulator

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 thre...

Gaussian Blur

Trying to work out why the gaussian blur in OpenCV is different from that of SciPy... The differences are too small to see, but are still there. Following is an imshow of each channel of the difference image. And by looking at a singe row, we see that the difference spikes the whole intensity range. Edit: That was pretty much staring me in the face wasn't it... uint8's are prone to integer overflow! Comparing the OpenCV implementation with two versions in SciPy now gives: Doing a pixel for pixel comparison on each channel between the SciPy and OpenCV examples: The second one is comparing an IIR filter implementation to a ndfilt.

Decoration to the rescue!

import numpy as np from scipy import signal from opencv import adaptors class scipyFromOpenCV ( object ): """This decorator can be used to wrap a function that takes and returns a numpy array into one that takes and retuns an opencv CvMat. """ def __init__ ( self , f ): self . f = f def __call__ ( self , image ): # Convert CvMat to ndarray np_image = adaptors . Ipl2NumPy ( image ) # Call the original function np_image_filtered = self . f ( np_image ) # Convert back to CvMat return adaptors . NumPy2Ipl ( np_image_filtered ) @scipyFromOpenCV def slowGaussianBlur ( matrix ): """Manual gaussian blur - Very very very slow!""" filterSize = 3 filt = gauss_kern ( filterSize ) r = signal . convolve ( matrix [:,:, 0 ], filt , 'same' ) g = signal . convolve ( matrix [:,:, 1 ], filt ...

Spyder

Enter the new matlab contender, Spyder: This would have been pretty handy over the last summer! All the pylab, scipy and numpy docs. The matplotlib integration is key. Eventually it would be awesome to see pydb and stepping through python scripts like you can for m files in matlab. But this is a huge step in the right direction! This is also my first post from Windows, good to see that Python(x,y) has been coming along nicely since I last looked at it. I will see how this Pydee (renamed to Spyder) goes on all three platforms that I now use.