惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Microsoft Azure Blog
Microsoft Azure Blog
J
Java Code Geeks
量子位
腾讯CDC
C
Check Point Blog
小众软件
小众软件
IT之家
IT之家
I
InfoQ
Hugging Face - Blog
Hugging Face - Blog
Stack Overflow Blog
Stack Overflow Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
GbyAI
GbyAI
Apple Machine Learning Research
Apple Machine Learning Research
大猫的无限游戏
大猫的无限游戏
博客园_首页
S
SegmentFault 最新的问题
The Cloudflare Blog
阮一峰的网络日志
阮一峰的网络日志
aimingoo的专栏
aimingoo的专栏
P
Proofpoint News Feed
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Google DeepMind News
Google DeepMind News
T
Tailwind CSS Blog
Martin Fowler
Martin Fowler

博客园 - Coca-code

202609142132_《Unix(唯一) sample code》 202603171903_《实时OS Kernel》 202603162259_《CSS格子(1-9 js版)》 202603162216_《CSS格子(1-9)》 202503271546_《Idiot js code of map app》 202503241513_《Root entrance function of 'Hello Mars' 》 202403172356_《Initial sys. of C》 202401221657_《React相关(一) 写法》 202401102331 -《Flex9格布局——从1到9》 202312142321_《遍历 for customised data structure 》 202311141210——《一些修改表字段的sql语句》 202310061823_《运行这个页面,背后是一大堆配置》 202310061227-《心得:低版本mysql配置一,些轮子插件》 202310032035_《近期撸码心得》 202310031029——《个人在Lombok使用中遇到的错误及解决》 202309301820_《Spring boot项目,继承mybatis-generator遇到的问题及解决》 202309272035-《maven依赖已下载,但还是报红,解决办法》 202309272022-《idea编辑器,maven解析依赖慢,解决办法》 202306112142-《最近开发心得...》 202306062001-《远程Linux服务器——安装tomcat8、jdk1.8、mysql5——mysql 用sql建表时提示utf8错误....》 202305281631-《远程Linux服务器——安装tomcat8、jdk1.8、mysql5——mysql workerbench连接出错》
202608051132_《theta & freguency measure distance _algori...
Coca-code · 2026-08-05 · via 博客园 - Coca-code
#include <math.h>
#include <stddef.h>
#include <stdint.h>

#define SPEED_OF_LIGHT_M_S 299792458.0

typedef struct {
    double freq_hz;  /* tone frequency for this channel */
    double i;        /* in-phase component  */
    double q;        /* quadrature component */
} cs_tone_t;


/**********************************************************
* 相位斜率测距。
*
* 往返相位随 phi = -4*pi*f*d/c 增加,因此对展开相位与频率进行最小二乘拟合
* 可以通过斜率得到距离:
* d = -斜率 * c / (4*pi)
*
* 音调必须按升序排列并且频率间隔足够小,这样每步的相位变化保持在 ±pi 以内
* (这限定了最大测距:d_max = c / (4 * delta_f))。
*
* 成功返回 0 并将距离写入 *distance_m,输入错误返回 -1。
**********************************************************/

 
int cs_estimate_distance(const cs_tone_t *tones, size_t n, double *distance_m)
{
    if (tones == NULL || distance_m == NULL || n < 2U) {
        return -1;
    }

    double prev_raw = atan2(tones[0].q, tones[0].i);
    double unwrapped = prev_raw;

    /* Accumulators for the least-squares fit. */
    double sum_f = 0.0, sum_p = 0.0, sum_ff = 0.0, sum_fp = 0.0;

    for (size_t k = 0U; k < n; ++k) {
        if (k > 0U) {
            double raw = atan2(tones[k].q, tones[k].i);
            double delta = raw - prev_raw;

            /* Wrap the increment into (-pi, pi] before accumulating. */
            while (delta > M_PI)  { delta -= 2.0 * M_PI; }
            while (delta <= -M_PI) { delta += 2.0 * M_PI; }

            unwrapped += delta;
            prev_raw = raw;
        }

        const double f = tones[k].freq_hz;
        sum_f  += f;
        sum_p  += unwrapped;
        sum_ff += f * f;
        sum_fp += f * unwrapped;
    }

    const double nd = (double)n;
    const double denom = (nd * sum_ff) - (sum_f * sum_f);

    /* Degenerate: all tones at the same frequency, no slope to fit. */
    if (fabs(denom) < 1e-9) {
        return -1;
    }

    const double slope = ((nd * sum_fp) - (sum_f * sum_p)) / denom;
    double d = -slope * SPEED_OF_LIGHT_M_S / (4.0 * M_PI);

    /* Noise and multipath can push the fit slightly negative at close range. */
    if (d < 0.0) {
        d = 0.0;
    }

    *distance_m = d;
    return 0;
}