@@ -0,0 +1,86 | |||||
|
1 | #include <Python.h> | |||
|
2 | #include <numpy/arrayobject.h> | |||
|
3 | #include <math.h> | |||
|
4 | ||||
|
5 | ||||
|
6 | static PyObject *HS_algorithm(PyObject *self, PyObject *args) { | |||
|
7 | double navg; | |||
|
8 | PyObject *data_obj, *data_array; | |||
|
9 | ||||
|
10 | if (!PyArg_ParseTuple(args, "Od", &data_obj, &navg)) { | |||
|
11 | return NULL; | |||
|
12 | } | |||
|
13 | ||||
|
14 | data_array = PyArray_FROM_OTF(data_obj, NPY_FLOAT64, NPY_IN_ARRAY); | |||
|
15 | ||||
|
16 | if (data_array == NULL) { | |||
|
17 | Py_XDECREF(data_array); | |||
|
18 | Py_XDECREF(data_obj); | |||
|
19 | return NULL; | |||
|
20 | } | |||
|
21 | double *sortdata = (double*)PyArray_DATA(data_array); | |||
|
22 | int lenOfData = (int)PyArray_SIZE(data_array) ; | |||
|
23 | double nums_min = lenOfData*0.75; | |||
|
24 | if (nums_min <= 5) nums_min = 5; | |||
|
25 | double sump = 0; | |||
|
26 | double sumq = 0; | |||
|
27 | long j = 0; | |||
|
28 | int cont = 1; | |||
|
29 | double rtest = 0; | |||
|
30 | while ((cont == 1) && (j < lenOfData)) { | |||
|
31 | sump = sump + sortdata[j]; | |||
|
32 | sumq = sumq + pow(sortdata[j], 2); | |||
|
33 | if (j > nums_min) { | |||
|
34 | rtest = (double)j/(j-1) + 1/navg; | |||
|
35 | //printf("%ld\n", j); | |||
|
36 | //printf("%f \n", rtest); | |||
|
37 | //printf("%f \n", sump); | |||
|
38 | if ((sumq*j) > (rtest*pow(sump, 2))) { | |||
|
39 | j = j - 1; | |||
|
40 | sump = sump - sortdata[j]; | |||
|
41 | sumq = sumq - pow(sortdata[j],2); | |||
|
42 | cont = 0; | |||
|
43 | } | |||
|
44 | } | |||
|
45 | //printf("%ld\n", j); | |||
|
46 | j = j + 1; | |||
|
47 | } | |||
|
48 | ||||
|
49 | double lnoise = sump / j; | |||
|
50 | ||||
|
51 | Py_DECREF(data_array); | |||
|
52 | ||||
|
53 | // return PyLong_FromLong(lnoise); | |||
|
54 | return PyFloat_FromDouble(j); | |||
|
55 | } | |||
|
56 | ||||
|
57 | ||||
|
58 | static PyMethodDef noiseMethods[] = { | |||
|
59 | { "HS_algorithm", HS_algorithm, METH_VARARGS, "Applies hildebrand_sekhon algorithm" }, | |||
|
60 | { NULL, NULL, 0, NULL } | |||
|
61 | }; | |||
|
62 | ||||
|
63 | #if PY_MAJOR_VERSION >= 3 | |||
|
64 | ||||
|
65 | static struct PyModuleDef noisemodule = { | |||
|
66 | PyModuleDef_HEAD_INIT, | |||
|
67 | "_HS_algorithm", | |||
|
68 | "Applies hildebrand_sekhon algorithm", | |||
|
69 | -1, | |||
|
70 | noiseMethods | |||
|
71 | }; | |||
|
72 | ||||
|
73 | #endif | |||
|
74 | ||||
|
75 | #if PY_MAJOR_VERSION >= 3 | |||
|
76 | PyMODINIT_FUNC PyInit__HS_algorithm(void) { | |||
|
77 | Py_Initialize(); | |||
|
78 | import_array(); | |||
|
79 | return PyModule_Create(&noisemodule); | |||
|
80 | } | |||
|
81 | #else | |||
|
82 | PyMODINIT_FUNC init_HS_algorithm() { | |||
|
83 | Py_InitModule("_HS_algorithm", noiseMethods); | |||
|
84 | import_array(); | |||
|
85 | } | |||
|
86 | #endif |
General Comments 0
You need to be logged in to leave comments.
Login now