From b5bae8499411e674753709fabdf2633cd8c7b6e5 Mon Sep 17 00:00:00 2001 From: Thomas Kolb Date: Wed, 17 May 2023 22:27:40 +0200 Subject: [PATCH] utils: new and improved plotting scripts --- impl/utils/plot_amp_phase.py | 28 ++++++++++++++++++++ impl/utils/plot_spectrum_loop.py | 44 ++++++++++++++++++++++++++++++++ impl/utils/plot_timedomain.py | 13 ++++++---- 3 files changed, 80 insertions(+), 5 deletions(-) create mode 100755 impl/utils/plot_amp_phase.py create mode 100755 impl/utils/plot_spectrum_loop.py diff --git a/impl/utils/plot_amp_phase.py b/impl/utils/plot_amp_phase.py new file mode 100755 index 0000000..a70a40d --- /dev/null +++ b/impl/utils/plot_amp_phase.py @@ -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() diff --git a/impl/utils/plot_spectrum_loop.py b/impl/utils/plot_spectrum_loop.py new file mode 100755 index 0000000..fd82924 --- /dev/null +++ b/impl/utils/plot_spectrum_loop.py @@ -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() diff --git a/impl/utils/plot_timedomain.py b/impl/utils/plot_timedomain.py index 84c4bd8..9ae8a77 100755 --- a/impl/utils/plot_timedomain.py +++ b/impl/utils/plot_timedomain.py @@ -7,13 +7,16 @@ 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.") + 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.fromfile(infile, dtype=np.float32, count=1) - data = np.fromfile(infile, dtype=np.complex64) - t = np.arange(0, T*data.size, T) pp.plot(t, np.real(data), 'r-')