\section{High level simulations}
The main purpose of the high-level simulations is to verify and demonstrate the theoretical approach of the previous chapters and to evaluate the performance of the proposed algorithms under various conditions.  The following simulations include different scenarios such as, different types of noise signals and different considerations of transfer functions. The goal is to verify different approaches before taking the step to the implementation of said algorithms on the low-power \ac{DSP}.\\ \\
The implementation is conducted in Python, which provides a flexible environment for numerical computations and data visualization. The simulation is graphically represented using the Matplotlib-library. 
\subsection{Adaptive noise reduction algorithm implementation}
The high-level implementation of the \ac{ANR} algorithm follows the theoretical framework outlined in Subchapter 2.5, specifically Equation \ref{equation_lms}. The algorithm is designed to adaptively filter out noise from a desired signal using a reference noise signal. The implementation of the \ac{ANR} function includes the following key steps:
\begin{itemize}
    \item Initialization: Define arrays to store the reference noise samples (Sample Line), the filter coefficients (Filter Line), the processed output samples (output), and the updated filter coefficients (coefficient\_matrix) over time. Then a sequence of input samples is processed iteratively.
    \item Filtering Process:  The reference noise samples fill up the Sample Line, the filter coefficients in the Filter Line (initially set to zero) are then multiplied with the corresponding reference noise samples before being added up to an accumulator.
    \item Error Calculation: The accumulator is then subtracted from the current input sample to produce the output sample, which represents the error signal.
    \item Coefficient Update: Every filter coefficient is then updated, by adding a term, which consists out of the error signal multiplied by the corresponding reference noise sample, scaled by the step size. The adaption step parameter allows controlling how often the coefficients are updated.
    \item Iteration: Repeat the process for all samples of the input signal.
\end{itemize}
The flow diagram in Figure \ref{fig:fig_anr_logic} illustrates the logical flow of the \ac{ANR} algorithm, while the code snippet in Listing \ref{lst:lst_anr_code} provides the concrete code implementation of the \ac{ANR}-function.
\begin{figure}[H]
    \centering
    \includegraphics[width=0.9\linewidth]{Bilder/fig_anr_logic.jpg}
    \caption{Flow diagram of the code implementation of the \ac{ANR} algorithm.}
    \label{fig:fig_anr_logic}
\end{figure}
\begin{listing}[H]
\centering
\begin{lstlisting}[style=pythonstyle]
def anr_function(input, ref_noise, coefficients, mu, adaption_step=1):
    sample_count = len(input)
    filter_line = np.zeros(coefficients)
    sample_line = np.zeros(coefficients)
    output = np.zeros(sample_count)
    coefficient_matrix = np.zeros((sample_count, coefficients))

    for n in range(sample_count):
        sample_line = np.roll(sample_line, 1)
        sample_line[0] = ref_noise[n]

        accumulator = np.dot(filter_line, sample_line)
        error = input[n] - accumulator
        output[n] = error

        filter_line += mu * error * sample_line
        coefficient_matrix[n, :] = filter_line

    return output, coefficient_matrix
\end{lstlisting}
\caption{High-level implementation of the \ac{ANR} algorithm in Python}
\label{lst:lst_anr_code}
\end{listing}
\noindent The algorithm implementation shall now be put under test by different use cases to demonstrate the functionality and performance under different scenarios, varying from simple to complex ones. Every use case includes graphical representations of the desired signal, the corrupted signal, the reference noise signal, the filter output, the error signal and the evolution of selected filter coefficients over time. In contrary to a realistic setup, the desired signal is available, allowing to evaluate the performance of the algorithm based on the \ac{SNR}-Gain in dB and also visually by the amplitude of the error signal (difference between the desired signal and the filter output). The error signal and the \ac{SNR}-Gain are calculated as follows:
\begin{gather}
    \label{equation_snr_gain_error}
    \text{P}_{\text{Error signal}} = \text{P}_{\text{Desired signal}} - \text{P}_{\text{Filter-output}} \\
    \label{equation_snr_gain}
    \text{SNR-Gain} = 10 \cdot \log_{10}\frac{\text{P}_{\text{Desired signal}}}{\text{P}_{\text{Noise signal}}} - 10 \cdot \log_{10}\frac{\text{P}_{\text{Desired signal}}}{\text{P}_{\text{Error signal}}} 
\end{gather}
with $\text{P}_{\text{Desired signal}}$ being the power of the desired signal, $\text{P}_{\text{Noise signal}}$ being the power of the noise signal and $\text{P}_{\text{Error signal}}$ being the power of the error signal, which is the difference between the desired signal and the filter output. A positive \ac{SNR}-Gain indicates an improvement in signal quality, while a negative \ac{SNR}-Gain indicates a degradation in signal quality after applying the \ac{ANR} algorithm.
\subsection{Simple ANR use cases}
To evaluate the general functionality and performance of the \ac{ANR} algorithm from Listing \ref{lst:lst_anr_code}, a set of three simple, artificial scenarios are introduced. These examples shall serve as a showcase to demonstrate the general functionality, the possibilities and the limitations of the \ac{ANR} algorithm.\\ \\
In all three scenarios, a chirp signal with a frequency range from 100-1000 Hz is used as the desired signal, which is then corrupted with a sine wave (Use case 1 and 2) or a Gaussian white noise (Use case 3) as noise signal respectively. In this simple setup, the corruption noise signal is also available as the reference noise signal. Every approach is conducted with 16 filter coefficients and a step size of 0.01. The four graphs in the respective first plot show the desired signal, the corrupted signal, the reference noise signal and the filter output. The two graphs in the respective second plot show the performance of the filter in form of the resulting error signal and the evolution of three exemplary filter coefficients over time.\\ \\
\noindent This artificial setup could be solved analytically, as the signals do not pass separate, different transfer functions, meaning, that the reference noise signal is the same as the corruption noise signal. Though, this simple setup would not require an adaptive filter approach, it nevertheless allows to clearly evaluate the performance of the \ac{ANR} algorithm in different scenarios. Also, due to the fact that the desired signal is known, it is possible to evaluate the performance of the algorithm in a simple way.
\subsubsection{Simple use case 1: Sine noise at 2000 Hz}
In the first use case, a sine wave with a frequency of 2000 Hz, which lies outside the frequency spectrum of the chirp, is used as noise signal to corrupt the desired signal. The shape of the initial desired signal is still clearly recognizable, even if its shape is affected in the higher frequency area. The filter output in Figure \ref{fig:fig_plot_1_sine_1.png} shows a satisfying performance of the \ac{ANR} algorithm, as the noise is almost completely removed from the corrupted signal after the filter coefficients have adapted.
\begin{figure}[H]
    \centering
    \includegraphics[width=1.0\linewidth]{Bilder/fig_plot_1_sine_1.png}
    \caption{Desired signal, corrupted signal, reference noise signal and filter output of simple use case 1}
    \label{fig:fig_plot_1_sine_1.png}
\end{figure}
\noindent The error signal in Figure \ref{fig:fig_plot_2_sine_1.png} confirms this observation, as the signal converges basically to zero after 500 ms. The evolution of the filter coefficients also indicates a quick convergence, meaning that the algorithm has adapted effectively to minimize the error over time.
\begin{figure}[H]
    \centering
    \includegraphics[width=1.0\linewidth]{Bilder/fig_plot_2_sine_1.png}
    \caption{Error signal and filter coefficient evolution of simple use case 1}
    \label{fig:fig_plot_2_sine_1.png}
\end{figure}
\subsubsection{Simple use case 2: Sine noise at 500 Hz}
The second use case resembles the first one, but instead of a 2000 Hz sine wave, a sine wave with a frequency of 500 Hz is used as noise signal. This means, that the noise signal now overlaps with the frequency spectrum of the chirp signal, making the noise cancellation task more challenging, as an oscillation beacon in the area of 500 Hz appears. Also, in contrary to use case 1, the shape of the initial chirp is now far less recognizable. The filter output in Figure \ref{fig:fig_plot_1_sine_2.png} indicates that the \ac{ANR} algorithm is still able to significantly reduce the noise from the corrupted signal,
\begin{figure}[H]
    \centering
    \includegraphics[width=1.0\linewidth]{Bilder/fig_plot_1_sine_2.png}
    \caption{Desired signal, corrupted signal, reference noise signal and filter output of simple use case 2}
    \label{fig:fig_plot_1_sine_2.png}
