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

Subversion Repositories tcp_socket

[/] [tcp_socket/] [trunk/] [chips2/] [examples/] [example_5.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
    import scipy as s
9
except ImportError:
10
    print "You need scipy to run this script!"
11
    exit(0)
12
 
13
try:
14
    import numpy as n
15
except ImportError:
16
    print "You need numpy to run this script!"
17
    exit(0)
18
 
19
try:
20
    from matplotlib import pyplot
21
except ImportError:
22
    print "You need matplotlib to run this script!"
23
    exit(0)
24
 
25
children = []
26
def cleanup():
27
    for child in children:
28
        print "Terminating child process"
29
        child.terminate()
30
atexit.register(cleanup)
31
 
32
def run_c(file_name):
33
    process = subprocess.Popen(["../c2verilog", "iverilog", "run", str(file_name)])
34
    children.append(process)
35
    process.wait()
36
    children.remove(process)
37
 
38
def test():
39
    run_c("fft.c")
40
    x_re = [float(i) for i in open("x_re")]
41
    x_im = [float(i) for i in open("x_im")]
42
    fft_x_re = [float(i) for i in open("fft_x_re")]
43
    fft_x_im = [float(i) for i in open("fft_x_im")]
44
 
45
    time_complex = [i + (j*1.0) for i, j in zip(x_re, x_im)]
46
    numpy_complex = s.fft(time_complex)
47
    numpy_magnitude = n.abs(numpy_complex)
48
 
49
    chips_complex = [i + (j*1.0j) for i, j in zip(fft_x_re, fft_x_im)]
50
    chips_magnitude = n.abs(chips_complex)
51
 
52
    f, subplot = pyplot.subplots(3, sharex=True)
53
 
54
    pyplot.subplots_adjust(hspace=1.0)
55
    subplot[0].plot(x_re, 'g')
56
    subplot[1].plot(numpy_magnitude, 'r')
57
    subplot[2].plot(chips_magnitude, 'b')
58
    pyplot.xlim(0, 1023)
59
    subplot[0].set_title("Time Domain Signal (64 point sine)")
60
    subplot[1].set_title("Frequency Spectrum - Numpy")
61
    subplot[2].set_title("Frequency Spectrum - Chips")
62
    subplot[0].set_xlabel("Sample")
63
    subplot[1].set_xlabel("Sample")
64
    subplot[2].set_xlabel("Sample")
65
    pyplot.savefig("../docs/source/examples/images/example_5.png")
66
    pyplot.show()
67
 
68
def indent(lines):
69
    return "\n    ".join(lines.splitlines())
70
 
71
def generate_docs():
72
 
73
    documentation = """
74
 
75
Fast Fourier Transform
76
----------------------
77
 
78
This example builds on the Taylor series example. We assume that the sin and
79
cos routines have been placed into a library of math functions math.h, along
80
with the definitions of :math:`\\pi`, M_PI.
81
 
82
The `Fast Fourier Transform (FFT) <http://en.wikipedia.org/wiki/Fast_Fourier_transform>`_
83
is an efficient method of decomposing discretely sampled signals into a frequency spectrum, it
84
is one of the most important algorithms in Digital Signal Processing (DSP).
85
`The Scientist and Engineer's Guide to Digital Signal Processing <http://www.dspguide.com/>`_
86
gives a straight forward introduction, and can be viewed on-line for free.
87
 
88
The example shows a practical method of calculating the FFT using the
89
`Cooley-Tukey algorithm <http://en.wikipedia.org/wiki/Fast_Fourier_transform#Cooley.E2.80.93Tukey_algorithm>`_.
90
 
91
 
92
.. code-block:: c
93
 
94
    %s
95
 
96
The C code includes a simple test routine that calculates the frequency spectrum of a 64 point sine wave.
97
 
98
.. image:: images/example_5.png
99
 
100
"""%indent(open("fft.c").read())
101
 
102
    document = open("../docs/source/examples/example_5.rst", "w").write(documentation)
103
 
104
test()
105
generate_docs()

powered by: WebSVN 2.1.0

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