MathTBox Library for .NET

New product

Download MathTBox Library                 view Licensing

1 General remarks

The MathTBox class is intended to speed up the software development in

.NET environment.

It offers an intuitive and simple interface to various frequently used mathematical routines. Please note that FFT(), IFFT(), FFT2() and IFFT2() methods offer an interface to the FFTW library available for free download at http://www.fftw.org Therefore, please make sure that the FFTW-DLLs:

  • libfftw3-3.dll, libfftw3f-3.dll and libfftw3-3l.dll

are saved in the startup folder of your application.

Along with this manual, the class is distributed with the source of the demo project MTBoxDemo, which can be freely downloaded at

http://bntech.info and can be used as a quick guide to the MathTBox class.

2 Constructors

MathTBox()

Reference: MathToolBox.dll

3 Methods

3.1 FFT - Fast Fourier transform

public FFTResult FFT(double[] WaveForm, double SamplingTime = 1.0)

Computes the Fourier transform of the array WaveForm, using the fast Fourier transform (FFT) technique. The output is scaled to return the exact amplitude spectrum.

3.1.1 Parameters

  • WaveForm

type: System.Double []

This is the array holding the equidistantly sampled data points of the signal.

  • SamplingTime

type: System.Double

This is the time interval between two consecutive data points. Note: the

value should be in seconds in order to get the resulting frequencies in

Hz.

3.1.2 Return Value

type: struct FFTResult

{

double[] Frequencies;

double[] Amplitudes;

double[] Real;

double[] Imaginary;

int ErrCode;

}

where Frequencies is the array with the frequencies extracted from the time domain signal, Amplitudes are the amplitudes at the corresponding frequencies, Real and Imaginary are the real and imaginary parts of FFT.

Note that Amplitudes[i] = qReal[i]2 + I maginary[i]2.

ErrCode = 0 in case of success or ErrCode = -1 otherwise.

3.1.3 Example

The following example computes the Fourier transform of the function:

y = 2.5 · sin (2π · 50 · t) + 1.2 · cos (2π · 70 · t) + 3.0 · sin (2π · 45 · t) + 1.5

sampled with a sampling time0.001 s in the interval [0; 1] seconds.

using MathToolBox;

//....

int N =1001; // length of the array

double[] WaveForm = new double[N]; // holder for the sampled data

double[] Time = new double[N];// array with the points in time

double SamplingTime = 0.001; // sampling time in seconds

/*now fill the WaveForm array*/

for (int ind = 0; ind < N; ind++)

{

double t = (double)ind*SamplingTime; Time[ind] = t;

WaveForm[ind] =2.5*Math.Sin(2*pi*50.0*t)+1.2*Math.Cos(2*pi*70.0*t)+3.0*Math.Sin(2*pi* 45.0 * t) +1.5;

}

MathTBox mb = new MathTBox();// Construct a MathTBox object

FFTResult fftr = mb.FFT(WaveForm, SamplingTime);// call the FFT method.

double [] Amplitudes =fftr.Amplitudes;

double [] Frequencies = fftr.Frequencies;

/*use some tool to plot the result...*/

(a) The time domain signal. (b) The amplitude spectrum.

FFTfft

3.2 IFFT - Inverse fast Fourier transform

public IFFTResult IFFT(double[] Real, double[] Imaginary)

public IFFTResult IFFT(double[] Real, double[] Imaginary, double[] Frequencies)

returns the inverse discrete Fourier transform of a pair of real/imaginary arrays obtained with a fast Fourier transform (FFT). The version with 3 input parameters accepts in addition an array of frequencies computed with FFT() and fills the Time array in the IFFTResult structure with meaningful values.

Note: the frequencies should be in Hz in order to get the resulting time in seconds.

3.2.1 Parameters

  • Real

type: System.Double []

This is the real part of FFT.

  • Imaginary

type: System.Double []

This is the imaginary part of FFT.

  • Frequencies

type: System.Double []

These are the frequencies computed with FFT.

3.2.2 Return Value

type: struct IFFTResult

{

double[] Time;

double[] Waveform;

int ErrCode;

}

here Waveform holds the timedomain signal sampled at times specified in Time. The IFFT() version with two input arguments fills the Time array with consecutive numbers 0, 1, 2, ....

ErrCode = 0 in case of success or ErrCode = -1 otherwise.

3.2.3 Example

The following example uses IFFT() to remove the DC component of the function:

y = 2.5 · sin (2π · 50 · t) + 1.2 · cos (2π · 70 · t) + 3.0 · sin (2π · 45 · t) + 7

sampled with a sampling time 0.001 s in the interval [0; 0.5] seconds.

using MathToolBox;

//....

int N =501; // length of the array

double[] WaveForm = new double[N]; // holder for the sampled data

double[] Time = new double[N];// array with the points in time

double SamplingTime = 0.001; // sampling time in seconds

/*now fill the WaveForm array*/

for (int ind = 0; ind < N; ind++)

{

double t = (double)ind*SamplingTime;

Time[ind] = t;

WaveForm[ind]=2.5*Math.Sin(2*pi*50*t)+1.2*Math.Cos(2*pi*70*t)+3.0*Math.Sin(2*pi*45*t)+7;

}

/*compute the FFT first*/

MathTBox mb = new MathTBox();// Construct a MathTBox object

FFTResult fftr = mb.FFT(WaveForm, SamplingTime);// call the FFT method.

/*set the DC component to 0*/

fftr.Real[0] = 0;

fftr.Imaginary[0] = 0;

/*use IFFT to get the time profile of the modified signal*/

IFFTResult IfftRes = mb.IFFT(fftr.Real, fftr.Imaginary, fftr.Frequencies);

Time = IfftRes.Time;

Waveform = IfftRes.Waveform;

/*use some tool to plot the result...*/

fft test

Figure 1: The original signal (red) and the one with the DC component removed (green).

3.3 LowPass - lowpass filter

public IFFTResult LowPass(double[] WaveForm, double SamplingTime, double MaxFrequencyHz)

A numerical filter that removes all frequency components above the cutoff frequency

MaxFrequencyHz.

  • MaxFrequencyHz

type: System.Double

the cutoff frequency of the filter. Note: the value must be in Hz

3.3.2 Return Value

type: struct IFFTResult

{

double[] Time;

double[] Waveform;

int ErrCode;

}

here Waveform holds the time-domain signal sampled at times specified in

Time.

ErrCode = 0 in case of success or ErrCode = -1 otherwise.

3.3.3 Example

see example for HighPass

3.4 BandPass - band-pass filter

public IFFTResult BandPass(double[] WaveForm, double SamplingTime, double MinFrequencyHz,

double MaxFrequencyHz)

A numerical filter that has full transmission in the pass band

Min Frequency Hz < f < Max Frequency Hz

and complete attenuation outside.

  • MinFrequency Hz

type: System.Double

the minimum frequency of the band pass (see above). Note: the value must be in Hz

  • MaxFrequencyHz

type: System.Double

the maximum frequency of the band pass (see above). Note: the value must be in Hz

3.4.2 Return Value

type: struct IFFTResult

{

double[] Time;

double[] Waveform;

int ErrCode;

}

here Waveform holds the time-domain signal sampled at times specified in

Time. ErrCode = 0 in case of success or ErrCode = -1 otherwise.

3.4.3 Example

see example for HighPass

3.5 HighPass - high-pass filter

public IFFTResult HighPass(double[] WaveForm, double SamplingTime, double MinFrequencyHz)

A numerical filter that removes all frequency components below

MinFrequencyHz.

3.5.1 Parameters

  • WaveForm

type: System.Double []

This is the array holding the equidistantly sampled data points of the signal.

  • SamplingTime

type: System.Double

This is the time interval between two consecutive data points.

Note: the value must be in seconds.

  • MinFrequencyHz

type: System.Double

the minimum frequency, all frequency components below this frequency will be removed.

Note: the value must be in Hz

3.5.2 Return Value

type: struct IFFTResult

{

double[] Time;

double[] Waveform;

int ErrCode;

}

here Waveform holds the timedomain signal sampled at times specified in

Time.

ErrCode = 0 in case of success or ErrCode = -1 otherwise.

3.5.3 Example

The following example uses HighPass() to filter the frequencies below 60 Hz in the function:

y = 2.5 · sin (2π · 50 · t) + 1.2 · cos (2π · 70 · t) + 3.0 · sin (2π · 45 · t) + 0.5

sampled with a sampling time 0.001 s in the interval [0; 1] seconds.

fft

Figure 2: The original signal (red) and the one with applied high-pass filter at 60 Hz (green).

using MathToolBox;

//....

int N =1001; // length of the array

double[] WaveForm = new double[N]; // holder for the sampled data

double[] Time = new double[N];// array with the points in time

double SamplingTime = 0.001; // sampling time in seconds

/*now fill the WaveForm array*/

for (int ind = 0; ind < N; ind++)

