Plots weiter

This commit is contained in:
Patrick Hangl
2026-05-08 12:27:44 +02:00
parent 53fbf70ed1
commit c44a2bf6bc
6 changed files with 154 additions and 74 deletions
+105 -46
View File
@@ -48,7 +48,7 @@
" modified_signal = np.fft.irfft(modified_freq_signal, n=N)\n",
" return modified_signal\n",
"\n",
"# High-Level ANR Algorithmmus\n",
"# High-Level ANR Algorithmmus - nicht deterministisch, daher nicht gleiche Ergebnisse wie in C\n",
"def anr_function(input, ref_noise, coefficients, mu, adaption_step = 1):\n",
" coefficient_matrix = np.zeros((len(input), coefficients), dtype=np.float32)\n",
" output=np.zeros(input.shape[0], dtype=np.float32)\n",
@@ -88,8 +88,8 @@
" error = input[n] - accumulator\n",
" output[n] = error\n",
" # update_filter_coeffcients: Filterkoeffizienten adaptieren\n",
" if (n % adaption_step) == 0: # bei Rate x/adatpion_step: if (n % adaption_step) < x:\n",
" #if abs(error) > 0.00: # nur adaptieren wenn Fehler über Schwellwert\n",
" #if (n % adaption_step) == 0: # bei Rate x/adatpion_step: if (n % adaption_step) < x:\n",
" if (abs(error)*ref_noise[n]) > 0.000001: # nur adaptieren wenn Fehler über Schwellwert\n",
" counter += 1\n",
" filter_line += mu * error * sample_line\n",
" # Filterkoeffizienten expoertieren\n",
@@ -966,6 +966,8 @@
"update_gain_breathing = update_rate_breathing[:, 2]\n",
"update_cycles_breathing = update_rate_breathing[:, 4]\n",
"update_load_breathing = update_rate_breathing[:, 5]\n",
"update_cycles_breathing_new = update_rate_breathing[:, 7]\n",
"update_load_breathing_new = update_rate_breathing[:, 8]\n",
"\n",
"# Sortieren\n",
"idx = np.argsort(x)\n",
@@ -973,6 +975,8 @@
"update_gain_breathing = update_gain_breathing[idx]\n",
"update_cycles_breathing = update_cycles_breathing[idx]\n",
"update_load_breathing = update_load_breathing[idx]\n",
"update_cycles_breathing_new = update_cycles_breathing_new[idx]\n",
"update_load_breathing_new = update_load_breathing_new[idx]\n",
"\n",
"# Smoothing\n",
"x_smooth = np.linspace(x.min(), x.max(), 300)\n",
@@ -980,6 +984,8 @@
"update_gain_smooth = make_interp_spline(x, update_gain_breathing)(x_smooth)\n",
"update_cycles_smooth = make_interp_spline(x, update_cycles_breathing)(x_smooth)\n",
"update_load_smooth = make_interp_spline(x, update_load_breathing)(x_smooth)\n",
"update_cycles_smooth_new = make_interp_spline(x, update_cycles_breathing_new)(x_smooth)\n",
"update_load_smooth_new = make_interp_spline(x, update_load_breathing_new)(x_smooth)\n",
"\n",
"diff_smooth = np.abs(update_gain_smooth - update_cycles_smooth)\n",
"idx_max = np.argmax(diff_smooth)\n",
@@ -988,17 +994,31 @@
"y2_max = update_cycles_smooth[idx_max]\n",
"\n",
"# Plot\n",
"plt.figure(figsize=(15, 7))\n",
"plt.plot(x_smooth, update_gain_smooth, linestyle='--', color='indianred', linewidth=2, alpha=0.9, label='SNR-Gain')\n",
"plt.plot(x_smooth, update_cycles_smooth, linestyle='-.', color='skyblue', linewidth=2, alpha=0.9, label='Cycles/Sample')\n",
"plt.plot(x_smooth, update_load_smooth, linestyle=':', color='forestgreen', linewidth=2, alpha=0.9, label='DSP Load')\n",
"plt.plot([x_max, x_max], [y1_max, y2_max], color='black', linestyle=':', linewidth=2)\n",
"figure1, ax1 = plt.subplots(figsize=(15, 7))\n",
"ax1.plot(x_smooth, update_gain_smooth, linestyle='--', color='indianred', linewidth=2, alpha=0.9, label='SNR-Gain')\n",
"ax1.plot(x_smooth, update_cycles_smooth, linestyle='-.', color='skyblue', linewidth=2, alpha=0.9, label='Cycles/Sample')\n",
"ax1.plot(x_smooth, update_load_smooth, linestyle=':', color='forestgreen', linewidth=2, alpha=0.9, label='DSP Load')\n",
"ax1.plot([x_max, x_max], [y1_max, y2_max], color='black', linestyle=':', linewidth=2)\n",
"\n",
"plt.scatter(x, update_gain_breathing, color='indianred', s=40)\n",
"plt.scatter(x, update_cycles_breathing, color='skyblue', s=40)\n",
"plt.scatter(x, update_load_breathing, color='forestgreen', s=40)\n",
"ax1.scatter(x, update_gain_breathing, color='indianred', s=40)\n",
"ax1.scatter(x, update_cycles_breathing, color='skyblue', s=40)\n",
"ax1.scatter(x, update_load_breathing, color='forestgreen', s=40)\n",
"\n",
"plt.text(x_max, (y1_max + y2_max)/2+0.1,\n",
"# Plot\n",
"figure2, ax2 = plt.subplots(figsize=(15, 7))\n",
"ax2.plot(x_smooth, update_gain_smooth, linestyle='--', color='indianred', linewidth=2, alpha=0.9, label='SNR-Gain')\n",
"ax2.plot(x_smooth, update_cycles_smooth, linestyle='-.', color='skyblue', linewidth=2, alpha=0.3)\n",
"ax2.plot(x_smooth, update_load_smooth, linestyle=':', color='forestgreen', linewidth=2, alpha=0.3)\n",
"ax2.plot(x_smooth, update_cycles_smooth_new, linestyle='-', color='skyblue', linewidth=2, alpha=0.9, label='Cycles/Sample (New)')\n",
"ax2.plot(x_smooth, update_load_smooth_new, linestyle='-', color='forestgreen', linewidth=2, alpha=0.9, label='DSP Load (New)')\n",
"\n",
"ax2.scatter(x, update_gain_breathing, color='indianred', s=40)\n",
"ax2.scatter(x, update_cycles_breathing, color='skyblue', s=40, alpha=0.3)\n",
"ax2.scatter(x, update_load_breathing, color='forestgreen', s=40, alpha=0.3)\n",
"ax2.scatter(x, update_cycles_breathing_new, color='skyblue', s=40)\n",
"ax2.scatter(x, update_load_breathing_new, color='forestgreen', s=40)\n",
"\n",
"ax1.text(x_max, (y1_max + y2_max)/2+0.1,\n",
" f'Max. offset at update rate {x_max:.2f}',\n",
" fontsize=20,\n",
" ha='left')\n",
@@ -1014,19 +1034,30 @@
"})\n",
"\n",
"\n",
"plt.xlabel(\"Update Rate\")\n",
"plt.ylabel(\"Relative Performance\")\n",
"plt.grid(True, linestyle='-.', alpha=0.4)\n",
"ax1.set_xlabel(\"Update Rate\")\n",
"ax1.set_ylabel(\"Relative Performance\")\n",
"ax2.set_xlabel(\"Update Rate\")\n",
"ax2.set_ylabel(\"Relative Performance\")\n",
"ax1.grid(True, linestyle='-.', alpha=0.4)\n",
"ax2.grid(True, linestyle='-.', alpha=0.4)\n",
"#Spines auf ganzen Plot anwenden\n",
"plt.gca().yaxis.set_major_formatter(mtick.PercentFormatter(1.0))\n",
"plt.gca().spines['top'].set_visible(False)\n",
"plt.gca().spines['right'].set_visible(False)\n",
"plt.gca().invert_xaxis()\n",
"plt.legend(frameon=False, loc='upper right')\n",
"plt.tight_layout()\n",
"ax1.yaxis.set_major_formatter(mtick.PercentFormatter(1.0))\n",
"ax2.yaxis.set_major_formatter(mtick.PercentFormatter(1.0))\n",
"ax1.spines['top'].set_visible(False)\n",
"ax1.spines['right'].set_visible(False)\n",
"ax2.spines['top'].set_visible(False)\n",
"ax2.spines['right'].set_visible(False)\n",
"ax1.invert_xaxis()\n",
"ax2.invert_xaxis()\n",
"ax1.legend(frameon=False, loc='upper right')\n",
"ax2.legend(frameon=False, loc='upper right')\n",
"figure1.tight_layout()\n",
"figure2.tight_layout()\n",
"if PLOT == True:\n",
" plt.savefig(f'plots/fig_snr_update_rate', dpi=600)\n",
"plt.show()\n"
" figure1.savefig(f'plots/fig_snr_update_rate', dpi=600)\n",
" figure2.savefig(f'plots/fig_snr_update_rate_new', dpi=600)\n",
"figure1.show()\n",
"figure2.show()\n"
]
},
{
@@ -1232,6 +1263,8 @@
"error_gain = data_error_threshold[:, 2]\n",
"error_cycles = data_error_threshold[:, 6]\n",
"error_load = data_error_threshold[:, 7]\n",
"error_cycles_new = data_error_threshold[:, 9]\n",
"error_load_new = data_error_threshold[:, 10]\n",
"\n",
"# Sortieren\n",
"idx = np.argsort(x)\n",
@@ -1239,13 +1272,16 @@
"error_gain = error_gain[idx]\n",
"error_cycles = error_cycles[idx]\n",
"error_load = error_load[idx]\n",
"\n",
"error_cycles_new = error_cycles_new[idx]\n",
"error_load_new = error_load_new[idx]\n",
"# Smoothing\n",
"x_smooth = np.linspace(x.min(), x.max(), 300)\n",
"\n",
"gain_smooth = make_interp_spline(x, error_gain)(x_smooth)\n",
"cycles_smooth = make_interp_spline(x, error_cycles)(x_smooth)\n",
"comp_smooth = make_interp_spline(x, error_load)(x_smooth)\n",
"load_smooth = make_interp_spline(x, error_load)(x_smooth)\n",
"cycles_smooth_new = make_interp_spline(x, error_cycles_new)(x_smooth)\n",
"load_smooth_new = make_interp_spline(x, error_load_new)(x_smooth)\n",
"\n",
"diff_smooth = np.abs(gain_smooth - cycles_smooth)\n",
"idx_max = np.argmax(diff_smooth)\n",
@@ -1254,17 +1290,31 @@
"y2_max = cycles_smooth[idx_max]\n",
"\n",
"# Plot\n",
"plt.figure(figsize=(15, 7))\n",
"plt.plot(x_smooth, gain_smooth, linestyle='--', color='indianred', linewidth=2, alpha=0.9, label='SNR-Gain')\n",
"plt.plot(x_smooth, cycles_smooth, linestyle='-.', color='skyblue', linewidth=2, alpha=0.9, label='Cycles/Sample')\n",
"plt.plot(x_smooth, comp_smooth, linestyle=':', color='forestgreen', linewidth=2, alpha=0.9, label='DSP Load')\n",
"plt.plot([x_max, x_max], [y1_max, y2_max], color='black', linestyle=':', linewidth=2)\n",
"figure1, ax1 = plt.subplots(figsize=(15, 7))\n",
"ax1.plot(x_smooth, gain_smooth, linestyle='--', color='indianred', linewidth=2, alpha=0.9, label='SNR-Gain')\n",
"ax1.plot(x_smooth, cycles_smooth, linestyle='-.', color='skyblue', linewidth=2, alpha=0.9, label='Cycles/Sample')\n",
"ax1.plot(x_smooth, load_smooth, linestyle=':', color='forestgreen', linewidth=2, alpha=0.9, label='DSP Load')\n",
"ax1.plot([x_max, x_max], [y1_max, y2_max], color='black', linestyle=':', linewidth=2)\n",
"\n",
"plt.scatter(x, error_gain, color='indianred', s=40)\n",
"plt.scatter(x, error_cycles, color='skyblue', s=40)\n",
"plt.scatter(x, error_load, color='forestgreen', s=40)\n",
"ax1.scatter(x, error_gain, color='indianred', s=40)\n",
"ax1.scatter(x, error_cycles, color='skyblue', s=40)\n",
"ax1.scatter(x, error_load, color='forestgreen', s=40)\n",
"\n",
"plt.text(x_max, (y1_max + y2_max)/2+0.21,\n",
"figure2, ax2 = plt.subplots(figsize=(15, 7))\n",
"ax2.plot(x_smooth, gain_smooth, linestyle='--', color='indianred', linewidth=2, alpha=0.9, label='SNR-Gain')\n",
"ax2.plot(x_smooth, cycles_smooth, linestyle='-.', color='skyblue', linewidth=2, alpha=0.3)\n",
"ax2.plot(x_smooth, load_smooth, linestyle=':', color='forestgreen', linewidth=2, alpha=0.3)\n",
"ax2.plot(x_smooth, cycles_smooth_new, linestyle='-', color='skyblue', linewidth=2, alpha=0.9, label='Cycles/Sample (New)')\n",
"ax2.plot(x_smooth, load_smooth_new, linestyle='-', color='forestgreen', linewidth=2, alpha=0.9, label='DSP Load (New)')\n",
"\n",
"\n",
"ax2.scatter(x, error_gain, color='indianred', s=40)\n",
"ax2.scatter(x, error_cycles, color='skyblue', s=40, alpha=0.3)\n",
"ax2.scatter(x, error_load, color='forestgreen', s=40, alpha=0.3)\n",
"ax2.scatter(x, error_cycles_new, color='skyblue', s=40,)\n",
"ax2.scatter(x, error_load_new, color='forestgreen', s=40)\n",
"\n",
"ax1.text(x_max, (y1_max + y2_max)/2+0.21,\n",
" f'Max. offset at threshold {x_max:.2f}',\n",
" fontsize=20,\n",
" ha='left')\n",
@@ -1279,19 +1329,28 @@
" 'legend.fontsize': 25 # Legende\n",
"})\n",
"\n",
"plt.xlabel(\"Error Threshold\")\n",
"plt.ylabel(\"Relative Performance\")\n",
"plt.grid(True, linestyle='-.', alpha=0.4)\n",
"ax1.set_xlabel(\"Error Threshold\")\n",
"ax1.set_ylabel(\"Relative Performance\")\n",
"ax2.set_xlabel(\"Error Threshold\")\n",
"ax2.set_ylabel(\"Relative Performance\")\n",
"ax1.grid(True, linestyle='-.', alpha=0.4)\n",
"ax2.grid(True, linestyle='-.', alpha=0.4)\n",
"#Spines auf ganzen Plot anwenden\n",
"plt.gca().yaxis.set_major_formatter(mtick.PercentFormatter(1.0))\n",
"plt.gca().spines['top'].set_visible(False)\n",
"plt.gca().spines['right'].set_visible(False)\n",
"#plt.gca().invert_xaxis()\n",
"plt.legend(frameon=False, loc='upper right')\n",
"plt.tight_layout()\n",
"ax1.yaxis.set_major_formatter(mtick.PercentFormatter(1.0))\n",
"ax2.yaxis.set_major_formatter(mtick.PercentFormatter(1.0))\n",
"ax1.spines['top'].set_visible(False)\n",
"ax1.spines['right'].set_visible(False)\n",
"ax2.spines['top'].set_visible(False)\n",
"ax2.spines['right'].set_visible(False)\n",
"ax1.legend(frameon=False, loc='upper right')\n",
"ax2.legend(frameon=False, loc='upper right')\n",
"figure1.tight_layout()\n",
"figure2.tight_layout()\n",
"if PLOT == True:\n",
" plt.savefig(f'plots/fig_snr_error_threshold', dpi=600)\n",
"plt.show()\n"
" figure1.savefig(f'plots/fig_snr_error_threshold', dpi=600)\n",
" figure2.savefig(f'plots/fig_snr_error_threshold_new', dpi=600)\n",
"figure1.show()\n",
"figure2.show()\n"
]
},
{
@@ -1473,7 +1532,7 @@
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"display_name": ".venv (3.9.13)",
"language": "python",
"name": "python3"
},