Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

C# Fast Fourier Transform

I'm currently translating an older VB6 project that makes heavy use of Measurement Studio over to C# in the .Net Framework 2.0 as part of an internship. I've had decent luck with the code so far, but every once in awhile I run into something altogether bafflingly nasty.

 

The old code uses a cwDSP object to do some FFT stuff. Example:

 

-------------------------------------------

Public Sub FFT(float[] ZData)

    Dim realdata As Variant

    Dim imgdata As Variant

 

    ...

 

    cwDSP1.ReFFT ZData, realdata, imgdata

 

    ...

End Sub

-------------------------------------------

 

Both realdata and imgdata are interpretted as single-dimension arrays of doubles. I tried doing the same with the axCWDSP object in C#, but it won't accept an array of doubles. It will only accept a "ref object". I tried casting my double[]s as (object)s, and the compiler stopped complaining, but I get a runtime error: "Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))"

 

I also tried passing them in a loop of each individual entry in the array, like:

 

for (int i = 0; i < realData.Length; ++i){

    axCWDSP1.ReFFT(ZData[i], ref realData[i], ref imgData[i]);

}

 

Regardless as to whether or not they've been cast as (object)s, I get a "Type Mismatch" error. I've got no idea what I'm supposed to do from here. Alternatively, I heard another topic mention a "Transforms" class that is a newer .NET equivalent, and does the same kind of thing. I have yet to find it or any other real information on it. If someone could tell me how I'm either supposed to be using the DSP object, or how to take advantage of the Transforms.FFT() function, I would be very greatful.

 

Thanks for your time and consideration.

0 Kudos
Message 1 of 3
(7,855 Views)

KChaloux,

 

I assume this is what you require.  However you do need the Analysis Enterprise library.

 

Also I'm a VB programmer so this is converted from VB.

 

Try something like: -

 

        public NationalInstruments.ComplexDouble[] fft(double[] realData, double[] imagData)  
         {  
            // allocate an array to hold complex data  
             NationalInstruments.ComplexDouble[] zData = new NationalInstruments.ComplexDouble[]{};  
            // create array of complex double from arrays of 'Real' and 'Imaginary' doubles  
             zData = NationalInstruments.ComplexDouble.ComposeArray(realData, imagData);  
             // Computes, in place, the one-dimensional Fast Fourier Transform (FFT) of a complex time-domain signal.   

             NationalInstruments.Analysis.Dsp.Transforms.Fft(zData);  

             // zData is now the FFT of complex time signal   

             return zData;  

            // There is also  

            // Computes the one-dimensional Fast Fourier Transform (FFT) of a complex time-domain signal with the specified shift.   

            //NationalInstruments.Analysis.Dsp.Transforms.Fft(zData,True)  
            // where   
            //Parameters()  

            //inputData()  

            //Type: array< NationalInstruments..::.ComplexDouble >[]()[]  

            //The complex time-domain signal.   

            //shiftDC()  

            //Type: System..::.Boolean  

            //Specifies whether to shift the DC component to the center of the FFT. If shiftDC is set to true (True in Visual Basic), the FFT is DC-centered.  

            // The first element of the FFT is the -PI frequency element.   

            // The floor(number of elements in FFT / 2)th element of FFT is the DC component.   

            //       Return Value  

            //The FFT of the time-domain signal.  

            //Return zData  
         } 
  

 

Hope this helps

 

StevieB

 

 

NationalInstruments.

Message 2 of 3
(7,839 Views)

This is exceptionally helpful. I actually did discover the Analysis library earlier today, but I wouldn't have known to use ComposeArray in conjunction. Thanks for the sample, and the help over all.

0 Kudos
Message 3 of 3
(7,832 Views)