{

double t = (double)ind*SamplingTime; Time[ind] = t;

WaveForm[ind]=2.5*Math.Sin(2*pi*50*t)+1.2*Math.Cos(2*pi*70*t)+3*Math.Sin(2*pi*45*t)+0.5;

}

MathTBox mb = new MathTBox();// Construct a MathTBox object

IFFTResult IfftRes = mb.HighPass(WaveForm, SamplingTime, 60.0);

Time = IfftRes.Time;

Waveform = IfftRes.Waveform;

/*use some tool to plot the result...*/

3.6 FFT2 - 2D fast Fourier transform

public FFT2Result FFT2(double[][] WaveForm)

Computes the two-dimensional discrete Fourier transform of the input array, which is assumed to be in row-major order. The leftmost dimension is Y and the rightmost is X.

Note: the method automatically rearranges the output arrays Amplitudes, Real and Imaginary in the FFT2Result structure by moving the zero frequency component to the center of the corresponding array. The elements of the output arrays are scaled to give the exact amplitude spectrum.

3.6.1 Parameters

  • WaveForm

type: System.Double [][]

This is the array holding the data points of the signal. It is assumed to be in row-major order i.e. the leftmost dimension is Y and the rightmost is X.

3.6.2 Return Value

type: struct FFT2Result

{

double[] FrequenciesX;

double[] FrequenciesY;

double[][] Amplitudes;

double[][] Real;

double[][] Imaginary;

int ErrCode;

}

Here FrequenciesX and FrequenciesY are the 1D spatial frequencies with values in the interval [−0.5; 0.5]. Amplitudes, Real and Imaginary are 2D arrays holding the results of the Fourier transform. The elements of these arrays are rearranged by moving the zero-frequency component to the center. The Amplitudes array isincluded only for convenience since Amplitudes[i, j] =qReal[i, j]2 + I maginary[i, j]2.

ErrCode = 0 in case of success or ErrCode = -1 otherwise.

3.6.3 Example - Airy pattern

The following example computes the Fourier transform of a disc with a radius of 50 pixel.

(a) The disc with radius of 50 pixel.

fft

(b) The 2D FFT-spectrum - the Airy pattern.

/*create an 300 x 400 array*/

double[][] WaveForm = new double[300][];

double Radius = 50;

/*fill the 2D array*/

for (int y = 0; y < 300; y++)

{

WaveForm[y] = new double[400];

for (int x = 0; x <400; x++)

{

double xr = (double)x - 200.0;

double yr = (double)y -150.0;

WaveForm[y][x]=Math.Sqrt(xr * xr + yr * yr) < Radius ? 255 : 0;

}

}

/*calculate the 2D FFT*/

MathTBox mb = new MathTBox();

FFT2Result fft2r = mb.FFT2(WaveForm);

/*Create and display a Bitmap image of the 2D array and of its FFT*/

/*check the MTBoxDemo project for details*/

3.7 IFFT2 - 2D inverse fast Fourier transform

public IFFT2Result IFFT2(double[][] Real, double[][] Imaginary)

Computes the two-dimensional discrete inverse Fourier transform using input arrays Real and Imaginary obtained with FFT.

Note: The both input arrays should be in rearranged form, such that the zero- frequency component is in center of the array. For example this should be the case if Real and Imaginary have been obtained using FFT2() (see above).

3.7.1 Parameters

  • Real

type: System.Double [][]

  • Imaginary

type: System.Double [][]

These are the real and imaginary parts of the 2D FFT.

3.7.2 Return Value

type: struct IFFT2Result

{

double[][] Waveform;

int ErrCode;

}

here Waveform holds the resulting 2D spatial distribution. ErrCode = 0 in case of success or ErrCode = -1 otherwise.

3.7.3 Example

Compute the forward and the inverse 2D Fourier transform of the distribution given with the equation:

2W

I (x, y) = 255 · |cos( π · (x2 + y2))|, where W is the size of the image.

int W = 400;

double[][] WaveForm = new double[W][];

/*fill the 2D array*/

for (int y = 0; y < W; y++)

{

WaveForm[y] = new double[W];

for (int x = 0; x < W; x++)

{

double xr = (double)x - (double)W / 2.0;

double yr = (double)y - (double)W / 2.0;

WaveForm[y][x] =255.0* Math.Abs(Math.Cos(2* Math.PI * 0.25 / W * (xr*xr+yr*yr)));

}

}

/*calculate the 2D FFT*/ MathTBox mb = new MathTBox();

