Skip to content

Instantly share code, notes, and snippets.

@cbassa
Created December 17, 2019 14:44
Show Gist options
  • Save cbassa/f53663dcda76e4db2dc78369bc5b63b7 to your computer and use it in GitHub Desktop.
Save cbassa/f53663dcda76e4db2dc78369bc5b63b7 to your computer and use it in GitHub Desktop.
Waterfall plotter
#!/usr/bin/env python
from __future__ import print_function
import numpy as np
import matplotlib
#matplotlib.use("Agg")
import matplotlib.pyplot as plt
import sys
# Get filename
fname=sys.argv[1]
if len(sys.argv)==3:
outfname=sys.argv[2]
else:
outfname=fname.replace("receiving_","").replace(".dat",".png")
# Open file
fp=open(fname)
timestamp = np.fromfile(fp, dtype="|S32", count=1)[0]
nchan = np.fromfile(fp, dtype='>i4', count=1)[0]
samp_rate = np.fromfile(fp, dtype='>i4', count=1)[0]
nfft_per_row = np.fromfile(fp, dtype='>i4', count=1)[0]
center_freq = np.fromfile(fp, dtype='>f4', count=1)[0]
endianness = np.fromfile(fp, dtype='>i4', count=1)[0]
print(timestamp, nchan, samp_rate, nfft_per_row, center_freq)
# Set dtypes
dt = np.dtype([('tabs', 'int64'), ('spec', 'float32', (nchan, ))])
data = np.fromfile(fp, dtype=dt)
fp.close()
nint = data.shape[0]
tabs = data['tabs'] / 1000000.0
trel = np.arange(nint) * nfft_per_row * nchan / float(samp_rate)
freq = np.linspace(-0.5*samp_rate, 0.5*samp_rate, nchan, endpoint=False) / 1000.0
#print(np.polyfit(np.arange(nint), tabs, 1))
#print(np.polyfit(np.arange(nint), trel, 1))
print(np.mean(tabs - trel), np.std(tabs - trel))
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(10, 8))
ax1.plot(tabs, tabs - trel, label="absolute")
ax1.set_xlabel("Absolute time (s)")
ax1.set_ylabel("Time difference (abs - rel) (s)")
ax2.imshow(data['spec'], aspect="auto", origin="lower", extent=[np.min(freq), np.max(freq), np.min(tabs), np.max(tabs)])
ax2.set_xlabel("Frequency (kHz)")
ax2.set_ylabel("Absolute time (s)")
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment