The following example shows how to use the WaveEasy run-time along with the GtWave driver. The example is written in VB.NET. The example opens a form that displays several controls as shown below:
WaveEasyRT/GX11X0 Example Form
The form controls can be used as follows:
1. Initialize GX11X0 – The GX1100 to the slot specified in the Slot edit box. Use this button if you have a GX11X0 board installed.
2. Create WaveEasy File – This will create a WaveEasy file that contains two segments. The first segment will contain a formula with multiple formulas and the second will perform a FIRBandPass filter on the same formula that is associated with the first segment.
3. View Wave File in WaveEasy – This will display the waveform created in the step 2 in WaveEasy.
4. Load Wave File to GX11X0 – This will load the file created in step 2 to the GX11X0 . Use this button if you have a GX11X0 board installed.
5. Calc FFT and Save to WaveEasy File – This will calculate an FFT on the multiple frequencies formula using the DSP module.
6. View FFT in WaveEasy – This will display the waveform created in the step 5 in WaveEasy.
The example does not require a GX11X0 board except for steps 1 and 4. A WaveEasy license is required in order to run the example.
The WaveEasyEasyRTExampleForm.vbproj in the WaveEasy folder contains the complete sources for this example. The following is the code used for handling the example form:
Imports WaveEasyRtLib
Imports System.Math
Public Class WaveEasyRtExampleForm
Dim m_nSlot As Int16
Dim m_nHandle As Int16
Dim m_sWaveFileName1 As String = "WaveEasyRTExampleSinLowPass.WaveEasy"
Dim m_sWaveFileName2 As String = "WaveEasyRTExampleSinFFT.WaveEasy"
Dim m_wwave1 As New WWaveform
Dim m_wwave2 As New WWaveform
Private Sub WaveEasyViewFile(ByVal sWaveFileName As String)
Dim iProcessID As Integer = 0
Dim sWaveEasyPath As String = ""
' launch WaveEasy to view the specified file
Try
sWaveEasyPath = My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\App Paths\WaveEasy.exe", Nothing, Nothing)
iProcessID = Shell(sWaveEasyPath + " " + sWaveFileName)
Catch
End Try
If iProcessID = 0 Then
MsgBox("Unable to Launch WaveEasy: '" + sWaveEasyPath + " '" + sWaveFileName + "'")
Else
'AppActivate(iProcessID)
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub btnInitialize_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInitialize.Click
Dim nStatus As Int16
Dim s As New String(" ", 256)
m_nSlot = Val(tbSlot.Text)
GTWAVE.GtWaveInitialize(m_nSlot, m_nHandle, nStatus)
If nStatus < 0 Then
GTWAVE.GtWaveGetErrorString(nStatus, s, 256, nStatus)
MsgBox("Unable to Initialize the GX1120 on Slot " + Str(m_nSlot) + " : " + s)
End If
tssStatus.Text = "Board Initialized Successfully"
End Sub
Private Sub btnCreateWaveFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateWaveFile.Click
Dim wseg As WSegment
If IsNothing(m_wwave1) = False Then
' delete content
m_wwave1 = New WWaveform
End If
' setup the wave
m_wwave1.QuantizationBits = 12
m_wwave1.SamplingRate = 1000000
m_wwave1.Name = "MyWaveform"
m_wwave1.AmplitudeMax = 5.0
m_wwave1.AmplitudeMin = -5.0
m_wwave1.Description = "Waveform created using WaveEasyRT library"
' setup the existing first segment
wseg = m_wwave1.Segments(0)
wseg.Name = "Sin w. Multiple Freq."
wseg.PointsCount = 2000
wseg.Formula = "Sin(10 * x) + 0.8 * Sin(50 * x) + 0.5 * Sin(100 * x) + 0.2 * Sin(200 * x)"
' insert second segment
wseg = m_wwave1.InsertSegment(-1, "LowPass", 2000, "FIRBandPass(Sin(10 * x) + 0.8 * Sin(50 * x) + 0.5 * Sin(100 * x) + 0.2 * Sin(200 * x), 5, 0.0001, 0.005)")
' save the file - require waveeasy license
If m_wwave1.SaveAs(m_sWaveFileName1) = False Then
MsgBox("Unable to Save : '" + m_sWaveFileName1 + "', WSegment.SaveAs require a WaveEasy license.")
End If
End Sub
Private Sub btnViewWaveFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnViewWaveFile.Click
' create the wave if not exist
If IsNothing(m_wwave1) Then
btnCreateWaveFile_Click(sender, e)
End If
' display in WaveEasy
WaveEasyViewFile(m_sWaveFileName1)
End Sub
Private Sub btnLoadWaveFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadWaveFile.Click
Dim nStatus As Int16
Dim acChar(512) As Char
' initialize the card if not exist
If m_nHandle <= 0 Then
btnInitialize_Click(sender, e)
End If
If m_nHandle <= 0 Then
Return
End If
' create the wave if not exist
If IsNothing(m_wwave1) Then
btnCreateWaveFile_Click(sender, e)
End If
' another way of initializing the board with data - require licence
Dim lPoints As Int32 = m_wwave1.PointsCount
Dim awPoints(lPoints) As UInt16
' load points from array
m_wwave1.GetPoints(0, lPoints, awPoints)
GTWAVE.GtWaveArbWriteWaveformData(m_nHandle, 0, 0, lPoints, awPoints, nStatus)
If nStatus < 0 Then
GTWAVE.GtWaveGetErrorString(nStatus, acChar, 512, nStatus)
MsgBox("Unable to load points to the GX1120 board")
End If
' load points from a file
GTWAVE.GtWaveArbFileLoad(m_nHandle, 0, m_sWaveFileName1, 0, 0, -1, nStatus)
If nStatus < 0 Then
GTWAVE.GtWaveGetErrorString(nStatus, acChar, 512, nStatus)
MsgBox("Unable to load WaveEasy file '" + m_sWaveFileName1 + "':" + acChar)
End If
End Sub
Private Sub btnCalcFFT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalcFFT.Click
Dim wseg As WSegment
Dim dsp As New WDSP
Dim lPoints As Long
Dim l As Long
Dim dMaxAmplitude As Double
Dim vSignal As Object = Nothing
Dim vFFTReal As Object = Nothing
Dim vFFTPhase As Object = Nothing
Dim vFFTMagnitude As Object = Nothing
Dim vFFTImaginary As Object = Nothing
If IsNothing(m_wwave2) = False Then
' delete content
m_wwave2 = New WWaveform
End If
' setup the wave
m_wwave2.QuantizationBits = 12
m_wwave2.SamplingRate = 1000000
m_wwave2.Name = "FFT, Frequency Spectrum Examples"
m_wwave2.AmplitudeMax = 5.0
m_wwave2.AmplitudeMin = -5.0
m_wwave2.Description = "Calc FFT and display freq spectrum on 'Sin(10 * x) + 0.8 * Sin(50 * x) + 0.5 * Sin(100 * x) + 0.2 * Sin(200 * x)'"
' *** Segment 1: setup the first segment
wseg = m_wwave2.Segments(0)
wseg.Name = "Sin w. Multiple Freq."
wseg.PointsCount = 2000
wseg.Formula = "Sin(10 * x) + 0.8 * Sin(50 * x) + 0.5 * Sin(100 * x) + 0.2 * Sin(200 * x)"
' *** segment 2
' retrieve the first segments points data
lPoints = wseg.PointsCount
wseg.GetPoints(0, lPoints, vSignal, enumWPointsDataType.WPointsDataTypeAmplitudeDouble)
' use FFT to transform time domain signal into frequency domain spectrum
dsp.FFT(vSignal, vFFTReal, vFFTImaginary)
' convert FFT complex data into easier to understand polar coordinate (Magnitude + Phase)
dsp.FFTComplexToPolar(vFFTReal, vFFTImaginary, vFFTMagnitude, vFFTPhase)
' scale magnitude to within +-5, since we use the same axis to display the spectrum (instead of Hz)
lPoints = UBound(vFFTMagnitude) + 1
For l = 0 To lPoints - 1
If Abs(vFFTMagnitude(l)) > dMaxAmplitude Then
dMaxAmplitude = Abs(vFFTMagnitude(l))
End If
Next
For l = 0 To lPoints - 1
vFFTMagnitude(l) = vFFTMagnitude(l) * 5 / dMaxAmplitude
Next
' save Magnitude data into another segment which can be viewed using WaveEasy
' use only first half of the mag. array since the second half is a reverse image of the first
wseg = m_wwave2.InsertSegment(, "Frequency Spectrum Magnitude using FFT", lPoints / 2)
wseg.SetPoints(0, lPoints / 2, vFFTMagnitude, enumWPointsDataType.WPointsDataTypeAmplitudeDouble)
' *** segment 3
' now use the invert FFT to restore the original wave
dsp.FFTInvert(vFFTReal, vFFTImaginary, vSignal)
lPoints = UBound(vSignal) + 1
wseg = m_wwave2.InsertSegment(, "Invert FFT", lPoints)
wseg.SetPoints(0, lPoints, vSignal, enumWPointsDataType.WPointsDataTypeAmplitudeDouble)
' save the file
If m_wwave2.SaveAs(m_sWaveFileName2) = False Then
MsgBox("Unable to Save : '" + m_sWaveFileName1 + "', WSegment.SaveAs require a WaveEasy license.")
End If
' *** segment 3
' FFT band filter
dsp.FFTBandPassFilter(vSignal, vFFTMagnitude, 5, 0.005, 0.015)
lPoints = UBound(vFFTMagnitude) + 1
wseg = m_wwave2.InsertSegment(, "FFT Band Pass Filter", lPoints)
wseg.SetPoints(0, lPoints, vFFTMagnitude, enumWPointsDataType.WPointsDataTypeAmplitudeDouble)
' save the file
If m_wwave2.SaveAs(m_sWaveFileName2) = False Then
MsgBox("Unable to Save : '" + m_sWaveFileName1 + "', WSegment.SaveAs require a WaveEasy license.")
End If
End Sub
Private Sub btnViewFFT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnViewFFT.Click
' create the wave if not exist
If IsNothing(m_wwave2) Then
btnCalcFFT_Click(sender, e)
End If
' display in WaveEasy
WaveEasyViewFile(m_sWaveFileName2)
End Sub
End Class