\end{figure}
\noindent Figure \ref{fig:fig_plot_2_sine_2.png} shows a significant increase of the amplitude of the error signal compared to use case 1, especially around the 500 Hz frequency of the noise signal. Also, the adaption of the coefficients shows far more variance compared to use case 1, with a complete rearrangement in the area of 500 Hz. This indicates that the \ac{ANR} algorithm is struggling to adapt effectively in a scenario, where the noise signal overlaps with the desired signal.
\begin{figure}[H]
    \centering
    \includegraphics[width=1.0\linewidth]{Bilder/fig_plot_2_sine_2.png}
    \caption{Error signal and filter coefficient evolution of simple use case 2}
    \label{fig:fig_plot_2_sine_2.png}
\end{figure}
\subsubsection{Simple use case 3: Gaussian white noise}
The last on of our three simplified use cases involves the use of a Gaussian white noise signal as the noise signal to corrupt the desired signal. This scenario represents a more complex situation, as white noise contains a broad spectrum of frequencies and is not deterministic, making it more challenging for the \ac{ANR} algorithm to effectively generate a clean output. Nevertheless, the filter output in Figure \ref{fig:fig_plot_1_noise.png} demonstrates that the \ac{ANR} algorithm is capable of significantly reducing the noise from the desired signal, although the amplitude of the filter output varies, indicating difficulties adapting due to the broad frequency spectrum of the noise.
\begin{figure}[H]
    \centering
    \includegraphics[width=1.0\linewidth]{Bilder/fig_plot_1_noise.png}
    \caption{Desired signal, corrupted signal, reference noise signal and filter output of simple use case 3}
    \label{fig:fig_plot_1_noise.png}
\end{figure}
\noindent The error signal in Figure \ref{fig:fig_plot_2_noise.png} shows a noticeable variance compared to the previous use cases, especially at the beginning of the signal, where low frequencies dominate. The evolution of the filter coefficients show an interesting pattern, as only the coefficient in the beginning adapts significantly, while the others remain relatively stable around zero. Also, the \ac{SNR}-Gain of this use case is significantly lower compared to the previous use cases, which can be explained by the fact that the noise signal contains a broad spectrum of frequencies, making it more difficult for the \ac{ANR} algorithm to effectively isolate and reduce the noise from the desired signal.
\begin{figure}[H]
    \centering
    \includegraphics[width=1.0\linewidth]{Bilder/fig_plot_2_noise.png}
    \caption{Error signal and filter coefficient evolution of simple use case 3}
    \label{fig:fig_plot_2_noise.png}
\end{figure}
\subsection{Intermediate use case}
After the general functionality of the \ac{ANR} algorithm has been verified with the above simple and artificial use cases, a more complex and intermediate scenario is now introduced. In this use case, a real-world audio track of a person speaking on TV (see top graph in Figure \ref{fig:fig_plot_1_wav.png}) is used as the desired signal, which is then corrupted with a dominant breathing noise as the noise signal. This scenario represents a more realistic application of the \ac{ANR} algorithm, as it involves complex audio signals with varying frequency components and relatively high dynamics, but still keeps the advantage of having the clean signal available for performance evaluation. Also, again, the same noise which corrupts the desired signal is used as the reference noise signal, as no transfer functions are applied on the signals.
\begin{figure}[H]
    \centering
    \includegraphics[width=1.0\linewidth]{Bilder/fig_plot_1_wav.png}
    \caption{Desired signal, corrupted signal, reference noise signal and filter output of the intermediate \ac{ANR} use case}
    \label{fig:fig_plot_1_wav.png}
\end{figure}
\noindent The filter output in Figure \ref{fig:fig_plot_1_wav.png} indicates already graphically, that the audio track of the person speaking is significantly more intelligible after the application of the \ac{ANR} algorithm - the prominent breathing noise is clearly reduced in the filter output compared to the corrupted signal.
\begin{figure}[H]
    \centering
    \includegraphics[width=1.0\linewidth]{Bilder/fig_plot_2_wav.png}
    \caption{Error signal and filter coefficient evolution of the intermediate \ac{ANR} use case}
    \label{fig:fig_plot_2_wav.png}
