-
uint16_t arrTo1PulseWidth(volatile float arr[], int size, float targetWidth) { int iterator = 0, numBitWidths=0; uint16_t goodDataCount = 0, pass = 0; for(iterator=0;iterator<size;iterator++) { // // if the item is less than 10 times the bit width, // if(arr[iterator] < targetWidth*10) { // // if the item is not within +/-MARGINPERCENT% of the targetWidth, // then it is a multiple of 1-bit // bool belowBound = arr[iterator] < targetWidth*(1.0-MARGINPERCENT); bool aboveBound = arr[iterator] > targetWidth*(1.0+MARGINPERCENT); if(belowBound || aboveBound) { // // estimate how many bit-widths this is // numBitWidths = (int)((arr[iterator]/targetWidth)+0.5); // // multiply the multi-bit baudrate value by the estimated // number of bits to make this a 1-bit baud estimate // arr[iterator] = arr[iterator]/numBitWidths; // // find if this new value is within the bounds // belowBound = arr[iterator] < targetWidth*(1.0-MARGINPERCENT); aboveBound = arr[iterator] > targetWidth*(1.0+MARGINPERCENT); if(belowBound || aboveBound) { arr[iterator] = 0; // discard if not within margins } else { goodDataCount++; // iterate good data counter } } else { // // this is a 1-bit value so increment the counter for it // goodDataCount++; } } else { arr[iterator] = 0; } } // // if at least MINSAMPLEPERCENT% of the sampled values // are "good" samples, then return a pass // if((float)goodDataCount/(float)size > MINSAMPLEPERCENT) { pass=1; } return(pass); //return if the array had enough good samples or not }
用于将一个浮点数数组转换为1脉冲宽度。函数的输入参数包括一个浮点数数组、数组大小和目标宽度。函数的输出是一个16位无符号整数,表示数组中是否有足够的“好”样本。函数的具体实现如下:
1. 遍历数组中的每个元素,如果元素小于目标宽度的10倍,则进行下一步处理,否则将该元素设置为0。
2. 如果元素不在目标宽度的±MARGINPERCENT范围内,则将其视为多个1位的波特率值,并将其乘以估计的位数,以使其成为1位波特率估计值。
3. 如果新值仍然不在目标宽度的±MARGINPERCENT范围内,则将其设置为0,否则将其视为“好”数据并将“好”数据计数器加1。
4. 如果“好”数据计数器占数组大小的比例大于MINSAMPLEPERCENT,则返回1,否则返回0。
tune_via_uart.c
最新推荐文章于 2024-07-15 23:55:55 发布