Skip to content

Commit

Permalink
fix: corrected how the values to increment are computed, now it depen…
Browse files Browse the repository at this point in the history
…ds on the type of data which are in the calibration file (sensitivity or end_to_end values) by adding a new parameter
  • Loading branch information
SimLrt32 committed Aug 11, 2023
1 parent c250b3d commit 53de70a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
28 changes: 20 additions & 8 deletions pyhydrophone/hydrophone.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,18 +150,20 @@ def end_to_end_calibration(self, p_ref=1.0):
gain_upa = (self.Vpp / 2.0) / (mv * ma)
return 10 * np.log10(gain_upa**2)

def get_freq_cal(self, sep=',', freq_col_id=0, sens_col_id=1, start_data_id=0):
def get_freq_cal(self, val='sensitivity', sep=',', freq_col_id=0, val_col_id=1, start_data_id=0):
"""
Compute a dataframe with all the frequency dependent sensitivity values from the calibration file
Parameters
----------
val : str
Can be 'sensitivity' or 'end_to_end' depending on what are the values in the calibration file
sep : str
Separator between the different columns in csv or txt files
freq_col_id : int
Id of the frequency column in the file (starts with 0)
sens_col_id : int
Id of the sensitivity column in the file (starts with 0)
val_col_id : int
Id of the values column in the file (starts with 0)
start_data_id : int
Id of the first line with data (without title) in the file (starts with 0)
"""
Expand All @@ -172,16 +174,16 @@ def get_freq_cal(self, sep=',', freq_col_id=0, sens_col_id=1, start_data_id=0):
elif self.calibration_file.suffix == '.xlsx':
df = pd.read_excel(self.calibration_file, header=None)

df = df.iloc[:, (i for i in range(len(df.columns)) if i == freq_col_id or i == sens_col_id)]
df = df.iloc[:, (i for i in range(len(df.columns)) if i == freq_col_id or i == val_col_id)]
df = df[start_data_id:]
df = df.dropna(subset=[df.columns[0]])
df = df.replace('[A-Za-z:]', '', regex=True).astype(float)
df = df.reset_index(drop=True)
df.columns = ['frequency', 'sensitivity']
df.columns = ['frequency', val]

self.freq_cal = df

def freq_cal_inc(self, frequencies):
def freq_cal_inc(self, frequencies, p_ref=1.0):
"""
Returns a dataframe with the frequency dependent values to increment from the selected frequencies you give from
the data you want to increment
Expand All @@ -190,23 +192,33 @@ def freq_cal_inc(self, frequencies):
----------
frequencies : 1d array
Frequencies from the data you want to increment with frequency dependent calibration
p_ref: float
Reference pressure to compute db from
Returns
-------
df_freq_inc : pandas Dataframe
Frequency dependent values to increment in your data
"""
df = self.freq_cal
val = df.columns[1]
min_freq = df['frequency'][0]
max_freq = df['frequency'][df.shape[0] - 1]
interpol = scipy.interpolate.interp1d(df['frequency'], df['sensitivity'], kind='linear')
interpol = scipy.interpolate.interp1d(df['frequency'], df[val], kind='linear')

frequencies_below = frequencies.compress(frequencies < min_freq)
frequencies_between = frequencies.compress(np.logical_and(frequencies >= min_freq, frequencies <= max_freq))
frequencies_above = frequencies.compress(frequencies > max_freq)

freq_dep_cal = interpol(frequencies_between)
freq_cal_inc = freq_dep_cal - (-1 * self.end_to_end_calibration())

if val == 'sensitivity':
mv = 10 ** (freq_dep_cal / 20.0) * p_ref
ma = 10 ** (self.preamp_gain / 20.0) * p_ref
gain_upa = (self.Vpp / 2.0) / (mv * ma)
freq_cal_inc = 10 * np.log10(gain_upa**2) - self.end_to_end_calibration()
elif val == 'end_to_end':
freq_cal_inc = freq_dep_cal - self.end_to_end_calibration()
freq_cal_inc = np.concatenate((np.zeros(frequencies_below.shape), freq_cal_inc, np.zeros(frequencies_above.shape)))
df_freq_inc = pd.DataFrame(data=np.vstack((frequencies, freq_cal_inc)).T, columns=['frequency', 'inc_value'])

Expand Down
6 changes: 3 additions & 3 deletions pyhydrophone/rtsys.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class RTSys(Hydrophone):
def __init__(self, name, model, serial_number, sensitivity, preamp_gain, Vpp, mode, channel='A',
string_format="%Y-%m-%d_%H-%M-%S", calibration_file=None):
super().__init__(name, model, serial_number, sensitivity, preamp_gain, Vpp, string_format, calibration_file,
sep=';', freq_col_id=0, sens_col_id=1, start_data_id=0)
val='sensitivity', sep=';', freq_col_id=0, val_col_id=1, start_data_id=0)
self.cal_freq = 250
self.cal_value = 114
self.mode = mode
Expand Down Expand Up @@ -285,5 +285,5 @@ def calibrate(self, file_path, zip_mode=False):
self.preamp_gain = ampl
self.Vpp = 5.0

def get_freq_cal(self, sep=';', freq_col_id=0, sens_col_id=1, start_data_id=0):
super().get_freq_cal(sep=sep, freq_col_id=freq_col_id, sens_col_id=sens_col_id, start_data_id=start_data_id)
def get_freq_cal(self, val='sensitivity', sep=';', freq_col_id=0, val_col_id=1, start_data_id=0):
super().get_freq_cal(sep=sep, freq_col_id=freq_col_id, val_col_id=val_col_id, start_data_id=start_data_id)
4 changes: 2 additions & 2 deletions tests/test_new_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ def test_st(self):
serial_number = 67416073
calibration_file = pathlib.Path("./test_data/soundtrap/calibration_data.xlsx")

st = pyhy.SoundTrap(name, model, serial_number, calibration_file=calibration_file, freq_col_id=1,
sens_col_id=29, start_data_id=6)
st = pyhy.SoundTrap(name, model, serial_number, calibration_file=calibration_file, val='sensitivity',
freq_col_id=1, val_col_id=29, start_data_id=6)

def test_amar(self):
# Hydrophone Setup
Expand Down

0 comments on commit 53de70a

Please sign in to comment.