0

I'm converting C++ code to C# code, it happens to be the Fast Fourier Transform on an image in the Frequency Domain. Just to give some context.

Here is a link to the C++ Code : fft.cc

I have this function called Step, its signature is this in C++:

void step ( int n, int mj, float a[], float b[], float c[], float d[], float w[], float sgn );

and is called like this:

step ( n, mj, &x[0*2+0], &x[(n/2)*2+0], &y[0*2+0], &y[mj*2+0], w, sgn );

I want to convert it to C#, now this function operates on either Y or X arrays depending on whether its a Forward Fast Fourier Transform or a Backward Fast Fourier Transform. (More context)

What I don't understand, is that in C#, doing x[0*2+0] does nothing.... firstly putting an integer in the square brackets actually calls for a variable at a position in that array.

But what is it doing in C++, I know that the & is equiv to the ref C# keyword, it is saying get the contents from the array that is being pointed too, but I feel that in this context it means more than just that.

So how would you call that function in C#, obviously this fails:

step(n, mj, x[0 * 2 + 0], x[(n / 2) * 2 + 0], y[0 * 2 + 0], y[mj * 2 + 0], w, sgn);

3 Answers 3

2

In C++, this is passing the address of an entry of the array to the function. This doesn't work in C#, because you cannot take the address of an entry of an array. I suggest rewriting the declaration to pass the indices only and pass the array separately. Or make the x and y array a member of the containing class.

Uh, or if you're only interested in the single element, you could pass the element instead. Remember that float b[] in C++ is the same as float* b, so if skip only accesses b[0] (and not [b+1]), then that would probably be the easier solution).

When we have a C++ declaration like

void step (float a[]) 

which is the same as

void step (float* a)

And we call it with

step(&x[2]);

The function step sees an array of float that starts at the second entry of x. So, inside step

float f = a[0];

would refer to the element at x[2];

Of course, one could also address a[27], but that's prone for errors, since we do not know the (remaining) length of x.

To help finding the optimal solution in this case, one needs to know what step does with the arguments. Would it be possible to post the step function or parts of it?

Sign up to request clarification or add additional context in comments.

3 Comments

could you elaborate in code please, I'm finding it a little hard to understand, is C++ passing arrays to the function or single floats?
It is passing the address of an entry of the array to the function. &x[0*2+0] means: Take the address of the float that is at [0*2+0] (in this particular case: The start of the array) and pass it to the function. In C++, this is yet another array, so inside the function, a will be an array whose index is now 0 where the argument was. I'll try to explain this above a bit.
Ahhh, so it is literally trying to get a subset of the array specified starting at the index passed through. Sound cheers
0

Remove addresses, no way of changing a value except making it a class with functions to change the values or returning them. instead of float a[] use float[] a

Here's how your function looks like, no headers needed:

public class entity
{
    public void Step(int n, int mj, float a[], float[] b, float[] c, float[] d, float[] w, float sgn)
    {
       ...
    }

    Step(0, 0, new[] { 1f, 1f }, new[] { 1f, 1f }, new[] { 1f, 1f }, new[] { 1f, 1f }, new[] { 1f, 1f }, 1f);
}

1 Comment

This doesn't really help, as you pass in new data instead of references to the existing data arrays.
0

The C++ version lets you pass in pointers (think references) to certain elements in the data.

For a C# version, I would suggest you pass in the two arrays directly, and then only pass in the indices, so

 void step ( int n, int mj, float a[], float b[], float c[], float d[], 
      float w[], float sgn );

becomes

void Step(float[] x, float[] y, int n, int mj, int ai, int bi, int ci, int di,
     int w, float sign)
{ 
      float a = x[ai];
      float b = x[bi];
      float y = y[ci];

      // .... compute whatever


     x[ai] = a;
     x[bi] = b;
     //...
}

And if you encounter something like a[5] in the C++ function, you can simply use

float aa = x[ai+5];

but of course you need to think about writing the new value back, if necessary.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.