utils: new and improved plotting scripts

This commit is contained in:
Thomas Kolb 2023-05-17 22:27:40 +02:00
parent a7db93ab9e
commit b5bae84994
3 changed files with 80 additions and 5 deletions

28
impl/utils/plot_amp_phase.py Executable file
View file

@ -0,0 +1,28 @@
#!/usr/bin/env python3
import sys
import numpy as np
import matplotlib.pyplot as pp
with open(sys.argv[1], 'rb') as infile:
header = infile.read(4)
if header == b'CPX_':
T = np.fromfile(infile, dtype=np.float32, count=1)
data = np.fromfile(infile, dtype=np.complex64)
else:
print(f"Error: Format {header} not implemented yet.")
exit(1)
t = np.arange(0, T*data.size, T)
amp = np.absolute(data)
phase = np.angle(data)
pp.plot(t, amp, 'r-')
pp.legend(['Amplitude'])
pp.twinx()
pp.plot(t, phase, 'b-')
pp.legend(['Phase [radians]'])
pp.xlabel('Time [s]')
pp.show()

View file

@ -0,0 +1,44 @@
#!/usr/bin/env python3
import sys
import numpy as np
import matplotlib.pyplot as pp
from matplotlib.animation import FuncAnimation
def load_data():
with open(sys.argv[1], 'rb') as infile:
header = infile.read(4)
if header != b'CPX_':
print(f"Error: not a complex signal file. Format {header} not implemented yet.")
exit(1)
T = np.fromfile(infile, dtype=np.float32, count=1)
data = np.fromfile(infile, dtype=np.complex64)
f = np.arange(-1/(2*T), 1/(2*T), 1/T/data.size)[:data.size]
spec = 20*np.log10(np.absolute(np.fft.fftshift(np.fft.fft(data))) / data.size)
return spec, f
fig = pp.figure()
ax = pp.subplot()
spec, f = load_data()
line, = ax.plot(f, spec, 'b-')
ax.legend(['Spectrum'])
ax.set_xlabel('Frequency [Hz]')
def update(*args):
spec, f = load_data()
line.set_ydata(spec)
return [line]
anim = FuncAnimation(fig, update, interval=500)
pp.show()

View file

@ -7,12 +7,15 @@ import matplotlib.pyplot as pp
with open(sys.argv[1], 'rb') as infile:
header = infile.read(4)
if header != b'CPX_':
print(f"Error: not a complex signal file. Format {header} not implemented yet.")
exit(1)
if header == b'CPX_':
T = np.fromfile(infile, dtype=np.float32, count=1)
data = np.fromfile(infile, dtype=np.complex64)
elif header == b'FLT_':
T = np.fromfile(infile, dtype=np.float32, count=1)
data = np.fromfile(infile, dtype=np.float32)
else:
print(f"Error: Format {header} not implemented yet.")
exit(1)
t = np.arange(0, T*data.size, T)