def plot_spectrum(f, p, cen=0.0, span=None, dbm=False, filename=None, title='Spectrum'): """Plot average power in dBm per Hz""" if not span: mask = (f != 0) | (f == 0) else: mask = (f <= span / 2) & (f >= -span / 2) if dbm: plt.plot(f[mask], IQBase.get_dbm(p[mask])) else: plt.plot(f[mask], p[mask]) plt.xlabel("Delta f [Hz] @ {}".format(get_eng_notation(cen, 'Hz'))) plt.title(title) if dbm: plt.ylabel('Power Spectral Density [dBm/Hz]') else: plt.ylabel('Power Spectral Density') plt.grid(True) if filename is not None: plt.savefig(filename + '.png') # , bbox_inches='tight') plt.close()
def plot_spectrogram(xx, yy, zz, cen=0.0, cmap=cm.jet, dpi=300, dbm=False, filename=None, title='Spectrogram'): """ Plot the calculated spectrogram :param xx: :param yy: :param zz: :param cen: :return: """ delta_f = np.abs(np.abs(xx[0, 1]) - np.abs(xx[0, 0])) delta_t = np.abs(np.abs(yy[1, 0]) - np.abs(yy[0, 0])) if dbm: sp = plt.pcolormesh(xx, yy, IQBase.get_dbm(zz), cmap=cmap) else: sp = plt.pcolormesh(xx, yy, zz, cmap=cmap) cb = plt.colorbar(sp) plt.xlabel( "Delta f @ {} (resolution = {})".format(get_eng_notation(cen, unit='Hz'), get_eng_notation(delta_f, unit='Hz'))) plt.ylabel('Time [sec] (resolution = {})'.format( get_eng_notation(delta_t, 's'))) plt.title(title) if dbm: cb.set_label('Power Spectral Density [dBm/Hz]') else: cb.set_label('Power Spectral Density') if filename is not None: plt.savefig(filename + '.png', dpi=dpi, bbox_inches='tight') plt.close()
def read_result_xml(filename): """ Read the resulting saved trace file SpecAn from the RSA5000 series :param filename: :return: """ with open(filename, 'rb') as f: ba = f.read() xml_tree_root = et.fromstring(ba) for elem in xml_tree_root.iter(tag='Count'): count = int(elem.text) for elem in xml_tree_root.iter(tag='XStart'): start = float(elem.text) for elem in xml_tree_root.iter(tag='XStop'): stop = float(elem.text) for elem in xml_tree_root.iter(tag='y'): pwr = float(elem.text) p = np.zeros(count) i = 0 for elem in xml_tree_root.iter(tag='y'): p[i] = float(elem.text) i += 1 f = np.linspace(start, stop, count) # return watts for compatibility return f, IQBase.get_watt(p)
def write_spectrum_to_csv(ff, pp, filename, center=0): a = np.concatenate((ff, pp, IQBase.get_dbm(pp))) b = np.reshape(a, (3, -1)).T np.savetxt( filename, b, header='Delta f [Hz] @ {:.2e} [Hz]|Power [W]|Power [dBm]'.format( center), delimiter='|')
def plot_frame_power(yy, frame_power): """ Plot frame power, i.e. trapezoid along each time frame :param yy: :param frame_power: :return: """ plt.plot(yy[:, 0], IQBase.get_dbm(frame_power)) plt.ylabel('Power [dBm]') plt.xlabel('Time [sec]') plt.title('Frame power')
def plot_dbm_per_hz(f, p, cen=0.0, span=None, filename='', to_file=False): """Plot average power in dBm per Hz""" if not span: mask = (f != 0) | (f == 0) else: mask = (f <= span / 2) & (f >= -span / 2) plt.plot(f[mask], IQBase.get_dbm(p[mask])) plt.xlabel("Delta f [Hz] @ {}".format(get_eng_notation(cen, 'Hz'))) plt.title(filename) plt.ylabel("Power Spectral Density [dBm/Hz]") plt.grid(True) if to_file: plt.savefig(filename) plt.close()
def plot_spectrogram(xx, yy, zz, cen=0.0, cmap=cm.jet, dpi=300, dbm=False, filename=None, title='Spectrogram'): """ Plot the calculated spectrogram :param xx: :param yy: :param zz: :param cen: :return: """ delta_f = np.abs(np.abs(xx[0, 1]) - np.abs(xx[0, 0])) delta_t = np.abs(np.abs(yy[1, 0]) - np.abs(yy[0, 0])) if dbm: sp = plt.pcolormesh(xx, yy, IQBase.get_dbm(zz), cmap=cmap) else: sp = plt.pcolormesh(xx, yy, zz, cmap=cmap) cb = plt.colorbar(sp) plt.xlabel("Delta f @ {} (resolution = {})".format( get_eng_notation(cen, unit='Hz'), get_eng_notation(delta_f, unit='Hz'))) plt.ylabel('Time [sec] (resolution = {})'.format( get_eng_notation(delta_t, 's'))) plt.title(title) if dbm: cb.set_label('Power Spectral Density [dBm/Hz]') else: cb.set_label('Power Spectral Density') if filename is not None: plt.savefig(filename + '.png', dpi=dpi, bbox_inches='tight') plt.close()