corr_T = []
for i, t1 in tqdm(enumerate(T), total=len(T)):
# shortcut plot
if i % skip_step != 0:
continue
# pulse width time
if t1 > T.max() - tau_p_sec:
break
_slice_chirp = slice(round(t1*fs_sampling), round((t1+tau_p_sec)*fs_sampling))
# reference pulse
kernel = np.zeros(T.shape)
kernel[_slice_chirp] = ra_chirp_temp
# correlation
corr = correlate(kernel.real[_slice_chirp], reflect_noise.real[_slice_chirp], mode='same')
corr_center = corr[int(len(corr)/2)]
if i == 0:
corr_T.append(corr_center)
else:
corr_T.extend([corr_center]*skip_step)
# visualization
plt.figure(figsize=(26, 18), dpi=200, facecolor='w', edgecolor='k')
plt.subplot(6, 1, 1)
plt.grid(True,)
plt.plot(tau, corr, alpha=0.8, color='green', label='Correlation Value')
plt.scatter([0], corr_center, label='Target Center', color='orange')
plt.scatter([tau[np.argmax(corr)]], corr[np.argmax(corr)], label='Maximum Peak Correlation', color='black')
plt.legend()
plt.ylim(-50, 400)
plt.title(f'Local Correlation Time:{t1: .2f}')
plt.ylabel('Correlation')
plt.subplot(6, 1, 2)
plt.plot(T, kernel, color='orange', label='Kernel')
plt.grid(True,)
plt.title('Reference Pulse Chirp Signal')
plt.xlabel('Time axis [sec]')
plt.ylabel('Amplitude')
reflect_noise = noise.copy()
reflect_noise[slice_chirp] = ((1 - SNR)*ra_chirp_temp + SNR*noise_tau_sec)
plt.subplot(6, 1, 3)
plt.grid(True,)
plt.plot(T, reflect_noise.real, 'c')
plt.xlabel('Time axis [sec]')
plt.ylabel('Real')
plt.subplot(6, 1, 4)
plt.grid(True,)
plt.plot(T, kernel, 'navy', alpha=0.7, label='Kernel')
plt.plot(T, reflect_noise.real, 'magenta', alpha=0.4, label='Noise Reflect')
plt.legend()
plt.ylabel('Amplitude')
plt.subplot(6, 1, 5)
plt.grid(True,)
plt.plot(range(i+1), np.array(corr_T), 'b', alpha=0.8, label='Peak')
plt.scatter([i], corr_center, label='Center Correlation', color='red')
plt.legend()
plt.xlim(0, int(len(T) - len(ra_chirp_temp)))
plt.ylim(-50, 400)
plt.title('Correlation Value')
plt.xlabel('Sample [n]')
plt.ylabel('Correlation')
f, t, Sxx = spectrogram(reflect_noise, fs_sampling)
plt.subplot(6, 1, 6)
plt.grid(True,)
plt.pcolormesh(t, f, Sxx, shading='gouraud', label='spectrum', vmax=1e-1, cmap='inferno')
plt.vlines(t1 + round(tau_p_sec/2), ymin=0, ymax=50, color='red', label='position')
plt.legend()
plt.ylabel('Spectrum')
plt.xlabel('Time [sec]')
plt.tight_layout()
os.makedirs('frames', exist_ok=True)
plt.savefig(os.path.join('frames', f'impulse_respose_frame_{str(i).zfill(5)}.png'), format='png')
plt.clf()
plt.close()