C语言互相关函数

1 篇文章 0 订阅

C语言中编写自定义的互相关函数

代码块

//互相关函数_from word 
//该函数可以生成out_xcorr.txt文件
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define NN 10

void xcorr(float *r, unsigned short *x, unsigned short *y, int N);

int main()
{  
    unsigned short x[10]={1,1,2,2,3,4,5,6,7,8};
    unsigned short y[10]={1,3,4,5,6,7,8,9,5,2};
    float r[19] = {0};

    FILE *fp_out;
    int delay;

    xcorr(r, x, y, NN);
    //Open the file to write
    if((fp_out=fopen("out_xcorr.txt","wt")) == NULL)
    {
        printf("Cannot open this file!\n");
        exit(0);
    }

    for(delay = -NN + 1; delay < NN; delay++)           
        fprintf(fp_out,"%d %f\n",delay,r[delay + NN - 1]);

    fclose(fp_out);
    //在命令框中显示结果
    for(delay = -NN + 1; delay < NN; delay++)           
        printf("%d %f\n",delay,r[delay + NN - 1]);
    system("pause");
    return 0;
}

void xcorr(float *r, unsigned short *x, unsigned short *y, int N)
{
    float sxy;
    int    delay,i,j;

    for(delay = -N + 1; delay < N; delay++)
    {
        //Calculate the numerator
        sxy = 0;
        for(i=0; i<N; i++)
        {
            j = i + delay;
            if((j < 0) || (j >= N))  //The series are no wrapped,so the value is ignored
                continue;
            else
                sxy += (x[i] * y[j]);
        }

        //Calculate the correlation series at "delay"
        r[delay + N - 1] = sxy;
    }
}

程序编译的结果生成了txt文件

  • 11
    点赞
  • 49
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值