Skip to content
Snippets Groups Projects
Commit b6a2d615 authored by ferrari's avatar ferrari
Browse files

Add spectrogram control

parent b62a26ac
No related branches found
No related tags found
No related merge requests found
......@@ -259,6 +259,8 @@ class Callback(object):
self.cursor = None
self.f_cursor = None
self.reset_b = None
self.spec_b = None
self.nfft = 128
def shift_left(self, event):
self.p = max(0, self.p - FSSR*13)
......@@ -349,7 +351,7 @@ class Callback(object):
if len(click) != 2*int(10e-3*self.sr):
np.pad(click, (0, 2*int(10e-3*self.sr) - len(click)), mode='constant')
self.view_data[self.curr][0][0].set_ydata(norm(click))
spec = np.flipud(20*np.log10(plt.mlab.specgram(click, Fs=self.sr, NFFT=128, noverlap=127)[0]))
spec = np.flipud(20*np.log10(plt.mlab.specgram(click, Fs=self.sr, NFFT=self.nfft, noverlap=self.nfft-1)[0]))
self.view_data[self.curr][1][0].set_data(spec)
self.view_data[self.curr][1][0].set_clim(spec.max()-SPSC, spec.max())
self.view_data[self.curr][2][0].set_ydata(norm(np.correlate(click, click, 'same')[-int(10e-3*self.sr):]))
......@@ -412,6 +414,45 @@ class Callback(object):
plt.pause(0.2)
self.fax.get_figure().set_constrained_layout(False)
def increase_freq(self, event):
self.view_ax[self.curr][1].set_ylim(0, self.view_ax[self.curr][1].get_ylim()[1]+1e3)
plt.draw()
def decrease_freq(self, event):
self.view_ax[self.curr][1].set_ylim(0, max(self.view_ax[self.curr][1].get_ylim()[1]-1e3,1e3))
plt.draw()
def increase_res(self, event):
if self.nfft > int(10e-3*self.sr):
return
click = self.view_data[self.curr][0][0].get_ydata()
self.nfft *= 2
spec = np.flipud(20*np.log10(plt.mlab.specgram(click, Fs=self.sr, NFFT=self.nfft, noverlap=self.nfft-1)[0]))
self.view_data[self.curr][1][0].set_data(spec)
self.view_data[self.curr][1][0].set_clim(spec.max()-SPSC, spec.max())
if self.nfft > int(10e-3*self.sr):
self.spec_b['plus_res'].label.set_text('Can\'t go\nhigher')
else:
self.spec_b['plus_res'].label.set_text(f'{self.nfft*2}\nbins')
self.spec_b['minus_res'].label.set_text(f'{self.nfft//2}\nbins')
plt.draw()
def decrease_res(self, event):
if self.nfft <8:
return
click = self.view_data[self.curr][0][0].get_ydata()
self.nfft //= 2
spec = np.flipud(20*np.log10(plt.mlab.specgram(click, Fs=self.sr, NFFT=self.nfft, noverlap=self.nfft-1)[0]))
self.view_data[self.curr][1][0].set_data(spec)
self.view_data[self.curr][1][0].set_clim(spec.max()-SPSC, spec.max())
self.spec_b['plus_res'].label.set_text(f'{self.nfft*2}\nbins')
if self.nfft <8:
self.spec_b['minus_res'].label.set_text('Can\'t go\nlower')
else:
self.spec_b['minus_res'].label.set_text(f'{self.nfft//2}\nbins')
plt.draw()
def _set_label(self, ind=None, dic=None):
if ind is None:
ind = self.curr
......@@ -431,7 +472,6 @@ class Callback(object):
self.view_data[ind][2][1].set_visible(state)
self.view_data[ind][3][1].set_visible(state)
def reset_curr(self, event):
self.df[self.offset[self.curr_ind[self.curr], 0]] = EMLN.copy()
self._set_label()
......@@ -533,9 +573,21 @@ def init(in_path, channel, low=2e3, high=20e3):
reset_b.on_clicked(callback.reset_curr)
callback.reset_b = reset_b
# text_b_ax = plt.subplot(gs[-1,6:8])
# text_b = TextBox(text_b_ax, 'Individue #\nof current')
# # text_b.on_clicked(callback.resize)
freq_p_b_ax = plt.subplot(gs[2, 0])
freq_m_b_ax = plt.subplot(gs[3, 0])
freq_res_p_b_ax = plt.subplot(gs[4, 0])
freq_res_m_b_ax = plt.subplot(gs[5, 0])
freq_p_b = Button(freq_p_b_ax, '+\n1kHz')
freq_p_b.on_clicked(callback.increase_freq)
freq_m_b = Button(freq_m_b_ax, '-\n1kHz')
freq_m_b.on_clicked(callback.decrease_freq)
freq_res_p_b = Button(freq_res_p_b_ax, '256\nbins')
freq_res_p_b.on_clicked(callback.increase_res)
freq_res_m_b = Button(freq_res_m_b_ax, '64\nbins')
freq_res_m_b.on_clicked(callback.decrease_res)
spec_button = {'plus': freq_p_b, 'minus': freq_m_b, 'plus_res': freq_res_p_b, 'minus_res': freq_res_m_b}
callback.spec_b = spec_button
data_view = [[2 * [None] for _ in range(4)] for _ in range(3)]
m_cursor = [None for _ in range(3)]
......@@ -578,6 +630,8 @@ def init(in_path, channel, low=2e3, high=20e3):
if j != 1:
ax_view[i][j].set_yticks([])
else:
ax_view[i][j].set_ylim(0, min(20e3, sr/2))
ax_view[i][j].set_yticks(ax_view[i][j].get_yticks())
ax_view[i][j].set_yticklabels((ax_view[i][j].get_yticks() / 1e3).astype(int))
# m_cursor2[i][0].linev.set_linestyle('--')
# m_cursor2[i][1].linev.set_linestyle('--')
......@@ -587,7 +641,8 @@ def init(in_path, channel, low=2e3, high=20e3):
callback.view_data = data_view
return {'callback': callback, 'fig': fig, 'gridspec': gs, 'buttons':
{'b_left': b_left, 'b_right': b_right, 'play_b': play_b, 'resize_b': resize_b, 'r_button': r_button,
'fs_click': cid, 'reset_b': reset_b}} # Needed to keep the callbacks alive
'fs_click': cid, 'reset_b': reset_b ,
'spec_button': spec_button}} # Needed to keep the callbacks alive
def reset(callback, in_path, channel, low=2e3, high=20e3):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment