自相关实操流程

假设我有一个信号: 1,1,1,1,1,3,3,2,2,2,2,4,1,2,4,2,1,1,1

1、求信号的均值(可以先归一化)

avg = 1.8947

2、计算均方差

(1-1.8947)平方

(1-1.8947)平方

(1-1.8947)平方

(1-1.8947)平方

(1-1.8947)平方

(3-1.8947)平方

(3-1.8947)平方

(2-1.8947)平方

(2-1.8947)平方

(2-1.8947)平方

(2-1.8947)平方

(4-1.8947)平方

(1-1.8947)平方

(2-1.8947)平方

(4-1.8947)平方

(2-1.8947)平方

(1-1.847)平方

(1-1.8947)平方

(1-1.8947)平方

其结果为:

复制代码
各点与均值的偏差: ['-0.895', '-0.895', '-0.895', '-0.895', '-0.895', '1.105', '1.105', '0.105', '0.105', '0.105', '0.105', '2.105', '-0.895', '0.105', '2.105', '0.105', '-0.895', '-0.895', '-0.895']
分母 = Σ(x[i]-μ)² = 20.8421
3、计算延迟k=1的协方差
复制代码
i= 0: (1-1.895)×(1-1.895) = -0.895×-0.895 = 0.801
i= 1: (1-1.895)×(1-1.895) = -0.895×-0.895 = 0.801
i= 2: (1-1.895)×(1-1.895) = -0.895×-0.895 = 0.801
i= 3: (1-1.895)×(3-1.895) = -0.895×1.105 = -0.989
i= 4: (3-1.895)×(3-1.895) = 1.105×1.105 = 1.221
i= 5: (3-1.895)×(2-1.895) = 1.105×0.105 = 0.116
i= 6: (2-1.895)×(2-1.895) = 0.105×0.105 = 0.011
i= 7: (2-1.895)×(2-1.895) = 0.105×0.105 = 0.011
i= 8: (2-1.895)×(2-1.895) = 0.105×0.105 = 0.011
i= 9: (2-1.895)×(4-1.895) = 0.105×2.105 = 0.221
i=10: (4-1.895)×(1-1.895) = 2.105×-0.895 = -1.884
i=11: (1-1.895)×(2-1.895) = -0.895×0.105 = -0.094
i=12: (2-1.895)×(4-1.895) = 0.105×2.105 = 0.221
i=13: (4-1.895)×(2-1.895) = 2.105×0.105 = 0.221
i=14: (2-1.895)×(1-1.895) = 0.105×-0.895 = -0.094
i=15: (1-1.895)×(1-1.895) = -0.895×-0.895 = 0.801
i=16: (1-1.895)×(1-1.895) = -0.895×-0.895 = 0.801
i=17: (1-1.895)×(1-1.895) = -0.895×-0.895 = 0.801

分子 = Σ(x[i]-μ)(x[i+1]-μ) = 15.0263

4、最终计算自相关的值

复制代码
延迟1自相关 R[1] = 分子/分母 = 15.0263 / 20.8421 = 0.721
直观理解

这个0.721的值意味着:

  • 当延迟为1时,信号与其自身有72.1%的相似性

  • 这很高,说明相邻数据点之间有很强的相关性

  • 在信号A中,你经常看到 1,1、2,2、3,3 这样的连续相同值,这导致了高自相关

JAVA编程实现:
复制代码
package model.MyTools;

