source: python/aubio/median.py @ 83c6734

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

import 0.1.7.1

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