Some code taken from my BSc project. If anyone can look for improvements on how I carried it out that would be great.