Class IrSequence

java.lang.Object
org.harctoolbox.ircore.IrSequence
All Implemented Interfaces:
Serializable, Cloneable
Direct Known Subclasses:
ModulatedIrSequence

public class IrSequence extends Object implements Cloneable, Serializable
This class models an IR Sequence, which is a sequence of pulse pairs.

It consists of a sequence of positive real numbers, each representing a gap (for odd indices) or flash (for even indices). The duration of each flash or gap is the value of the entry in micro seconds."

The length of the IrSequence (as reported by getLength()) is therefore always even. To get the "length in bursts", divide by 2.

The class is (almost) immutable: it can only be constructed and then read from. Also note that there is no modulation frequency herein.

See Also:
  • Field Details

  • Constructor Details

  • Method Details

    • initRandom

      public static void initRandom(long seed)
      Initialize the random number generator of the class (used for addNoise(...) in a predictable way, using the seed provided.
      Parameters:
      seed -
    • initRandom

      public static void initRandom()
      Initialize the random number generator of the class (used for addNoise(...) in a non-predictable way.
    • concatenate

      public static IrSequence concatenate(Collection<IrSequence> sequences)
      Concatenates the elements in the argument.
      Parameters:
      sequences -
      Returns:
      new IrSequence
    • concatenate

      public static IrSequence concatenate(IrSequence... sequences)
    • toInterleavingList

      public static List<Double> toInterleavingList(Collection<? extends Number> list)
    • toInts

      public static int[] toInts(Iterable<? extends IrSequence> list)
    • toDoublesPreservingSigns

      public static double[] toDoublesPreservingSigns(String str)
    • stripDecoratedString

      public static String stripDecoratedString(String in)
      Removes all "decorations" like signs, commas and semicolons. In particular, it is assumed that the numbers really are interleaving flash and gaps.
      Parameters:
      in -
      Returns:
    • normalize

      public static String normalize(String in, Double dummyGap, boolean useSigns, String separator) throws OddSequenceLengthException
      Throws:
      OddSequenceLengthException
    • get

      public final double get(int i)
      Returns the i'th value, a duration in micro seconds. If i is even, it is a flash (light on), if i is odd, a gap (light off).
      Parameters:
      i - index
      Returns:
      duration in microseconds,
    • toInts

      public final int[] toInts()
      Returns an array of integers of durations. This is a copy of the original data, so it might be manipulated without affecting the original instance.
      Returns:
      integer array of durations in micro seconds.
    • toDoubles

      public final double[] toDoubles()
      Returns an array of doubles of durations. This is a copy of the original data, so it might be manipulated without affecting the original instance.
      Returns:
      double array of durations in micro seconds.
    • toPulses

      public final int[] toPulses(double frequency)
      For the frequency given as argument, computes an array of durations in number of periods in the given frequency.
      Parameters:
      frequency - Frequency in Hz.
      Returns:
      integer array of durations in periods of frequency.
    • append

      public IrSequence append(IrSequence tail, int repetitions)
      Returns an IrSequence consisting of this sequence, with repetitions copies of the first argument appended.
      Parameters:
      tail - IrSequence to be appended.
      repetitions - Number of copies to append.
      Returns:
      new IrSequence
    • append

      public IrSequence append(IrSequence tail)
      Returns an IrSequence consisting of this sequence, with one copy of the argument appended.
      Parameters:
      tail - IrSequence to be appended.
      Returns:
      new IrSequence
    • append

      public IrSequence append(double delay) throws InvalidArgumentException
      Appends a delay to the end of the IrSequence. Original is left untouched.
      Parameters:
      delay - microseconds of silence to be appended to the IrSequence.
      Returns:
      Copy of object with additional delay at end.
      Throws:
      InvalidArgumentException
    • clone

      public IrSequence clone()
      Creates and returns a copy of this object.
      Overrides:
      clone in class Object
      Returns:
      A copy of this IrSequence.
    • subSequence

      public IrSequence subSequence(int start, int length) throws InvalidArgumentException
      Returns a new IrSequence consisting of the length durations.
      Parameters:
      start - Index of first duration
      length - Length of new sequence
      Returns:
      IrSequence, a subsequence of the current
      Throws:
      InvalidArgumentException - if length or start are not even.
    • truncate

      public IrSequence truncate(int length) throws InvalidArgumentException
      Returns a new IrSequence consisting of the first length durations. Equivalent to subSequence with first argument 0.
      Parameters:
      length - Length of new sequence
      Returns:
      IrSequence
      Throws:
      InvalidArgumentException - if length not even.
    • chop

      public List<IrSequence> chop(double threshold)
      Chops a IrSequence in parts. Every gap of length ≥ threshold cause a cut.
      Parameters:
      threshold - minimal gap in microseconds to cause a cut.
      Returns:
      List of IrSequences
    • addToFlashes

      public IrSequence addToFlashes(double amount)
      Adds an amount to all flashes.
      Parameters:
      amount - Amount to add in microseconds.
      Returns:
      New instance
    • addToGaps

      public IrSequence addToGaps(double amount)
      Adds an amount to all flashes, and subtract it from all gaps. For generating test data for decoders etc.
      Parameters:
      amount - Amount (positive or negative) to add in microseconds.
      Returns:
      new instance
    • flashExcess

      public IrSequence flashExcess(double amount)
      Adds an amount to all flashes, and subtract it from all gaps. For generating test data for decoders etc.
      Parameters:
      amount - Amount (positive or negative) to add in microseconds.
      Returns:
      new instance
    • addNoise

      public IrSequence addNoise(double max)
      Adds a random number in the interval [-max, max) to each flash, and subtract it from the next gap. For generating test data for decoders etc. NOTE: initRandom must have been called before this call.
      Parameters:
      max - max amount to add/subtract, in microseconds.
      Returns:
      new instance
    • approximatelyEquals

      public boolean approximatelyEquals(IrSequence irSequence)
      Compares two IrSequences for equality.
      Parameters:
      irSequence - to be compared against this. Default tolerances essentially allows for numerical rounding errors, not much more.
      Returns:
      equality
    • approximatelyEquals

      public boolean approximatelyEquals(IrSequence irSequence, double absoluteTolerance, double relativeTolerance)
      Compares two IrSequences for (approximate) equality.
      Parameters:
      irSequence - to be compared against this.
      absoluteTolerance - tolerance threshold in microseconds.
      relativeTolerance - relative threshold, between 0 and 1.
      Returns:
      equality within tolerance.
    • approximatelyEquals

      public boolean approximatelyEquals(int beginning, int compareStart, int length, double absoluteTolerance, double relativeTolerance, double lastLimit)
      Compares two segments of the current IrSequences for (approximate) equality.
      Parameters:
      beginning - start of first subsequence
      compareStart - start of second subsequence
      length - length to be compared
      absoluteTolerance - tolerance threshold in microseconds.
      relativeTolerance - relative threshold, between 0 and 1.
      lastLimit -
      Returns:
      if the subsequences are approximately equal.
    • approximatelyEquals

      public boolean approximatelyEquals(int beginning, int compareStart, int length, double absoluteTolerance, double relativeTolerance)
      Compares two segments of the current IrSequences for (approximate) equality.
      Parameters:
      beginning - start of first subsequence
      compareStart - start of second subsequence
      length - length to be compared
      absoluteTolerance - tolerance threshold in microseconds.
      relativeTolerance - relative threshold, between 0 and 1.
      Returns:
      if the subsequences are approximately equal.
    • getLength

      public final int getLength()
      Returns the number of gaps and flashes. Always even. Divide by 2 to get number of bursts.
      Returns:
      number of gaps/flashes.
    • getLastGap

      public final Double getLastGap()
      Return last entry, or null if the data is empty.
      Returns:
      last entry, or null if the data is empty.
    • isEmpty

      public final boolean isEmpty()
      Returns:
      emptyness of the sequence.
    • containsZeros

      public final boolean containsZeros()
      Returns true if and only if the sequence contains durations of zero length.
      Returns:
      existence of zero durations.
    • replaceZeros

      public final boolean replaceZeros(double replacement)
      Replace all zero durations. Changes the signal in-place.
      Parameters:
      replacement - Duration in micro seconds to replace zero durations with.
      Returns:
      if the signal was changed.
    • getTotalDuration

      public final double getTotalDuration()
      Computes the total duration of the IR sequence modeled.
      Returns:
      Length of the IR sequence in microseconds.
    • getTotalDuration

      public double getTotalDuration(int begin, int length)
      Computes the total duration of a subsequence of the IR sequence modeled.
      Parameters:
      begin - start of subsequence.
      length - length of subsequence.
      Returns:
      Length of the IR sequence in microseconds.
    • firstBigGap

      public int firstBigGap(int start, double limit)
    • toString

      public String toString()
      Generates a pretty string representing the object.
      Overrides:
      toString in class Object
      Returns:
      nice string.
    • toString

      public String toString(boolean alternatingSigns)
    • toString

      public String toString(boolean alternatingSigns, String separator)
    • toString

      public String toString(boolean alternatingSigns, String separator, String prefix, String suffix)
      Generates a pretty string representing the object. If argument true, generate string with alternating signs.
      Parameters:
      alternatingSigns - if true, generate alternating signs.
      separator -
      prefix -
      suffix -
      Returns:
      nice string.