Below is the code for generation of Fourier Series
of any Function.
The program uses N point DFT, of sampled version of analog signal function entered in the text box.
This is code for Fourier.vb
1 2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72 73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144 145 146
147
148
149
150
151
152
153
154
155
156
| Imports System.Math
Imports MSScriptControl
Public Class Fourier
Private Sub Fourier_Load(ByVal sender As System.Object,_ ByVal e As System.EventArgs) Handles MyBase.Load
CurvePic.BackColor = Color.White
End Sub
Private Sub DrawBtn_Click(ByVal sender As System.Object,_ ByVal e As System.EventArgs) Handles DrawBtn.Click
Dim CurveScript As New MSScriptControl.ScriptControl
CurveScript.Language = "VBscript"
Dim CurveGraph As Graphics
Dim x, y As Double
Dim ox, oy As Integer
Dim posx, posy As Long
Dim curve(,) As Double
Dim xacc As Double
Dim scl As Double
Dim count As Long = 0
ox = CurvePic.Width / 2
oy = CurvePic.Height / 2
xacc = 0.01
scl = 10
CurveGraph = Graphics.FromHwnd(CurvePic().Handle)
CurveGraph.Clear(Color.White)
ReDim curve(CurvePic.Width / (xacc * scl), 2)
For x = -CurvePic.Width / (2 * scl) To _ CurvePic.Width / (2 * scl) Step xacc
CurveScript.ExecuteStatement("x=" & x)
CurveScript.ExecuteStatement("pi=" & PI)
CurveScript.ExecuteStatement("u=" & u(x))
Try
y = CurveScript.Eval(FunctionTxt.Text)
'Me.Text = CStr(y)
Catch e1 As OverflowException
y = 0
Catch e2 As Exception
MsgBox(e2.ToString)
Exit For
End Try
curve(count, 0) = x
curve(count, 1) = y
posx = ox + curve(count, 0) * scl
posy = oy - curve(count, 1) * scl
Try
CurveGraph.DrawRectangle(pen:=Pens.Blue, _ height:=1, width:=1, x:=posx, y:=posy)
Catch e1 As OverflowException
posx = CurvePic.Width
posy = CurvePic.Height
End Try
count += 1
Next
Dim N As Long
Dim k As Double
Dim nf As Double
Dim Reyf As Double
Dim imyf As Double
Dim ampyf As Double
Dim angyf As Double
Dim fsclx, fscly As Double
Dim famp As Long
Dim fang As Long
N = count
count = 0
fsclx = CurvePic.Width / N
fscly = CurvePic.Height / (2 * N)
Dim dReyf As Double = 0
Dim dImyf As Double = 0
Dim kr As Double
Dim posref, posimf As Long
For k = -(N - 1) / 2 To (N - 1) / 2 Step 1
dReyf = 0
dImyf = 0
For nf = 0 To N - 1 Step 1
Reyf = dReyf + curve(nf, 1) * Cos(2 * PI * nf * k / N)
imyf = dImyf - curve(nf, 1) * Sin(2 * PI * nf * k / N)
dReyf = Reyf
dImyf = imyf
Next
ampyf = (Reyf ^ 2 + imyf ^ 2) ^ 0.5
angyf = Atan(imyf / Reyf)
Try
famp = oy - ampyf * fscly
fang = oy - angyf * (CurvePic.Height / 6)
Catch e1 As Exception
MsgBox(e1.ToString)
Exit For
End Try
kr = N / 2 + k
posref = ox + Reyf * CurvePic.Width / N
posimf = oy - imyf * CurvePic.Height / N
Try
CurveGraph.DrawRectangle(pen:=Pens.Red,_ height:=1, width:=1, x:=CInt(kr * fsclx), y:=famp)
CurveGraph.DrawRectangle(pen:=Pens.DarkKhaki,_ height:=1, width:=1, x:=CInt(kr * fsclx), y:=fang)
CurveGraph.DrawRectangle(pen:=Pens.Black, _ width:=1, height:=1, x:=posref, y:=posimf)
'Me.Text = CStr(N) + "," + CStr(famp) + "," +_ CStr(fang) + "," + CStr(k)
Catch e1 As OverflowException
famp = 0
Catch e2 As Exception
MsgBox(e2.ToString)
Exit For
End Try
Next
End Sub
Public Function u(ByVal t As Double) As Double
If t < 0 Then
Return 0
Else
Return 1
End If
End Function
End Class |
now the code for Fourier.Designer.vb
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Fourier
Inherits System.Windows.Forms.Form
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.CurvePic = New System.Windows.Forms.PictureBox()
Me.DrawBtn = New System.Windows.Forms.Button()
Me.FunctionTxt = New System.Windows.Forms.TextBox()
Me.StatusTxt = New System.Windows.Forms.TextBox()
CType(Me.CurvePic, System.ComponentModel.ISupportInitialize)_ .BeginInit()
Me.SuspendLayout()
'
'CurvePic
'
Me.CurvePic.Location = New System.Drawing.Point(12, 12)
Me.CurvePic.Name = "CurvePic"
Me.CurvePic.Size = New System.Drawing.Size(768, 413)
Me.CurvePic.TabIndex = 0
Me.CurvePic.TabStop = False
'
'DrawBtn
'
Me.DrawBtn.Location = New System.Drawing.Point(12, 431)
Me.DrawBtn.Name = "DrawBtn"
Me.DrawBtn.Size = New System.Drawing.Size(75, 23)
Me.DrawBtn.TabIndex = 1
Me.DrawBtn.Text = "Transform"
Me.DrawBtn.UseVisualStyleBackColor = True
'
'FunctionTxt
'
Me.FunctionTxt.Location = New System.Drawing.Point(93, 434)
Me.FunctionTxt.Name = "FunctionTxt"
Me.FunctionTxt.Size = New System.Drawing.Size(215, 20)
Me.FunctionTxt.TabIndex = 2
Me.FunctionTxt.Text = "10*sin(x)"
'
'StatusTxt
'
Me.StatusTxt.Location = New System.Drawing.Point(314, 433)
Me.StatusTxt.Name = "StatusTxt"
Me.StatusTxt.Size = New System.Drawing.Size(221, 20)
Me.StatusTxt.TabIndex = 3
'
'Fourier
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(792, 466)
Me.Controls.Add(Me.StatusTxt)
Me.Controls.Add(Me.FunctionTxt)
Me.Controls.Add(Me.DrawBtn)
Me.Controls.Add(Me.CurvePic)
Me.Name = "Fourier"
Me.Text = "Forier Transform"
CType(Me.CurvePic, System.ComponentModel.ISupportInitialize)_ .EndInit()
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Friend WithEvents CurvePic As System.Windows.Forms.PictureBox
Friend WithEvents DrawBtn As System.Windows.Forms.Button
Friend WithEvents FunctionTxt As System.Windows.Forms.TextBox
Friend WithEvents StatusTxt As System.Windows.Forms.TextBox
End Class |
Note: if code doesn't work try to remove underscores at the end of various line and then try.
Download Demo executable here.
No comments:
Post a Comment
If the contents is insufficient or if there any error, please write here....
Suggestion are welcome: