OpenCores
URL https://opencores.org/ocsvn/tcp_socket/tcp_socket/trunk

Subversion Repositories tcp_socket

[/] [tcp_socket/] [trunk/] [chips2/] [examples/] [example_2.py] - Blame information for rev 4

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 4 jondawson
#!/usr/bin/env python
2
 
3
import subprocess
4
import atexit
5
from math import pi
6
 
7
try:
8
    from matplotlib import pyplot
9
except ImportError:
10
    print "You need matplotlib to run this script!"
11
    exit(0)
12
 
13
children = []
14
def cleanup():
15
    for child in children:
16
        print "Terminating child process"
17
        child.terminate()
18
atexit.register(cleanup)
19
 
20
def run_c(file_name):
21
    process = subprocess.Popen(["../c2verilog", "iverilog", "run", str(file_name)])
22
    children.append(process)
23
    process.wait()
24
    children.remove(process)
25
 
26
def test():
27
    run_c("taylor.c")
28
    x = [float(i) for i in open("x")]
29
    sin_x = [float(i) for i in open("sin_x")]
30
    cos_x = [float(i) for i in open("cos_x")]
31
    pyplot.xticks(
32
        [-2.0*pi, -pi, 0, pi,  2.0*pi],
33
        [r'$-2\pi$', r"$-\pi$", r'$0$', r'$\pi$', r'$2\pi$'])
34
    pyplot.plot(x, sin_x, label="sin(x)")
35
    pyplot.plot(x, cos_x, label="cos(x)")
36
    pyplot.ylim(-1.1, 1.1)
37
    pyplot.xlim(-2.2 * pi, 2.2 * pi)
38
    pyplot.title("Trig Functions")
39
    pyplot.xlabel("x (radians)")
40
    pyplot.legend(loc="upper left")
41
    pyplot.savefig("../docs/source/examples/images/example_2.png")
42
    pyplot.show()
43
 
44
def indent(lines):
45
    return "\n    ".join(lines.splitlines())
46
 
47
def generate_docs():
48
 
49
    documentation = """
50
 
51
Approximating Sine and Cosine functions using Taylor Series
52
-----------------------------------------------------------
53
 
54
In this example, we calculate an approximation of the cosine functions using
55
the `Taylor series <http://en.wikipedia.org/wiki/Taylor_series>`_:
56
 
57
.. math::
58
 
59
    \\cos (x) = \\sum_{n=0}^{\\infty} \\frac{(-1)^n}{(2n)!} x^{2n}
60
 
61
 
62
The following example uses the Taylor Series approximation to generate the Sine
63
and Cosine functions. Successive terms of the taylor series are calculated
64
until successive approximations agree to within a small degree. A Sine
65
function is also synthesised using the identity :math:`sin(x) \\equiv cos(x-\\pi/2)`
66
 
67
.. code-block:: c
68
 
69
    %s
70
 
71
A simple test calculates Sine and Cosine for the range :math:`-2\\pi <= x <= 2\\pi`.
72
 
73
.. image:: images/example_2.png
74
 
75
"""%indent(open("taylor.c").read())
76
 
77
    document = open("../docs/source/examples/example_2.rst", "w").write(documentation)
78
 
79
test()
80
generate_docs()

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.