-4

I am tying to get around how you will multiply the values in 2 arrays (as an input) to get an output. The problem I have is the how to increment the loops to achieve the task shown below

#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];

/*

The two arrays should be filled by users but use the arrays below for test:

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;
 }

This is part of a bigger task concerning filter of a sampled signal but it is this multiplication that I cant get done.

Tobey
  • 1,400
  • 1
  • 10
  • 25
  • 3
    Have you tried anything, or expect us to give you the code? – Luchian Grigore Dec 03 '12 at 22:41
  • Use for loops to iterate through the arrays and multiply them. – Gustavo Litovsky Dec 03 '12 at 22:47
  • 2
    Because this is such a basic thing to do, I really recommend that you pick up a programming book: http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list – Gustavo Litovsky Dec 03 '12 at 22:49
  • This isn't multiplication - this is *convolution*. – Paul R Dec 03 '12 at 23:02
  • Maybe I'm a bit dense, but I'm unclear on the pattern of products that you're after. You have provided the first three and last three, but I have no idea what the middle three are. Should this work for input arrays of any size? Can the second array be bigger than the first? Can they be the same size? I need to know the algorithm before I can tell you how to program it. – csj Dec 03 '12 at 23:08
  • @PaulR yess yor rightbut for the course i am doing, we dont need to understand that – Tobey Dec 03 '12 at 23:09
  • Please explain the algorithm, so that we can actually help you. For now, it looks like the length of `array3` should be the sum of the lengths of `array1` and `array2`. – Danilo Piazzalunga Dec 03 '12 at 23:12
  • Are you doing [dot product?](http://mathinsight.org/dot_product_examples) – Derek W Dec 03 '12 at 23:15
  • @toby: try to express how `array3` should be filled not using numbers, but using positions of `array1` and array2`. For what I can understand from your example: `array3[0] = array2[0]*array1[0]`; `array3[1] = array2[0]*array1[1] + array2[1]*array2[0]`; etc. – Danilo Piazzalunga Dec 03 '12 at 23:24
  • @csj 13 ... This should work for arrays of any size however the signal input has to be bigger than the filter I.e. array1 is bigger than array2 – Tobey Dec 03 '12 at 23:26
  • @DaniloPiazzalunga: Yes, it should be filled in that order – Tobey Dec 03 '12 at 23:29

4 Answers4

1

Take a look here for information on convolution.

Once you understand the process, then it should not be that difficult to code. There's also a C++ algorithm for 2-dimensional convolution included at this website.

Derek W
  • 9,708
  • 5
  • 58
  • 67
0

Suppose you have three arrays of length N:

  • input1, the first input array
  • input2, the second input array
  • output, the output array

Then, for i from 0 to N - 1, loop and put the result of input1[i] * input2[i] into output[i] (assuming dot product).

Danilo Piazzalunga
  • 7,590
  • 5
  • 49
  • 75
0

I haven't read a word about convolution in a good 7 years. The following code (which works on your test values) is based entirely on your examples. Since you have not articulated the required algorithm in any formal manner, I cannot be sure that this code will produce the desired output for other inputs.

I have changed around some of your variable names, and re-ordered some of the code you provided so I could actually think through things.

#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;
}
csj
  • 21,818
  • 2
  • 20
  • 26
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]);
    }
}

Hope this helps.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
CNLSH
  • 303
  • 1
  • 6
  • 13