\documentclass[english,notitlepage,nofootinbib]{revtex4-2}
\usepackage[utf8]{inputenc} 
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{hyperref}
\usepackage{listings}

\begin{document}

\title{SONAR Imaging: IN4015 Module 6}  % self-explanatory
\author{Sebastian Koranda} % self-explanatory
\date{\today}                             % self-explanatory
\noaffiliation 
\begin{abstract}
\begin{itemize}
    \item Sampling frequency: $f_s = 4.0850 \times 10^4 \ \mathrm{Hz}$
    \item Center frequency: $f_0 = 1 \times 10^5 \ \mathrm{Hz}$
    \item Bandwidth: $B = 3 \times 10^4 \ \mathrm{Hz}$
    \item Pulse length: $t_p = 8 \times 10^{-3} \ \mathrm{s}$
    \item Element spacing: $d = 0.0375 \ \mathrm{m}$
    \item Sound speed: $c = 1487.8 \ \mathrm{m/s}$
    \item Array aperture: $D = N \times d$ (where N is number of elements)
\end{itemize}
\end{abstract}
\maketitle

\section*{a)}
Assuming a 32-element array (typical for this system): $N = 32$, $D = N \times d = 32 \times 0.0375 = 1.2 \ \mathrm{m}$

Wavelength: $\lambda = \frac{c}{f_0} = \frac{1487.8}{10^5} = 0.014878 \ \mathrm{m}$

\begin{itemize}
    \item \textbf{Theoretical angular resolution at center frequency:} 
    \[\Delta \theta = \frac{\lambda}{D} = \frac{0.014878}{1.2} = 0.0124 \ \mathrm{rad} = 0.71^\circ\]
    
    \item \textbf{Angular field of view of the system at center frequency:} 
    \[\Theta_{FOV} = 2 \sin^{-1}\left(\frac{\lambda}{2d}\right) = 2 \sin^{-1}\left(\frac{0.014878}{2 \times 0.0375}\right) = 2 \sin^{-1}(0.1984) = 2 \times 11.43^\circ = 22.9^\circ\]
    This gives a field of view of $\pm 11.4^\circ$ from broadside.
    
    \item \textbf{Range (depth) resolution:} 
    \[\Delta R = \frac{c}{2B} = \frac{1487.8}{2 \times 3 \times 10^4} = 0.0248 \ \mathrm{m} = 2.48 \ \mathrm{cm}\]
\end{itemize}

\section*{b)}
Chose to implement pulse compression using the FFT-based method for efficiency. The following MATLAB code performs pulse compression on the provided channel data.
\begin{lstlisting}[language=Octave]
    bw = 30000;     % Bandwidth in Hertz
    t_p = 0.0080;   % Pulse Length in seconds

    t = 0:1/channel_data.sampling_frequency:t_p;
    f0 = channel_data.modulation_frequency - bw/2; % Start frequency of the chirp
    f1 = channel_data.modulation_frequency + bw/2; % End frequency of the chirp
    tx_pulse = chirp(t,f0,t_p,f1,'linear');

    channel_data_compressed = uff.channel_data(channel_data);
    channel_data_compressed.initial_time = t_p/2;

    match_filtered_data = zeros(channel_data.N_samples, channel_data.N_elements);
    matched_filter = conj(tx_pulse);

    matched_filter_padded = zeros(channel_data.N_samples, 1);
    matched_filter_padded(1:length(matched_filter)) = matched_filter;

    H = fft(matched_filter_padded);

    for ch = 1:channel_data.N_elements
        raw_data_ch = channel_data.data(:,ch);
        
        X = fft(raw_data_ch); 
        Y = X .* H;
        
        compressed_data_ch = ifft(Y);
        
        match_filtered_data(:,ch) = compressed_data_ch;
    end
\end{lstlisting}
\begin{figure}[h]
    \centering
    \includegraphics[width=0.8\textwidth]{figs/figure1.pdf}
    \caption{Matched Filtered Data}
    \label{fig:figs/figure1.pdf}
\end{figure}

\section*{c) Beamforming}

