32 #ifndef SPL_Sequence1_hpp
33 #define SPL_Sequence1_hpp
40 #define SPL_SEQUENCE1_DEBUG
43 #define SPL_SEQUENCE1_USE_NEW_CONV
45 #if defined(SPL_SEQUENCE1_DEBUG)
46 #define SPL_SEQUENCE1_INLINE
49 #define SPL_SEQUENCE1_INLINE inline
57 #include <SPL/config.hpp>
129 Sequence1(
int startInd,
int size,
const T& value);
140 template <
class InputIterator>
141 Sequence1(
int startInd,
int size, InputIterator data);
152 template <
class OtherT>
185 template <
class OtherT>
325 ConstIterator
begin()
const;
344 ConstIterator
end()
const;
407 void fill(
const T& value);
422 friend std::ostream& operator<<(std::ostream& out, const Sequence1<U>&);
448 startInd_(startInd), data_(size)
455 const T& data) : startInd_(startInd), data_(size, data)
461 template <
class InputIterator>
463 InputIterator data) : startInd_(startInd), data_(size, data)
470 startInd_(0), data_(f)
476 const Array1<T>& data) : startInd_(startInd), data_(data)
482 startInd_(f.startInd_), data_(f.data_)
492 template <
class OtherT>
494 startInd_(f.startInd_), data_(f.data_)
506 startInd_ = f.startInd_;
512 template <
class OtherT>
516 startInd_ = f.startInd_;
530 assert((getSize() == 0 && f.
getSize() == 0) ||
542 assert((getSize() == 0 && f.
getSize() == 0) ||
554 assert((getSize() == 0 && f.
getSize() == 0) ||
566 assert((getSize() == 0 && f.
getSize() == 0) ||
614 return startInd_ + getSize();
620 return data_.getSize();
626 return data_.isShared();
636 assert(i >= startInd_ && i < startInd_ + getSize());
637 return data_(i - startInd_);
643 assert(i >= startInd_ && i < startInd_ + getSize());
644 return data_(i - startInd_);
655 return data_.begin();
668 return data_.begin();
688 assert(getSize() > 0);
695 assert(getSize() > 0);
725 std::ostream& operator<<(std::ostream& out, const Sequence1<T>& f)
727 out << f.getStartInd() <<
" ";
748 if (!(in >> startInd)) {
755 in.setstate(std::ios::failbit);
758 typename std::vector<T> data;
760 for (
int i = 0; i < size; ++i) {
762 if (!(in >> value)) {
765 data.push_back(value);
896 for (
int i = i0; i < i1; ++i) {
901 for (
int i = i0; i < i1; ++i) {
1037 while (src != f.
end()) {
1038 if (
absVal((*src) - (*dst)) > threshold) {
1069 for (
int i = 0; i < size; ++i) {
1070 result(startInd + i) = f(startInd + i);
1117 if (i >= f.getStartInd() && i < f.getEndInd()) {
1124 if (i < f.getStartInd()) {
1125 i = f.getStartInd();
1126 }
else if (i >= f.getEndInd()) {
1127 i = f.getEndInd() - 1;
1129 assert(i >= f.getStartInd() && i < f.getEndInd());
1133 if (i < f.getStartInd() || i >= f.getEndInd()) {
1134 i = f.getStartInd() +
mod(i - f.getStartInd(), f.getSize());
1136 assert(i >= f.getStartInd() && i < f.getEndInd());
1140 if (i < f.getStartInd() || i >= f.getEndInd()) {
1141 int tmp =
mod(i - f.getStartInd(), 2 * f.getSize() - 2);
1142 i = f.getStartInd() + std::min(tmp, 2 * f.getSize() - 2 - tmp);
1144 assert(i >= f.getStartInd() && i < f.getEndInd());
1158 Sequence1<T> convolveFull(
const Sequence1<T>& f,
const Sequence1<T>& g)
1160 Sequence1<T> seq(f);
1161 Sequence1<T> filt(g);
1162 Sequence1<T> result(f.getStartInd() + g.getStartInd(),
1163 f.getSize() + g.getSize() - 1);
1164 convolveHelper(seq.begin(), seq.end(), filt.begin(), filt.end(),
1165 result.begin(), T());
1172 Sequence1<T> convolveSameDomainZeroExt(
const Sequence1<T>& f,
1173 const Sequence1<T>& g)
1175 Sequence1<T> result(f.getStartInd(), f.getSize());
1178 if (g.getStartInd() >= 0) {
1179 shift = g.getStartInd();
1183 skip = -g.getStartInd();
1185 convolveHelper2(f.begin(), f.end(), g.begin(), g.end(),
1186 result.begin() + shift, skip, result.getSize() - shift, T());
1208 #if defined(SPL_SEQUENCE1_USE_NEW_CONV)
1211 return convolveSameDomainZeroExt(f, g);
1214 return convolveFull(f, g);
1246 sum += getExtSeqValue(f, n - k, mode) * g(k);
1251 assert(resultIter == result.
end());
1272 assert(factor >= 1);
1280 *dstIter = *srcIter;
1305 assert(factor >= 1);
1306 assert(pad >= 0 && pad < factor);
1307 int n = f.
getSize() + std::max((f.
getSize() - 1), 0) * (factor - 1) + pad;
1313 *dstIter = *srcIter;
1316 for (
int i = factor - 1; i > 0; --i) {
1323 *dstIter = *srcIter;
1326 for (
int i = pad; i > 0; --i) {
1345 inline int getCosetOffset(
int type,
int numPhases,
int index)
1353 result = index - (numPhases - 1);
1359 result = (numPhases - 1) - index;
1386 assert(numPhases >= 2);
1388 for (
int i = 0; i < numPhases; ++i) {
1389 int offset = getCosetOffset(type, numPhases, i);
1409 const int numPhases = comps.getSize();
1410 assert(numPhases >= 2);
1412 for (
int i = 0; i < numPhases; ++i) {
1413 int offset = getCosetOffset(type, numPhases, i);
long roundTowardZeroDiv(long x, long y)
Compute a quotient with the result rounded towards zero.
Definition: math.hpp:152
SPL_SEQUENCE1_INLINE bool operator==(const Sequence1< T > &f, const Sequence1< T > &g)
Test two sequences for equality.
Definition: Sequence1.hpp:993
T sum() const
Get the sum of the elements in the sequence.
Definition: Sequence1.hpp:700
SPL_SEQUENCE1_INLINE bool operator!=(const Sequence1< T > &f, const Sequence1< T > &g)
Test two sequences for inequality.
Definition: Sequence1.hpp:1012
A one-dimensional sequence class with lazy copying and reference counting.
Definition: Sequence1.hpp:85
SPL_SEQUENCE1_INLINE Sequence1< T > translate(const Sequence1< T > &f, int delta)
Translate a sequence by the specified amount.
Definition: Sequence1.hpp:1090
Sequence1 & operator*=(const Sequence1 &f)
Multiply elementwise this sequence by another one.
Definition: Sequence1.hpp:550
int getEndInd() const
Get the end index for the sequence.
Definition: Sequence1.hpp:612
Definition: Arcball.hpp:48
T mod(T x, T y)
Compute the remainder after division.
Definition: math.hpp:184
T absVal(T x)
The absolute value function.
Definition: math.hpp:73
SPL_SEQUENCE1_INLINE bool approxEqual(const Sequence1< T > &f, const Sequence1< T > &g, T threshold=1e-9)
Test two sequences for approximate equality.
Definition: Sequence1.hpp:1022
Sequence1 & operator=(const Sequence1 &f)
The assignment operator.
Definition: Sequence1.hpp:503
Sequence1 & operator+=(const Sequence1 &f)
Add another sequence to this one.
Definition: Sequence1.hpp:526
Sequence1< T > subsequence(const Sequence1< T > &f, int startInd, int size)
Extract a subsequence from a sequence.
Definition: Sequence1.hpp:1065
std::vector< T >::const_iterator ConstIterator
A constant iterator for the array elements.
Definition: Array1.hpp:158
Sequence1< T > upsample(const Sequence1< T > &f, int factor, int pad=0)
Upsample a sequence by the specified factor.
Definition: Sequence1.hpp:1303
Sequence1< T > convolve(const Sequence1< T > &f, const Sequence1< T > &g, int mode=ConvolveMode::full)
Compute the convolution of two sequences.
Definition: Sequence1.hpp:1205
static const int full
The full convolution result (i.e., the same as "full" in MATLAB)
Definition: Sequence.hpp:57
~Sequence1()
The destructor.
Definition: Sequence1.hpp:487
void fill(const T &value)
Set all of the elements in the sequence to the specified value.
Definition: Sequence1.hpp:782
long ceilDiv(long x, long y)
Compute the ceiling of a quotient.
Definition: math.hpp:212
Sequence1 & translate(int delta)
Translate (i.e., shift) a sequence by the specified displacement.
Definition: Sequence1.hpp:794
Sequence1 & operator-=(const Sequence1 &f)
Subtract another sequence from this one.
Definition: Sequence1.hpp:538
ConstIterator end() const
Get an iterator referencing just after the last element in the sequence.
Definition: Sequence1.hpp:659
static const int sameDomainPerExt
Periodic extension.
Definition: Sequence.hpp:66
static const int sameDomainConstExt
Constant extension.
Definition: Sequence.hpp:63
Array1< Sequence1< T > > polyphaseSplit(const Sequence1< T > &seq, int type, int numPhases)
Split a sequence into its polyphase components.
Definition: Sequence1.hpp:1383
A one-dimensional array class with lazy copying and reference counting.
Definition: Array1.hpp:83
Sequence1 & operator/=(const Sequence1 &f)
Divide elementwise this sequence by another one.
Definition: Sequence1.hpp:562
T ElemType
The type of the element in the sequence.
Definition: Sequence1.hpp:94
Sequence1< int > IntSequence1
Integer sequence.
Definition: Sequence1.hpp:1427
Sequence1< double > RealSequence1
Real sequence.
Definition: Sequence1.hpp:1424
Sequence1()
The default constructor.
Definition: Sequence1.hpp:442
This file contains various mathematical functions/code.
#define SPL_SEQUENCE1_INLINE
Prevent the inlining of functions.
Definition: Sequence1.hpp:47
Sequence1< T > downsample(const Sequence1< T > &f, int factor)
Downsample a sequence by the specified factor.
Definition: Sequence1.hpp:1270
Array1< T >::ConstIterator ConstIterator
The const iterator for the sequence.
Definition: Sequence1.hpp:97
Common header for sequence classes.
int getStartInd() const
Get the start index for the sequence.
Definition: Sequence1.hpp:606
std::istream & operator>>(std::istream &in, Sequence1< T > &f)
Input a sequence from a stream.
Definition: Sequence1.hpp:744
Sequence1< T > add(const Sequence1< T > &f, const Sequence1< T > &g)
Compute the sum of two sequences with potentially differing domains.
Definition: Sequence1.hpp:878
void swapArray(Array1< T > &data)
Swap the data for the underlying array and the specified array.
Definition: Sequence1.hpp:776
static const int sameDomainSymExt0
Symmetric periodic extension.
Definition: Sequence.hpp:69
SPL_SEQUENCE1_INLINE Sequence1< T > operator*(const Sequence1< T > &f, const T &a)
Compute a scalar multiple of a sequence.
Definition: Sequence1.hpp:967
Sequence1< T > polyphaseJoin(const Array1< Sequence1< T > > &comps, int type)
Reassemble a sequence from its polyphase components.
Definition: Sequence1.hpp:1407
static const int sameDomainZeroExt
The same as "same" in MATLAB.
Definition: Sequence.hpp:60
T min() const
Get the minimum element in the sequence.
Definition: Sequence1.hpp:686
SPL_SEQUENCE1_INLINE Sequence1< T > operator/(const Sequence1< T > &f, const T &a)
Divide a sequence by a scalar.
Definition: Sequence1.hpp:980
bool isShared() const
Is the array for this sequence shared with another array?
Definition: Sequence1.hpp:624
SPL_SEQUENCE1_INLINE Sequence1< T > operator-(const Sequence1< T > &f, const T &a)
Subtract a value from a sequence.
Definition: Sequence1.hpp:941
int getSize() const
Get the length of the sequence.
Definition: Sequence1.hpp:618
This file contains the Array1 template class and supporting code.
const T & operator()(int i) const
Get the specified element in the sequence.
Definition: Sequence1.hpp:634
SPL_SEQUENCE1_INLINE Sequence1< T > operator+(const Sequence1< T > &f, const T &a)
Add a value to a sequence.
Definition: Sequence1.hpp:928
Array1< T > getArray() const
Get a copy of the underlying array.
Definition: Sequence1.hpp:788
std::vector< T >::iterator Iterator
A mutable iterator for the array elements.
Definition: Array1.hpp:155
Array1< T >::Iterator Iterator
The mutable iterator for the sequence.
Definition: Sequence1.hpp:100
ConstIterator begin() const
Get an iterator referencing the first element in the sequence.
Definition: Sequence1.hpp:652
T max() const
Get the maximum element in the sequence.
Definition: Sequence1.hpp:693