FFT,快速傅里也变换

时间:2012-03-23 09:42:26
【文件属性】:

文件名称:FFT,快速傅里也变换

文件大小:14KB

文件格式:DOCX

更新时间:2012-03-23 09:42:26

FFT

fft程序, Dim REX(512) 'REX[ ] holds the real part of the frequency domain Dim IMX(512) 'IMX[ ] holds the imaginary part of the frequency domain Const N = 512 Public Sub fft() pi = 3.14159265 'Set constants 1000 'THE FAST FOURIER TRANSFORM 'copyright © 1997-1999 by California Technical Publishing 'published with permission from Steven W Smith, www.dspguide.com 'GUI by logix4u , www.logix4u.net 'modified by logix4u, www.logix4.net 1010 'Upon entry, N% contains the number of points in the DFT, REX[ ] and 1020 'IMX[ ] contain the real and imaginary parts of the input. Upon return, 1030 'REX[ ] and IMX[ ] contain the DFT output. All signals run from 0 to N%-1. 1060 NM1% = N% - 1 1070 ND2% = N% / 2 1080 M% = CInt(Log(N%) / Log(2)) 1090 J% = ND2% 1100 ' 1110 For i% = 1 To N% - 2 'Bit reversal sorting 1120 If i% >= J% Then GoTo 1190 1130 TR = REX(J%) 1140 TI = IMX(J%) 1150 REX(J%) = REX(i%) 1160 IMX(J%) = IMX(i%) 1170 REX(i%) = TR 1180 IMX(i%) = TI 1190 K% = ND2% 1200 If K% > J% Then GoTo 1240 1210 J% = J% - K% 1220 K% = K% / 2 1230 GoTo 1200 1240 J% = J% + K% 1250 Next i% 1260 ' 1270 For L% = 1 To M% 'Loop for each stage 1280 LE% = CInt(2 ^ L%) 1290 LE2% = LE% / 2 1300 UR = 1 1310 UI = 0 1320 SR = Cos(pi / LE2%) 'Calculate sine & cosine values 1330 SI = -Sin(pi / LE2%) 1340 For J% = 1 To LE2% 'Loop for each sub DFT 1350 JM1% = J% - 1 1360 For i% = JM1% To NM1% Step LE% 'Loop for each butterfly 1370 IP% = i% + LE2% 1380 TR = REX(IP%) * UR - IMX(IP%) * UI 'Butterfly calculation 1390 TI = REX(IP%) * UI + IMX(IP%) * UR 1400 REX(IP%) = REX(i%) - TR 1410 IMX(IP%) = IMX(i%) - TI 1420 REX(i%) = REX(i%) + TR 1430 IMX(i%) = IMX(i%) + TI 1440 Next i% 1450 TR = UR 1460 UR = TR * SR - UI * SI 1470 UI = TR * SI + UI * SR 1480 Next J% 1490 Next L% 1500 ' End Sub Private Sub btn_clr_Click() ipplot.Cls FuncGen.Clear End Sub Private Sub btn_generate_Click() FuncGen.SamplingRate = CInt(txtsamp.Text) FuncGen.amplitude = Slideramp.Value If opt_sin.Value = True Then FuncGen.GenSine CInt(txtfreq.Text) End If If opt_cos.Value = True Then FuncGen.GenCos CInt(txtfreq.Text) End If If opt_impulse.Value = True Then FuncGen.GenImpulse Val(txt_impulsepos.Text) End If If Opt_sqr.Value = True Then FuncGen.GenSquare CInt(txtfreq.Text) End If ipplot.Cls plotip End Sub Private Sub Command1_Click() output.Cls End Sub Private Sub Command2_Click() On Error Resume Next If Optreal.Value = True Then output.ForeColor = vbRed For cnt = 0 To 156 outputarray(cnt) = REX(cnt) Next cnt End If If Optimg.Value = True Then output.ForeColor = vbWhite For cnt = 0 To 156 outputarray(cnt) = IMX(cnt) Next cnt End If If Optmag.Value = True Then output.ForeColor = vbYellow For cnt = 0 To 156 outputarray(cnt) = Sqr((IMX(cnt) * IMX(cnt)) + (REX(cnt) * REX(cnt))) Next cnt End If If Optphase.Value = True Then output.ForeColor = vbCyan For cnt = 0 To 156 outputarray(cnt) = Atn(IMX(cnt) / REX(cnt)) Next cnt End If If Optpower.Value = True Then output.ForeColor = vbGreen For cnt = 0 To 156 outputarray(cnt) = (IMX(cnt) * IMX(cnt)) + (REX(cnt) * REX(cnt)) Next cnt End If For cnt = 0 To 256 If (maxvalue < outputarray(cnt)) Then maxvalue = outputarray(cnt) End If Next cnt oldval = (2600 / (2 * Screen.TwipsPerPixelY)) - (outputarray(1) * ((950 / Screen.TwipsPerPixelY) / maxvalue)) For cnt = 1 To 256 output.Line (cnt - 1, oldval)-(cnt, (2600 / (2 * Screen.TwipsPerPixelY)) - (outputarray(cnt) * ((950 / Screen.TwipsPerPixelY) / maxvalue))) oldval = (2600 / (2 * Screen.TwipsPerPixelY)) - (outputarray(cnt) * ((950 / Screen.TwipsPerPixelY) / maxvalue)) Next cnt End Sub Private Sub Command3_Click() MsgBox "Developed and published by : " + vbNewLine + "LOGIX4U" + vbNewLine + "www.logix4u.net" + vbNewLine + vbNewLine + "Algorithms by : " + vbNewLine + "Steven W Smith " + vbNewLine + "www.dspguide.com", vbInformation, "About" End Sub Private Sub Command4_Click() On Error Resume Next Label5.Visible = True DoEvents If Opthamm.Value = True Then FuncGen.ApplyHamming ipplot.Cls plotip End If If Opthann.Value = True Then FuncGen.ApplyHanning ipplot.Cls plotip End If If Optblack.Value = True Then FuncGen.ApplyBlackman ipplot.Cls plotip End If For cnt = 1 To 512 REX(cnt) = FuncGen.Samples(cnt) IMX(cnt) = 0 Next cnt fft If Optreal.Value = True Then output.ForeColor = vbRed For cnt = 0 To 156 outputarray(cnt) = REX(cnt) Next cnt End If If Optimg.Value = True Then output.ForeColor = vbWhite For cnt = 0 To 156 outputarray(cnt) = IMX(cnt) Next cnt End If If Optmag.Value = True Then output.ForeColor = vbYellow For cnt = 0 To 156 outputarray(cnt) = Sqr((IMX(cnt) * IMX(cnt)) + (REX(cnt) * REX(cnt))) Next cnt End If If Optphase.Value = True Then output.ForeColor = vbCyan For cnt = 0 To 156 outputarray(cnt) = Atn(IMX(cnt) / REX(cnt)) Next cnt End If If Optpower.Value = True Then output.ForeColor = vbGreen For cnt = 0 To 156 outputarray(cnt) = Sqr((IMX(cnt) * IMX(cnt)) + (REX(cnt) * REX(cnt))) Next cnt End If For cnt = 0 To 256 If (maxvalue < outputarray(cnt)) Then maxvalue = outputarray(cnt) End If Next cnt oldval = (2600 / (2 * Screen.TwipsPerPixelY)) - (outputarray(1) * ((950 / Screen.TwipsPerPixelY) / maxvalue)) For cnt = 1 To 256 output.Line (cnt - 1, oldval)-(cnt, (2600 / (2 * Screen.TwipsPerPixelY)) - (outputarray(cnt) * ((950 / Screen.TwipsPerPixelY) / maxvalue))) oldval = (2600 / (2 * Screen.TwipsPerPixelY)) - (outputarray(cnt) * ((950 / Screen.TwipsPerPixelY) / maxvalue)) Next cnt Label5.Visible = False End Sub Private Sub Form_Activate() ipplot.Line (0, 1450 / Screen.TwipsPerPixelY)-(10000, 1450 / Screen.TwipsPerPixelY), vbRed End Sub Private Sub Form_Paint() On Error Resume Next Dim maxvalue, oldval As Double ipplot.Cls plotip End Sub Private Sub opt_impulse_Click() If opt_impulse.Value = True Then txt_impulsepos.Enabled = True End If End Sub


网友评论