Package org.harctoolbox.ircore
Class IrSequence
java.lang.Object
org.harctoolbox.ircore.IrSequence
- All Implemented Interfaces:
Serializable
,Cloneable
- Direct Known Subclasses:
ModulatedIrSequence
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 Summary
-
Constructor Summary
ConstructorDescriptionConstructs an empty IrSequence,IrSequence
(double[] inData) Constructs an IrSequence from the parameter data.IrSequence
(double[] inData, Double dummyGapDuration) IrSequence
(int[] inData) Constructs an IrSequence from the parameter data.IrSequence
(int[] idata, int offset, int length) Constructs an IrSequence from the parameter data.IrSequence
(String string) IrSequence
(String[] strings) IrSequence
(String string, Double dummyGapDuration) Constructs an IrSequence from the parameter data.IrSequence
(Collection<? extends Number> collection) Constructs an IrSequence from the parameter data.IrSequence
(IrSequence src) Constructs an IrSequence from the parameter data, by cloning.IrSequence
(IrSequence src, int start, int length) -
Method Summary
Modifier and TypeMethodDescriptionaddNoise
(double max) Adds a random number in the interval [-max, max) to each flash, and subtract it from the next gap.addToFlashes
(double amount) Adds an amount to all flashes.addToGaps
(double amount) Adds an amount to all flashes, and subtract it from all gaps.append
(double delay) Appends a delay to the end of the IrSequence.append
(IrSequence tail) Returns an IrSequence consisting of this sequence, with one copy of the argument appended.append
(IrSequence tail, int repetitions) Returns an IrSequence consisting of this sequence, with repetitions copies of the first argument appended.boolean
approximatelyEquals
(int beginning, int compareStart, int length, double absoluteTolerance, double relativeTolerance) Compares two segments of the current IrSequences for (approximate) equality.boolean
approximatelyEquals
(int beginning, int compareStart, int length, double absoluteTolerance, double relativeTolerance, double lastLimit) Compares two segments of the current IrSequences for (approximate) equality.boolean
approximatelyEquals
(IrSequence irSequence) Compares two IrSequences for equality.boolean
approximatelyEquals
(IrSequence irSequence, double absoluteTolerance, double relativeTolerance) Compares two IrSequences for (approximate) equality.chop
(double threshold) Chops a IrSequence in parts.clone()
Creates and returns a copy of this object.static IrSequence
concatenate
(Collection<IrSequence> sequences) Concatenates the elements in the argument.static IrSequence
concatenate
(IrSequence... sequences) final boolean
Returns true if and only if the sequence contains durations of zero length.int
firstBigGap
(int start, double limit) flashExcess
(double amount) Adds an amount to all flashes, and subtract it from all gaps.final double
get
(int i) Returns the i'th value, a duration in micro seconds.final Double
Return last entry, ornull
if the data is empty.final int
Returns the number of gaps and flashes.final double
Computes the total duration of the IR sequence modeled.double
getTotalDuration
(int begin, int length) Computes the total duration of a subsequence of the IR sequence modeled.static void
Initialize the random number generator of the class (used for addNoise(...) in a non-predictable way.static void
initRandom
(long seed) Initialize the random number generator of the class (used for addNoise(...) in a predictable way, using the seed provided.final boolean
isEmpty()
static String
final boolean
replaceZeros
(double replacement) Replace all zero durations.static String
Removes all "decorations" like signs, commas and semicolons.subSequence
(int start, int length) Returns a new IrSequence consisting of the length durations.final double[]
Returns an array of doubles of durations.static double[]
toInterleavingList
(Collection<? extends Number> list) final int[]
toInts()
Returns an array of integers of durations.static int[]
toInts
(Iterable<? extends IrSequence> list) final int[]
toPulses
(double frequency) For the frequency given as argument, computes an array of durations in number of periods in the given frequency.toString()
Generates a pretty string representing the object.toString
(boolean alternatingSigns) Generates a pretty string representing the object.truncate
(int length) Returns a new IrSequence consisting of the first length durations.
-
Field Details
-
DUMMYGAPDURATION
public static final double DUMMYGAPDURATION- See Also:
-
EMPTY
-
-
Constructor Details
-
IrSequence
public IrSequence()Constructs an empty IrSequence, -
IrSequence
Constructs an IrSequence from the parameter data. The input data must be semantically compatible: the data at even indicies are denoting flash; the ones at odd indicies gaps. Signs on the input data are not evaluated, but disposed. There must be an even number of entries.- Parameters:
inData
- Array of input durations.- Throws:
OddSequenceLengthException
-
IrSequence
- Throws:
OddSequenceLengthException
-
IrSequence
Constructs an IrSequence from the parameter data.- Parameters:
inData
-- Throws:
OddSequenceLengthException
-
IrSequence
Constructs an IrSequence from the parameter data.- Parameters:
collection
- Collection of numbers representing durations.- Throws:
OddSequenceLengthException
-
IrSequence
- Throws:
OddSequenceLengthException
-
IrSequence
- Throws:
OddSequenceLengthException
-
IrSequence
Constructs an IrSequence from the parameter data. It is assumed that flashes and gaps are interleaved. Signs, commas, and semicolons are ignored.- Parameters:
string
- String of durations, possibly "decorated".dummyGapDuration
- if the string consists of an odd number of numbers, a gap of this length is silently adding to the sequence.- Throws:
OddSequenceLengthException
-
IrSequence
Constructs an IrSequence from the parameter data.- Parameters:
idata
- Dataoffset
- First index to be usedlength
- Length of used subset of the idata array.- Throws:
OddSequenceLengthException
InvalidArgumentException
-
IrSequence
Constructs an IrSequence from the parameter data, by cloning.- Parameters:
src
- Original
-
IrSequence
- Parameters:
src
-start
-length
-- Throws:
InvalidArgumentException
-
-
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
Concatenates the elements in the argument.- Parameters:
sequences
-- Returns:
- new IrSequence
-
concatenate
-
toInterleavingList
-
toInts
-
toDoublesPreservingSigns
-
stripDecoratedString
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
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
Returns an IrSequence consisting of this sequence, with one copy of the argument appended.- Parameters:
tail
- IrSequence to be appended.- Returns:
- new IrSequence
-
append
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
Creates and returns a copy of this object. -
subSequence
Returns a new IrSequence consisting of the length durations.- Parameters:
start
- Index of first durationlength
- Length of new sequence- Returns:
- IrSequence, a subsequence of the current
- Throws:
InvalidArgumentException
- if length or start are not even.
-
truncate
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
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
Adds an amount to all flashes.- Parameters:
amount
- Amount to add in microseconds.- Returns:
- New instance
-
addToGaps
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
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
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
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 subsequencecompareStart
- start of second subsequencelength
- length to be comparedabsoluteTolerance
- 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 subsequencecompareStart
- start of second subsequencelength
- length to be comparedabsoluteTolerance
- 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
Return last entry, ornull
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
Generates a pretty string representing the object. -
toString
-
toString
-
toString
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.
-