关于滤波的问题
我有一个滤波器的程序如下,现有一系列数据等待滤波,如何运用下列程序实现呢?Private Sub CmdDesi_Click()
Dim h(256) As Double '采样的数组
Const pi = 3.1415926
ReDim a(128), b(128), h1(128)
'以下为要设计的低通滤波器的技术指标
t = Val(Text1.Text) '抽样周期
as1 = Val(Text2.Text) '阻带衰减指数
ws = Val(Text3.Text) '阻带边界频率
ap = Val(Text4.Text) '通带衰减指数
wp = Val(Text5.Text) '通带边界频率
wc = 2 * pi * wp * t '截止频率
n = 2 * 2 / Abs(ws - wp) / t + 0.99999999999 '截取的窗宽度
m = n
If m Mod 2 = 1 Then '判断n为奇数还是偶数,如为奇数,如下计算
h((n - 1) / 2) = wc / pi
For i = 0 To (n - 1) / 2
h(i) = Sin(wc * (i - (n - 1) / 2)) / pi / (i - (n - 1) / 2)
h(n - i - 1) = h(i)
Next i
Else '如为偶数,如下计算
For i = 0 To n / 2
h(i) = Sin(wc * (i - (n - 1) / 2)) / pi / (i - (n - 1) / 2)
h(n - i - 1) = h(i)
Next i
End If
For i = 0 To n - 1
h(i) = h(i) * (0.54 - 0.46 * Cos(2 * pi * i / (n - 1))) '用汉明窗加权(单位抽样响应)
Next i
'以下为显示汉明窗序列
Pic1.Line (20, 20)-(20, 200)
Pic1.Line (20, 100)-(300, 100)
For i = 0 To n - 1
Pic1.Line (20 + 3 * i, 100)-(20 + 3 * i, 100 - 100 * h(i))
Next i
Pic1.Line (20 + 3 * (n - 1), 110)-(20 + 3 * (n - 1), 90)
Pic1.PSet (20 + 3 * (n - 1), 110)
Pic1.Print Int(n) '显示截取的窗宽度n值
'以下为计算幅频、相频部分
For i = 0 To 128
a(i) = 0
b(i) = 0
Next i
k = 0
For i = 0 To 127
a(i) = h((n - 1) / 2) * Cos(i * pi * (n - 1) / 256)
b(i) = -h((n - 1) / 2) * Sin(i * pi * (n - 1) / 256)
For j = 1 To (n - 1) / 2
a(i) = a(i) + 2 * h((n - 1) / 2 - j) * Cos(pi * j * i / 128) * Cos(i * pi * (n - 1) / 256)
b(i) = b(i) - 2 * h((n - 1) / 2 - j) * Cos(pi * j * i / 128) * Sin(i * pi * (n - 1) / 256)
Next j
amp = Sqr(a(i) ^ 2 + b(i) ^ 2) '幅频大小
b(i) = Atn(b(i) / a(i)) '相频大小,Atn是Tan的反函数
If i > 0 Then
If b(i) > 0 And b(i - 1) < 0 Then
k = k + 1
End If
End If
b(i) = b(i) - pi * k / 2
a(i) = 20 * Log(amp) / Log(10) '以db(分贝)表示幅频大小
Next i
x = 20 + 508 * ((wp - ws) / 2 + ws) * 0.005 '计算截止频率的位置
Pic1.Line (20, 240)-(300, 240)
Pic1.Line (20, 240)-(20, 420)
Pic1.PSet (20, 240)
For i = 0 To 127
Pic1.Line -(20 + 2 * i, 240 - 2 * a(i)) '画幅频曲线
Next i
Pic1.Line (x, 235)-(x, 245)
Pic1.PSet (x - 10, 220)
temp = Format(((wp - ws) / 2 + ws) * 2 * t, "#0.00")
Pic1.Print temp '显示截止频率
Pic1.PSet (x + 15, 220)
Pic1.Print "π"
Pic1.PSet (290, 220)
Pic1.Print "π"
Pic1.Line (25, 280)-(20, 280)
Pic1.PSet (0, 270)
Pic1.Print -20
Pic1.Line (25, 320)-(20, 320)
Pic1.PSet (0, 310)
Pic1.Print -40
Pic1.Line (25, 360)-(20, 360)
Pic1.PSet (0, 350)
Pic1.Print -60
Pic1.Line (25, 400)-(20, 400)
Pic1.PSet (0, 390)
Pic1.Print -80
'以下为画相频曲线
Pic1.Line (320, 320)-(620, 320)
Pic1.Line (320, 240)-(320, 420)
Pic1.PSet (320, 320)
For i = 0 To 127
Pic1.Line -(320 + 2 * i, 320 - b(i))
Next i
End Sub
Private Sub Form_Load()
Pic1.Scale (0, 0)-(630, 470)
Text6.Text = "海明窗"
End Sub 太好了,谢谢LZ,如果有一些小波这样的高级算法就好了:@D
页:
[1]