How to take n samples from vector?

Viewed 229

My question is how to choose n element from vec.begin() to vec.end() such that we almost cover all the vector elements?

vector<double> take_100_samples(vector<double>& in)
{
  vector<double> vec(100);
  double step = (in.size() - 1) / (100 - 1);

// sampling process

  return vec;
} 

for example if in vector has size of 200, we choose every two element of it (i.e. in[0], in[2] , in[4], ... ,in[199]) but what if the size is not dividable by 100?

Is there efficient way for doing that?
Regards

2 Answers

The usual way to do so is to get the floating point fraction and then rounding it to an integer:

double step = in.size() / 100.;

for(int i = 0; i < 100; ++i)
{
    auto index = std::lround(i * step);
    // use index
}

Of course, adapt the step computation as you did as well as the loop if you want to get the first and last elements all the time.

My interpretation is that you want to downsample a signal with an asynchronous frequency.

One way to do this is to implement a kind of Digital PLL (Phase Lock Loop).

Let us assume that you have N = 1030 elements at input and that you want to get K = 100 elements.
The average rate is equal to rate = 10.3. In practice, you will have to use a step equal to 10 or 11 depending of the current position of the indices.

If you are in "advance" : step = 10 (slow down)
If you are "late" : step = 11 (speed up)

Test to know if you late or in advance:
The current "read" index i is equal to the sum of the former steps. k is the output index
if i < rate*k : you are late
else you are in advance

This is the basic idea. Some small adaptations are possible, for example to insure getting the last element.

EDIT: I just 'rediscovered' Mathieu Brucher's solution. In my first too fast reading of it, I was misled by the original error (corrected) on step calculation in his answer. It is clear that results will be equivalent, except that Mathieu Brucher's implementation is simpler. In this situation, the only interest (?) of my answer is to provide another interpretation of the process

Related