Python Procedural 2d map generator explanation - python

So i found a certain procedural map generator in Python and I understand parts of it but i'm having a very hard time piecing it together to be able to modify it to suite my needs so I was wondering if it is possible for someone to explain step by step what exactly the generator does. Overall I understand the concept but the way it is written makes it very hard for me to follow the math involved.
The generator is here and some explanations would be welcomed and probably help anyone else trying to learn procedural generation as this example honestly yields beautiful results.

First you must understand how Perlin Noise works.
I recommend you write your own Perlin Noise code, something minimal, then play a bit with it and see the results.
Then move on to more advanced techniques and variants.
Here for example the user has some settings which more or less control the output:
basePerlinValue = (snoise2(float(x)*perlinScale, float(y)*perlinScale, octaves=8, persistence=0.5, lacunarity=2.0, repeatx=2048, repeaty=2048, base=perlinOffset) + 1)/2.0;
Play with them and see how they affect the result.
Octaves is standard Perlin Noise stuff.

Related

Coding solution to code step by step algorithm-solver

This is a rather general question: There is this webpage which lets you calculate Eigenvalues, Eigenspaces, etc... It also gives you step-by-step solutions. Is there any coding language or module (preferably in Python) which makes it easier to code something like this, or must this be coded manually?
Thank you for your help.

Using A-Weighting on time signal

