source: python/aubio/median.py @ fa11ce0

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since fa11ce0 was 20d8266, checked in by Paul Brossier <piem@piem.org>, 15 years ago

python: more update headers to GPLv3

  • Property mode set to 100644
File size: 2.0 KB
RevLine 
[96fb8ad]1"""Copyright (C) 2004 Paul Brossier <piem@altern.org>
2print aubio.__LICENSE__ for the terms of use
3"""
4
5__LICENSE__ = """\
[20d8266]6  Copyright (C) 2004-2009 Paul Brossier <piem@aubio.org>
[96fb8ad]7
[20d8266]8  This file is part of aubio.
[96fb8ad]9
[20d8266]10  aubio is free software: you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14
15  aubio is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  GNU General Public License for more details.
19
20  You should have received a copy of the GNU General Public License
21  along with aubio.  If not, see <http://www.gnu.org/licenses/>.
[96fb8ad]22"""           
23
24"""
25original author Tim Peters
26modified by Paul Brossier <piem@altern.org>
27inspired from http://www.ics.uci.edu/~eppstein/161/python/peters-selection.py
28"""
29
30def short_find(a, rank):
[0484dc1]31    """ find the rank-th value in sorted a """
32    # copy to b before sorting
33    b = a[:]
34    b.sort()
35    return b[rank - 1]
[96fb8ad]36
37def percental(a, rank):
[0484dc1]38    """ Find the rank'th-smallest value in a, in worst-case linear time. """
[96fb8ad]39    n = len(a)
40    assert 1 <= rank <= n
41    if n <= 7:
42        return short_find(a, rank)
43
44    ## Find median of median-of-7's.
45    ##medians = [short_find(a[i : i+7], 4) for i in xrange(0, n-6, 7)]
46    #median = find(medians, (len(medians) + 1) // 2)
47   
48    # modified to Find median
49    median = short_find([a[0], a[-1], a[n//2]], 2)
50
51    # Partition around the median.
52    # a[:i]   <= median
53    # a[j+1:] >= median
54    i, j = 0, n-1
55    while i <= j:
56        while a[i] < median:
57            i += 1
58        while a[j] > median:
59            j -= 1
60        if i <= j:
61            a[i], a[j] = a[j], a[i]
62            i += 1
63            j -= 1
64
65    if rank <= i:
66        return percental(a[:i], rank)
67    else:
68        return percental(a[i:], rank - i)
69
Note: See TracBrowser for help on using the repository browser.