-4

我想弄清楚如何将 2 个数组中的值相乘(作为输入)以获得输出。我遇到的问题是如何增加循环以实现如下所示的任务

#include <iostream>

using namespace std;

main()
{
 int* filter1, *signal, fsize1 = 0, fsize2 = 0, i = 0;

 cout << " enter size of filter and signal" << endl;
 cin >> fsize1 >> fsize2;

filter1 = new int [fsize1];
signal = new int [fsize2];

cout << " enter filter  values" << endl;
for (i = 0; i < fsize1; i++)
cin >> filter1[i];
 cout << " enter  signal values" << endl;
for (i = 0; i < fsize2; i++)
cin >> signal[i];

/*

这两个数组应该由用户填写,但使用下面的数组进行测试:

int array1[6] = {2, 4, 6, 7, 8, 9};
int array2[3] = {1, 2, 3};

The output array should be

array3[8]= {1 * 2, (1 * 4 + 2 * 2), (1 * 6 + 2 * 4 + 3 * 2), (1 * 7 + 2 * 6 + 3 * 4), (1 * 8 + 2 * 7 + 3 * 6), (1 * 9 + 2 * 8 + 3 * 7), (2 * 9 + 3 * 8), 3 * 9}


*/


 return 0;
 }

这是有关采样信号过滤器的更大任务的一部分,但我无法完成这种乘法。

4

4 回答 4

1

在这里查看有关卷积的信息。

一旦你理解了这个过程,那么编码就不应该那么难了。该网站还包含一个用于二维卷积的 C++ 算法。

于 2012-12-03T23:22:19.727 回答
0

假设您有三个长度数组N

  • input1, 第一个输入数组
  • input2,第二个输入数组
  • output, 输出数组

然后,对于ifrom 0to N - 1,循环并将结果input1[i] * input2[i]放入 into output[i](假设为点积)。

于 2012-12-03T22:46:51.813 回答
0

我已经有 7 年没有读过关于卷积的文章了。以下代码(适用于您的测试值)完全基于您的示例。由于您没有以任何正式的方式阐明所需的算法,我无法确定此代码是否会为其他输入产生所需的输出。

我已经更改了您的一些变量名称,并重新排序了您提供的一些代码,以便我可以真正思考问题。

#include <iostream>
using namespace std;

void main ()
{
    int *signal, *filter, signalLength = 0, filterLength = 0, i = 0;

    cout << "Enter size of signal and filter" << endl;
    cin >> signalLength >> filterLength;

    signal = new int [signalLength];
    filter = new int [filterLength];

    cout << "Enter  signal values" << endl;
    for (i = 0; i < signalLength; i++)
    {
        cin >> signal[i];
    }

    cout << "Enter filter  values" << endl;
    for (i = 0; i < filterLength; i++)
    {
        cin >> filter[i];
    }

    // It was a stated requirement that the filter array be smaller than
    // the signal array.
    // add a check here and act accordingly

    int outputLength = signalLength + filterLength - 1;
    int *output = new int[outputLength];
    int signalLeft = 0;
    int signalRight = 1;
    int filterLeft = 0;
    int filterRight = 1;
    int outputIndex = 0;
    while (signalLeft < signalLength)
    {
        int indexWidth = signalRight - signalLeft;


        // I sure hope I've interpretted your question correctly.
        // I recommend you read over this loop
        // carefully to ensure it matches your understanding of the problem at hand.
        output[outputIndex] = 0;
        int j = 0;
        while (j < indexWidth)
        {
            output[outputIndex] += signal[signalLeft + j] * 
                                   filter[filterRight - j - 1];

            ++j;
        }


        // The left and right indexes will start the loop 1 index apart
        // The right indexes will increment until the distance between
        //    left and right is equal to the length of the filter array.
        // Then, the signal indexes will increment until the right signal
        //   index is equal to the length of the signal array.
        // Then, both left indexes will increment until the left and right
        //   indexes are again 1 index apart.
        if (filterRight < filterLength)
        {
            ++signalRight;
            ++filterRight;
        }
        else if (signalRight < signalLength)
        {
            ++signalLeft;
            ++signalRight;
        }
        else
        {
            ++signalLeft;
            ++filterLeft;
        }

        ++outputIndex;
    }


    for (i = 0; i < outputLength; ++i)
    {
            cout << i << ": " << output[i] << endl;
    }


    char dummy;
    cin >> dummy;
}
于 2012-12-04T02:20:05.243 回答
0
int newLength = fSize1 + fSize2 - 1;

int *array3 = new int[newLength];
int count = 0;

// initialize all array3 value to 0
for(int k = 0; k<newLength; k++)
{
    array3[k] = 0;
}

for(int i = 0; i<newLength; i++)
{
    for(int j = 0; j<fSize2; j++)
    {
        // to avoid outofbound error
        if(i==0)
            array3[i] = array2[i] * array1[j];
        else
            array3[i] = array3[i- 1] + (array2[i] * array1[j]);
    }
}

希望这可以帮助。

于 2012-12-04T02:29:48.773 回答