diff --git a/app/cocal_methods.py b/app/cocal_methods.py index 9e93f0593ffe0aa8df43d0d90959ea762dfd7dd6..95ec856b0046ee11e13b595b68ad1ef83dec1b64 100644 --- a/app/cocal_methods.py +++ b/app/cocal_methods.py @@ -47,6 +47,7 @@ class CocalMethods: self.dut_path = self.session_dir.joinpath("upload.wav") self.dcc_path = self.session_dir.joinpath("dcc.xml") + self.result_image_path = self.session_dir.joinpath("transfer_behaviour.png") ##### TESTING # self.ref_path = os.path.abspath( @@ -375,6 +376,54 @@ class CocalMethods: return tree + def generate_spectral_domain_visualization(self): + + # visualize coefficient uncertainties by plotting the spectrum uncertainty via MC + draw_samples = lambda size: np.random.multivariate_normal( + ba, ba_cov, size=size + ) + + w = np.linspace(0, np.pi, 50) + f = w / (2 * np.pi) * frame_rate + w_exp = np.exp(-1j * w) # ??? + evaluate = lambda sample: complex_2_real_imag( + self.freqz_core(sample, Nb, w_exp) + ) + + umc_kwargs = { + "draw_samples": draw_samples, + "evaluate": evaluate, + "runs": 20, + "blocksize": 8, + "n_cpu": 1, + "compute_full_covariance": True, + "return_histograms": False, + } + + h_ri, h_cov, _, output_shape = UMC_generic(**umc_kwargs) + h = real_imag_2_complex(h_ri) + h_unc = real_imag_2_complex(np.sqrt(np.diag(h_cov))) + + # visualize result + fig, ax = plt.subplots(2, 1) + ax[0].plot(f, np.abs(h)) + ax[0].fill_between( + f, + np.abs(h) - np.abs(h_unc), + np.abs(h) + np.abs(h_unc), + alpha=0.3, + label="unc", + ) + ax[0].scatter( + self.ref_frequency[mask], + np.abs(self.dut_spectrum[mask] / self.ref_spectrum[mask]), + label="ref", + ) + ax[0].legend() + ax[1].plot(f, h_unc) + plt.savefig(self.result_image_path) + #plt.show() + def perform_dummy_computations(self): self.start_date = datetime.date.today().isoformat() @@ -734,52 +783,6 @@ class CocalMethods: self.transfer_behavior = {"IIR": {"a": a, "b": b, "Uba": ba_cov}} - if self.generate_plots: - # visualize coefficient uncertainties by plotting the spectrum uncertainty via MC - draw_samples = lambda size: np.random.multivariate_normal( - ba, ba_cov, size=size - ) - - w = np.linspace(0, np.pi, 50) - f = w / (2 * np.pi) * frame_rate - w_exp = np.exp(-1j * w) # ??? - evaluate = lambda sample: complex_2_real_imag( - self.freqz_core(sample, Nb, w_exp) - ) - - umc_kwargs = { - "draw_samples": draw_samples, - "evaluate": evaluate, - "runs": 20, - "blocksize": 8, - "n_cpu": 1, - "compute_full_covariance": True, - "return_histograms": False, - } - - h_ri, h_cov, _, output_shape = UMC_generic(**umc_kwargs) - h = real_imag_2_complex(h_ri) - h_unc = real_imag_2_complex(np.sqrt(np.diag(h_cov))) - - # visualize result - fig, ax = plt.subplots(2, 1) - ax[0].plot(f, np.abs(h)) - ax[0].fill_between( - f, - np.abs(h) - np.abs(h_unc), - np.abs(h) + np.abs(h_unc), - alpha=0.3, - label="unc", - ) - ax[0].scatter( - self.ref_frequency[mask], - np.abs(self.dut_spectrum[mask] / self.ref_spectrum[mask]), - label="ref", - ) - ax[0].legend() - ax[1].plot(f, h_unc) - plt.savefig(self.session_dir.joinpath("spectrum_uncertainty.png")) - #plt.show() def hull_correlation_offset(self, signal_a, signal_b): # estimate delay between two signals by comparing their hulls diff --git a/app/main.py b/app/main.py index 19ff5d1611024009fb5d244f36bda1e93153bd3a..c5346555ff4c338f87081b2fd69d5bc89aa9e3ac 100644 --- a/app/main.py +++ b/app/main.py @@ -75,6 +75,9 @@ def cocal_task(db: Session = Depends(get_db), hash = hash): # generate (dummy) DCC cocal.generate_dcc() # generate_dummy_dcc + # generate visualization of transfer behaviour + cocal.generate_spectral_domain_visualization() + # release state crud.set_status(db, name="general", state="ready")