急求VB编程代码!关于FFT傅立叶变换以及滤波的!!高手快帮帮忙
VB新手,非计算机专业。但这是老师布置下来的作业,自己做不来要求1:读取excel中的时域序列后做FFT,变换成频谱
2:通过低通滤波和带通滤波,得到两种不同的频域序列。(切点频率,截断点的选择详细说明下)
3:使用IFFT,将2中得到的两组频域序列变换回时域序列,并绘制时序图。
目的就是通过滤波将时间序列的趋势项与周期项分离!!网上有C语言以及matlaB的关于这方面的代码。但VB的却找不到 不知道有米有高手能帮帮忙写个VB的
--------------------编程问答-------------------- 窗体上放一个picturebox,名称改为picI_FFT。
在窗体中输入以下代码
Option Explicit
'*模块********************************************************
'FFT0 数组下标以0开始
'AR() 数据实部 AI() 数据虚部
'N 数据点数,为2的整数次幂
'NI 变换方向 1为正变换,-1为反变换
'***************************************************************
Const fftIn = 128
Const Pi = 3.1415926
Public Function FFT0(AR() As Double, AI() As Double, N As Long, ni As Long)
Dim i As Long, j As Long, k As Long, L As Long, M As Long
Dim IP As Long, LE As Long
Dim L1 As Long, N1 As Long, N2 As Long
Dim SN As Double, TR As Double, TI As Double, WR As Double, WI As Double
Dim UR As Double, UI As Double, US As Double
M = NTOM(N)
N2 = N / 2
N1 = N - 1
SN = ni
j = 1
For i = 1 To N1
If i < j Then
TR = AR(j - 1)
AR(j - 1) = AR(i - 1)
AR(i - 1) = TR
TI = AI(j - 1)
AI(j - 1) = AI(i - 1)
AI(i - 1) = TI
End If
k = N2
While (k < j)
j = j - k
k = k / 2
Wend
j = j + k
Next i
For L = 1 To M
LE = 2 ^ L
L1 = LE / 2
UR = 1#
UI = 0#
WR = Cos(Pi / L1)
WI = SN * Sin(Pi / L1)
For j = 1 To L1
For i = j To N Step LE
IP = i + L1
TR = AR(IP - 1) * UR - AI(IP - 1) * UI
TI = AI(IP - 1) * UR + AR(IP - 1) * UI
AR(IP - 1) = AR(i - 1) - TR
AI(IP - 1) = AI(i - 1) - TI
AR(i - 1) = AR(i - 1) + TR
AI(i - 1) = AI(i - 1) + TI
Next i
US = UR
UR = US * WR - UI * WI
UI = UI * WR + US * WI
Next j
Next L
If SN <> -1 Then
For i = 1 To N
AR(i - 1) = AR(i - 1) / N
AI(i - 1) = AI(i - 1) / N
Next i
End If
End Function
Private Function NTOM(N As Long) As Long
Dim ND As Single
ND = N
NTOM = 0
While (ND > 1)
ND = ND / 2
NTOM = NTOM + 1
Wend
End Function
Private Sub Form_Load()
'*使用**********
Dim i As Integer
Dim xr(128) As Double
Dim xi(128) As Double
Dim IaIn(128) As Double
'赋值,IaIn(i)是采得的数据。
For i = 0 To 128
IaIn(i) = Sin(i) + 0.5 * Sin(10 * i)
xr(i) = 100 * IaIn(i)
xi(i) = 0
Next
'FFT变换
Call FFT0(xr(), xi(), 128, 1)
'绘图
picI_FFT.Scale (0, 100)-(fftIn - 1, -10)
picI_FFT.DrawWidth = 2
For i = 0 To fftIn - 1
picI_FFT.Line (i, Abs(xr(i)))-(i + 1, Abs(xr(i + 1))), vbRed
' picI_FFT.Line (i, Abs(xi(i)))-(i + 1, Abs(xi(i + 1))), vbBlue
' picI_FFT.Line (i, (xr(i) * xr(i) + xi(i) * xi(i)) \ 128)-(i + 1, (xr(i + 1) * xr(i + 1) + xi(i + 1) * xi(i + 1)) \ 128), vbBlack
Next i
End Sub
当然,这个是简单的版本。还有个快速的版本。关键地方用了汇编优化。 --------------------编程问答-------------------- 再问一下,怎样把用VB打开excel表格然后读取里面的数据并且显示出来呢?2楼代码里的128是数据的个数吗?我是VB菜鸟啊,认真请教哦 --------------------编程问答-------------------- 我,buzhidao --------------------编程问答--------------------
Public Function fft(ByRef Data() As Double) As Double()
ReDim ffft(128, 2) As Double
Dim length As Integer
length = UBound(Data, 1) + 1
' Dim numArray(length - 1, 2) As Double
Dim index As Integer
Dim num5 As Integer
Dim num6 As Integer
Dim num7 As Integer
Dim num10 As Integer
Dim num3 As Integer
Dim num2 As Integer
Dim num11 As Integer
Dim num9 As Integer
num9 = length
Dim num8 As Integer
num8 = CInt(Math.Log(CDbl(num9)) / Math.Log(2#))
Dim numArray2(128) As Double
Dim numArray3(128) As Double
Dim numArray4(128) As Double
Dim numArray5(128) As Double
For index = 0 To num9 - 1
numArray2(index) = Data(index)
numArray3(index) = 0#
Next
Dim a As Double
Dim num14 As Double
num14 = 6.28318530717959 / CDbl(num9)
index = 0
While index < (num9 \ 2)
numArray4(index) = Math.Sin(a)
numArray5(index) = Math.Cos(a)
a = a + num14
index = index + 1
Wend
num7 = num9
num3 = 1
For num2 = 1 To num8
num7 = num7 / 2
num6 = 0
For num11 = 1 To num3
num10 = 0
index = num6
While index <= ((num7 + num6) - 1)
num5 = index + num7
a = numArray2(index) - numArray2(num5)
num14 = numArray3(index) - numArray3(num5)
numArray2(index) = numArray2(index) + numArray2(num5)
numArray3(index) = numArray3(index) + numArray3(num5)
If num10 = 0 Then
numArray2(num5) = a
numArray3(num5) = num14
Else
numArray2(num5) = (a * numArray5(num10)) + (num14 * numArray4(num10))
numArray3(num5) = (num14 * numArray5(num10)) - (a * numArray4(num10))
End If
num10 = num10 + num3
index = index + 1
Wend
num6 = (num6 + num7) + num7
Next
num3 = num3 + num3
Next
num5 = num9 \ 2
For index = 1 To (num9 - 1)
num6 = num9
If num5 < index Then
Dim num12 As Double
num12 = numArray2(index)
numArray2(index) = numArray2(num5)
numArray2(num5) = num12
num12 = numArray3(index)
numArray3(index) = numArray3(num5)
numArray3(num5) = num12
End If
num6 = num6 / 2
Do While num5 >= num6
num5 = num5 - num6
num6 = num6 / 2
If num5 = 0 Then
Exit Do
End If
Loop
num5 = num5 + num6
Next
For index = 0 To num9 - 1
numArray(index, 0) = numArray2(index)
numArray(index, 1) = numArray3(index)
numArray(index, 2) = ((numArray2(index)) ^ 2# + (numArray3(index)) ^ 2#) ^ 0.5
Next
fft = numArray
End Function
我的程序是这样的,有点区别,不知道区别在哪? 我现在默认了数组为128
如果你需要动态,请
redim各数组
输入:一串数据
输出:FFT变换后的数据,1维是实部 ,2维是虚部
--------------------编程问答-------------------- 很久没上论坛了,看到有新回复很开心,无论怎样谢谢LS热心的人 --------------------编程问答--------------------
什么意思? 我们的代码没用吗?? 至于后面的东西,属于数学知识了,你不贴些东西给人参考,我们怎么帮你呢? --------------------编程问答-------------------- FFT的代码是可以用的,只是由于本人能力问题,不知为啥函数调用不了。。。至于中间滤波那部分涉及到信号处理以及积分,书上也没写明白,我不知道怎么打出来。。。等我去网上找找有没有相关的内容,截个图过来
补充:VB , 基础类