Im trying to solve this for a couple of weeks now but it seems like Im not able to wrap my head around this. The task is pretty simple: Im getting a signal in voltage from a microfone and in the end I want to know how loud in dB(A) it is out there.
There are so many problems I dont even know where to start. Lets begin with my idea.
Im converting the voltsignal into a signal in pascal [Pa].
Using a FFT on that signal so I know which frequencies im dealing with.
Then somehow I should implement the A-Weighting on that, but since im handling my values in [Pa] I cant just multiply or add my A-Weighning.
Going with an iFFT and getting back to my timesignal.
Going from Pa to dB.
Calculate RMS and Im done. (Hopefully)
The main problem is the A-Weighting. I realy dont get the idea how I can implement it on a live signal? And since the FFT leads to complex values Im also a little confused by that.
Maybe you get the idea/problem/workflow and help me to at least getting a little bit closer to the goal.
A little disclaimer, I am 100% new to the world of acoustics so please make sure to explain it like you would explain it a little child :D and Im programming with python.
Thanks in advance for your time!
To give you a short answer. This task can be done in only a few steps, utilizing the waveform_analysis package and Parseval's theorem.
The most simple implementation I can come up with is:
Time domain A-weighting filtering the signal - Using this library -
import waveform_analysis
weighted_signal = waveform_analysis.A_weight(signal, fs)
Take the RMS of the signal (utilizing that the power of the time domain equals the power of the frequency domain - Parseval's theorem). -
import numpy as np
rms_value = np.sqrt(np.mean(np.abs(weighted_signal)**2))
Convert this amplitude to dB -
result = 20 * np.log10(rms_value)
This gives you the results in dB(A)FS, if you run these three snippets together.
To get the dB(A)Pa value, you need to know what 0 dBPa corresponds to in dBFS. This is usually done by having a calibrated source such as https://www.grasacoustics.com/products/calibration-equipment/product/756-42ag
One flaw of this implementation is not pre-windowing the time signal. This is, on the other hand, not an issue for sufficently long signals.
Hello Christian Fichtelberg and welcome to StackOveflow. I believe your question could be answered more easily in DSP StackExchange but I will try to provide some quick and dirty answer.
In order to avoid taking the signal to the frequency domain, do the multiplication there (I urge you to the fact that convolution in the time domain - where your signal "resides" - is equivalent to multiplication in the frequency domain. If unfamiliar with this please have a quick look in Wikipedia's convolution page) you can implement the A-Weighting filter in the time-domain and perform some kind of convolution there.
I won't go into the details of the possible pros and cons of each method (time-domain convolution vs frequency domain multiplication). You could have a search on DSP SE or look into some textbook on DSP (such as Oppenheim's Digital Signal Processing, or an equivalent book by Proakis and Manolakis).
According to IEC 61672-1:2013 the digital filter should be "translated" from the analogue filter (a good way to do so is to use the bilinear transform). The proposed filter is a quite "simple" IIR (Infinite Impulse Response) filter.
I will skip the implementation here as it has been provided by others. Please find a MATLAB implementation, a Python implementation (most probably what you are seeking for your application), a quite "high-level" answer on DSP SE with some links and information on designing filters for arbitrary sample rates on DSP SE.
Finally, I would like to mention that if you manage to create a ("smooth enough") polynomial approximation to the curve of the A-Weighting filter you could possibly perform a frequency domain multiplication of the frequency response and the polynomial to change the magnitude only of the spectrum and then perform an iFFT to go back to time domain. This should most probably provide an approximation to the A-Weighted signal. Please note here that this is NOT the correct way to do filtering so treat it with caution (if you decide to try it at all) and only as a quick solution to perform some checks.

Algorithms for implementing analytic calculus

I'm interested in writing a program in Python that can parse a mathematical expression and carry out operations on it in algebraic form. Most of these will be pretty easy, something like 2x+5x+xy. My program would take this and return the simplified 7x+xy. However I'd also like to eventually extend it's functionality to calculus as well, so if it's given something like integrate: 5x it should be able to return (5x^2)/2 + c. I'm aware that for this the sympy library can be used, but I'm interested in implementing it myself as a learning process. Are there any algorithims that exist to carry out algebraic calculus for any arbitrary integral/differential I could implement, or is this a more daunting task I've set for myself here? Any reasources would be appreciated.
It might be worthwhile to ask why you're looking at doing this, and understanding the sorts of things that you want to be able to integrate, because I think you might not know that you've actually stumbled onto a very difficult question.
If you are looking for an exercise to learn about simple low level polynomial integration and python - ie. something of the form x^2 + 2x + 7 - and the idea is to learn about string manipulation and simple math, then by all means I think you should attempt to do this and see how far you can get. Look at the various math related libaries (like Sympy or Numpy) and see what you can get.
If you are looking to produce a powerful tool to be used for Calculus, then I think you should rethink your idea and consider using something like Wolfram Alpha. A project like this could take years and you might not get a good result from it.

Explanation of pathfinding/a* algorithm?

Do someone want to explain me (in pseudocode too) how the pathfinding/a* algorithm can be implemented in Python? I searched all over the internet but I am not able to understand how to calculate H-cost or G-cost. I found some source code, but it is too difficult for me (I am still a newbie).
Let's think I have a map like this:
'''
S0000000000000000000000000100000000000
00000000000000000000100000001000000000
10000111110101000001100000001000000000
01111100010001000010100000010000000000
00000100001111100100100001110000110000
00001100000001011000000100000001000000
00000100000000101000000000011110000001
00000010000000000111111111100000000011
00000001000000000000000001100000111100
00000000110000000000000000010011000000
00000000010000000001100000001000000000
00000000010000000000110000000111111000
00000000010000000000010000000000000000
00000000011000111111111111111111111111
000000000100000110000010000000000000E0
00000000001100000100001000000000000000
00000000000100000011000000000000000000
00000000000010000000000000000000000000
'''
Where 1s are obstacles, 'S' is the starting point and 'E' is the end.
How can I implement that algorithm? It would be great also if you can link an internet page who explain it in a easy way.

Sorting Random Numbers in my array

I have made an array in python that generates 20 random numbers 50-100. I want to know how to sort the numbers in my array. I am in 8th grade and this is for my computing class. Can this even be done? My teacher mentioned some kind of sorting algorithm, but told me not to use sort(). I am using python. Thanks
this is what i have so far
from random import*
array = range(20)
for i in range(20):
array[i] = randint(50, 100)
print array
Since it's an homework I won't give you the solution, but a starting point http://en.wikipedia.org/wiki/Sorting_algorithm where you can find an introduction to sorting algorithms!
Just choose one and try to implement it! Then if you do something wrong you can ask for help ;)
Everyone has posted excellent answers. I want to point you to this page, because it has a set of good animations for various different sorting algorithms.
Good luck!
For educational purposes, this should be a good place to start: http://en.wikipedia.org/wiki/Bubble_sort It's not a very good sorting algorithm, but once you understand the principles you can move on to more advanced sorting algorithms (which you will probably learn in class eventually).
Essentially, you compare every element to the next one, and swap them if they are out of order. Continue doing this until your array is sorted. Good luck!
This can be done and there are a number of ways to do it, you need to implement your own sorting algorithm, examples of which are very easy to come accross.
I will point you towards a python implementation of one of these algorithms with excellent explanation but i strongly suggest you read up on this sort of stuff as it is the foundation of computer science imho.
go here for a python quicksort program.. but please do the work around it.
Also i have to reccommend Introduction to Algorithms by thomas h cormen which is an awesome book and took me through a lot of stuff, and I still look at it to this day. A word of warning on this book.. it is not a light read.
If you find all this a bit heavy I found a youtube clip which goes through bubble sort.

Categories