跨越窗户

时间:2014-01-24 23:22:04

标签: c++ vector

假设我有一个向量:

x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

我需要做的是将此向量拆分为blocksize的块大小overlap

blocksize = 4

overlap = 2

结果将是包含4值的6大小的2D矢量。

x[0] = [1, 3, 5, 7, 9, 11]

x[1] = [ 2 4 6 8 10 12]

....

我尝试使用以下功能实现此功能:

std::vector<std::vector<double> > stride_windows(std::vector<double> &data, std::size_t 
NFFT, std::size_t overlap)
{
    std::vector<std::vector<double> > blocks(NFFT); 

    for(unsigned i=0; (i < data.size()); i++)
    {
        blocks[i].resize(NFFT+overlap);
        for(unsigned j=0; (j < blocks[i].size()); j++)
        {
            std::cout << data[i*overlap+j] << std::endl;
        }
    }
}

这是错误的,并且,细分。

std::vector<std::vector<double> > frame(std::vector<double> &signal, int N, int M)
{
         unsigned int n = signal.size();
         unsigned int num_blocks = n / N;


         unsigned int maxblockstart = n - N;
         unsigned int lastblockstart = maxblockstart - (maxblockstart % M);
         unsigned int numbblocks = (lastblockstart)/M + 1;

         std::vector<std::vector<double> > blocked(numbblocks);

         for(unsigned i=0; (i < numbblocks); i++)
         {
                 blocked[i].resize(N);

             for(int j=0; (j < N); j++)
            {
                blocked[i][j] = signal[i*M+j];
            }
        }

         return blocked;
}

我写了这个函数,认为它完成了上述操作,然而,它只会存储:

X[0] = 1, 2, 3, 4

x[1] = 3, 4, 5, 6

.....

有人可以解释我如何修改上述功能以允许overlap跳过?

此功能类似于:Rolling window

编辑:

我有以下矢量:

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14

我想将此向量拆分为子块(从而创建2D向量),并且参数overlap重叠,因此在这种情况下,参数将为:size=4 overlap = 2,然后会创建以下2D矢量:

`block0 = [ 1  3  5  7  9 11]

 block1 = [ 2  4  6  8 10 12]

 block2 = [ 3  5  7  9 11 13]

 block3 = [ 4  6  8 10 12 14]`

基本上,已经创建了4个块,每个块都包含一个值,overlap

跳过该元素

编辑2:

这是我需要去的地方:

overlap的值会与向量中的展示位置x的结果重叠:

block1 = [1, 3, 5, 7, 9, 11] 

来自实际矢量块的注意事项:

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14

Value: 1 -> This is pushed into block "1"

Value 2 -> This is not pushed into block "1" (overlap is skip 2 places in the vector)

Value 3 -> This is pushed into block "1" 

value 4 -> This is not pushed into block "1" (overlap is skip to places in the vector)

value 5 -> This is pushed into block "1"

value 6 -> "This is not pushed into block "1" (overlap is skip 2 places in the vector)

value 7 -> "This value is pushed into block "1"

value 8 -> "This is not pushed into block "1" (overlap is skip 2 places in the vector)"

value 9 -> "This value is pushed into block "1"

value 10 -> This value is not pushed into block "1" (overlap is skip 2 places in the 
                                                     vector)

value 11 -> This value is pushed into block "1"

第2块

Overlap = 2; 

value 2 - > Pushed back into block "2" 
value 4 -> Pushed back into  block "2"
value 6, 8, 10 etc.. 

所以每次,在这种情况下,矢量的位置被“重叠”跳过,它的值是2 ..

这是预期的输出:

[[ 1  3  5  7  9 11]
 [ 2  4  6  8 10 12]
 [ 3  5  7  9 11 13]
 [ 4  6  8 10 12 14]]

1 个答案:

答案 0 :(得分:1)

如果我理解正确,那你就非常接近了。您需要以下内容。我使用了int,因为坦率地说它比double = P

更容易打字
#include <iostream>
#include <algorithm>
#include <vector>
#include <limits>
#include <iterator>

std::vector<std::vector<int>>
split(const std::vector<int>& data, size_t blocksize, size_t overlap)
{
    // compute maximum block size
    std::vector<std::vector<int>> res;
    size_t minlen = (data.size() - blocksize)/overlap + 1;
    auto start = data.begin();
    for (size_t i=0; i<blocksize; ++i)
    {
        res.emplace_back(std::vector<int>());
        std::vector<int>& block = res.back();

        auto it = start++;
        for (size_t j=0; j<minlen; ++j)
        {
            block.push_back(*it);
            std::advance(it,overlap);
        }
    }
    return res;
}

int main()
{
    std::vector<int> data { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 };

    for (size_t i=2; i<6; ++i)
    {
        for (size_t j=2; j<6; ++j)
        {
            std::vector<std::vector<int>> blocks = split(data, i, j);

            std::cout << "Blocksize = " << i << ", Overlap = " << j << std::endl;
            for (auto const& obj : blocks)
            {
                std::copy(obj.begin(), obj.end(), std::ostream_iterator<int>(std::cout, " "));
                std::cout << std::endl;
            }
            std::cout << std::endl;
        }
    }
    return 0;
}

<强>输出

Blocksize = 2, Overlap = 2
1 3 5 7 9 11 13 
2 4 6 8 10 12 14 

Blocksize = 2, Overlap = 3
1 4 7 10 13 
2 5 8 11 14 

Blocksize = 2, Overlap = 4
1 5 9 13 
2 6 10 14 

Blocksize = 2, Overlap = 5
1 6 11 
2 7 12 

Blocksize = 3, Overlap = 2
1 3 5 7 9 11 
2 4 6 8 10 12 
3 5 7 9 11 13 

Blocksize = 3, Overlap = 3
1 4 7 10 
2 5 8 11 
3 6 9 12 

Blocksize = 3, Overlap = 4
1 5 9 
2 6 10 
3 7 11 

Blocksize = 3, Overlap = 5
1 6 11 
2 7 12 
3 8 13 

Blocksize = 4, Overlap = 2
1 3 5 7 9 11 
2 4 6 8 10 12 
3 5 7 9 11 13 
4 6 8 10 12 14 

Blocksize = 4, Overlap = 3
1 4 7 10 
2 5 8 11 
3 6 9 12 
4 7 10 13 

Blocksize = 4, Overlap = 4
1 5 9 
2 6 10 
3 7 11 
4 8 12 

Blocksize = 4, Overlap = 5
1 6 11 
2 7 12 
3 8 13 
4 9 14 

Blocksize = 5, Overlap = 2
1 3 5 7 9 
2 4 6 8 10 
3 5 7 9 11 
4 6 8 10 12 
5 7 9 11 13 

Blocksize = 5, Overlap = 3
1 4 7 10 
2 5 8 11 
3 6 9 12 
4 7 10 13 
5 8 11 14 

Blocksize = 5, Overlap = 4
1 5 9 
2 6 10 
3 7 11 
4 8 12 
5 9 13 

Blocksize = 5, Overlap = 5
1 6 
2 7 
3 8 
4 9 
5 10