import testHtt.SystemTools;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class TestCorrecation {
        /**
         *  PPG信号检测方法
         * 主要通过自相关函数来判别是否可以对同一个信号进行周期的判断
         * author:huitao
         * time:2025-11-13
         * @param single
         * @return
         */
        //自相关函数的编程(true表示高度相关,false表示不相关)
        public  static boolean CorrelationTools(List<Double> single){
            double lat=0;
            //取平均值
            double avg= SystemTools.getAvagetValue(single);
            //求差值
            List<Double> offsetList= new ArrayList<>();
            double offsetResult=0;
            for(int i=0;i<single.size();i++){
                double offset=single.get(i)-avg;
                offsetList.add(offset);
                offsetResult += offset*offset; //计算平方
            }
            offsetResult = offsetResult/single.size();//0位置的相关系数
            String formattedNumber = String.format("%.1f", offsetResult);
            System.out.print(formattedNumber+"\n");
            //计算延迟1的相关
            //打点器1
            List<Double> nList=getNListValue(single,20);
            double point1=0;//获取位置1的点的关系
            for(int j=0;j<single.size();j++){
                double x1=single.get(j);
                double x2=nList.get(j);
                double NValue=(x1-avg)*(x2-avg);
                point1 += NValue;
            }
            point1 = point1/single.size();
            System.out.print(point1+"\n");
            //获取最终自相关系数的关系
            double ResultCorrelation=point1/offsetResult;
            String formattedNumber1 = String.format("%.1f", ResultCorrelation);
            System.out.print(formattedNumber1+"\n");
            boolean flag=false;
            if(ResultCorrelation>=0.3){
                flag=true;
            }
            return flag;
        }

        //获取具体位置的对比关系
        public static List<Double> getNListValue(List<Double> llopp,int index){
            List<Double> nList1=new  ArrayList();
            List<Double> nList2=new  ArrayList();
            List<Double> result=new  ArrayList();
            for(int k=0;k<llopp.size();k++){
                if(k>index){
                    nList2.add(llopp.get(k));//开始
                }else{
                    nList1.add(llopp.get(k));//结束
                }
            }
            result.addAll(nList2);
            result.addAll(nList1);
            return result;
        }

    //通过文件读取数据
    private static List<Double> readFile(String path) {
        List<Double> ll=new ArrayList<>();
        String line="";
        try
        {
            BufferedReader in=new BufferedReader(new FileReader(path));
            while ((line=in.readLine()) != null)
            {
                //表示字符串长度不受限制
//                String[] str=line.split(" ",-1);
                //先把外面的替换掉
                String c = line.replaceAll("\r|\n", "");
                String[] str=c.split(" ");
                for(int i=0;i<str.length;i++){
                    if (i % 5 == 0 || i % 5 == 1 || i % 5 == 2) {
                        double dat = Double.parseDouble(str[i]);
//                            System.out.print(dat+"\n");
                        ll.add(dat);
                    }
                }
            }
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.print(ll.size());
        return ll;
    }
    public static void main(String[] args){
        String path="C:\\Users\\seer\\Desktop\\王总错误数据\\bbbbb.txt";
//        String path="C:\\Users\\seer\\Desktop\\王总错误数据\\2025-08-06 103859\\a7c9bff53f2e4a70af7a9f641552507a_1754447849073-35.txt";
//        String path="C:\\Users\\seer\\Desktop\\测量无效\\vlide\\error2.txt";
        List<Double> lp=readFile(path);
        boolean fg=CorrelationTools(lp);
        System.out.print(fg);
    }

}
相关推荐
做萤石二次开发的哈哈5 小时前
连锁门店、园区、交通枢纽的视频如何统一汇集?应用模板的接入与分权管理详解
人工智能·物联网·音视频·二次开发·视频监控·蓝海aiot一站式工作台·萤石云开放平台
老金带你玩AI5 小时前
全程拿着手机,我让dot准备了一场AI分享
人工智能
nvd115 小时前
Flink 批处理驱动的湖仓 AI 财务研报流水线:从双流广播汇聚到 Agent 智能决策与原生 Iceberg 落盘全景实战
大数据·人工智能·flink
码爸5 小时前
线性回归算法
算法·机器学习·线性回归
snhna5 小时前
向量数据库深度实战:从 HNSW 到 pgvector、Milvus 选型与 RAG 落地
数据库·人工智能·python·postgresql·milvus
架构技术专栏5 小时前
Agent 工具调用:从工具声明到执行反馈
前端·人工智能·后端
Coder-magician6 小时前
《代码随想录》刷题打卡day51:图论-part09(dijistra堆优化,Bellman_ford算法)
算法·图论
点云-激光雷达-Slam-三维牙齿6 小时前
100,000 Records CSV File - India Traffic Accident Analysis Dataset.7z
人工智能·数据集
TechWayfarer6 小时前
精度极限的突破:AI与IPv6如何重塑高精度IP查询
人工智能·网络协议·tcp/ip·transformer
Nil2086 小时前
leetcode 763划分字母区间
算法·leetcode·职场和发展