Showing posts with label sidenotes. Show all posts
Showing posts with label sidenotes. Show all posts

Sunday, 3 September 2017

Let Python do the job for you: AutoCAD drawings printing bot

Recently I’ve been getting familiar with AutoCAD and at the same time I’m trying to improve my Python skills. Odd mix, huh?!

While trying to improve my Python skills I thought I could exercise myself on automating boring tasks. I remembered that a few years ago I was given a boring job which involved printing a lot of drawings directly in PDF format from Autocad. I was too lazy to print the drawings one by one and I knew the process could be automated. At the time I put together a Python script that did the job fine, but it was a bit messy. I thought I could make an improved version.

When nice APIs are not available, such as in the case of AutoCAD (at least that was the case a few years ago, nowdays things may have changed), using Pyautogui may help in the task of automating boring tasks.

Sunday, 10 May 2015

Spectrogram representation of the B note from a guitar

A spectrogram is a visual representation of the spectrum of frequencies as they change with respect of another variable (in our case time). Matlab offers a great function to plot the frequencies against time, therefore I decided to try it on the recordings I used in the previous post. Here is the result I got

untitled

Now, some comments. This representation of the frequencies tells us that the stronger components of our signal are below 1.5 KHz (1500 Hz) which is what we expected, however it looks like there are no other significant frequencies above 7 KHz and this is weird since in our FT of the signal we found higher frequencies (although with lower magnitude). Notice how the the higher the magnitude in the FT, the longer the frequency appear in the spectrogram.

Now, by using a simple tool I generated another spectrogram which seems to contain also the higher frequencies the one in Matlab did not have.

B.wav

This spectrogram is better in tune with our FT of the signal, since it shows that our signal has frequencies up to 15 KHz as the FT showed

Immagine 3

The program I used to generate the second spectrogram is Spek, it is free, and fast, however it has very few options. Below are some useful links

http://spek.cc/

http://en.wikipedia.org/wiki/Spectrogram

Calculate Fourier transform of a gaussian using Matlab

While wondering around in the Matlab documentation I found out there is a simple way to calculate the Fourier transform of any function using Matlab. Furthermore by using the function pretty() you can print out the result in a more friendly manner.

Let’s calculate for example the Fourier transform of the function u(x) = e^(-x^2). This calculation could also be done on paper: by applying the definition I wrote in the earlier article one finds out that:

