source: python/aubio/onsetcompare.py @ c0ec39c

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since c0ec39c 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: 3.5 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""" this file contains routines to compare two lists of onsets or notes.
24it somewhat implements the Receiver Operating Statistic (ROC).
25see http://en.wikipedia.org/wiki/Receiver_operating_characteristic
26"""
27
28from numarray import *
29
30def onset_roc(la, lb, eps):
31    """ thanks to nicolas wack for the rewrite"""
32    """ compute differences between two lists """
33    """ feature: scalable to huge lists """
34    n, m = len(la), len(lb)
35    if m == 0 :
36        return 0,0,0,n,0
37    missed, bad = 0, 0
38    # find missed ones first
39    for x in la:
40        correspond = 0
41        for y in lb:
42            if abs(x-y) <= eps:
43                correspond += 1
44        if correspond == 0:
45            missed += 1
46    # then look for bad ones
47    for y in lb:
48        correspond = 0
49        for x in la:
50            if abs(x-y) <= eps:
51               correspond += 1
52        if correspond == 0:
53            bad += 1
54    ok    = n - missed
55    hits  = m - bad
56    total = n
57    return ok,bad,missed,total,hits
58   
59   
60def notes_roc (la, lb, eps):
61    """ creates a matrix of size len(la)*len(lb) then look for hit and miss
62    in it within eps tolerance windows """
63    gdn,fpw,fpg,fpa,fdo,fdp = 0,0,0,0,0,0
64    m = len(la)
65    n = len(lb)
66    x =           resize(la[:,0],(n,m))
67    y = transpose(resize(lb[:,0],(m,n)))
68    teps =  (abs(x-y) <= eps[0]) 
69    x =           resize(la[:,1],(n,m))
70    y = transpose(resize(lb[:,1],(m,n)))
71    tpitc = (abs(x-y) <= eps[1]) 
72    res = teps * tpitc
73    res = add.reduce(res,axis=0)
74    for i in range(len(res)) :
75        if res[i] > 1:
76            gdn+=1
77            fdo+=res[i]-1
78        elif res [i] == 1:
79            gdn+=1
80    fpa = n - gdn - fpa
81    return gdn,fpw,fpg,fpa,fdo,fdp
82
83def load_onsets(filename) :
84    """ load onsets targets / candidates files in arrays """
85    l = [];
86   
87    f = open(filename,'ro')
88    while 1:
89        line = f.readline().split()
90        if not line : break
91        l.append(float(line[0]))
92   
93    return l
94
95"""
96def onset_roc (la, lb, eps):
97    \"\"\" build a matrix of all possible differences between two lists \"\"\"
98    \"\"\" bug: not scalable to huge lists \"\"\"
99        n, m        = len(la), len(lb)
100    if m ==0 :
101        return 0,0,0,n,0
102        missed, bad = 0, 0
103        x           = resize(la[:],(m,n))
104        y           = transpose(resize(lb[:],(n,m)))
105        teps        = (abs(x-y) <= eps)
106        resmis      = add.reduce(teps,axis = 0)
107        for i in range(n) :
108        if resmis[i] == 0:
109            missed += 1
110    resbad = add.reduce(teps,axis=1)
111    for i in range(m) :
112        if resbad[i] == 0:
113            bad += 1
114    ok    = n - missed
115    hits  = m - bad
116    total = n
117    return ok,bad,missed,total,hits
118"""
119
Note: See TracBrowser for help on using the repository browser.