用C实现暴力自相关方法 [英] Implementing brute-force autocorrelation method using C

查看:69
本文介绍了用C实现暴力自相关方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用C来实现本维基百科文章中看到的第一个自相关公式- https://en.wikipedia.org/wiki/Autocorrelation#Properties

I am trying to use C to implement the first autocorrelation formula seen in this Wikipedia article - https://en.wikipedia.org/wiki/Autocorrelation#Properties

我确切地知道我需要用数学术语做些什么,但是尝试将其实现为迭代过程非常棘手.

I know exactly what I need to do in mathematical terms, but trying to implement this as an iterative process is proving quite tricky.

最终,我将使用从CSV文件导入到数组中的幅度值来确定音符的音高,但现在我正在使用测试数组:

Eventually I'm going to be using amplitude values imported from a CSV file into an array in order to determine the pitch of a note, but for now I'm using a test array:

int sig[] = { 0, 2, 6, 14, 25, 13, 11, 10, 5, 1 };
int N = sizeof(sig) / sizeof(sig[0]);

我现在需要像这样计算输出数组:

I now need to calculate an output array like so:

(sig[0] * sig[N-1]) 
(sig[0] * sig[N-2]) + (sig[1] * sig[N-1])
(sig[0] * sig[N-3]) + (sig[1] * sig[N-2]) + (sig[2] * sig[N-1])

以此类推...

我尝试使用for循环实现此功能,但是我不确定每次如何保持添加新值.sig [0]总是要乘以sig [Nx],每次添加新的迭代时都增加1,然后将保留该新迭代,并且乘以的sig [Nx]也将开始减少通过1.

I have tried implementing this using for loops but I'm not sure how to keep adding a new value every time. sig[0] is always going to be multiplied by sig[N-x], increasing by 1 each time a new iteration is added, and then this new iteration will remain, and the sig[N-x] that is multiplied by will also start to decrease by 1.

我知道诸如

int n=0;
int m=0;
int a[N];

for(m = 0; m = N-1; m++)
    for(n = N-m; n = 0; n--)
        a[N] = a[N-1] + sig[m] * sig[n-1]; 

将不起作用,因为当实际发生的是两个值相乘每次都不断变化时,这只会将以前的计算添加到新的计算中

won't work as this will just add the previous calculation to the new one, when what actually happens is the two values that are multiplied together keep changing each time

有人有什么建议吗?我的主要问题是,它实际上不是一个迭代",因为每次迭代之间都没有相似的值.

Does anyone have any suggestions? My main issue is that it isn't really an 'iteration', as there are no similar values between each iteration.

推荐答案

也许是这样的:

int i, j
int a[N];

for (i = 0; i <= N-1; i++) {
    a[i] = 0; 
    for (j = 0; j <= i; j++) {
        a[i] = a[i] + sig[j] * sig[N-i+j-1]; 
    }
}

与我的问题相比:

  • 将循环条件从分配更改为条件并固定开始/结束值
  • 将输出数组的初始化添加到0
  • 修复了输出数组分配以匹配示例公式的情况

这篇关于用C实现暴力自相关方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