from __future__ import division from collections import defaultdict from virgotools import * from matplotlib.mlab import psd, window_hanning import matplotlib.pyplot as plt table = ''' 8113.3 SDB1_BL_H 8217.3 SDB1_FR_V 8316.3 SDB1_FL_V 8418.3 SDB1_BL_V 8515.3 SDB1_FL_H 8714.3 SDB1_FR_H 8747 SDB1_OMC2 8813.3 SDB1_BR_V 8912.3 SDB1_BR_H 12831.3 SDB1_OMC1 ''' freqs = [] sources = [] for line in table.splitlines(): if not line: continue freq, source = line.split() freqs.append(float(freq)) sources.append(source) candidates = defaultdict(list) # linear coupling for f, s in zip(freqs, sources): candidates[f].append(s) # first order mixing for f1, s1 in zip(freqs, sources): for f2, s2 in zip(freqs, sources): if f1 > f2: candidates[f1 + f2].append('%s + %s' % (s1, s2)) if f1 - f2 > 0: candidates[f1 - f2].append('%s - %s' % (s1, s2)) # harmonics of OMC lines for f, s in zip(freqs, sources): if 'OMC' not in s: continue for i in range(2,5): candidates[f * i].append('%i * %s' % (i, s)) for f, s in sorted(candidates.iteritems()): print '%10.1f Hz: %s' % (f, ', '.join(s)) # get data chan = 'SDB2_B1_PD1_Audio_100k' gstart = utc2gps(2017,7,10,16,0,0) dur = 100 data = getChannel(chan, gstart, dur) # calculate spectrum twin = 10 nfft = int(data.fsample * twin) PSD, freq = psd(data.data, NFFT=nfft, Fs=data.fsample, window=window_hanning, noverlap=nfft/2) ASD = PSD**0.5 # plotting plt.semilogy(freq, ASD, label='Measured spectrum') def alias(fsample, f): return abs(f - fsample * round(f / fsample)) label_y = 1e-5 df = 10 for fline, sources in candidates.iteritems(): if fline <= data.fsample / 2: color = 'r' label = 'normal line' else: color = 'green' fline = alias(data.fsample, fline) label = 'aliased line' plt.axvline(fline, color=color, linestyle='--', label=label) s = ', '.join(source for source in sources) plt.text(fline + df, label_y, s,rotation=90, ha='left', va='bottom') plt.xlabel('Frequency (Hz)') plt.ylabel('%s (%s/rtHz)' % (chan, data.unit)) plt.title('Mixing of lines in %s\nfrequency resolution %.2f Hz, using %i seconds of data at %s' % (chan, 1/twin, dur, gps2str(gstart))) # skip double legends http://stackoverflow.com/a/13589144 from collections import OrderedDict handles, labels = plt.gca().get_legend_handles_labels() by_label = OrderedDict(zip(labels, handles)) plt.legend(by_label.values(), by_label.keys()) plt.show()