MATLAB中的循环缓冲区,**无需**复制旧数据 [英] Circular buffer in MATLAB, **without** copying old data

查看:163
本文介绍了MATLAB中的循环缓冲区,**无需**复制旧数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此处有一些不错的帖子(例如)有关如何在MATLAB中制作循环缓冲区的信息.但是,从它们的角度来看,我认为它们不适合我的应用程序,因为我要寻找的是MATLAB中的循环缓冲区解决方案,它不涉及任何对旧数据的复制.

There are some good posts on here (such as this one) on how to make a circular buffer in MATLAB. However from looking at them, I do not believe they fit my application, because what I am seeking, is a circular buffer solution in MATLAB, that does NOT involve any copying of old data.

举一个简单的例子,让我们说我一次处理50个样本,并且每次迭代读取10个样本.我将首先运行5次迭代,填满缓冲区,最后处理50个样本.所以我的缓冲区将是

To use a simple example, let us say that I am processing 50 samples at a time, and I read in 10 samples each iteration. I would first run through 5 iterations, fill up my buffer, and in the end, process my 50 samples. So my buffer will be

[B1 B2 B3 B4 B5]

,其中每个"B"都是10个样本的块.

, where each 'B' is a block of 10 samples.

现在,我阅读了接下来的10个样本,称它们为B6.我希望我的缓冲区现在看起来像:

Now, I read in the next 10 samples, call them B6. I want my buffer to now look like:

[B2 B3 B4 B5 B6]

问题是这样的-我不想每次都复制旧数据B2,B3,B4,B5,因为它会变得很昂贵. (我有非常大的数据集).

The catch is this - I do NOT want to copy the old data, B2, B3, B4, B5 everytime, because it becomes expensive in time. (I have very large data sets).

我想知道是否有一种方法可以在不复制旧"数据的情况下进行.谢谢.

I am wondering if there is a way to do this without any copying of 'old' data. Thank you.

推荐答案

一种快速实现循环缓冲区的方法是使用模数向后旋转到前面.这将稍微修改您指定的数据顺序,但是如果您仅用最新的so代替

One way to quickly implement a circular buffer is to use modulus to circle back around to the front. This will slightly modify the order of the data from what you specified but may be faster and equivalent if you simply replace the oldest data with the newest so instead of

[B2 B3 B4 B5 B6]

你得到

[B6 B2 B3 B4 B5]

通过使用如下代码:

bufferSize = 5;

data = nan(bufferSize,1)';

for ind = 1:bufferSize+2  

    data(mod(ind-1, bufferSize)+1) = ind

end

这适用于任意大小的数据.

and this works for arbitrary sized data.

如果您不熟悉模,则mod函数可有效返回除法运算的其余部分.因此mod(3,5)返回3mod(6,5)返回1mod(7,5)返回2,依此类推,直到到达mod(10,5)再次等于0.这使我们可以在每次到达终点时都移回起点,从而环绕"矢量.代码中的+1-1是因为MATLAB从1而不是0开始其向量索引,因此要正确计算数学,必须在执行mod之前先删除1,然后再将其重新添加以获得正确的索引.结果是,当您尝试将第6个元素写入向量时,将go并将其写入向量中的第1个位置.

If you're not familiar with modulo, the mod function returns effectively the remainder of a division operation. So mod(3,5) returns3, mod(6,5) returns 1, mod(7,5) returns 2 and so on until you reach mod(10,5) which equals 0 again. This allows us to 'wrap around' the vector by moving back to the start every time we reach the end. The +1 and -1 in the code is because MATLAB starts its vector indices at 1 rather than 0 so to get the math to work out right you have to remove 1 before doing the mod then add it back in to get the right index. The result is that when you try and write the 6th element to your vector is goes and writes it to the 1st position in the vector.

这篇关于MATLAB中的循环缓冲区,**无需**复制旧数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