我正在尝试使传入数据(保存最大和最小选择)简单过滤,例如:每秒44100个样本,但是屏幕必须显示1000。我在44.1个样本的范围内选择一个最大值或最小值,并且输出屏幕。但是,该算法不是很准确。在代码中,它看起来像这样:
伪算法示例
float max = 0;
float min = 0;
float filter = 0;
float step = 44100/1000;
for(int i = 0 ; i < 44100; i++){
if(input[i] > 0)
if(max < input[i])
max = input[i];
if(input[i] < 0)
if(min > input[i])
min = input[i];
filter++;
if(filter >= step){
filter = filter - step;
//1st version (bad version)
memory[count] = max + min;
//2nd version (bad version)
if(max > abs(min))
memory[count] = max;
else if(max < abs(min))
memory[count] = min;
//3nd version (only maximum)
memory[count] = max; //work great, but only > 0
//4nd version (only minimum)
memory[count] = min; //work great, but only < 0
max = 0;
min = 0;
count++;
if(count >= 1000)
count = 0;
};
};
我究竟做错了什么?另外,一切正常(最大或最小),但是将所有组件连接在一起时,结果会很差。
我有图片,但是不能在此处粘贴。
链接到此帖子下的图片。
最佳答案
要正确计算一组数字的最小值/最大值,必须正确初始化值。通过将它们设置为0
,您会遇到发现的问题。您基本上有两种初始化min
/ max
的方法:
对于(1),如果您知道自己的数据始终在-100到+100之间,则可以执行以下操作:
min = 101;
max = -101;
请注意,如果您的输入可以是类型范围内的任何值,则此方法将无效。对于(2),您可以执行以下操作:
float max = input[0];
float min = input[0];
...
for (int i ... )
{
...
if (filter >= step)
{
...
min = input[i + 1]; // Be aware of overflow on the last element if
max = input[i + 1]; // input[] is exactly 44100 elements in size
}
}
关于c++ - 示波器算法,动态数据输入,极限输出数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29031918/