Skip to content

Commit 308d0a4

Browse files
deploy: b38ef41
1 parent f152b34 commit 308d0a4

File tree

21 files changed

+459
-125
lines changed

21 files changed

+459
-125
lines changed

.buildinfo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# Sphinx build info version 1
22
# This file hashes the configuration used when building these files. When it is not found, a full rebuild will be done.
3-
config: ec6fb0df0e6675ee5292768e06579b02
3+
config: 4625281ba65eb502fa067fc22a709c3c
44
tags: 645f666f9bcd5a90fca523b33c5a78b7
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""
2+
Cython file to call the C routine
3+
"""
4+
5+
import numpy as np
6+
cimport numpy as cnp
7+
8+
cdef extern:
9+
void AGC(int nAGC, int npts, float *amp, float *ampAGC)
10+
11+
12+
def agc (int nAGC, float[:] amp):
13+
14+
# create the output array
15+
cdef cnp.ndarray[float, ndim=1] ampAGC = np.zeros_like(amp)
16+
17+
npts = amp.shape[0]
18+
19+
# call the C function
20+
AGC(nAGC, npts, &amp[0], &ampAGC[0])
21+
22+
return ampAGC
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
Pure python (and not bery numpy smart) version of the Automatic Gain Control function
5+
"""
6+
7+
import numpy as np
8+
9+
def agc(nAGC, amp):
10+
"""
11+
run an automatic gain control filter onver the input array
12+
13+
:param nAGC: width of window, number of elements.
14+
:type nAGC: integer
15+
16+
:param amp: input amplitude data
17+
:type amp: 1-d numpy array of float32
18+
19+
:returns ampAGC: a numpy array of the filtered data.
20+
21+
"""
22+
23+
#make sure input array is as expected:
24+
amp = np.asarray(amp, dtype=np.float32)
25+
if len(amp.shape) != 1:
26+
raise ValueError("amp must be a rank-1 array")
27+
28+
npts = amp.shape[0]
29+
30+
nAGC2=nAGC/2
31+
ampAGC = np.zeros_like(amp)
32+
absamp = np.zeros_like(amp)
33+
34+
absamp = np.abs(amp)
35+
36+
37+
for i in xrange(nAGC2, npts - nAGC2):
38+
fmax=0.0
39+
for j in range(i-nAGC2,i+nAGC2+1):
40+
if absamp[j] > fmax:
41+
fmax = absamp[j]
42+
ampAGC[i] = amp[i]/fmax
43+
44+
return ampAGC
45+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/* C version of AGC code */
2+
3+
#include <stdio.h>
4+
5+
void AGC(int nAGC, int npts, float *amp, float *ampAGC);
6+
7+
void AGC(int nAGC, int npts, float *amp, float *ampAGC) {
8+
9+
float fmax;
10+
float absamp[npts];
11+
int i, j, nAGC2;
12+
13+
14+
for (i = 0; i < npts; i++){
15+
ampAGC[i] = 0.0;
16+
absamp[i] = fabs(amp[i]);
17+
}
18+
nAGC2 = nAGC / 2;
19+
20+
for (i = nAGC2; i < npts-nAGC2; i++){
21+
fmax = 0.0;
22+
for ( j=(i-nAGC2); j < i+nAGC2+1; j++ ){
23+
if ( absamp[j] > fmax ) {
24+
fmax = absamp[j];
25+
}
26+
}
27+
ampAGC[i] = amp[i] / fmax;
28+
}
29+
return;
30+
}
31+
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
cython version version of the Automatic Gain Control function
5+
6+
Timings:
7+
first version (pure python):
8+
9+
In [23]: timeit agc_cython.agc(10, np.arange(1000, dtype=np.float32))
10+
100 loops, best of 3: 10.7 ms per loop
11+
12+
typing inputs:
13+
14+
100 loops, best of 3: 17.3 ms per loop
15+
16+
Typing counters and memoryviews of arrays
17+
1000 loops, best of 3: 1.68 ms per loop
18+
19+
typing loop counters
20+
1000 loops, best of 3: 613 us per loop
21+
22+
forgot to type fmax:
23+
10000 loops, best of 3: 119 us per loop
24+
25+
added boundscheck=False
26+
10000 loops, best of 3: 117 us per loop
27+
28+
use memoryviews of cython arrays
29+
10000 loops, best of 3: 39.4 us per loop
30+
31+
use cython arrays:
32+
100 loops, best of 3: 6.12 ms per loop
33+
34+
c array for temp array:
35+
100 loops, best of 3: 6.34 ms per loop
36+
37+
"""
38+
39+
import cython
40+
import numpy as np
41+
cimport numpy as cnp
42+
43+
#from libc.stdlib cimport malloc, free
44+
45+
@cython.boundscheck(False)
46+
@cython.cdivision(True)
47+
def agc( int nAGC, cnp.ndarray[float, ndim=1, mode='c'] amp):
48+
"""
49+
run an automatic gain control filter onver the input array
50+
51+
:param nAGC: width of window, number of elements.
52+
:type nAGC: integer
53+
54+
:param amp: input amplitude data
55+
:type amp: 1-d numpy array of float32
56+
57+
:returns ampAGC: a numpy array of the filtered data.
58+
59+
"""
60+
61+
cdef float fmax
62+
cdef unsigned int i, j
63+
64+
cdef unsigned int npts = amp.shape[0]
65+
66+
cdef unsigned int nAGC2 = nAGC / 2
67+
68+
cdef cnp.ndarray[float, ndim=1, mode='c'] ampAGC = np.zeros_like(amp)
69+
cdef cnp.ndarray[float, ndim=1, mode='c'] absamp = np.abs(amp)
70+
71+
72+
for i in range(nAGC2, npts - nAGC2):
73+
fmax=0.0
74+
for j in range(i-nAGC2,i+nAGC2+1):
75+
if absamp[j] > fmax:
76+
fmax = absamp[j]
77+
ampAGC[i] = amp[i]/fmax
78+
79+
return ampAGC
80+

_sources/interfacing_with_c/c_python.rst.txt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ https://cython.org/
4343
ctypes
4444
------
4545

46-
Ctypes comes with PYthon out of the box.
46+
Ctypes comes with Python out of the box.
4747

4848

4949
SWIG, SIP, ETC.
@@ -57,7 +57,14 @@ EXAMPLE:
5757

5858
Same as the one for fortran: a automatic gain control filter:
5959

60-
:download:`agc_example.zip`
60+
:download:`agc_example/agc_c.c`
61+
62+
:download:`agc_example/agc_c_cy.pyx`
63+
64+
:download:`agc_example/agc_cython.pyx`
65+
66+
:download:`agc_example/agc_python.py`
67+
6168

6269

6370

_sources/weak_references.rst.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ Chris Barker
1313

1414
``https://github.com/PythonCHB``
1515

16+
**NOTE:** These notes were written for Python 2. The principles remain the same (for now... work on a GIL-free python is close!), but Python 3 has stronger and smarter support for garbage collection, so this is all less critical (though still good to understand!)
17+
1618
==================
1719
Memory Management
1820
==================
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/* Compatability shim for jQuery and underscores.js.
2+
*
3+
* Copyright Sphinx contributors
4+
* Released under the two clause BSD licence
5+
*/
6+
7+
/**
8+
* small helper function to urldecode strings
9+
*
10+
* See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURIComponent#Decoding_query_parameters_from_a_URL
11+
*/
12+
jQuery.urldecode = function(x) {
13+
if (!x) {
14+
return x
15+
}
16+
return decodeURIComponent(x.replace(/\+/g, ' '));
17+
};
18+
19+
/**
20+
* small helper function to urlencode strings
21+
*/
22+
jQuery.urlencode = encodeURIComponent;
23+
24+
/**
25+
* This function returns the parsed url parameters of the
26+
* current request. Multiple values per key are supported,
27+
* it will always return arrays of strings for the value parts.
28+
*/
29+
jQuery.getQueryParameters = function(s) {
30+
if (typeof s === 'undefined')
31+
s = document.location.search;
32+
var parts = s.substr(s.indexOf('?') + 1).split('&');
33+
var result = {};
34+
for (var i = 0; i < parts.length; i++) {
35+
var tmp = parts[i].split('=', 2);
36+
var key = jQuery.urldecode(tmp[0]);
37+
var value = jQuery.urldecode(tmp[1]);
38+
if (key in result)
39+
result[key].push(value);
40+
else
41+
result[key] = [value];
42+
}
43+
return result;
44+
};
45+
46+
/**
47+
* highlight a given string on a jquery object by wrapping it in
48+
* span elements with the given class name.
49+
*/
50+
jQuery.fn.highlightText = function(text, className) {
51+
function highlight(node, addItems) {
52+
if (node.nodeType === 3) {
53+
var val = node.nodeValue;
54+
var pos = val.toLowerCase().indexOf(text);
55+
if (pos >= 0 &&
56+
!jQuery(node.parentNode).hasClass(className) &&
57+
!jQuery(node.parentNode).hasClass("nohighlight")) {
58+
var span;
59+
var isInSVG = jQuery(node).closest("body, svg, foreignObject").is("svg");
60+
if (isInSVG) {
61+
span = document.createElementNS("http://www.w3.org/2000/svg", "tspan");
62+
} else {
63+
span = document.createElement("span");
64+
span.className = className;
65+
}
66+
span.appendChild(document.createTextNode(val.substr(pos, text.length)));
67+
node.parentNode.insertBefore(span, node.parentNode.insertBefore(
68+
document.createTextNode(val.substr(pos + text.length)),
69+
node.nextSibling));
70+
node.nodeValue = val.substr(0, pos);
71+
if (isInSVG) {
72+
var rect = document.createElementNS("http://www.w3.org/2000/svg", "rect");
73+
var bbox = node.parentElement.getBBox();
74+
rect.x.baseVal.value = bbox.x;
75+
rect.y.baseVal.value = bbox.y;
76+
rect.width.baseVal.value = bbox.width;
77+
rect.height.baseVal.value = bbox.height;
78+
rect.setAttribute('class', className);
79+
addItems.push({
80+
"parent": node.parentNode,
81+
"target": rect});
82+
}
83+
}
84+
}
85+
else if (!jQuery(node).is("button, select, textarea")) {
86+
jQuery.each(node.childNodes, function() {
87+
highlight(this, addItems);
88+
});
89+
}
90+
}
91+
var addItems = [];
92+
var result = this.each(function() {
93+
highlight(this, addItems);
94+
});
95+
for (var i = 0; i < addItems.length; ++i) {
96+
jQuery(addItems[i].parent).before(addItems[i].target);
97+
}
98+
return result;
99+
};
100+
101+
/*
102+
* backward compatibility for jQuery.browser
103+
* This will be supported until firefox bug is fixed.
104+
*/
105+
if (!jQuery.browser) {
106+
jQuery.uaMatch = function(ua) {
107+
ua = ua.toLowerCase();
108+
109+
var match = /(chrome)[ \/]([\w.]+)/.exec(ua) ||
110+
/(webkit)[ \/]([\w.]+)/.exec(ua) ||
111+
/(opera)(?:.*version|)[ \/]([\w.]+)/.exec(ua) ||
112+
/(msie) ([\w.]+)/.exec(ua) ||
113+
ua.indexOf("compatible") < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec(ua) ||
114+
[];
115+
116+
return {
117+
browser: match[ 1 ] || "",
118+
version: match[ 2 ] || "0"
119+
};
120+
};
121+
jQuery.browser = {};
122+
jQuery.browser[jQuery.uaMatch(navigator.userAgent).browser] = true;
123+
}

_static/documentation_options.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var DOCUMENTATION_OPTIONS = {
22
URL_ROOT: document.getElementById("documentation_options").getAttribute('data-url_root'),
3-
VERSION: '1.0.0',
3+
VERSION: '2.0.0',
44
LANGUAGE: 'en',
55
COLLAPSE_INDEX: false,
66
BUILDER: 'html',

_static/jquery.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)