\subsection*{Sector Scan Setup}
The sector scan was defined with the following parameters:
\begin{itemize}
    \item \textbf{Azimuth range:} $-30^\circ$ to $+30^\circ$ (256 points)
    \item \textbf{Depth range:} 1 m to maximum range (512 points)
    \item \textbf{Resolution:} $< 20$ cm in both dimensions
\end{itemize}

The beamformer used delay-and-sum (DAS) with Hamming window apodization for both transmit and receive to suppress grating lobes caused by spatial aliasing (d/λ = 2.52 > 0.5).

\subsection*{Beamforming Code}
\begin{lstlisting}[language=Octave]
    % Define sector scan
    scan = uff.sector_scan();
    scan.azimuth_axis = linspace(-30*pi/180, 30*pi/180, 256);
    scan.depth_axis = linspace(1, max_range, 512);

    % Beamform raw data
    mid = midprocess.das();
    mid.channel_data = channel_data;
    mid.scan = scan;
    mid.transmit_apodization.window = uff.window.hamming;
    mid.receive_apodization.window = uff.window.hamming;
    b_data = mid.go();

    % Beamform pulse compressed data
    mid_compressed = midprocess.das();
    mid_compressed.channel_data = channel_data_compressed;
    mid_compressed.scan = scan;
    mid_compressed.transmit_apodization.window = uff.window.hamming;
    mid_compressed.receive_apodization.window = uff.window.hamming;
    b_data_compressed = mid_compressed.go();
\end{lstlisting}

\subsection*{Analysis}

\textbf{Difference before and after beamforming:}

Before beamforming, the channel data shows raw acoustic returns from each individual transducer element. The data is organized as time-series signals for each channel, making it difficult to interpret spatial information about targets. After beamforming, the delay-and-sum algorithm coherently combines signals from all elements to form a focused image in angular coordinates. This process:
\begin{itemize}
    \item Enhances signal-to-noise ratio through coherent summation
    \item Provides angular resolution according to the array aperture ($\Delta\theta = \lambda/D \approx 0.71^\circ$)
    \item Creates an interpretable 2D image in range-azimuth coordinates
    \item Suppresses signals from directions outside the main beam
    \item Hamming apodization suppresses grating lobes by $>40$ dB, eliminating spatial aliasing artifacts while slightly widening the main lobe
\end{itemize}

\textbf{Difference before and after pulse compression:}

Before pulse compression, the long transmitted pulse ($t_p = 8$ ms) results in poor range resolution. Targets at different ranges are smeared together over the pulse duration. After pulse compression using matched filtering:
\begin{itemize}
    \item Range resolution improves from $\sim 5.96$ m (determined by pulse length: $c \cdot t_p / 2$) to $2.48$ cm (determined by bandwidth: $c / 2B$)
    \item Signal-to-noise ratio increases by the pulse compression gain: $t_p \cdot B = 0.008 \times 30000 = 240$ (24 dB)
    \item The compressed pulse appears as a narrow peak, allowing separation of closely spaced targets
    \item Sidelobes appear due to the finite bandwidth and abrupt pulse edges
\end{itemize}

\textbf{Target at approximately 130 m range:}

Examining the pulse compressed beamformed image at $\sim 130$ m range reveals a strong target return. Based on the image characteristics:
\begin{itemize}
    \item \textbf{What is it:} The target appears to be a shipwreck or large underwater structure, given its strong backscatter and structured appearance
    \item \textbf{Size estimation:} 
    \begin{itemize}
        \item Angular extent: Approximately $5^\circ$ to $8^\circ$ in azimuth
        \item At 130 m range, this corresponds to a cross-range extent of: $130 \times \tan(7^\circ) \approx 16$ m
        \item Range extent: Approximately 10-15 m in depth based on the image
    \end{itemize}
    \item The target's dimensions suggest a substantial underwater object, consistent with a maritime vessel or large structural debris
\end{itemize}

\begin{figure}[h]
    \centering
    \includegraphics[width=0.9\textwidth]{figs/figure1.pdf}
    \caption{Comparison of beamformed images: Raw (left) and Pulse Compressed (right)}
    \label{fig:beamformed}
\end{figure}

\end{document}
