next up previous contents index
Next: Memory organisation Up: Module TArray Previous: Matrices and vectors   Contents   Index


Working with sub-arrays and Ranges

A powerful mechanism is included in array classes for working with sub-arrays. The class Range can be used to specify range of array indexes in any of the array dimensions. Any regularly spaced index range can be specified, using the start and end index and an optional step (or stride). It is also possible to specify the start index and the number of elements. In the following example, a simple low-pas filter, on a one dimensional stream (Vector) has been written using sub-arrays:


// Input Vector containing a noisy periodic signal
  Vector in(1024), out(1024);
  in = RandomSequence(RandomSequence::Gaussian, 0., 1.);
  for(int kk=0; kk<in.Size(); kk++)
    in(kk) += 2*sin(kk*0.05);
// Compute the output vector by a simple low pass filter
  Vector out(1024);
  int w = 2;
  for(int k=w; k<in.Size()-w; k++)
    out(k) = in(Range(k-w, k+w).Sum()/(2.*w+1.);



Reza Ansari
2001-03-07