mirror of
https://gitlab.science.ru.nl/mthesis-edeboone/m-thesis-introduction.git
synced 2024-12-22 11:33:32 +01:00
ZH:lib/snr optional debugging plot in function
This commit is contained in:
parent
91016be038
commit
370b6f366a
2 changed files with 77 additions and 16 deletions
|
@ -49,12 +49,12 @@ if __name__ == "__main__":
|
||||||
os.makedirs(fig_dir, exist_ok=True)
|
os.makedirs(fig_dir, exist_ok=True)
|
||||||
|
|
||||||
# Read in antennas from file
|
# Read in antennas from file
|
||||||
f_beacon, tx, antennas = beacon.read_beacon_hdf5(antennas_fname)
|
f_beacon, tx, antennas = beacon.read_beacon_hdf5(antennas_fname, traces_key='filtered_traces')
|
||||||
_, __, txdata = beacon.read_tx_file(tx_fname)
|
_, __, txdata = beacon.read_tx_file(tx_fname)
|
||||||
|
|
||||||
# general properties
|
# general properties
|
||||||
dt = antennas[0].t[1] - antennas[0].t[0] # ns
|
dt = antennas[0].t[1] - antennas[0].t[0] # ns
|
||||||
beacon_pb = lib.passband(f_beacon-1e-3, f_beacon+1e-3) # GHz
|
beacon_pb = lib.passband(f_beacon, None) # GHz
|
||||||
beacon_amp = np.max(txdata['amplitudes'])# mu V/m
|
beacon_amp = np.max(txdata['amplitudes'])# mu V/m
|
||||||
|
|
||||||
# General Bandpass
|
# General Bandpass
|
||||||
|
@ -62,19 +62,39 @@ if __name__ == "__main__":
|
||||||
high_bp = args.passband_high if args.passband_high >= 0 else np.inf # GHz
|
high_bp = args.passband_high if args.passband_high >= 0 else np.inf # GHz
|
||||||
pb = lib.passband(low_bp, high_bp) # GHz
|
pb = lib.passband(low_bp, high_bp) # GHz
|
||||||
|
|
||||||
|
noise_pb = pb
|
||||||
|
|
||||||
if args.use_passband: # Apply filter to raw beacon/noise to compare with Filtered Traces
|
if args.use_passband: # Apply filter to raw beacon/noise to compare with Filtered Traces
|
||||||
myfilter = lambda x: block_filter(x, dt, pb[0], pb[1])
|
myfilter = lambda x: block_filter(x, dt, pb[0], pb[1])
|
||||||
else: # Compare raw beacon/noise with Filtered Traces
|
else: # Compare raw beacon/noise with Filtered Traces
|
||||||
myfilter = lambda x: x
|
myfilter = lambda x: x
|
||||||
|
|
||||||
##
|
##
|
||||||
|
## Debug plot of Beacon vs Noise SNR
|
||||||
|
##
|
||||||
|
if True:
|
||||||
|
ant = antennas[0]
|
||||||
|
|
||||||
|
fig, ax = plt.subplots(figsize=figsize)
|
||||||
|
_ = lib.signal_to_noise(myfilter(beacon_amp*ant.beacon), myfilter(ant.noise), samplerate=1/dt, signal_band=beacon_pb, noise_band=noise_pb, debug_ax=ax)
|
||||||
|
|
||||||
|
ax.set_title("Spectra and passband")
|
||||||
|
ax.set_xlabel("Frequency")
|
||||||
|
ax.set_ylabel("Amplitude")
|
||||||
|
low_x, high_x = min(beacon_pb[0], noise_pb[0]), max(beacon_pb[1] or 0, noise_pb[1])
|
||||||
|
ax.set_xlim(low_x, high_x)
|
||||||
|
|
||||||
|
if fig_dir:
|
||||||
|
fig.savefig(path.join(fig_dir, path.basename(__file__) + f".debug_plot.pdf"))
|
||||||
|
##
|
||||||
## Beacon vs Noise SNR
|
## Beacon vs Noise SNR
|
||||||
##
|
##
|
||||||
if True:
|
if True:
|
||||||
beacon_snrs = [ lib.signal_to_noise(myfilter(beacon_amp*ant.beacon), myfilter(ant.noise), samplerate=1/dt, signal_band=beacon_pb, noise_band=pb) for ant in antennas ]
|
N_samples = len(antennas[0].beacon)
|
||||||
|
beacon_snrs = [ lib.signal_to_noise(myfilter(beacon_amp*ant.beacon), myfilter(ant.noise), samplerate=1/dt, signal_band=beacon_pb, noise_band=noise_pb) for i, ant in enumerate(antennas) ]
|
||||||
|
|
||||||
fig, ax = plt.subplots(figsize=figsize)
|
fig, ax = plt.subplots(figsize=figsize)
|
||||||
ax.set_title("Maximum Beacon/Noise SNR")
|
ax.set_title(f"Maximum Beacon/Noise SNR (N_samples:{N_samples:.1e})")
|
||||||
ax.set_xlabel("Antenna no.")
|
ax.set_xlabel("Antenna no.")
|
||||||
ax.set_ylabel("SNR")
|
ax.set_ylabel("SNR")
|
||||||
ax.plot([ int(ant.name) for ant in antennas], beacon_snrs, 'o', ls='none')
|
ax.plot([ int(ant.name) for ant in antennas], beacon_snrs, 'o', ls='none')
|
||||||
|
|
|
@ -1,6 +1,10 @@
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
|
|
||||||
|
from lib import direct_fourier_transform as dtft
|
||||||
|
|
||||||
|
import matplotlib.pyplot as plt # for debug plotting
|
||||||
|
|
||||||
passband = namedtuple("passband", ['low', 'high'], defaults=[0, np.inf])
|
passband = namedtuple("passband", ['low', 'high'], defaults=[0, np.inf])
|
||||||
|
|
||||||
def get_freq_spec(val,dt):
|
def get_freq_spec(val,dt):
|
||||||
|
@ -27,9 +31,19 @@ def bandpass_mask(freqs, band=passband()):
|
||||||
|
|
||||||
return low_pass & high_pass
|
return low_pass & high_pass
|
||||||
|
|
||||||
def bandpower(samples, samplerate=1, band=passband(), normalise_bandsize=True):
|
def bandpower(samples, samplerate=1, band=passband(), normalise_bandsize=True, debug_ax=False):
|
||||||
fft, freqs = get_freq_spec(samples, samplerate)
|
bins = 0
|
||||||
|
fft, freqs = get_freq_spec(samples, 1/samplerate)
|
||||||
|
bandmask = [False]*len(freqs)
|
||||||
|
|
||||||
|
if band[1] is None:
|
||||||
|
# Only a single frequency given
|
||||||
|
# use a DTFT for finding the power
|
||||||
|
time = np.arange(0, len(samples), 1/samplerate)
|
||||||
|
|
||||||
|
real, imag = dtft(band[0], time, samples)
|
||||||
|
power = np.sum(np.abs(real**2 + imag**2))
|
||||||
|
else:
|
||||||
bandmask = bandpass_mask(freqs, band=band)
|
bandmask = bandpass_mask(freqs, band=band)
|
||||||
|
|
||||||
if normalise_bandsize:
|
if normalise_bandsize:
|
||||||
|
@ -37,20 +51,47 @@ def bandpower(samples, samplerate=1, band=passband(), normalise_bandsize=True):
|
||||||
else:
|
else:
|
||||||
bins = 1
|
bins = 1
|
||||||
|
|
||||||
power = np.sum(np.abs(fft[bandmask])**2)
|
bins = max(1, bins)
|
||||||
|
|
||||||
return power/bins
|
power = 1/bins * np.sum(np.abs(fft[bandmask])**2)
|
||||||
|
|
||||||
def signal_to_noise(samples, noise, samplerate=1, signal_band=passband(), noise_band=None):
|
# Prepare plotting variables if an Axes is supplied
|
||||||
|
if debug_ax:
|
||||||
|
if any(bandmask):
|
||||||
|
min_f, max_f = min(freqs[bandmask]), max(freqs[bandmask])
|
||||||
|
else:
|
||||||
|
min_f, max_f = 0, 0
|
||||||
|
|
||||||
|
if band[1] is None:
|
||||||
|
min_f, max_f = band[0], band[0]
|
||||||
|
|
||||||
|
if debug_ax is True:
|
||||||
|
debug_ax = plt.gca()
|
||||||
|
|
||||||
|
l = debug_ax.plot(freqs, np.abs(fft), alpha=0.9)
|
||||||
|
|
||||||
|
amp = np.sqrt(power)
|
||||||
|
|
||||||
|
if min_f != max_f:
|
||||||
|
debug_ax.plot( [min_f, max_f], [amp, amp], alpha=0.7, color=l[0].get_color(), ls='dashed')
|
||||||
|
debug_ax.axvspan(min_f, max_f, color=l[0].get_color(), alpha=0.2)
|
||||||
|
else:
|
||||||
|
debug_ax.plot( min_f, amp, '4', alpha=0.7, color=l[0].get_color(), ms=10)
|
||||||
|
|
||||||
|
return power
|
||||||
|
|
||||||
|
def signal_to_noise(samples, noise, samplerate=1, signal_band=passband(), noise_band=None, debug_ax=False):
|
||||||
if noise_band is None:
|
if noise_band is None:
|
||||||
noise_band = signal_band
|
noise_band = signal_band
|
||||||
|
|
||||||
if noise is None:
|
if noise is None:
|
||||||
noise = samples
|
noise = samples
|
||||||
|
|
||||||
noise_power = bandpower(noise, samplerate, noise_band)
|
if debug_ax is True:
|
||||||
|
debug_ax = plt.gca()
|
||||||
|
|
||||||
signal_power = bandpower(samples, samplerate, signal_band)
|
noise_power = bandpower(noise, samplerate, noise_band, debug_ax=debug_ax)
|
||||||
|
|
||||||
|
signal_power = bandpower(samples, samplerate, signal_band, debug_ax=debug_ax)
|
||||||
|
|
||||||
return (signal_power/noise_power)**0.5
|
return (signal_power/noise_power)**0.5
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue