You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
howto » coding masterclass<br />
Enter resistor and<br />
capacitor values to<br />
calculate the 3dB<br />
cut-off frequency.<br />
d = ( v2 x sin(2Ð) ) / g<br />
...where ‘d’ is the distance travelled,<br />
‘v’ is the initial velocity, theta (‘θ’)<br />
is the initial projection angle off<br />
the ground and ‘g’ is gravity<br />
(9.8 metres per second-squared).<br />
Rearranging the equation so that ‘v’<br />
is the subject, we get:<br />
This is true even if you’re just<br />
mucking around in the Python Shell,<br />
but from then on, you can use the<br />
module functions as you like —<br />
and there are plenty to play with,<br />
including in other modules.<br />
One common need is the rounding of<br />
real numbers, or turning real numbers<br />
into integers. Say, for example, you<br />
have a real number ’12.65’. You may<br />
decide you want to round it up to 13,<br />
or you may want to round it down to<br />
12. Try these in the Python Shell:<br />
print(math.ceil(12.65))<br />
print(math.floor(12.65))<br />
The first returns the ‘ceiling’ integer<br />
of 12.65 (i.e. 13), while the second<br />
returns the ‘floor’ integer, which,<br />
in this example will be ‘12’. In other<br />
words, ‘math.ceil()’ rounds up to the<br />
nearest integer, while ‘math.floor()’<br />
108 www.apcmag.com<br />
The dds.py code<br />
creates a DDS table<br />
to any sample count<br />
you choose.<br />
rounds down to the nearest integer.<br />
Negative numbers give the impression<br />
of working in reverse — ‘floor’ gives<br />
you the next integer towards minusinfinity<br />
and ‘ceil’, the next towards<br />
zero, but it makes sense.<br />
GOAL-LINE DROP-OUT<br />
Let’s try something more practical.<br />
In rugby league, a goal-line drop-out<br />
is taken when a player drop-kicks the<br />
ball from underneath the cross-bar on<br />
the try-line down the field towards<br />
the opposition try-line. These days, a<br />
typical drop-out travels 50 metres on<br />
the fly (before the first bounce). Using<br />
the math module and some projectile<br />
motion theory, we can get Python to<br />
calculate the initial velocity required<br />
to kick the ball that distance. The<br />
mathematical equation for the<br />
maximum horizontal distance<br />
travelled is:<br />
v = sqrt( (d x g) /<br />
sin(2θ) )<br />
...where ‘sqrt’ is the square-root.<br />
Now we’re ready to create a small<br />
Python app.<br />
Download the source code for this<br />
month from our website (apcmag.com/<br />
magstuff) and unzip it, then open up<br />
the Python IDLE editor and load<br />
‘dropout.py’. You’ll see we’ve included<br />
both of these equations, to calculate<br />
either the required speed given the<br />
distance and angle, or the distance<br />
given the initial speed and angle.<br />
The key to the code is the math<br />
module’s ‘sqrt()’, ‘sin()’ and ‘radian()’<br />
functions. The ‘math.sqrt()’ function<br />
takes the square-root of the supplied<br />
parameter, the ‘math.sin()’ calculates<br />
the sine of the given value. However,<br />
this function expects to see the value<br />
in ‘radians’, not degrees. One radian<br />
is (180/pi) degrees, but rather than<br />
do the conversion by-hand, Python<br />
includes the ‘math.radian()’ function,<br />
which converts degrees into radians<br />
more accurately. You’ll see by<br />
embedding the math.radian()<br />
function inside the math.sin()<br />
function, we send the output of the<br />
radian() function immediately as<br />
input to the sin() function and get<br />
the right result.<br />
DIRECT DIGITAL SYNTHESIS<br />
In electronics, a popular new method<br />
for generating sinewaves is ‘direct<br />
digital synthesis’ and it’s used in<br />
everything from radio transmission<br />
to music synthesisers. It takes a table