实时求一段0、1序列1的比例

#define  MIN(a,b) ((a) < (b) ? (a) : (b))
#define FLAG_BUFF_LEN_BYTE 125
#define FLAG_BUFF_LEN_BIT ( FLAG_BUFF_LEN_BYTE * 8)
#define LONG_SMOOTH_NUM 1000

unsigned short short_count; ///< Number of  shorts
unsigned char buff[FLAG_BUFF_LEN_BYTE]; ///< The flag buff, save as bit
int pre_count;
int index_byte; ///< The index by byte
unsigned int index_bit;  ///< The index by bit
float ratio_short_mean; ///< The short ratio
float ratio_long_smooth;    ///< The long ratio

short_count = 0;
memset(buff, 0, sizeof(buff));
pre_count = 0;
index_byte = 0;
index_bit = 1;
ratio_short_mean = 0;
ratio_long_smooth = 0;


if (flag)
{
    count += (1 - ((buff[index_byte] & (unsigned char)index_bit) != 0 ? 1 : 0)); // if bit is 0, plus 1;else do nothing
    buff[index_byte] |= index_bit; // flush new value
}
else
{
    count -= (1 - ((buff[index_byte] & (unsigned char)index_bit) == 0 ? 1 : 0)); // if bit is 1, minus 1;else do nothing
    buff[index_byte] &= (~index_bit); // flush new value
}

index_bit <<= 1;
if (index_bit > 128)
{
    index_bit = 1;
    index_byte = (index_byte + 1) % FLAG_BUFF_LEN_BYTE;
}

if (pre_count < 1073741824)
{
    pre_count++;
}

ratio_short_mean = (float)short_count / MIN(pre_count, FLAG_BUFF_LEN_BIT);
ratio_long_smooth = ((pre_count - 1) * ratio_long_smooth + flag) / MIN(pre_count, LONG_SMOOTH_NUM);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值