\end{figure}
\noindent The error signal in Figure \ref{fig:fig_plot_2_wav.png} confirms the function of the algorithm and shows maxima corresponding to the maxima in the breathing noise, indicating the moments, when the \ac{ANR} algorithm is setting its coefficients again to adapt to the changing noise characteristics. It makes sense, that the adaption of the filter coefficients causes repeating maxima in the error signal, as the noise signal now is not static or periodic, but rather dynamic and changing it frequency and amplitude over time. The \ac{SNR}-Gain of 6.41 dB also indicates a significant improvement in signal quality and can be compared against the complex use case in the next subchapter, where the same audio tracks are used, but now with different transfer functions applied to them.
\subsection{Complex ANR use case}
To close the topic of high-level simulations of the \ac{ANR} algorithm, a more complex and realistic use case is finally introduced. In this scenario, the same two audio tracks of the previous use case are utilized - but now they pass different transfer functions, which mimic the case that the sensor recording the corrupted signal, shows another frequency response characteristic as the one recording the reference noise signal. Additionaly, a delay of 2 ms between the corruption noise signal and the reference noise signal is introduced - this simulates the realsitic case, that the microphones are located on different locations, leading to a small delay due to traveling time of the signal. Now, an analytical solution is not possible anymore, as the signals are affected in different ways, making it impossible to simply subtract the noise signal from the corrupted signal. This scenario represents a realistic application of the \ac{ANR} algorithm, as it involves complex audio signals as well as signals passing different signal paths while being delayed.\\ \\
Using  a sampling frequency of 20 kHz, a delay of 2 ms represents a shift of 40 samples. As the corrpution noise signal, which the alorithm tries to filter out of the corrupoted signal, is now delayed by these 40 samples, the filter needs more coefficients to aqquire a certain ``memory'' - this means, that the used 16 tap filter is not sufficient anymore. To prove the correct function of the \ac{ANR} algorithm, the filter length is increased by 40 taps up to 56 taps. \\ \\
The now introduced challenges are making adaptive noise reduction the only feasible approach to reduce the noise from the corrupted signal.
\begin{figure}[H]
    \centering
    \includegraphics[width=1.0\linewidth]{Bilder/fig_plot_3_wav_complex.png}
    \caption{Two different sensor sensitivity curves used for recording the corrupted signal and the reference noise signal}
    \label{fig:fig_plot_3_wav_complex.png}
\end{figure}
\noindent Figure \ref{fig:fig_plot_3_wav_complex.png} illustrates the sensitivity curve of two different microphones used to record the corrupted signal and the reference noise signal respectively, resulting in two different transfer functions applied to the signals. The effect of transfer functions on the noise signal is shown in Figure \ref{fig:fig_plot_4_wav_complex.png}, where the top graph shows the noise signal at its source, while the second and third graph show the effect of the two different transfer functions resulting in the corruption noise signal and the reference noise signal respectively.
\begin{figure}[H]
    \centering
    \includegraphics[width=1.0\linewidth]{Bilder/fig_plot_4_wav_complex.png}
    \caption{The raw noise signal recorded with two different sensors, showing the effect of different transfer functions on the signal}
    \label{fig:fig_plot_4_wav_complex.png}
\end{figure}
\noindent To evaluate the performance of the \ac{ANR} algorithm in this complex scenario, the corrupted signal is recorded with the primary sensor while the reference noise signal is recorded with secondary sensor. The filter output and \ac{SNR}-Gain in Figure \ref{fig:fig_plot_1_wav_complex.png} display an excellent performance at 10.26 dB. 
\begin{figure}[H]
    \centering
    \includegraphics[width=1.0\linewidth]{Bilder/fig_plot_1_wav_complex.png}
    \caption{Desired signal, corrupted signal, reference noise signal and filter output of the complex \ac{ANR} use case}
    \label{fig:fig_plot_1_wav_complex.png}
\end{figure}
\noindent The error signal in Figure \ref{fig:fig_plot_2_wav_complex.png} shows, as expected, a small and further decreasing amplitude, indicating a nearly full removal of the corruption noise. 
\begin{figure}[H]
    \centering
    \includegraphics[width=1.0\linewidth]{Bilder/fig_plot_2_wav_complex.png}
    \caption{Error signal and filter coefficient evolution of the complex \ac{ANR} use case}
    \label{fig:fig_plot_2_wav_complex.png}
\end{figure}
\noindent As now the functionality of the \ac{ANR} algorithm has been verified in different scenarios, varying from simple to complex, the next chapter of this thesis focuses on the implementation of the algorithm on the low-power \ac{DSP}.