hamnet70/impl/utils/plot_spectrum.py

25 lines
644 B
Python
Raw Normal View History

#!/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_':
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)
pp.plot(f, spec, 'b-')
pp.legend(['Spectrum'])
pp.xlabel('Frequency [Hz]')
pp.show()