Question:
How do I create a Sine Wave in Visual Basic 10?
Jake
2011-04-13 09:38:14 UTC
I need to create a sine wave on a Panel. Using the inputs from track bars of: Amplitude, Frequency, and Phase Change. I have a feeling that the formula should look something like this:

amp * Math.Sin(freq * S + phase)

where amp is the amplitude, freq is the frequency, S is the amount of time passed based on a timer and phase is the phase change.

I believe that should work and all i need to do is get this to draw on the panel.

any help would be greatly appreciated.
Three answers:
MarkG
2011-04-14 10:45:22 UTC
This question has many parts to it. Its like asking how to build a house and expecting detailied instructions on the building the foundation , the framing and the roof.





It appears that you have a handle on the math part and understand what a sine wave is. So I assume that you can do the math to create an array of X,Y data points of your sine wave as well as the concept of scaling data so that it fits on a graph.



What you need to know is how to create a graphic and display it on a form. Then using those concepts you will take your sinewave data and scale it so that it fits withing the dimensions of your BMP image



Below is some code i had kicking around which creates a BMP (bit map) graphic and displays it in an image box control. you can place the image control within a panel and size the control to match the panel size.



This code will create a new BMP and draw a grid of horizontal and vertical lines. Later you will write code using similar techniques to plot a series of small lines on the BMP that will make your sine wave visible on the graph.



Dim gr As Graphics

Dim sizeX, sizeY, idx, row, rows, col, cols, space As Integer

Dim gryPen As New Pen(Color.Gray, 1)



row = 0

col = 0



sizeX = m_gridBM.Width

sizeY = m_gridBM.Height

rows = 10

space = sizeY / rows

cols = sizeX / space



gr = Graphics.FromImage(m_gridBM)

gr.Clear(Color.Black)



'draw horizontal gradicule lines

gr.DrawLine(gryPen, 0, row, sizeX, row)

For idx = 1 To rows

row = idx * space

If idx = (rows / 2) Then

'make the middle line thicker

gryPen.Width = 3

Else

gryPen.Width = 1

End If

gr.DrawLine(gryPen, 0, row, sizeX, row)



Next

'draw verticle gradicule lines

gr.DrawLine(gryPen, col, 0, col, sizeY)

For idx = 1 To cols

col = idx * space

If idx = (cols / 2) Then

'make the middle line thicker

gryPen.Width = 3

Else

gryPen.Width = 1

End If

gr.DrawLine(gryPen, col, 0, col, sizeY)

Next



With Me.PictureBox1

.Width = sizeX

.Height = sizeY



.Image = m_gridBM

End With

m_gridBM.Save("C:/grid.bmp")





Once you understand how to make a graphic and display it in VB you can move on and learn how to compute a bunch of x,y data points for a Sine wave and scale these points so that they fit onto your BMP grid. Drawing a series of small lines from (X,Y) point to (X,Y) point using a different color and line thickness will display the Sinewave.
ecker
2016-11-08 00:21:29 UTC
"extra harmonic frequencies" ? that's no longer the way it extremely is finished, as how precisely could you upload those harmonics? the terrific way is to generate the sq. wave itself from scratch, consisting of by using an astable circuit. The sawtooth waveform is generated from the sq. wave with an integrator. yet another technique that facilitates any waveform to be generated is to keep a catalogue of digital values in a memory and cycle by using them, sending the digital numbers to a D-A converter, which generates the waveform.
2011-04-13 09:38:55 UTC
Yes.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...