Immagine 1 Now, by using some properties of the transform (you can check them at http://en.wikipedia.org/wiki/Fourier_transform), we can play around and plot some functions. Here is the original function and its FT:

Immagine 001

Let’s take the FT of e^(-(3x)^2)

Immagine 002

It’s worth noting that when the function gets thinner, its FT gets more spread and viceversa. This phenomena is useful to understand the uncertainty principle.

This you tube video explains the link between this mathematical tools and physics.
https://www.youtube.com/watch?v=V7UNvDN_EZo

If you’d like to know more about the FT, check the wikipedia page:
http://en.wikipedia.org/wiki/Fourier_transform

Here is the Matlab code I used

Sunday, 22 February 2015

Another Excel spreadsheet: Savings with LED lights

I recently uploaded a new Excel spreadsheet where I made a calculation of how much can be saved by replacing neon tubes with LED tubes.

Immagine 1

As you might have noticed, I do like using Excel for calculations too, and I set up a page where some of my little projects and calculations with Excel are shared. Here it is: Excel page.

Thursday, 8 January 2015

Some plots with matplotlib

I recently updated matplotlib and could finally use the style attribute from the updated module. I thought it might be a good time to plot some data and try it out.

In order to get a grasp on how some emerging markets have performed so far, I thought I could plot some data on the real GDP (GDP adjusted for inflation) and perhaps compare it to a big part of the EU’s economy (Germany).

The data I used is from Quandl a great source of data you should check it out https://www.quandl.com/

figure_1

gdp_growth

performance_relative_toGermany

Note: dotted lines indicates that the data is prospective (in this case obtained through linear regression).

Hope this was interesting

Friday, 15 August 2014

Arduino module GUI (Beta version)

Hi everyone! I have just completed a first, very basic GUI to get the Aduino module (which by the way you can find here) more user friendly.

The GUI is still a “Beta” version since I have created it with PyQt4 which I started learning only 3 days ago. I bet there are plenty of features which could be improved. I am probably going to revise and update this GUI, however here is a first “raw” version which, as far as the connection and communication with Arduino Uno is concerned, works fine.

This application is for educational purpose only, any commercial purpose is excluded.

You can download the executable for windows here. For Mac and Linux users, the source code is included in the zip folder however I am not sure the program will work since I have no experience with those operating system and their USB settings. Feedbacks are much appreciated.

Here are some screenshots and some useful information:

This is the main screen. The three buttons at the bottom essentially sum up everything that this program should help you doing.

Immagine 001

First of all, you need to connect Arduino Uno to the USB port and load your program in.
Then you can click on connection on your GUI and click “Set up connection”. The default port is com3 and 9600 baud. You can easily change these default settings in the connection menu available in the GUI.

Immagine 002

Once the connection has been established you can start interact with Arduino Uno.

By clicking on the button “Read from Arduino” the program asks you how many lines to read, there’s still no default values however I suggest to read not more than 100 lines since the program might slow down or crash.

Immagine 003

By clicking on the button “Send to Arduino”, the program asks you to enter the data to send. You can enter one of the three data types showed below in the following form:
1. Integer: 2
2. Character: b
3. List: [2,3,4,5,6,7,8]

The list can be as long as you want it to be.

Immagine 004

Hope this is useful.

Sunday, 20 July 2014

A small sidenote on cx_Freeze, converting (GUI) scripts into exe and the annoying dos window

Hi everyone!

As you know, Python scripts need to be interpreted in order to run on your computer. Let's say that you want to give your brand new program to your friend who, for some reason, is reluctant to install Python and does not like using the command line or related things. Well then building a simple GUI (Graphical User Interface), with Tkinter for instance, is the right choice. However, we have not solved the user friendliness issue since even with a GUI, our script needs to be run with Python. The solution then, is using the module cx_Freeze to "convert" our script into an executable file (.exe) which can be run on windows.
I have been using cx_Freeze for just 3 months I guess, and I find it practical and useful. There are other modules which serve the same purpose, however some of them (py2exe for instance) are still not available for Python 3. More information on http://cx-freeze.sourceforge.net/

Here are the main steps to convert your .py program into an .exe:
1.Check that your script works fine
2.Create a setup.py file as below
3.Put setup.py and your script in the folder pythonxx (python33 if you have python 3.3)
4.Open the command line (cmd) and go to the pythonxx folder
5.Type in: python33\python.exe setup.py build
6.Press enter. Python will build your exe and put a "Build" folder in your python33 folder.
7.Check out the Build folder in python33 where the executable lies. There you go!


The setup file should contain this (sqrt.py is the script to converted):
from cx_Freeze import setup, Executable

setup(
    name = "sqrt",
    version = "0.1",
    description = "test",  #you can put here whatever you want
    executables = [Executable("sqrt.py")],
    )


Once you have had fun with your new executable script, if your script had a GUI interface, such as Tkinter, you will find an annoying thing: the dos window!!! It will not go away no matter what you do. It just comes up every time you run the .exe. The solution to this problem: you need to edit the setup.py file and reconvert the script. Here is the setup.py if your script has a GUI such as Tkinter:

import sys
from cx_Freeze import setup, Executable

base = None
if (sys.platform == "win32"):
    base = "Win32GUI"


setup(
    name = "sqrt",
    version = "0.1",
    description = "test",
    executables = [Executable("sqrt.py",base=base)],
    )

And your dos window will not appear every time you run the .exe. I had a hard time finding this and I hope it will be useful to someone.
Enjoy!