0

如上所述,如何在不将所有值添加到浮点数的情况下平均一组值,这旨在用于 8 位/16 位微,以减轻时间计算浮点数。我想出了一个方法,但我担心它比将所有内容添加到浮点数中并划分更麻烦。我将在下面附上代码。

平均值减少到 ADCsample 数组中的第一个元素

平均.c

#include <xc.h>
#include "Average.h"
#include "Initialize.h"

/*
 (x+y)=((x&y)*2)+(x^y)
 (x+y)/2=(x&y)+((x^y)>>1)
 Does not overflow data type when done in pairs
 * No need for floats
 * array must have 2/4/8/16/32...values to be averaged
 * Accurate rounding
 */
void Average(void)
{
    x=0;
    for (x=0; x<ADCsamplesize; ++x)
    {
        ADCsamples[x].value=ADCsampleraw[x];
        ADCsamples[x].upflag=0;
    }
    
    x=0;
    y=0;
    z=ADCsamplesize/2;
    
    while(z!=0)
    {   
        if (((ADCsamples[y].value&0x01)^(ADCsamples[y+1].value&0x01))==1)       //is rounding needed? (even/odd mismatch)
        {
            if((ADCsamples[y].upflag^ADCsamples[y+1].upflag)==1)                //if ONE has been rounded before
            {
                    if (ADCsamples[y].upflag==1)                                //which to round down?
                    {
                        ADCsamples[y].value--;                               
                        ADCsamples[y].upflag=0;
                    }
                    else
                    {
                        ADCsamples[y+1].value--;
                        ADCsamples[y+1].upflag=0;
                    }
            }
            else                                                                //round up
            {
                if (ADCsamples[y].value!=65535)                                 //overflow protection
                {
                 ADCsamples[y].value++;                                         
                }
                else
                {
                    ADCsamples[y+1].value++;                                    //in the event of a mismatch y and y+1 cannot both be data type max
                }
                ADCsamples[x].upflag=1;                                         //mark as being rounded up
            }
        }
        else
        {
             ADCsamples[x].upflag=0;                                            //as elements are reused, clear rounded flag
        }
 
        ADCsamples[x].value=(ADCsamples[y].value&ADCsamples[y+1].value)+((ADCsamples[y].value^ADCsamples[y+1].value)>>1); //bitwise average of 2 values, not possible to overflow
        
        if (x+1!=z)                                                             
        {
            x++;
            y=y+2;
        }
        else
        {
            z=z>>1;
            x=0;
            y=0;
        }        
    }   
} 

平均.h

#ifndef AVERAGE_H
#define AVERAGE_H

#include <xc.h> // include processor files - each processor file is guarded.  

#ifdef  __cplusplus
extern "C" {
#endif /* __cplusplus */
    unsigned char z;
    unsigned char x;
    unsigned char y;
    unsigned char const ADCsamplesize=8;

    unsigned int ADCsampleraw[]=
    {
    123,516,4569,3521,852,456,981,852
    };

    typedef struct sample{
        unsigned int value;
        unsigned int upflag;
    }sample;

    sample ADCsamples[8];
    
    void Average(void);
    
#ifdef  __cplusplus
}
#endif /* __cplusplus */

#endif  /* XC_HEADER_TEMPLATE_H */

4

1 回答 1

0

这旨在用于 8 位/16 位微控制器,以缓解时间紧迫的浮点数。

IMO,OP通过限制“平均值......不超过值数据类型”选择了次优解决方案。

对于小型微型,以两倍于样本宽度的整数类型将值相加,然后除以。


如果仍然想在没有更宽类型且没有溢出的情况下进行平均,下面的工作就完成了。

// (n-1)*(n-1) should be <= UINT_MAX
int average_unsigned(unsigned n, const unsigned a[]) {
  if (n <= 0) {
    return 0;
  }
  unsigned partial = 0;
  unsigned residue = 0;
  for (unsigned i = 0; i < n; i++) {
    partial += a[i] / n; // If n is a true constant and power-of-2, 
    residue += a[i] % n; //   these 2 steps are fast   ***
  }
  return partial + residue / n;
}

*** 如果n是运行时变量,但仍是 2 的幂,请/ n>> log2n% n替换& (n-1)

于 2020-11-18T18:37:13.873 回答