FFT2Result fft2r = mb.FFT2(WaveForm);

/*calculate the 2D IFFT*/

IFFT2Result ifft2r = mb.IFFT2(fft2r.Real, fft2r.Imaginary);

/*Create and display a Bitmap image of the original 2D image and its in- verse transformed FFT*/

/*check the MTBoxDemo project for details*/

fft

Figure 3: The original image (above) and its inverse transformed FFT.

3.8 PolyFit - Polynomial curve fitting

public PolyFitResult PolyFit(double[] X, double[] Y, int N)

Finds the coefficients pi of a polynomial p0 + p1 · x + p2 · x2 + ... + pN · xN

of order N that fits to Y in a least squares sense.

3.8.1 Parameters

  • X

type: System.Double []

the points at which to evaluate the polynomial

  • Y

type: System.Double []

The data the polynomial has to fit to.

  • N

type: System.Int32 the polynomial order

3.8.2 Return Value

type: struct PolyFitResult

{

int Order;

double[] Coefficients;

int ErrCode;

}

here Order is the polynomial order. Coefficients is an array whose elements are the coefficients in ascending powers of the polynomial

p0 + p1 · x + p2 · x2 + ... + pN · xN ,

where Coefficients[0] = p0, Coefficients[1] = p1 and so on.

ErrCode = 0 in case of success or ErrCode = -1 otherwise.

3.8.3 Example

see example for PolyVal

3.9 PolyVal - Polynomial evaluation

public double[] PolyVal(double[] X, double[] Coefficients)

evaluates a polynomial.

3.9.1 Parameters

  • X

type: System.Double []

the points at which to evaluate the polynomial

  • Coefficients

type: System.Double []

The coefficients in ascending powers of the polynomial:

p0 + p1 · x + p2 · x2 + ... + pN · xN ,

Coefficients[0] = p0, Coefficients[1] = p1 and so on.

3.9.2 Return Value

type: System.Double []

the array with the values of the polynomial.

3.9.3 Example

The example below uses PolyFit to fit the data:

double[] Y = { -0.37, -0.70, -0.94, -1.59, -0.87, -1.63, -1.43, -1.11,-0.79, -0.82, -0.57, 0.21, 0.27, 1.30, 1.95, 2.70 };

observed at the points:

double[] X = { 0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8,0.9, 1, 1.10, 1.20, 1.30, 1.40, 1.50 };

to a second order polynomial.

using MathToolBox;

//....

/* fit the data using PolyFit() */ MathTBox mb = new MathTBox(); PolyFitResult pfr = mb.PolyFit(X, Y, 2);

/* now evaluate the polynomial using PolyVal() */

double [] FittedValues = mb.PolyVal(X, pfr.Coefficients);

/*use some tool to plot the result...*/

fft

Figure 4: The raw data (red) and the second order polynomial fit (green).

Go to TOP

€14.50 tax excl.

Download

Contents:

1

 General remarks

3

2

 Constructors

3

3

 Methods

3

3.1

 FFT-Fast Fouriertransform

3

3.1.1

 Parameters

3

3.1.2

 Return Value

4

3.1.3

 Example

4

3.2

 IFFT Inversefast Fouriertransform

5

3.2.1

 Parameters

5

3.2.2

 Return Value

6

3.2.3

 Example

6

3.3

 LowPass-low-passfilter

7

3.3.1

 Parameters

8

3.3.2

 Return Value

8

3.3.3

 Example

8

3.4

 BandPass-band-passfilter

8

3.4.1

 Parameters

9

3.4.2

 Return Value

9

3.4.3

 Example

9

3.5

 HighPass-high-passfilter

9

3.5.1

 Parameters

10

3.5.2

 Return Value

10

3.5.3

 Example

11

3.6

FFT2-2Dfast Fouriertransform

12

3.6.1

 Parameters

12

3.6.2

 Return Value

12

3.6.3

 Example-Airypattern

13

3.7

 IFFT2-2Dinversefast Fouriertransform

14

3.7.1

 Parameters

14

3.7.2

 Return Value

14

3.7.3

 Example

14

3.8

 PolyFit-Polynomial curvefitting

16

3.8.1

 Parameters

16

3.8.2

 Return Value

17

3.8.3

 Example

17

3.9

 PolyVal-Polynomial evaluation

17

3.9.1

 Parameters

17

3.9.2

 Return Value

17

3.9.3

 Example

18

PayPal

SEO by smuggler@abv.bg . . . . . . . . . . . . . . . . . .news sports rent a car . . . . . . .

....................................