The following post demonstrates the use of MathTBox library within Visual Basic for

The following post demonstrates the use of MathTBox library within Visual Basic for .NET environment.
The example that we are going to explore shows how to make a fast fourier transform of a time signal. The time signal can be any form of analog signal - for example audio, speech etc. In our case we are going to simulate it for simplicity.
Open ...

The following post demonstrates the use of MathTBox library within Visual Basic for .NET environment.
The example that we are going to explore shows how to make a fast fourier transform of a time signal. The time signal can be any form of analog signal - for example audio, speech etc. In our case we are going to simulate it for simplicity.
Open up Visual Studio and create a new windows forms for VB.net project.



Type in a name for the project and save the project in a dedicated folder on your drive. 
In design view add two chart controls on the form and a button.


AddChart.PNG     


Arrange the charts so that they are wide as the form and place them one below the other.

Change the Text property of the button to :"Calculate FFT"  and double click the button.
We have to import the Library and its DLL to the project - go to Project->add reference


In the resulting window choose browse and navigate to the directory where you store  MathToolBox.dll and select it.
copy libfftw3-3.dll, libfftw3f-3.dll and libfftw3-3l.dll  from the MathTbox folder to the bin/debug folder of the solution. ( The startup folder )
add the following to the top of the Form code - above the main class definition:

Imports MathToolBox

'Public Class Form1
.......

Finally declare a variable of the imported class:
Public MTB As MathTBox

 

Within the Button1_Click subroutine we are going to do the signal simulation, FFT calculation and
visualisation of the signal and the fft result on the chart controls.

The first task we have to do is generate a time signal. We are going to use the formula
from the manual, which you can download here:
y = 2.5 · sin (2π · 50 · t) + 1.2 · cos (2π · 100 · t) + 3.0 · sin (2π · 150 · t) + 1.5
With the help of this formula we are able to calculate , for every X the value of Y. The resulting signal will have 3 component frequencies (50Hz,100Hz,150Hz) and a positive offset.

Before we continue we have to make some explanations on measuring signals and values from the real - physical world
to a form the computer understands.
The way we pass information to the computer is by means of digitizers. These devices translate the values of different real life processes
into discrete set of points. One major characteristic of a digitizer is the speed with which it builds the set of points, this is also called
Sampling speed. In essence The sampling speed shows how many points can a digitizer acquire per second.

Returning back to our code, in order to calculate Y values we have to know our X values, which represent the time at which the given sample was taken.
We will simulate a digitizer with a 1Khz sampling speed. This will give us 1ms interval between each sample point and we will start at time 0.

we first declare array to hold 1000 samples, which with a 1Khz Sampling speed gives us 1 second of signal.

Dim elements(1000) As Double
We declare a variable to hold the time (X value ) and we start from zero.
Dim t As Double = 0

we generate our time series with a time between elements = 0.001

        For i = 0 To elements.Length - 1
elements(i) = 2.5 * Math.Sin(Math.PI * 2 * 50 * t) _
+ 1.2 * Math.Cos(Math.PI * 2 * 100 * t) _
+ 3 * Math.Sin(2 * Math.PI * 150 * t) + 1.5
'increment t for the next element
t += 0.001 ' 1 ms increments

        Next

We can now plot the generated signal to the first chart:

For i = 0 To elements.Length - 1

            Chart1.Series(0).Points.AddXY((i * 0.001), elements(i))
Next

In order to have the fft we have to instantiate the MTB variable.

MTB = New MathTBox

and to declare  a new variable of type FFTResult to hold the result of the fft() routine. This type can be found within MathToolBox namespace.
A detailed explanation of the type can be found in the manual.

Dim result As MathToolBox.FFTResult = MTB.FFT(elements, 0.001)

Here we assign the result variable values from the FFT() function contained in the MathToolBox  namespace. The parameters passed to the function are the array elements which is holding the points we generated, and the time in seconds between two consecutive points. This value is equal to 1 second divided by  Sampling speed - 1/1000 in our case.

That's it we are done. now we can plot the FFT in the second chart:

For i = 0 To result.Frequencies.Length - 1
Chart2.Series(0).Points.AddXY(result.Frequencies(i), result.Amplitudes(i))

        Next

Here is how the entire code should look:

Imports MathToolBox

Public Class Form1
    Public MTB As MathTBox

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    'y = 2.5 · sin (2π · 50 · t) + 1.2 · cos (2π · 100 · t) + 3.0 · sin (2π · 150 · t) + 1.5
    Dim elements(1000) As Double
    Dim t As Double = 0
    'First we generate our time series with a time between elements = 0.001
     For i = 0 To elements.Length - 1
           elements(i) = 2.5 * Math.Sin(Math.PI * 2 * 50 * t) _
                 + 1.2 * Math.Cos(Math.PI * 2 * 100 * t) _
                 + 3 * Math.Sin(2 * Math.PI * 150 * t) + 1.5
           'increment t for the next element
      t += 0.001

      Next

       For i = 0 To elements.Length - 1

            Chart1.Series(0).Points.AddXY((i * 0.001), elements(i))

      Next
      'instantiate mtb
      MTB = New MathTBox
      'declare a variable to hold the result of the fft transform
       Dim result As MathToolBox.FFTResult = MTB.FFT(elements, 0.001)

       ' draw the result on the chart
       For i = 0 To result.Frequencies.Length - 1
              Chart2.Series(0).Points.AddXY(result.Frequencies(i), result.Amplitudes(i))

         Next
    End Sub

End Class

and here is how the program looks:


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

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