gpt4 book ai didi

c++ - 跨窗

转载 作者:太空宇宙 更新时间:2023-11-04 11:41:16 24 4
gpt4 key购买 nike

假设我有一个 vector :

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

我需要做的是将这个 vector 拆分成大小为 blocksize 的 block ,并带有 overlap

block 大小 = 4

重叠 = 2

结果将是一个大小为 4 且包含 6 值的二维 vector 。

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;
}
}
}

这是错误的,and,段。

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

编辑:

我有以下 vector :

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

我想将这个 vector 拆分成子 block (从而创建一个二维 vector ),参数 overlap 有重叠,所以在这种情况下,参数将是:size =4 overlap=2,这将创建以下 2D vector :

`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 个 block ,每个 block 包含一个值,其中元素被 overlap

跳过

编辑 2:

这是我需要去的地方:

overlap 的值将根据 vector 内的位置与 x 的结果重叠:

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

来自实际 vector block 的通知:

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"

区 block 2

Overlap = 2; 

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

所以每次,vector 中的位置都被“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]]

最佳答案

如果我理解正确的话,你已经很接近了。你需要像下面这样的东西。我使用 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

关于c++ - 跨窗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21344296/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com