Skip to content

Commit 4388e15

Browse files
committed
AGC example
1 parent 019f745 commit 4388e15

File tree

7 files changed

+70
-37
lines changed

7 files changed

+70
-37
lines changed

Examples/week-08-extensions/cython/AGC_example/README.rst

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,24 @@ Automatic Gain Control Example
33

44
Automatic Gain Control is a signal processing procedure:
55

6-
http://en.wikipedia.org/wiki/Automatic_gain_control
6+
http://seismicreflections.globeclaritas.com/2013/04/agc-equaliser.html
77

88
This is an example of using fortran, C, and Cython to produce a fast AGC
99
filter for Python/numpy.
1010

11+
Thanks for the folks at IRIS (http://www.iris.edu) for this example and
12+
the Fortran code.
13+
14+
Using these examples:
15+
=====================
16+
17+
The c and cython versions should be usable with any pyton set up to
18+
compile extensions. They use setuptools to call the compiler.
19+
20+
The Fortran / f2c version requires a compatible Fortran compiler. g77
21+
should work fine, but getting a compatible build on anything but Linux
22+
is a trick, and out of scope for this README (sorry). Google for it --
23+
you should find what you need.
24+
25+
26+

Examples/week-08-extensions/cython/AGC_example/agc_c.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* C version of AGC code */
22

33
#include <stdio.h>
4+
#include <math.h>
45

56
void AGC(int nAGC, int npts, float *amp, float *ampAGC);
67

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,17 @@
11
#!/usr/bin/env python
22

3-
from distutils.core import setup
3+
from setuptools import setup, Extension
44
from Cython.Build import cythonize
5-
from distutils.extension import Extension
65

76
import numpy
87

98
cy = Extension("agc_cython",
10-
["agc_cython.pyx",],
11-
include_dirs = [numpy.get_include(),],
12-
)
9+
["agc_cython.pyx"],
10+
include_dirs=[numpy.get_include()],
11+
)
1312
c_wrap = Extension("agc_c_cy",
14-
["agc_c_cy.pyx", "agc_c.c"],
15-
include_dirs = [numpy.get_include(),],
16-
)
17-
18-
setup(
19-
# ext_modules = [cythonize("agc_cython.pyx"),
20-
# cythonize([c_wrap]),
21-
# ]
22-
ext_modules = cythonize( [cy, c_wrap] ),
23-
)
13+
["agc_c_cy.pyx", "agc_c.c"],
14+
include_dirs=[numpy.get_include()]
15+
)
2416

17+
setup(ext_modules=cythonize([cy, c_wrap]))

Examples/week-08-extensions/cython/AGC_example/test_agc.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,45 +28,46 @@
2828
import agc_cython
2929
import agc_python
3030
import agc_c_cy
31-
import agc_subroutine
31+
32+
# The Fortran / f2py version
33+
# import agc_subroutine
34+
3235

3336
def test_cython():
3437
# just make sure it runs.
3538
signal = np.arange(20, dtype=np.float32)
3639

3740
result = agc_cython.agc(4, signal)
3841

42+
3943
def test_c_wrap():
4044
# just make sure it runs.
4145
signal = np.arange(20, dtype=np.float32)
4246

4347
result = agc_c_cy.agc(4, signal)
4448

45-
def test_subroutine():
46-
# just make sure it runs.
47-
signal = np.arange(20, dtype=np.float32)
48-
49-
result = agc_subroutine.agc(4, signal)
5049

50+
# def test_subroutine():
51+
# # the Fortran / f2py version
52+
# # just make sure it runs.
53+
# signal = np.arange(20, dtype=np.float32)
5154

55+
# result = agc_subroutine.agc(4, signal)
5256

5357

5458
def test_cy_py_same():
5559
signal = np.arange(20, dtype=np.float32)
56-
60+
5761
cy_result = agc_cython.agc(4, signal)
5862
py_result = agc_python.agc(4, signal)
5963
c_cy_result = agc_c_cy.agc(4, signal)
60-
sub_result = agc_subroutine.agc(4, signal)
61-
64+
# sub_result = agc_subroutine.agc(4, signal)
65+
6266
print "cy:", cy_result
6367
print "py:", py_result
6468
print "c_cy", c_cy_result
65-
print "subroutine", sub_result
69+
# print "subroutine", sub_result
6670

6771
assert np.array_equal(cy_result, py_result)
6872
assert np.array_equal(cy_result, c_cy_result)
69-
assert np.array_equal(cy_result, sub_result)
70-
71-
72-
73+
# assert np.array_equal(cy_result, sub_result)

Examples/week-08-extensions/cython/integrate/cy_integrate1.pyx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ def integrate_f(a, b, N):
1818
"""
1919

2020
s = 0
21-
dx = (b-a)/N
21+
dx = (b - a) / N
2222
for i in range(N):
23-
s += f(a+i*dx)
23+
s += f(a + i * dx)
2424
return s * dx
25-
26-

Examples/week-08-extensions/cython/integrate/test_integrate.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
#!/usr/bin/env python
22

3+
"""
4+
Simple test script to get gross timings
5+
6+
Not unit tests
7+
"""
8+
39
from timer_context import Timer
410

511

612
def run_test(msg):
713
print msg
814
with Timer():
9-
result = integrate_f(0.0,10.0,1000000)
15+
result = integrate_f(0.0, 10.0, 1000000)
1016
print "result:", result
1117
print
1218

@@ -34,4 +40,3 @@ def run_test(msg):
3440
from cy_integrate7 import integrate_f
3541
run_test("Seventh Cython version:")
3642

37-

slides_sources/source/extensions.rst

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,25 @@ At each step, we'll time and look at the output from::
780780

781781
$cython -a cy_integrate1.pyx
782782

783+
AGC Example
784+
-----------
785+
786+
Another useful example is in:
787+
788+
Examples/week-08-extensions/AGC_example
789+
790+
This one impliments an Automatic Gain Control Signal processing filter.
791+
792+
It turns out that you can use some advanced numpy tricks to get pretty
793+
good performancew with this filter, but you can't get full-on speed
794+
without some compiled code.
795+
796+
This example uses:
797+
* Pure Cython
798+
* C called from Cython
799+
* f2py and Fortran
800+
801+
783802
Auto-generated wrappers
784803
=======================
785804

@@ -793,7 +812,7 @@ XDress
793812

794813
[also Boost-Python -- not really a wrapper generator]
795814

796-
f2Py -- for Fortran
815+
f2py -- for Fortran
797816

798817

799818
SWIG

0 commit comments

Comments
 (0)