Phương pháp biến đổi Fourier rời rạc hay còn gọi là phương pháp biến đổi Fourier nhanh (FFT-Fast Fourier Tranform) được cho bởi công thức sau:
∑−
=
= 1 − 0 N 2 k
N kn i k
n h e
H π
(A.1) Trong đó hk,Hn lần lượt là các giá trị rời rạc của hàm h trên miền thời gian và miền tần số với số mẫu là N. Phương trình (A.1) có thể thu được bằng cách xuất phát từ dạng biến đổi Fourier trên miền liên tục như sau:
(− ) ≈ (− )∆
=+∞∫ ∑=−
∞
−
1
0
2 exp 2
exp ) ( )
( N
k k n k
n
n h t if t dt h if t
f
H π π
n
k ikn N H
h exp(−2 / )=∆.
∆
= ∑ π (A.2) Với ∆ là khoảng cách giữa hai mẫu liên tiếp, tk là các thời điểm lấy mẫu, fc là tần số lấy mẫu và fn là tần số mẫu thứ n:
∆
=k. tk ;
= ∆ 2
1 fc ;
= ∆ N
fn n (A.3)
; 1 ..., 2 , 1 ,
0 −
= N
k n=−N/2,...,N/2 (A.4) Ta thấy rằng nếu ∆ càng nhỏ thì phổ tín hiệu thu được càng chính xác. Ngoài ra cũng cần phải chú ý là sau khi tìm được Hn ta phải chuyển về H( )fn thông qua (A.2) để thu được phổ đúng của tín hiệu, quá trình này được gọi là chuẩn hoá.
Tương tự ta tính được hàm biến đổi FFT ngược(IFFT):
N N kn
n n
k H e
h N 1 2 /
0
1 π
∑−
=
= (A.5) Và phương trình chuẩn hoá :
( )tk hk
h 1.
≈ ∆ (A.6)
Đồ án tốt nghiệp Đại học Phụ lục Từ phương trình (A.1) ta thấy Hn là một dãy tuần hoàn chu kỳ N (Hn+N =Hn) do đó ta tính toán Hn với n=0÷N-1 hay n=−N/2÷N/2 thì kết quả thu được là như nhau.
Vì vậy để tính toán phổ thông thường người ta tính toán Hn với n=0÷N −1 tuy nhiên phổ của tín hiệu (Hn) trong trường hợp này bị lệch đi một nửa bề rộng phổ. Do đó để thu được phổ đúng thì sau khi tính toán xong Hn ta phải dịch phổ đi một nửa bề rộng phổ (FFTShift).
Như vậy để tính toán phổ của tín hiệu theo phương pháp biến đổi Fourier rời rạc ta phải thực hiện qua ba giai đoạn. Ban đầu là ta thực hiện việc biến đổi FFT theo (A.1) sau đó dịch đi một nửa bề rộng phổ (ta sẽ thực hiện hai giai đoạn này bằng cách xây dựng hai hàm FFT và FFTShift) và cuối cùng là thực hiện chuẩn hoá như (A.2) để thu được phổ chính xác.
PHỤ LỤC B. Chương trình mô phỏng
Chương trình mô phỏng được viết băng ngôn ngữ Visual Basic, các thông số nhập vào để tính toán như hình (B.1). Sau đó có thể chọn vẽ dạng của sóng bơm, sóng Stoke trên miền thời gian và miền tần số, vẽ phổ khuyếch đại Raman, đặc tuyến công suất.
Hình B.1- Các tham số cần nhập trong chương trình mô phỏng.
‘******************************************************************
‘Chương trình mô phỏng được xây dưng bằng ngôn ngữ lập trình Visual Basic kết
‘hợp với công cụ Matlab.
‘Code by :Mai nguyên Dũng Private FontFile As String
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA"
(ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function AddFontResource Lib "gdi32" Alias "AddFontResourceA"
(ByVal lpFileName As String) As Long
Private Declare Function RemoveFontResource Lib "gdi32" Alias
"RemoveFontResourceA" (ByVal lpFileName As String) As Long Dim showmatLab As Boolean
Dim useMatlab As Boolean Private Type complex real As Double img As Double End Type Dim obj1
Dim j As Integer Dim dt As Double Dim tm As Double
Const n2 = 2.67 * 10 ^ -20
Const pi = 3.14159265358979 ' 3.1416 Const c = 3 * 10 ^ 8
Const h = 6.625 * 10 ^ -34 Const t11 = 12.2 * 10 ^ -15 Const t22 = 32 * 10 ^ -15
Dim Pp As Double, T0 As Double, z As Double
Dim lamdap As Double, lamdas As Double ‘Bước sóng bơm và sóng Stoke Dim Aeff As Double
Dim chisomax As Integer Dim SRSPump As Boolean
Dim Peakp As Double, Peaks As Double, Pth As Double, Ps As Double Dim gR As Double, gs As Double, gp As Double
Dim gamap As Double, gamas As Double, delta As Double, d As Double Dim aso(1024) As Double, phis(1024) As Double, asz(1024) As complex Dim apo(1024) As Double, phip(1024) As Double, apz(1024) As complex Dim aszT(1024) As Double, apzT(1024) As Double
Dim max_x As Double, max_y As Double
Dim label_xs As String, label_ys As String 'Chú giải trên các trục-Stoke Dim label_xp As String, label_yp As String 'Chú giải trên các trục- Pump Dim label_x As String, label_y As String
Dim ti_le_xs As Double, ti_le_ys As Double 'Ti le do thi xung Stoke Dim ti_le_xp As Double, ti_le_yp As Double
Dim fre(1024) As Double 'Các mẫu tần số Dim t(1024) As Double
Dim tp1(1024) As Double, ts1(1024) As Double
Dim newDraw As Boolean 'Cho biet ve tren mot do thi moi hay khong Dim Lw As Double 'Chiều dài Walk-Off
‘********************Hàm RamanGain*******************
Private Function RamanGain(ByVal lamdap As Double, ByVal df As Double)
‘Hàm tính hệ số khuyếch đại Raman với bước sóng bơm lamdap Dim k As Double, w As Double, tu As Double, mau As Double k = 1 / t11 ^ 2 + 1 / t22 ^ 2
w = 2 * pi * Abs(df) tu = 2 * w / t22
mau = (k - w ^ 2) ^ 2 + (2 * w / t22) ^ 2
RamanGain = 0.18 * 16 * pi * n2 / (3 * lamdap) * k * tu / mau End Function
‘********************Hàm beta***********************
Private Function beta(lamda As Double) As Double Dim b(3) As Double, n As Double, wdn As Double b(1) = 0.6961663
b(2) = 0.4079426 b(3) = 0.8974794 Dim l(3) As Double l(1) = 0.0684943 l(2) = 0.1162414 l(3) = 9.896161
Dim O(3) As Double, i As Integer, Om As Double For i = 1 To 3
O(i) = 2 * pi * c / l(i)
Next i
Om = 2 * pi * c / (lamda * 10 ^ 6) n = 1
wdn = 0 For i = 1 To 3
n = n + (b(i) * O(i) ^ 2) / (O(i) ^ 2 - Om ^ 2)
wdn = wdn + (b(i) * O(i) ^ 2 * Om ^ 2) / (O(i) ^ 2 - Om ^ 2) ^ 2 Next i
n = n ^ 0.5
beta = (n + wdn / n) / c 'beta = n
End Function
‘********************Hàm Poeff***********************
Private Function Poeff(ByVal Aeff As Double, ByVal Po As Double, ByVal z As Double, ByVal lamdap As Double)
‘Hàm tính công suất sóng Stoke ban đầu
Dim k As Double, w As Double, tu As Double, mau As Double k = 1 / t11 ^ 2 + 1 / t22 ^ 2
Dim df As Double
df = Val(txtDodichtan) * 10 ^ 12 w = 2 * pi * Abs(df)
tu = 2 * w / t22
mau = (k - w ^ 2) ^ 2 + (2 * w / t22) ^ 2 lamdas = c / (c / lamdap - df)
Dim dtu As Double, dmau As Double, d2mau As Double, d2g As Double, g2p As Double
dmau = 4 * w ^ 3 + 8 * w / (t22 ^ 2) - 4 * k * w dtu = 2 / t22
d2mau = 12 * w ^ 2 + 8 / t22 ^ 2 - 4 * k
d2g = Abs(d2tu / mau - d2mau * tu / mau ^ 2 - 2 * dtu * dmau / mau ^ 2 + 2 * (dmau) ^ 2 * tu / mau ^ 3)
g2p = 0.155 * 16 * pi * n2 / (3 * lamdap) * k * d2g Poeff = h * df * (2 * pi * Aeff / (g2p * Po * z)) ^ 0.5 End Function
‘********************Hàm ve_xung_vao()***********************
Private Function ve_xung_vao()
‘Ve xung vao
Dim Tmv As Double, i As Integer, dtv As Double
Dim tv(1024) As Double, yv(1024) As Double, tv1(1024) As Double Tmv = 20 * T0
dtv = Tmv / 1023 For i = 1 To 1024
tv(i) = (-Tmv / 2 + (i - 1) * dtv) tv1(i) = tv(i) / T0
yv(i) = sqrt(Ps) * Exp(-(tv1(i) ^ 2 / 2)) Next i
ti_le_x = tinh_ti_le(MaxT(tv)) ti_le_y = tinh_ti_le(MaxT(yv)) For i = 1 To 1024
tv(i) = tv(i) * 10 ^ ti_le_x yv(i) = yv(i) * 10 ^ ti_le_y Next i
label_x = "Time ,s x 10" & showScript(-ti_le_x) label_y = "V x 10" & showScript(-ti_le_y) Ve_truc_toa_do MaxT(tv) + 1, MaxT(yv) + 1 For i = 1 To 1024
Next i
Plot tv, yv, 1, 1
Form2.Labelx.Caption = label_x Form2.Labely.Caption = label_y End Function
‘********************Hàm Tinh_toan_thong_so()***********************
Private Function Tinh_toan_thong_so()
‘Tính toán các giá trị người sử dụng nhập vào Form2.Labelx.Caption = ""
Form2.Labely.Caption = ""
Pp = Val(txtPp.Text)
T0 = Val(txtT0) * 10 ^ -12
lamdap = (10 ^ -6) * Val(txtLamdaP) z = Val(Text1.Text)
Aeff = Val(txtAeff.Text) * (10 ^ -12)
gR = RamanGain(lamdap, Val(txtDodichtan) * 10 ^ 12) lamdas = c / (c / lamdap - Val(txtDodichtan) * 10 ^ 12) gamap = gama(Aeff, lamdap)
gamas = gama(Aeff, lamdas)
Ps = Poeff(Aeff, Pp, z, lamdap) If (z <> 0) Then
Pth = 16 * Aeff / (z * gR) End If
gs = gR / Aeff
gp = (lamdap / lamdas) * gs d = beta(lamdap) - beta(lamdas) Lw = T0 / d
delta = z * d / T0 End Function
‘********************Hàm SRS()***********************
Private Function SRS(fc As Double, pSample As Integer, ntype As Integer, nNo As Integer)
Dim max_x As Double, max_y As Double, i As Integer If (Pp >= Pth) And (z <> 0) Then
If Check1.Value = 1 Then
DrawStoke 1024, OptMienthoigian.Value
If OptMienthoigian.Value = True Then 'Neu la mien thoi gian For i = 1 To 1024
ts1(i) = ts1(i) * 10 ^ ti_le_xs aszT(i) = aszT(i) * 10 ^ ti_le_ys Next i
max_x = MaxT(ts1) + 1 max_y = MaxT(aszT) + 1 label_x = label_xs
label_y = label_ys
Ve_truc_toa_do max_x, max_y
Plot ts1, aszT, MaxT(ts1) + 1, MaxT(aszT) + 1, label_xs, label_ys Else 'Neu la mien tan so
For i = 1 To 1024
fre(i) = fre(i) * 10 ^ ti_le_xs aszT(i) = aszT(i) * 10 ^ ti_le_ys Next i
max_x = MaxT(fre) + 1 max_y = MaxT(aszT) + 1 label_x = label_xs
label_y = label_ys
Ve_truc_toa_do max_x, max_y
Plot fre, aszT, MaxT(fre) + 1, MaxT(aszT) + 1, label_xs, label_ys End If
Else 'Nếu cần vẽ xung Pump
DrawPump 1024, OptMienthoigian.Value
If OptMienthoigian.Value = True Then 'Neu ve o mien thoi gian For i = 1 To 1024
tp1(i) = tp1(i) * 10 ^ ti_le_xp apzT(i) = apzT(i) * 10 ^ ti_le_yp Next i
max_x = MaxT(tp1) + 1 max_y = MaxT(apzT) + 1 label_x = label_xp
label_y = label_yp
Ve_truc_toa_do max_x, max_y
Plot tp1, apzT, (tm / 2) * 10 ^ ti_le_x, Round(MaxT(apzT)) * 10 ^ ti_le_y + 1, label_xp, label_y
Else 'Neu la mien tan so For i = 1 To 1024
fre(i) = fre(i) * 10 ^ ti_le_xp apzT(i) = apzT(i) * 10 ^ ti_le_yp
Next i
max_x = 2 * fre(1024) max_y = MaxT(apzT) + 1 label_x = label_xp
label_y = label_yp
Ve_truc_toa_do max_x, max_y
Plot fre, apzT, 2 * fre(pSample), MaxT(apzT) + 1, label_x, label_y End If
End If End Function
‘********************Hàm Ve_hai_xung()***********************
Private Function Ve_hai_xung() ‘Vẽ hai xung trên miền thời gian Form2.Labelx.Caption = ""
Form2.Labely.Caption = ""
Form2.LblBuocsong.Caption = ""
Form2.lblKhoangcach.Caption = ""
newDraw = True
Dim tilex As Double, tiley As Double Dim i As Integer
Tinh_toan_thong_so
Dim max_x As Double, max_y As Double If (Pp >= Pth) And (z <> 0) Then
Ps = Poeff(Aeff, Pp, z, lamdap) DrawPump 1024, True
DrawStoke 1024, True If ti_le_xp < ti_le_xs Then tilex = ti_le_xp
Else
tilex = ti_le_xs End If
If ti_le_yp < ti_le_ys Then tiley = ti_le_yp
Else
tiley = ti_le_ys End If
For i = 1 To 1024
ts1(i) = ts1(i) * 10 ^ tilex aszT(i) = aszT(i) * 10 ^ tiley tp1(i) = tp1(i) * 10 ^ tilex apzT(i) = apzT(i) * 10 ^ tiley Next i
If MaxT(ts1) > MaxT(tp1) Then max_x = MaxT(ts1)
Else
max_x = MaxT(tp1) End If
If MaxT(aszT) > MaxT(apzT) Then max_y = MaxT(aszT)
Else
max_y = MaxT(apzT) End If
max_y = Round(max_y) + 1 label_x = label_xp
label_y = label_yp
Ve_truc_toa_do max_x, max_y Form2.Picture1.ForeColor = vbRed
Plot tp1, apzT, max_x, max_y, label_xp, label_yp Form2.Picture1.ForeColor = vbBlack
Plot ts1, aszT, max_x, max_y, label_xp, label_yp Form2.Labelx.Caption = label_x
Form2.Labely.Caption = label_y
Form2.lblKhoangcach.Caption = "z=" & z & "m"
Form2.lblChugiai.Caption = "H×nh 1 Sãng b¬m vµ sãng Stoke ë cù ly " & z
& " m"
End If
End Function
‘********************Hàm DrawPump()***********************
Private Function DrawPump(ByVal pSample As Integer, Optional timedomain As Boolean = True, Optional ByVal fc As Integer = 0)
‘Tính toán sóng bơm Dim i As Integer
Dim max1 As Double
Dim temp1 As Double, temp2 As Double If timedomain = True Then
tm = 20 * T0 Else
tm = 100 * T0 End If
dt = tm / (pSample - 1) For i = 1 To pSample
tp1(i) = (-tm / 2 + (i - 1) * dt) t(i) = tp1(i) / T0
apo(i) = (Pp ^ 0.5) * Exp(-0.5 * t(i) ^ 2) If (Pp < Pth) Or (z = 0) Then
phip(i) = -gamap * z * apo(i) ^ 2 apz(i).real = apo(i) * Cos(phip(i)) apz(i).img = apo(i) * Sin(phip(i))
imgProcess.Width = picProcess.Width / pSample * i Else
If useMatlab = False Then
phip(i) = Ps * sqrt(pi) / (2 * delta) * (erf(t(i) + delta) - erf(t(i))) Else
phip(i) = tinhPhip(delta, t(i)) End If
temp1 = Cos(gamap * z * (apo(i) ^ 2 + 2 * phip(i))) temp2 = Sin(gamap * z * (apo(i) ^ 2 + 2 * phip(i))) apz(i).real = apo(i) * Exp(-gp * z / 2 * phip(i)) * temp1 apz(i).img = -apo(i) * Exp(-gp * z / 2 * phip(i)) * temp2
imgProcess.Width = picProcess.Width / pSample * i End If
Next i
Peakp = TimMax(apz) max1 = MaxT(tp1)
ti_le_xp = tinh_ti_le(max1) ti_le_yp = tinh_ti_le(Peakp)
label_xp = "10" & showScript((-tinh_ti_le(max1))) label_yp = "10" & showScript((-tinh_ti_le(Peakp))) If timedomain = True Then ‘Vẽ trên miền thời gian For i = 1 To pSample
apzT(i) = Modul(apz(i)) '* (10 ^ ti_le_yp) tp1(i) = tp1(i) '* (10 ^ ti_le_xp)
Next i
If ti_le_xp <> 0 Then
label_xp = "Time,s x" & label_xp Else
label_xp = "Time,s"
End If
If ti_le_yp <> 0 Then
label_yp = "Amplifier V x" & label_yp Else
label_yp = "Amplifier V"
End If
Else 'Vẽ trên miền tần số
Dim apzF() As complex, aps() As complex ReDim apzF(pSample) As complex
ReDim aps(pSample) As complex
FFT apz, apzF ‘Biến đổi sang miền tần số
FFTShift apzF, aps ‘Dịch đi một nửa bề rộng phổ For i = 1 To pSample
apzT(i) = (dt * Modul(aps(i))) ^ 2 Next i
max_y = MaxT(apzT) ti_le_yp = tinh_ti_le(max_y) For i = 1 To pSample
apzT(i) = (dt * Modul(aps(i))) ^ 2 '* 10 ^ ti_le_yp Next i
For i = 1 To pSample
fre(i) = (fc + (-(pSample - 1) / 2 + i - 1) / (pSample * dt)) '* 10 ^ -11 Next i
max_x = MaxT(fre)
ti_le_xp = tinh_ti_le(max_x) For i = 1 To pSample
fre(i) = fre(i) '* 10 ^ ti_le_xp Next i
If ti_le_xp <> 0 Then
label_xp = "Frequency(f-f0),Hz x" & "10" & showScript(-ti_le_xp) Else
label_xp = "Frequency(f-f0),Hz"
End If
If ti_le_yp <> 0 Then
label_yp = "Power, W x" & "10" & showScript((-ti_le_yp)) Else
label_yp = "Power, W"
End If End If End Function
‘********************Hàm DrawStoke()***********************
Private Function DrawStoke(ByVal pSample As Integer, Optional ByVal timedomain As Boolean = True)
‘Tính toán với sóng Stoke Dim i As Integer
Dim temp1 As Double, temp2 As Double If timedomain = True Then
tm = 20 * T0
Else
tm = 100 * T0 End If
dt = tm / (pSample - 1) For i = 1 To pSample
ts1(i) = (-tm / 2 + (i - 1) * dt) - z * d t(i) = ts1(i) / T0
aso(i) = sqrt(Ps) * Exp(-0.5 * (t(i) + delta) ^ 2) If useMatlab = False Then
phis(i) = Pp * sqrt(pi) / (2 * delta) * (erf(t(i) + delta) - erf(t(i))) Else
phis(i) = TinhPhis(delta, t(i)) End If
temp1 = Cos(gamas * z * (aso(i) ^ 2 + 2 * phis(i))) temp2 = Sin(gamas * z * (aso(i) ^ 2 + 2 * phis(i))) asz(i).real = aso(i) * Exp(gs * z / 2 * phis(i)) * temp1 asz(i).img = -aso(i) * Exp(gs * z / 2 * phis(i)) * temp2
imgProcess.Width = (14 / 8) * picProcess.Width / pSample * i / 2 Next i
Peaks = TimMax(asz) If timedomain = True Then For i = 1 To pSample aszT(i) = Modul(asz(i))
imgProcess.Width = 7 / 8 * (picProcess.Width) + picProcess.Width / pSample * i / 16
Next i
max_x = MaxT(ts1) max_y = MaxT(aszT)
ti_le_xs = tinh_ti_le(max_x) ti_le_ys = tinh_ti_le(max_y) If ti_le_xs <> 0 Then
label_xs = "Time, s x 10" & showScript(-ti_le_xs) Else
label_xs = "Time, s"
End If
If ti_le_ys <> 0 Then
label_ys = "Amplifier, V x 10" & showScript(-ti_le_ys) Else
label_ys = "Amplifier, V"
End If
For i = 1 To pSample
ts1(i) = ts1(i) '* 10 ^ ti_le_xs aszT(i) = aszT(i) '* 10 ^ ti_le_ys
imgProcess.Width = 15 / 16 * (picProcess.Width) + picProcess.Width / pSample * i / 16
Next i
Else 'Ve trong mien tan so
Dim asf() As complex, ass() As complex ReDim asf(pSample) As complex
ReDim ass(pSample) As complex FFT asz, asf
FFTShift asf, ass For i = 1 To pSample
aszT(i) = ((dt * Modul(ass(i))) ^ 2)
imgProcess.Width = 7 / 8 * (picProcess.Width) + picProcess.Width / pSample * i / 16
Next i
For i = 1 To pSample
fre(i) = (fc + (-(pSample - 1) / 2 + i - 1) / (pSample * dt)) Next i
max_y = MaxT(aszT) max_x = MaxT(fre)
ti_le_xs = tinh_ti_le(max_x) ti_le_ys = tinh_ti_le(max_y) If ti_le_xs <> 0 Then
label_xs = "Frequency(f-f0),Hz x" & "10" & showScript(-ti_le_xs) Else
label_xs = "Frequency(f-f0),Hz"
End If
If ti_le_ys <> 0 Then
label_ys = "Power, W x" & "10" & showScript(-ti_le_ys) Else
label_ys = "Power, W"
End If
For i = 1 To pSample
fre(i) = fre(i) '* 10 ^ ti_le_xs aszT(i) = aszT(i) ' * 10 ^ ti_le_ys
imgProcess.Width = 15 / 16 * (picProcess.Width) + picProcess.Width / pSample * i / 16
Next i End If
Form2.lblKhoangcach.Caption = "z=" & z & " m"
End Function
‘********************Hàm tinhPhip()***********************
Private Function tinhPhip(ByVal delta As Double, ByVal tg As Double) As Double Dim str As String
str = Ps & " * sqrt(pi) / (2 * " & delta & ") * (erf(" & tg & "+ " & delta & ") - erf(" & tg & "))"
str = Mid(obj1.execute(str), 7) tinhPhip = Val(str)
End Function
‘********************Hàm TinhPhis()***********************
Private Function TinhPhis(ByVal delt As Double, ByVal tg As Double) As Double ‘Kết hợp sử dụng Matlab
Dim str As String
str = Pp & "*sqrt(pi) / (2 *" & delt & ") * (erf(" & tg & "+" & delta & ") - erf(" &
tg & "))"
str = Mid(obj1.execute(str), 7) TinhPhis = Val(str)
End Function
‘********************Hàm MaxT()***********************
Private Function MaxT(x() As Double) As Double ‘Tìm giá trị lớn nhất của một dãy số thực
Dim i As Integer, num As Integer, temp As Double num = UBound(x)
For i = 1 To num
If Abs(x(i)) > temp Then temp = Abs(x(i)) End If
Next i
MaxT = temp End Function
‘********************Hàm gama()***********************
Private Function gama(ByVal Aeff As Double, ByVal lamda As Double) As Double gama = n2 * 2 * pi / (lamda * Aeff)
End Function
Private Function expC(x As complex) As complex 'Ham mu so phuc
Dim temp As complex
temp.real = Exp(x.real) * Cos(x.img) temp.img = Exp(x.real) * Sin(x.img) expC = temp
End Function
‘********************Hàm TimMax()***********************
Private Function TimMax(x() As complex) As Double ‘Tìm số phức có modul lớn nhất trong một dãy số phức Dim number As Integer
number = UBound(x)
Dim temp As Double, temp2 As Double temp = Modul(x(1))
Dim i As Integer For i = 1 To number
If temp < Modul(x(i)) Then chisomax = i
temp = Modul(x(i)) End If
Next i
TimMax = temp End Function
‘********************Hàm FFT()***********************
Private Function FFT(MatrixIn() As complex, ByRef MatrixOut() As complex) ‘Hàm biến đổi Fourier nhanh
Dim numSamples As Integer Dim k As Integer, n As Integer Dim A As Double, b As Double
Dim reo() As Double, ImO() As Double, ReI() As Double, ImI() As Double numSamples = UBound(MatrixIn) '+ 1
ReDim MatrixOut(numSamples) As complex ReDim reo(numSamples) As Double
ReDim ImO(numSamples) As Double ReDim ReI(numSamples) As Double ReDim ImI(numSamples) As Double For n = 1 To numSamples
ReI(n) = MatrixIn(n).real ImI(n) = MatrixIn(n).img Next n
For n = 1 To numSamples reo(n) = 0
ImO(n) = 0
For k = 2 To numSamples / 2
A = Cos(2 * pi * (k - 1) * (n - 1) / numSamples) b = Sin(2 * pi * (k - 1) * (n - 1) / numSamples)
reo(n) = reo(n) + (ReI(k) + ReI(numSamples + 2 - k)) * A reo(n) = reo(n) + (-ImI(numSamples + 2 - k) + ImI(k)) * b ImO(n) = ImO(n) + (ImI(k) + ImI(numSamples + 2 - k)) * A ImO(n) = ImO(n) + (-ReI(k) + ReI(numSamples + 2 - k)) * b Next k
reo(n) = reo(n) + ReI(1) + ReI(numSamples / 2 + 1) * Cos(pi * (n - 1)) ImO(n) = ImO(n) + ImI(1) + ImI(numSamples / 2 + 1) * Cos(pi * (n - 1)) MatrixOut(n).real = reo(n)
MatrixOut(n).img = ImO(n) Next n
End Function
‘********************Hàm FFTShift()***********************
Private Function FFTShift(x() As complex, y() As complex) ‘Hàm dịch phổ-Dịch đi một nửa bề rộng phổ
Dim number As Integer number = UBound(x)
ReDim y(number) As complex Dim i As Integer
For i = 1 To number If i <= number / 2 Then
y(i).real = x(i + number / 2).real y(i).img = x(i + number / 2).img Else
y(i).real = x(i - number / 2).real y(i).img = x(i - number / 2).img End If
Next i End Function
‘********************Hàm Ve_truc_toa_do()***********************
Private Function Ve_truc_toa_do(ByVal xmax As Double, ByVal ymax As Double) ‘Hàm vẽ trục toạ độ
Form2.Labelx.Caption = ""
Form2.Labely.Caption = ""
Form2.Show Form2.Picture1.Cls
Form2.Picture1.ForeColor = vbBlack
Form2.Picture1.Scale (-xmax, ymax)-(xmax, -ymax / 2) Form2.Picture1.Line (-xmax, 0)-(xmax, 0)
Form2.Picture1.Line (0, 0)-(0, ymax)
Form2.Picture1.Line (-xmax, xmax / 20)-(-xmax, -xmax / 20) PrintPic -xmax, 0, -Round(xmax, 1), xmax, ymax
PrintPic 0, ymax, Round(ymax, 1)
Form2.Picture1.Line (-xmax / 40, ymax)-(xmax / 40, ymax) For i = -4 To 5
PrintPic i * xmax / 5, 0, Round(i * xmax / 5, 1), xmax, ymax ' Round(xmax / 5
Next i
For i = 1 To 4
PrintPic 0, i * ymax / 5, Round(i * ymax / 5, 1), xmax, ymax, False ' Round(xmax / 5)
Next i End Function
‘********************Hàm Plot()***********************
Private Function Plot(x() As Double, y() As Double, ByVal xmax As Double, ymax As Double, Optional ByVal label_x As String, Optional ByVal label_y As String)
‘Vẽ các điểm (x,y) của hai dãy số đầu vào lên trục toạ độ Dim num As Integer, i As Integer
num = UBound(x) For i = 1 To num - 1
If Round(y(i), 2) <> 0 Then If i Mod 5 = 0 Then
pause 0.006 End If
Form2.Picture1.Line (x(i), y(i))-(x(i + 1), y(i + 1)) End If
Next i
End Function
‘********************Hàm PrintPic()***********************
Private Function PrintPic(ByVal x As Double, ByVal y As Double, ByVal str As String, Optional ByVal xmax As Double, Optional ByVal ymax As Double, Optional ByVal hoz As Boolean = True)
‘Hàm vẽ các giá trị vạch đơn vị trên trục toạ độ If hoz = True Then ‘Trục ngang