Package PamUtils

Class PamUtils

java.lang.Object
PamUtils.PamUtils

public class PamUtils extends Object
Author:
Doug Gillespie
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static Double
    Get the average of two angles in degrees.
    static Double
    angleAverageDegrees(Double d1, Double d2, double w1, double w2)
    Get the weighted average of two angles in degrees.
    static double
    area(double[][] vertices)
    Calculate the area of a polygon
    static int
    calcHydrophoneNumber(int numberOfDelays)
    For N synchronised hydrophones there are N*(N-1)/2 time delays.
    static int
    calcTDNumber(int numberofHydrophones)
    For N synchronised hydrophones there are N*(N-1)/2 time delays.
    static double[]
    centroid(double[][] cluster)
    Returns the centroid of a cluster of points;
    static double
    clamp(double min, double value, double max)
    Clamps a value so that it has to sit between two values
    static int
    clamp(int min, int value, int max)
    Clamps a value so that it has to sit between two values
    static double
    constrainedAngle(double angle)
    Force an angle to sit 0invalid input: '<'= angle invalid input: '<' 360.
    static double
    constrainedAngle(double angle, double maxAngle)
    Force an angle to sit within some range.
    static double
    constrainedAngleR(double angle)
    Force an angle to sit 0invalid input: '<'= angle invalid input: '<' 2PI.
    static double
    constrainedAngleR(double angle, double maxAngle)
    Force an angle to sit within some range.
    static double
    constrainedNumber(double number, double maxNumber)
    Force a number to sit between two values.
    static double
    distance(double[] point1, double[] point2)
    Calculate the distance between two 3D points.
    static double
    distance(double x1, double y1, double z1, double x2, double y2, double z2)
    Calculate the distance between two 3D points.
    static boolean
    doesLineIntersectRect(double ax, double ay, double bx, double by, double minx, double miny, double maxx, double maxy)
    Test whether line AB intersects a rectangle defined by 2 opposite corners
    static boolean
    doLinesIntersect(double ax, double ay, double bx, double by, double cx, double cy, double dx, double dy)
    Test whether line AB intersects line CD.
    static Double
    Return the average ot two double numbers, either or both of which may be null
    static String
    formatFrequency(double f)
    Format a frequency in Hz as Hz, kHz, MHz, etc.
    static double
    getAbsMax(double[] array)
    Get the absolute maximum value of a double array
    static int[]
    getChannelArray(int channelMap)
    Turn a bitmap into an array of channel numbers.
    static String
    getChannelList(int channelMap)
    Create a string of channel numbers in the channel map separated by commas.
    static int
    getChannelPos(int singleChannel, int channelBitmap)
    Works out the index of a particular channel in a channel list - often, if channelBitmap is a set of continuous channels starting with 0, then the channel pos is the same as the single channel number.
    static int[]
    getChannelPositionLUT(int channelBitmap)
    Create a LUT which allows conversion of absolute to relative channel numbers
    static int
    getHighestChannel(int channelBitmap)
    Get the highest channel number in a channel map.
    static int
    getLowestChannel(int channelBitmap)
    Get the lowest channel number in a channel map.
    static int
    getMaxIndex(double[] array)
    Get the index of the maximum value within an array
    static double[]
    getMinAndMax(double[] array)
     
    static double[]
    getMinAndMax(double[][] array)
     
    static int
    getMinFftLength(long len)
    Get the next power of 2 size >= len
    note that this function will fail if len > 2^30
    static int
    getNthChannel(int singleChannel, int channelBitmap)
    Get's the number of the nth used channel in a bitmap.
    static int
    getNumChannels(int channelBitmap)
    Get the number of channels present in this bitmap.
    static int
    getSingleChannel(int channelMap)
    check that the bits represent a single channel and return the number of that channel
    static int[]
    getSortedInds(int[] data)
    Leave data alone, but create a list of indexes which will give the ascending order of data.
    static double[][]
    getSpherePoints(int n, double r)
    Get evenly spaced points of the surface of a sphere.
    static boolean
    hasChannel(int channelBitMap, int channel)
    Check whether a channel is present in a channel bitmap.
    static boolean
    hasChannelMap(int channelMap1, int channelMap2)
    Check whether a channel bitmap contains any other channels from another bitmap.
    indexM1(int numberOfHydrophones)
    IndexM1 and IndexM2 specify which hydrophones to calculate time delays between.
    indexM2(int numberOfHydrophones)
    IndexM1 and IndexM2 specify which hydrophones to calculate time delays between.
    static List<Double>
    linspace(double start, double stop, int n)
    Copy of the linspace function from MATLAB (generates linearly spaced vectors.
    static double
    linterp(double x0, double x1, double y0, double y1, double x)
    Linear interpolation.
    static int
    log2(int n)
    Gets the next integer log of 2 which will give 2^ans >= n, i.e.
    static int
    makeChannelMap(int nChannels)
    Make a simple bitmap for nChannels of data, i.e.
    static int
    makeChannelMap(int[] channelList)
    Make a channel bitmap from a list.
    static int
    makeChannelMap(int nChannels, int[] channelList)
    Deprecated. 
    static double
    minAngled(double angle1, double angle2)
    Find the minimum angle difference between two angles.
    static double
    minAngler(double angle1, double angle2)
    Find the minimum angle difference between two angles.
    static double
    nearest(double less, double value, double more)
    Returns the nearest value, less or more.
    static int
    nearest(int less, int value, int more)
    Returns the nearest value, less or more.
    static double
    roundNumber(double number, double step)
    round a number to the closest step size
    static double
    roundNumberDown(double number, double step)
    round a number down to the closest step size
    static double
    roundNumberUp(double number, double step)
    round a number up to the closest step size
    static double
    roundNumberUpP(double number, int nDP)
    Round a number up, maintining a set number of decimal places.
    static int
    SetBit(int bitMap, int bitNumber, boolean bitSet)
     
    static int
    SetBit(int bitMap, int bitNumber, int bitValue)
     

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • PamUtils

      public PamUtils()
  • Method Details

    • getSingleChannel

      public static int getSingleChannel(int channelMap)
      check that the bits represent a single channel and return the number of that channel
      Parameters:
      channelMap - bitmap for multiple channels
      Returns:
      singel channel (or -1 if >1 or zero channels)
    • getNumChannels

      public static int getNumChannels(int channelBitmap)
      Get the number of channels present in this bitmap. (calls Integer.bitCount(...))
      Parameters:
      channelBitmap - channel map
      Returns:
      number of bits set.
    • getChannelPos

      public static int getChannelPos(int singleChannel, int channelBitmap)
      Works out the index of a particular channel in a channel list - often, if channelBitmap is a set of continuous channels starting with 0, then the channel pos is the same as the single channel number. However, if there are gaps in the channelBitmap, then the channel pos will be invalid input: '<' than the channel Number.
      Parameters:
      singleChannel - single channel number (index, not map)
      channelBitmap -
      Returns:
      the channel position in the channel list or -1 if it isn't available
    • getNthChannel

      public static int getNthChannel(int singleChannel, int channelBitmap)
      Get's the number of the nth used channel in a bitmap. e.g. if the channelBitmap were 0xc (1100), then the 0th channel would be 2 and the 1st channel would be 3.
      Parameters:
      singleChannel - nth channel in a list
      channelBitmap - bitmap of all used channels.
      Returns:
      true channel number or -1 if the channel is not in the map.
    • getChannelPositionLUT

      public static int[] getChannelPositionLUT(int channelBitmap)
      Create a LUT which allows conversion of absolute to relative channel numbers
      Parameters:
      channelBitmap -
      Returns:
    • getHighestChannel

      public static int getHighestChannel(int channelBitmap)
      Get the highest channel number in a channel map.
      Parameters:
      channelBitmap -
      Returns:
      the last channel in the channel bitmap
    • getLowestChannel

      public static int getLowestChannel(int channelBitmap)
      Get the lowest channel number in a channel map.

      Returns -1 if the channel map is empty.

      Parameters:
      channelBitmap -
      Returns:
      the first channel in the channel bitmap
    • makeChannelMap

      public static int makeChannelMap(int nChannels)
      Make a simple bitmap for nChannels of data, i.e.

      if nChannels = 1, map = 1

      if nChannels = 2, map = 3

      if nChannels = 3, map = 7

      if nChannels = 4, map = 15

      etc.

      Parameters:
      nChannels -
      Returns:
      a bitmap representing the set channels
    • makeChannelMap

      @Deprecated public static int makeChannelMap(int nChannels, int[] channelList)
      Deprecated.
      Make a channel map from a list
      Parameters:
      nChannels - number of elements in the list
      channelList - list
      Returns:
      channel bitmap
    • makeChannelMap

      public static int makeChannelMap(int[] channelList)
      Make a channel bitmap from a list.
      Parameters:
      channelList - channel list
      Returns:
      bitmap of channels.
    • SetBit

      public static int SetBit(int bitMap, int bitNumber, int bitValue)
    • SetBit

      public static int SetBit(int bitMap, int bitNumber, boolean bitSet)
    • getMinFftLength

      public static int getMinFftLength(long len)
      Get the next power of 2 size >= len
      note that this function will fail if len > 2^30
      Parameters:
      len - input length
      Returns:
      a power of 2 greater than or equal to the input
    • log2

      public static int log2(int n)
      Gets the next integer log of 2 which will give 2^ans >= n, i.e. the minimum length required to FFT these data.
      Parameters:
      n - length
      Returns:
      log2 of mnimum data required to fit n.
    • linterp

      public static double linterp(double x0, double x1, double y0, double y1, double x)
      Linear interpolation. Define f() as the linear mapping from the domain [x0,x1] to the range [y0,y1]. Return y=f(x) according to this mapping.

      author Dave Mellinger

    • getChannelList

      public static String getChannelList(int channelMap)
      Create a string of channel numbers in the channel map separated by commas.
      Parameters:
      channelMap - channel map
      Returns:
      string of channel numbers.
    • getChannelArray

      public static int[] getChannelArray(int channelMap)
      Turn a bitmap into an array of channel numbers.
      Parameters:
      channelMap - channel map
      Returns:
      channel list array
    • hasChannel

      public static boolean hasChannel(int channelBitMap, int channel)
      Check whether a channel is present in a channel bitmap.
      Parameters:
      channelBitMap - - the channel bitmap to search
      channel - - the channel
      Returns:
      true if the bitmap contains the channel
    • hasChannelMap

      public static boolean hasChannelMap(int channelMap1, int channelMap2)
      Check whether a channel bitmap contains any other channels from another bitmap.
      Parameters:
      channelMap1 - - the channel bitmap to check
      channelMap2 - - the channel bitmap that contains at least one channel from chanelMpa1 to return true.
      Returns:
      true of channelMap1 contains at least one channels from channelMap2.
    • constrainedAngle

      public static double constrainedAngle(double angle)
      Force an angle to sit 0invalid input: '<'= angle invalid input: '<' 360.
      Parameters:
      angle - input angle (degrees)
      Returns:
      output angle (degrees)
    • constrainedAngleR

      public static double constrainedAngleR(double angle)
      Force an angle to sit 0invalid input: '<'= angle invalid input: '<' 2PI.
      Parameters:
      angle - input angle (radians)
      Returns:
      output angle (radians)
    • constrainedAngle

      public static double constrainedAngle(double angle, double maxAngle)
      Force an angle to sit within some range.
      Parameters:
      angle - input angle (degrees)
      maxAngle - maximum angle in degrees
      Returns:
      output angle (degrees)
    • constrainedAngleR

      public static double constrainedAngleR(double angle, double maxAngle)
      Force an angle to sit within some range.
      Parameters:
      angle - input angle (radians)
      maxAngle - maximum angle in radians
      Returns:
      output angle (radians)
    • minAngled

      public static double minAngled(double angle1, double angle2)
      Find the minimum angle difference between two angles.
      Parameters:
      angle1 - - the firs tangle (0-360 degrees)
      angle2 - - the firs tangle (0-360 degrees)
      Returns:
      the min angle in DEGREES
    • minAngler

      public static double minAngler(double angle1, double angle2)
      Find the minimum angle difference between two angles.
      Parameters:
      angle1 - - the firs tangle (0-2pi RADIANS)
      angle2 - - the firs tangle (0-2pi RADIANS)
      Returns:
      the min angle in RADIANS
    • constrainedNumber

      public static double constrainedNumber(double number, double maxNumber)
      Force a number to sit between two values.
      Parameters:
      number - - input numbers
      maxNumber - - the maximum Number
      Returns:
      output number.
    • roundNumber

      public static double roundNumber(double number, double step)
      round a number to the closest step size
      Parameters:
      number - number to round
      step - rounding step (e.g. 1, 100, 0.5, etc)
      Returns:
      rounded number
    • roundNumberUp

      public static double roundNumberUp(double number, double step)
      round a number up to the closest step size
      Parameters:
      number - number to round
      step - rounding step (e.g. 1, 100, 0.5, etc)
      Returns:
      rounded number
    • roundNumberDown

      public static double roundNumberDown(double number, double step)
      round a number down to the closest step size
      Parameters:
      number - number to round
      step - rounding step (e.g. 1, 100, 0.5, etc)
      Returns:
      rounded number
    • roundNumberUpP

      public static double roundNumberUpP(double number, int nDP)
      Round a number up, maintining a set number of decimal places.
      Parameters:
      number - number to round
      nDP - number of decimal places
      Returns:
      rounded number
    • doubleAverage

      public static Double doubleAverage(Double d1, Double d2)
      Return the average ot two double numbers, either or both of which may be null
      Parameters:
      d1 - number 1
      d2 - number 2
      Returns:
      the average (or just one if the other is null).
    • angleAverageDegrees

      public static Double angleAverageDegrees(Double d1, Double d2)
      Get the average of two angles in degrees. Uses trig so that the average of 359 and 1 will be 0, no t180.
      Parameters:
      d1 - angle 1
      d2 - angle 2
      Returns:
      average
    • angleAverageDegrees

      public static Double angleAverageDegrees(Double d1, Double d2, double w1, double w2)
      Get the weighted average of two angles in degrees. Uses trig so that the average of 359 and 1 will be 0, no t180.
      Parameters:
      d1 - angle 1
      d2 - angle 2
      w1 - weight for angle 1
      w2 - weight for angle 2;
      Returns:
      weighted average
    • formatFrequency

      public static String formatFrequency(double f)
      Format a frequency in Hz as Hz, kHz, MHz, etc.
      Parameters:
      f - frequency value in Hz
      Returns:
      Formatted string
    • getSortedInds

      public static int[] getSortedInds(int[] data)
      Leave data alone, but create a list of indexes which will give the ascending order of data.
      Uses a simple bubble sort, so only suitable for short arrays.
      Parameters:
      data - array to sort
      Returns:
      sort indexes
    • calcHydrophoneNumber

      public static int calcHydrophoneNumber(int numberOfDelays)
      For N synchronised hydrophones there are N*(N-1)/2 time delays. For N time delays there are 0.5+sqrt(1+8N)/2 hydrophones. This is a simple function which calculates the number of hydrophones from the number of time delays.
      Returns:
      the number of synchronised recievers whihc would be required to make the number of time delays,.
    • calcTDNumber

      public static int calcTDNumber(int numberofHydrophones)
      For N synchronised hydrophones there are N*(N-1)/2 time delays. For N time delays there are 0.5+sqrt(1+2N)/2 hydrophones. This is a simple function which calculates the number of time delays from the number of hydrophones.
      Returns:
      the number of time delays whihc would be produced by the number of synchronised hydrophones
    • getMinAndMax

      public static double[] getMinAndMax(double[][] array)
      Parameters:
      array - two dimensional array of doubles
      Returns:
      two element array with the min and max values of the input array
    • getMinAndMax

      public static double[] getMinAndMax(double[] array)
      Parameters:
      array - one dimensional array of doubles
      Returns:
      two element array with the min and max values of the input array
    • getAbsMax

      public static double getAbsMax(double[] array)
      Get the absolute maximum value of a double array
      Parameters:
      array - array of numbers
      Returns:
      maximum absoulte value.
    • getMaxIndex

      public static int getMaxIndex(double[] array)
      Get the index of the maximum value within an array
      Parameters:
      array - - the array in which to find the maximum value;
      Returns:
      the index of the array's maximum value.
    • linspace

      public static List<Double> linspace(double start, double stop, int n)
      Copy of the linspace function from MATLAB (generates linearly spaced vectors. It is similar to the colon operator ":" but gives direct control over the number of points and always includes the end points)
      Parameters:
      start - - the first value
      stop - - the end value
      n - - the number of evenly spaced points to return
      Returns:
      a list of evenly spaced points.
    • getSpherePoints

      public static double[][] getSpherePoints(int n, double r)
      Get evenly spaced points of the surface of a sphere. These can be normalised for evenly spaced vectors. Uses a spiral algorithm and golden ratio.
      See http://blog.marmakoide.org/?p=1 (accessed 30/06/2016) for algorithm.
      Parameters:
      n - The number of points to scater on the surface of the sphere.
      r - the radius of the sphere.
      an - array of evenly spaced points on the surface of the sphere.
    • clamp

      public static double clamp(double min, double value, double max)
      Clamps a value so that it has to sit between two values
      Parameters:
      min - min allowed value
      value - desired value
      max - max allowed value
      Returns:
      clamped value between min and max.
    • clamp

      public static int clamp(int min, int value, int max)
      Clamps a value so that it has to sit between two values
      Parameters:
      min - min allowed value
      value - desired value
      max - max allowed value
      Returns:
      clamped value between min and max.
    • nearest

      public static double nearest(double less, double value, double more)
      Returns the nearest value, less or more. If exactly in the middle it will return more
      Parameters:
      less - lower value
      value - value to compare
      more - upper value
      Returns:
      closest of less and more.
    • nearest

      public static int nearest(int less, int value, int more)
      Returns the nearest value, less or more. If exactly in the middle it will return more
      Parameters:
      less - lower value
      value - value to compare
      more - upper value
      Returns:
      closest of less and more.
    • distance

      public static double distance(double x1, double y1, double z1, double x2, double y2, double z2)
      Calculate the distance between two 3D points.
      Parameters:
      x1 - - x coordinate of the first point.
      y1 - - y coordinate of the first point.
      z1 - - z coordinate of the first point.
      x2 - - x coordinate of the second point.
      y2 - - y coordinate of the second point.
      z2 - - z coordinate of the second point.
      Returns:
      the distance between to the two points.
    • distance

      public static double distance(double[] point1, double[] point2)
      Calculate the distance between two 3D points.
      Parameters:
      point1 - - the first point
      point2 - - the second point
      Returns:
      the distance between the two points.
    • area

      public static double area(double[][] vertices)
      Calculate the area of a polygon
      Parameters:
      vertices - - a list of vertices
      Returns:
      - the area in vertice units.
    • centroid

      public static double[] centroid(double[][] cluster)
      Returns the centroid of a cluster of points;
      Parameters:
      cluster - - a list of points.
      Returns:
      the centroid of the cluster of points.
    • doLinesIntersect

      public static boolean doLinesIntersect(double ax, double ay, double bx, double by, double cx, double cy, double dx, double dy)
      Test whether line AB intersects line CD. 2D test only. Using equations from http://www.jeffreythompson.org/collision-detection/line-line.php and https://www.geeksforgeeks.org/program-for-point-of-intersection-of-two-lines/, with some help from https://stackoverflow.com/questions/563198/how-do-you-detect-where-two-line-segments-intersect
      Parameters:
      ax - x-value of start of line AB
      ay - y-value of start of line AB
      bx - x-value of end of line AB
      by - y-value of end of line AB
      cx - x-value of start of line CD
      cy - y-value of start of line CD
      dx - x-value of end of line CD
      dy - y-value of end of line CD
      Returns:
      true if lines intersect
    • doesLineIntersectRect

      public static boolean doesLineIntersectRect(double ax, double ay, double bx, double by, double minx, double miny, double maxx, double maxy)
      Test whether line AB intersects a rectangle defined by 2 opposite corners
      Parameters:
      ax - x-value of start of line AB
      ay - y-value of start of line AB
      bx - x-value of end of line AB
      by - y-value of end of line AB
      minx - x-value of one corner
      miny - y-value of one corner
      maxx - x-value of the opposite corner
      maxy - y-value of the opposite corner
      Returns:
      true if the line crosses any of the edges of the rectangle
    • indexM2

      public static ArrayList<Integer> indexM2(int numberOfHydrophones)
      IndexM1 and IndexM2 specify which hydrophones to calculate time delays between. In the case of a paired array this will simply be the hydrophones in pairs so Index M1 will be 0 and even numbers and IndexM2 will be odd numbers.
      Returns:
    • indexM1

      public static ArrayList<Integer> indexM1(int numberOfHydrophones)
      IndexM1 and IndexM2 specify which hydrophones to calculate time delays between. In the case of a paired array this will simply be the hydrophones in pairs so Index M1 will be 0 and even numbers and IndexM2 will be odd numbers.
      Returns: