Tue, 01 May 2018 00:10:23 +0200
[py3k] beginning to port to py3k
also write a proper setuptools based setup.py and convert some bin/* scripts
as entry_points.
66
2a97995628a3
added copyright header to every python file
David Douard <david.douard@logilab.fr>
parents:
48
diff
changeset
|
1 | # This program is free software; you can redistribute it and/or modify it under |
2a97995628a3
added copyright header to every python file
David Douard <david.douard@logilab.fr>
parents:
48
diff
changeset
|
2 | # the terms of the GNU General Public License as published by the Free Software |
2a97995628a3
added copyright header to every python file
David Douard <david.douard@logilab.fr>
parents:
48
diff
changeset
|
3 | # Foundation; either version 2 of the License, or (at your option) any later |
2a97995628a3
added copyright header to every python file
David Douard <david.douard@logilab.fr>
parents:
48
diff
changeset
|
4 | # version. |
2a97995628a3
added copyright header to every python file
David Douard <david.douard@logilab.fr>
parents:
48
diff
changeset
|
5 | # |
2a97995628a3
added copyright header to every python file
David Douard <david.douard@logilab.fr>
parents:
48
diff
changeset
|
6 | # This program is distributed in the hope that it will be useful, but WITHOUT |
2a97995628a3
added copyright header to every python file
David Douard <david.douard@logilab.fr>
parents:
48
diff
changeset
|
7 | # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
2a97995628a3
added copyright header to every python file
David Douard <david.douard@logilab.fr>
parents:
48
diff
changeset
|
8 | # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details |
2a97995628a3
added copyright header to every python file
David Douard <david.douard@logilab.fr>
parents:
48
diff
changeset
|
9 | # |
2a97995628a3
added copyright header to every python file
David Douard <david.douard@logilab.fr>
parents:
48
diff
changeset
|
10 | # You should have received a copy of the GNU General Public License along with |
2a97995628a3
added copyright header to every python file
David Douard <david.douard@logilab.fr>
parents:
48
diff
changeset
|
11 | # this program; if not, write to the Free Software Foundation, Inc., |
2a97995628a3
added copyright header to every python file
David Douard <david.douard@logilab.fr>
parents:
48
diff
changeset
|
12 | # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
86
96e30b092f70
[py3k] beginning to port to py3k
David Douard <david.douard@logilab.fr>
parents:
66
diff
changeset
|
13 | """Copyright (c) 2007-2018 David Douard (Paris, FRANCE). |
96e30b092f70
[py3k] beginning to port to py3k
David Douard <david.douard@logilab.fr>
parents:
66
diff
changeset
|
14 | http://www.logilab.org/project/pygpibtoolkit -- mailto:david.douard@sdfa3.org |
96e30b092f70
[py3k] beginning to port to py3k
David Douard <david.douard@logilab.fr>
parents:
66
diff
changeset
|
15 | |
66
2a97995628a3
added copyright header to every python file
David Douard <david.douard@logilab.fr>
parents:
48
diff
changeset
|
16 | """ |
2a97995628a3
added copyright header to every python file
David Douard <david.douard@logilab.fr>
parents:
48
diff
changeset
|
17 | |
46
235686915f92
add THD computation for simple traces of power spectrum
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
18 | import numpy |
235686915f92
add THD computation for simple traces of power spectrum
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
19 | |
86
96e30b092f70
[py3k] beginning to port to py3k
David Douard <david.douard@logilab.fr>
parents:
66
diff
changeset
|
20 | |
46
235686915f92
add THD computation for simple traces of power spectrum
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
21 | def thd(spectrum, squared=True, db=True): |
235686915f92
add THD computation for simple traces of power spectrum
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
22 | """ |
235686915f92
add THD computation for simple traces of power spectrum
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
23 | compute THD from a spectrum data, given by a simple 1D data |
235686915f92
add THD computation for simple traces of power spectrum
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
24 | vector. |
235686915f92
add THD computation for simple traces of power spectrum
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
25 | |
235686915f92
add THD computation for simple traces of power spectrum
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
26 | The result is the couple (fmax, thd), this latter being expressed |
235686915f92
add THD computation for simple traces of power spectrum
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
27 | in 'db' if db is True; if not, in percentage. |
86
96e30b092f70
[py3k] beginning to port to py3k
David Douard <david.douard@logilab.fr>
parents:
66
diff
changeset
|
28 | |
46
235686915f92
add THD computation for simple traces of power spectrum
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
29 | """ |
235686915f92
add THD computation for simple traces of power spectrum
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
30 | n = len(spectrum) |
235686915f92
add THD computation for simple traces of power spectrum
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
31 | # first, find the fundamental frequency |
235686915f92
add THD computation for simple traces of power spectrum
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
32 | fmax = spectrum.argmax() |
235686915f92
add THD computation for simple traces of power spectrum
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
33 | vmax = spectrum[fmax] |
235686915f92
add THD computation for simple traces of power spectrum
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
34 | |
235686915f92
add THD computation for simple traces of power spectrum
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
35 | # then, the compute the several max values for harmonics |
235686915f92
add THD computation for simple traces of power spectrum
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
36 | h_freqs = numpy.arange(0, n, fmax) |
235686915f92
add THD computation for simple traces of power spectrum
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
37 | h_vals = [] |
235686915f92
add THD computation for simple traces of power spectrum
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
38 | h_freqs2 = [fmax] |
235686915f92
add THD computation for simple traces of power spectrum
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
39 | for i, f in enumerate(h_freqs[2:]): |
235686915f92
add THD computation for simple traces of power spectrum
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
40 | fm = spectrum[f-5:f+5].argmax() |
235686915f92
add THD computation for simple traces of power spectrum
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
41 | h_vals.append(spectrum[f-5+fm]) |
235686915f92
add THD computation for simple traces of power spectrum
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
42 | h_freqs2.append((f-5.+fm)/(i+2)) |
235686915f92
add THD computation for simple traces of power spectrum
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
43 | h_vals = numpy.array(h_vals) |
235686915f92
add THD computation for simple traces of power spectrum
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
44 | h_freqs2 = numpy.array(h_freqs2) |
235686915f92
add THD computation for simple traces of power spectrum
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
45 | if not squared: |
235686915f92
add THD computation for simple traces of power spectrum
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
46 | h_vals = h_vals**2 |
235686915f92
add THD computation for simple traces of power spectrum
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
47 | vmax = vmax**2 |
235686915f92
add THD computation for simple traces of power spectrum
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
48 | thd = h_vals.sum()/vmax |
235686915f92
add THD computation for simple traces of power spectrum
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
49 | if db: |
235686915f92
add THD computation for simple traces of power spectrum
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
50 | thd = 10*numpy.log10(thd) |
235686915f92
add THD computation for simple traces of power spectrum
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
51 | else: |
235686915f92
add THD computation for simple traces of power spectrum
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
52 | thd = 100.0*numpy.sqrt(thd) |
235686915f92
add THD computation for simple traces of power spectrum
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
53 | return h_freqs2.mean(), thd |
235686915f92
add THD computation for simple traces of power spectrum
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
54 | |
86
96e30b092f70
[py3k] beginning to port to py3k
David Douard <david.douard@logilab.fr>
parents:
66
diff
changeset
|
55 | |
46
235686915f92
add THD computation for simple traces of power spectrum
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
56 | def thd_n(spectrum, squared=True, db=True): |
235686915f92
add THD computation for simple traces of power spectrum
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
57 | """ |
235686915f92
add THD computation for simple traces of power spectrum
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
58 | compute THD+N from a spectrum data, given by a simple 1D data |
235686915f92
add THD computation for simple traces of power spectrum
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
59 | vector. |
235686915f92
add THD computation for simple traces of power spectrum
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
60 | |
235686915f92
add THD computation for simple traces of power spectrum
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
61 | The result is the thdN, expressed in 'db' if db is True; if not, |
235686915f92
add THD computation for simple traces of power spectrum
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
62 | in percentage. |
86
96e30b092f70
[py3k] beginning to port to py3k
David Douard <david.douard@logilab.fr>
parents:
66
diff
changeset
|
63 | |
46
235686915f92
add THD computation for simple traces of power spectrum
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
64 | """ |
235686915f92
add THD computation for simple traces of power spectrum
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
65 | if not squared: |
235686915f92
add THD computation for simple traces of power spectrum
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
66 | spectrum = spectrum**2 |
235686915f92
add THD computation for simple traces of power spectrum
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
67 | fmax = spectrum.argmax() |
86
96e30b092f70
[py3k] beginning to port to py3k
David Douard <david.douard@logilab.fr>
parents:
66
diff
changeset
|
68 | w = int(fmax/10) # argh |
46
235686915f92
add THD computation for simple traces of power spectrum
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
69 | vmax = spectrum[fmax-w:fmax+w].sum() |
235686915f92
add THD computation for simple traces of power spectrum
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
70 | thd = (spectrum.sum()-vmax)/(vmax) |
235686915f92
add THD computation for simple traces of power spectrum
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
71 | if db: |
235686915f92
add THD computation for simple traces of power spectrum
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
72 | thd = 10*numpy.log10(thd) |
235686915f92
add THD computation for simple traces of power spectrum
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
73 | else: |
235686915f92
add THD computation for simple traces of power spectrum
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
74 | thd = 100.0*numpy.sqrt(thd) |
235686915f92
add THD computation for simple traces of power spectrum
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
75 | return thd |