Smoothing multibeam data using a convolution window

This example demonstrates how to use the Window Size property of the Code operator to smooth multibeam data in 3 dimensions. The smoothing is performed as a mean in the linear domain, using the Window size to define the number of samples in each dimension to include in the calculation.

List of operands

Operand 1 - Multibeam acoustic variable

Code

"""
Echoview Code Operator source file
========================================================================

Created by Echoview(R) 10.0.218 on Monday, 7 March 2019

See the Echoview help file for Code operator documentation and
examples.

NumPy User Guide: https://docs.scipy.org/doc/numpy/user/
NumPy Reference: https://docs.scipy.org/doc/numpy/Reference/
SciPy Reference Guide: https://docs.scipy.org/doc/scipy/Reference/

Echoview(R) is a registered trademark of Echoview Software Pty Ltd.
"""

# Authorship information
__author__ = "Echoview Software Pty Ltd. 2019."
__disclaimer__ = (
    "This example code is provided AS IS, without warranty of any "
    "kind, express or implied, including but not limited to the "
    "warranties of merchantability, fitness for a particular purpose "
    "and noninfringement. In no event shall Echoview Software Pty Ltd "
    "be liable for any claim, damages or other liability, arising "
    "from, out of or in connection with the use of this example code."
)
__version__ = "1.0"

# System Imports
from typing import List

from echoview import OperatorBase, OperandInput
import numpy as np


class Operator(OperatorBase):
    """
    Smoothing multibeam data using a convolution window
    ====================================================================

    Operands
    ---------------------

    * Operand 1 - Multibeam acoustic data

    Notes
    ---------------------

    This example demonstrates how to use the Window Size property of the code operator
    to smooth multibeam data in 3 dimensions.
    The smoothing is performed as a mean in the linear domain, using the window size
    to define the number of samples in each dimension to include in the calculation.
    """

    def result_type(self, input_types):
        return MeasurementType.MULTIBEAM_DOUBLE

    def eval(self, inputs: List[OperandInput]):
        
        # Create a 2D array with the same dimensions as the multibeam ping.
        # Then calculate a new single ping based on the mean of the pings within the defined window size

        SumLinear = np.zeros(
            (inputs[0].measurement.data.shape[0], inputs[0].measurement.data.shape[1]))
        for Measurement in inputs[0].window_measurements:
            DataLinear = np.power(10, np.divide(Measurement.data, 10))
            SumLinear = np.add(SumLinear, DataLinear)
        MeanLinear = np.divide(SumLinear, len(inputs[0].window_measurements))
        
        # Create a convolution array which is the same size as the specified window
        # Note we initialize this array to 1/window size so that it acts as a mean
        # calculation when applied.

        ConvolveArray = [1/self.window_size] * self.window_size
        
        # Then use this as a mean smoothing filter by convolving it with each column
        # and each row of the mean ping

        Column = 0
        while Column < MeanLinear.shape[0]:
            ColumnData = MeanLinear[Column, :]
            ColumnData = np.convolve(ColumnData, ConvolveArray, 'same')
            MeanLinear[Column, :] = ColumnData
            Column = Column + 1

        Row = 0
        while Row < MeanLinear.shape[1]:
            RowData = MeanLinear[:, Row]
            RowData = np.convolve(RowData, ConvolveArray, 'same')
            MeanLinear[:, Row] = RowData
            Row = Row + 1

        OutputMeasurementData = np.multiply(np.log10(MeanLinear), 10)
        return OutputMeasurementData 

See also

Code operator
About the Code operator
Using the Code operator