annotate phasediff.py @ 83:4cc3d0706dd1

Add suffixes down to atto
author Daniel O'Connor <doconnor@gsoft.com.au>
date Thu, 03 Oct 2024 08:56:07 +0930
parents b6dc43e6f6f0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
61
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
1 #!/usr/bin/env python
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
2
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
3
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
4
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
5 # Copyright (c) 2020
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
6 # Daniel O'Connor <darius@dons.net.au>. All rights reserved.
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
7 #
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
8 # Redistribution and use in source and binary forms, with or without
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
9 # modification, are permitted provided that the following conditions
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
10 # are met:
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
11 # 1. Redistributions of source code must retain the above copyright
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
12 # notice, this list of conditions and the following disclaimer.
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
13 # 2. Redistributions in binary form must reproduce the above copyright
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
14 # notice, this list of conditions and the following disclaimer in the
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
15 # documentation and/or other materials provided with the distribution.
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
16 #
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
17 # THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
18 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
19 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
20 # ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
21 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
22 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
23 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
24 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
25 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
26 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
27 # SUCH DAMAGE.
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
28 #
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
29
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
30 # Expected DB schema
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
31 # CREATE TABLE phasediff (
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
32 # name TEXT,
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
33 # time TIMESTAMP WITH TIME ZONE,
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
34 # delta NUMERIC(15, 12),
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
35 # cumulative NUMERIC(15, 12)
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
36 # );
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
37
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
38 import datetime
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
39 import matplotlib.pylab as pylab
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
40 import numpy
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
41 import psycopg2
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
42 import scipy.signal
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
43 import sqlite3
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
44 import sys
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
45 import time
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
46 # Should try this code instead: https://github.com/python-ivi/python-usbtmc
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
47 import usb488
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
48
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
49 def main():
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
50 u = usb488.USB488Device()
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
51 print('Found device')
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
52
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
53 # See "TDS2000 Programmer.pdf"
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
54 u.write('*IDN?')
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
55 print(('IDN reports ' + u.read()))
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
56
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
57 #dbh = sqlite3.connect('phasediff.db')
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
58 dbh = psycopg2.connect('host=vm11 user=phasediff dbname=phasediff')
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
59 test(u, dbh, 'delamerelts')
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
60
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
61 def test(u, dbh = None, name = None):
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
62 if dbh != None:
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
63 cur = dbh.cursor()
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
64
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
65 u.write('DATA:ENC RIB') # Big endian signed
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
66 u.write('DATA:WIDTH 2') # 2 bytes wide
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
67
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
68 u.write('CH1:SCALE?')
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
69 vscale1 = float(u.read(1).split()[1])
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
70 print(('Channel 1 scale is %.2f volts/div' % (vscale1)))
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
71 u.write('CH2:SCALE?')
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
72 vscale2 = float(u.read(1).split()[1])
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
73 print(('Channel 2 scale is %.2f volts/div' % (vscale2)))
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
74 u.write('HOR:MAIN:SCALE?')
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
75 hscale = float(u.read(1).split()[1])
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
76 print(('Horizontal scale is %.5f nsec/div' % (hscale * 1e9)))
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
77 # TEK2024B doesn't grok HOR:DIV? so hard code 10 (has 8 vertically)
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
78 acqwindow = hscale * 10.0
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
79 nomclockperiod = 1 / 10.7e6
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
80
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
81 cumdiff = 0
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
82 lastdiff = None
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
83 while True:
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
84 ary1, ary2 = acquire(u, vscale1, vscale2)
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
85
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
86 sampletime = acqwindow / len(ary1)
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
87
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
88 # Compute clock periods
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
89 # XXX: this is pretty noisy so we use the nominal clock period above for unwrapping
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
90 #period1 = getperiod(ary1) * sampletime
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
91 #period2 = getperiod(ary2) * sampletime
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
92
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
93 # Compute phased difference between the two
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
94 d = getpdiff(ary1, ary2) * sampletime
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
95 #d = getpdiffedge(ary1, ary2) * sampletime
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
96 if lastdiff == None:
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
97 # First time, init variables and start again
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
98 # XXX: should get the most recent cumuulative diff from the DB really..
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
99 lastdiff = d
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
100 cumdiff = d
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
101 continue
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
102
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
103 # Difference between difference
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
104 diff = d - lastdiff
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
105
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
106 # Check if it wrapped
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
107 if abs(diff) > nomclockperiod / 2:
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
108 if d > nomclockperiod / 2:
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
109 diff -= nomclockperiod
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
110 else:
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
111 diff += nomclockperiod
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
112
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
113 # Accumulate deltas
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
114 cumdiff += diff
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
115
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
116 # Log / insert into DB
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
117 now = datetime.datetime.now()
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
118 print(("%s Cumulative: %.2f nsec Delta: %.2f nsec Diff: %.2f nsec" % (
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
119 now.strftime('%Y/%m/%d %H:%M:%S.%f'),
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
120 cumdiff * 1e9, d * 1e9, diff * 1e9)))
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
121 if dbh != None:
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
122 cur.execute('INSERT INTO phasediff(name, time, delta, cumulative) VALUES(%s, %s, %s, %s)', (name, now, d, cumdiff))
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
123 dbh.commit()
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
124 lastdiff = d
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
125
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
126 def acquire(u, vscale1, vscale2):
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
127 u.write('ACQ:STATE 1') # Do a single acquisition
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
128 u.write('*OPC?')
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
129 u.read(2.0) # Wait for it to complete
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
130
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
131 u.write('DAT:SOU CH1') # Set the curve source to channel 1
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
132 u.write('CURVE?') # Ask for the curve data
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
133 then = time.time()
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
134 result = u.read(1.0) # Takes the CRO a while for this
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
135 now = time.time()
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
136 #print('CURVE read took %f milliseconds' % ((now - then) * 1000.0))
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
137 data1 = result[13:] # Chop off the header (should verify this really..)
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
138 ary1 = numpy.fromstring(data1, dtype = '>h')
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
139 ary1 = ary1 / 32768.0 * vscale1 # Scale to volts
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
140
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
141 u.write('DAT:SOU CH2') # Set the curve source to channel 2
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
142 u.write('CURVE?') # Ask for the curve data
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
143 then = time.time()
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
144 result = u.read(1.0) # Takes the CRO a while for this
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
145 now = time.time()
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
146 #print('CURVE read took %f milliseconds' % ((now - then) * 1000.0))
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
147 data2 = result[13:] # Chop off the header (should verify this really..)
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
148 ary2 = numpy.fromstring(data2, dtype = '>h')
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
149 ary2 = ary2 / 32768.0 * vscale2 # Scale to volts
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
150
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
151 return ary1, ary2
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
152
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
153 def getpdiff(ary1, ary2):
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
154 '''Return phase difference in samples between two signals by cross correlation'''
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
155
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
156 # Rescale to 0-1
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
157 ary1 = ary1 - ary1.min()
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
158 ary1 = ary1 / ary1.max()
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
159 ary2 = ary2 - ary2.min()
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
160 ary2 = ary2 / ary2.max()
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
161
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
162 # Cross correlate
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
163 corr = scipy.signal.correlate(ary1, ary2)
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
164
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
165 # Find peak
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
166 amax = corr.argmax()
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
167
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
168 return amax - (len(ary1) - 1)
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
169
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
170 def getpdiffedge(ary1, ary2):
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
171 '''Return phase difference in samples between two signals by edge detection'''
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
172
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
173 # Rescale to 0-1
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
174 ary1 = ary1 - ary1.min()
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
175 ary1 = ary1 / ary1.max()
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
176 ary2 = ary2 - ary2.min()
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
177 ary2 = ary2 / ary2.max()
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
178
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
179 # Find rising edge of each
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
180 ary1pos = numpy.argmax(ary1 > 0.2)
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
181 ary2pos = numpy.argmax(ary2 > 0.2)
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
182
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
183 return ary1pos - ary2pos
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
184
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
185 def getperiod(ary):
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
186 '''Return period of signal in ary in samples'''
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
187 # Compute auto correlation
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
188 corr = scipy.signal.correlate(ary, ary)
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
189
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
190 # Find peaks
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
191 peaks, _ = scipy.signal.find_peaks(corr)
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
192
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
193 # Find time differences between peaks
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
194 deltapeak = (peaks[1:-1] - peaks[0:-2])
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
195
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
196 # Return average of the middle few
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
197 return deltapeak[2:-2].mean()
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
198
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
199 def trend(dbh, name):
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
200 cur = dbh.cursor()
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
201 cur2 = dbh.cursor()
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
202 cur.execute('SELECT time, deltansec FROM phasediff WHERE name = %s', (name, ))
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
203
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
204 _, lastdelta = cur.fetchone()
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
205
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
206 count = 0
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
207 cumdiff = 0
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
208 for row in cur:
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
209 time, delta = row
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
210 diff = float(delta - lastdelta)
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
211 if abs(diff) > 45e-9:
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
212 if delta > 45e-9:
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
213 diff -= 1.0 / 10.7e6
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
214 else:
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
215 diff += 1.0 / 10.7e6
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
216 cumdiff += diff
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
217 #print('Cumulative: %.5f nsec Delta: %.5f nsec Last: %.5f nsec Diff: %.5f nsec' % (
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
218 # float(cumdiff) * 1e9, float(delta) * 1e9, float(lastdelta) * 1e9, float(diff) * 1e9))
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
219 lastdelta = delta
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
220 cur2.execute('INSERT INTO cumdiff (name, time, cummdiff) VALUES (%s, %s, %s)',
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
221 ( name, time, cumdiff))
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
222 count += 1
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
223 #if count % 20 == 0:
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
224 # sys.stdin.readline()
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
225 if count % 1000 == 0:
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
226 dbh.commit()
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
227 print(('Count %d: %s' % (count, str(time))))
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
228
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
229 if __name__ == '__main__':
b6dc43e6f6f0 Add code to measure phase difference between 2 signals by sampling
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
230 main()