数字滤波器设计
数字滤波器设计
| LeastSquaresFilterKernel | 使用最小二乘方法创建一个 FIR 滤波器 |
| FrequencySamplingFilterKernel | 使用频率采样创建一个 FIR 滤波器 |
| EquirippleFilterKernel | 使用等波纹法创建一个 FIR 滤波器 |
| ToDiscreteTimeModel | 根据模拟原型创建一个 IIR 数字滤波器 |
最小二乘方法
InverseFourierSequenceTransform[UnitBox[ω / 2.4], ω, n, FourierParameters -> {1, -1}]h = LeastSquaresFilterKernel[{"Lowpass", 1.2}, 15]Plot[{Abs[ListFourierSequenceTransform[h, x]], Piecewise[{{1, 0 ≤ x ≤ 1.2}}]}, {x, 0, π}, PlotRange -> All, Exclusions -> False, Filling -> {1 -> {2}}]最小二乘方法也称之为基于窗的方法. 通过对 FIR 应用由 LeastSquaresFilterKernel 产生的平滑窗函数,可以减小均方误差.
win = Array[HannWindow, 15, {-0.5, 0.5}]h2 = win h;
Plot[Evaluate[20 Log[10, Abs[ListFourierSequenceTransform[#, x]]]& /@ {h, h2}], {x, 0, π}, ImageSize -> 300]With[{wins = {DirichletWindow, BartlettWindow, HannWindow, BlackmanWindow}},
f = FourierTransform[#[x], x, w]& /@ wins;
LogLinearPlot[Evaluate[20 Log[10, Abs[#] / Limit[#, w -> 0]]& /@ f], {w, 1, 100}, PlotRange -> {0, -100}, PlotLegends -> wins]]频率采样方法
t = Table[{x, UnitBox[x / (0.9 π)]}, {x, 0, 3, 2π / 11}]Show[{Plot[UnitBox[x / (0.9 π)], {x, 0, π}, Exclusions -> None], ListPlot[t, PlotRange -> All, PlotStyle -> {Red, PointSize[Large]}, Filling -> 0, FillingStyle -> Red]}]{frequencies, amplitudes} = Transpose[t];
FrequencySamplingFilterKernel[amplitudes, 1]Show[{ListPlot[t, PlotRange -> {{0, π}, {-0.2, 1.2}}, PlotStyle -> {Red, PointSize[Large]}, Filling -> Axis, AxesLabel -> {ω, None}, Ticks -> {Automatic, {0., 0.5, 1.}}], Plot[{UnitBox[ω / (0.9 π)], Evaluate[Abs[ListFourierSequenceTransform[FrequencySamplingFilterKernel[amplitudes, 1], ω]]]}, {ω, 0, π}, Exclusions -> None]}]等波纹法
EquirippleFilterKernel[{{{0., 0.05}, {.1, 0.15}, {0.18, 0.25}, {0.3, 0.36}, {0.41, 0.5}} 2 π, {0., 1., 0., 1., 0.}, {10., 1., 3., 1., 20.}}, 55]Plot[20 Log[10, Abs[ListFourierSequenceTransform[%, ω]]], {ω, 0, π}, PlotRange -> All, GridLines -> Automatic]根据模拟原型创建一个数字滤波器
创建数字滤波器常用的一个方法是使用双线性变换将模拟原型变换为等同的数字滤波器. 由此所得的是用 TransferFunctionModel 表示的一个无限脉冲响应(IIR)滤波器,
| ToDiscreteTimeModel | 模拟滤波器的离散时间近似 |
tfa = TransferFunctionExpand[Chebyshev1FilterModel[{"Lowpass", {1., 3.}, {1., 40.}}, s]]//Choptfd = ToDiscreteTimeModel[tfa, 1, z, Method -> {"BilinearTransform"}]//ChopGraphicsRow[{Plot[Abs[tfd[E^I ω]]^2, {ω, 0, π}, AxesLabel -> {ω, None}, GridLines -> Automatic, AspectRatio -> 1 / 3], BodePlot[tfd, {0.1, N[π]}, GridLines -> Automatic, PlotLayout -> List]//First}]| TransferFunctionPoles | 提取模拟滤波器的极点 |
| TransferFunctionZeros | 提取模拟滤波器的零点 |
h = LeastSquaresFilterKernel[{"Lowpass", 1.2}, 9];
H = ListZTransform[h, z]tf = TransferFunctionModel[H, z, SamplingPeriod -> 1]//TransferFunctionCancelzeros = Flatten@TransferFunctionZeros[tf]ListPlot[Transpose[{Re[zeros], Im[zeros]}], PlotRange -> 2.2{{-1, 1}, {-1, 1}}, PlotStyle -> {PointSize[Large]}, AxesOrigin -> {0, 0}, Epilog -> {Dashed, Pink, Circle[]}, AspectRatio -> Automatic]FIR 滤波器
| ListConvolve | 卷积 FIR 滤波器和数据 |
| ImageConvolve | 卷积 FIR 滤波器和图像 |
| DiscreteConvolve | 两个信号的符号卷积 |
h = LeastSquaresFilterKernel[{"Lowpass", 0.5}, 21];
Plot[{Abs[ListFourierSequenceTransform[h, x]], Piecewise[{{1, 0 ≤ x ≤ .5}}]}, {x, 0, π}, PlotRange -> All, Exclusions -> False]a = ExampleData[{"Audio", "Apollo11ReturnSafely"}, "Audio"]res = Audio[ListConvolve[h, AudioData[a][[1]]], SampleRate -> AudioSampleRate[a]]img = [image];
row = ImageData[img, Interleaving -> False][[1, 100]];
ListLinePlot /@ {row, ListConvolve[(h / Total[h]), row, 12, 0]}ImageConvolve[img, Outer[Times, h, h]]IIR 滤波器
| RecurrenceFilter | 将 IIR 滤波器应用于信号 |
| RecurrenceTable | 迭代求解递推方程 |
| OutputResponse | 滤波器的输出响应 |
tfd = TransferFunctionModel[{{{1 + 3*z + 3*z^2 + z^3}}, -3 + 15*z - 25*z^2 + 21*z^3}, z,
SamplingPeriod -> 1];input = N@ArrayPad[{1}, {0, 20}];
RecurrenceFilter[tfd, input]RecurrenceFilter[{{21., -25., 15., -3. }, {1., 3., 3., 1.}}, input]使用 OutputResponse 计算脉冲响应:
OutputResponse[tfd, input]通过使用 RecurrenceTable 求解滤波器对应的递推方程模型,计算脉冲响应:
Module[{x, y},
x[n_] := DiscreteDelta[n];
RecurrenceTable[{21y[n] - 25 y[n - 1] + 15 y[n - 2] - 3 y[n - 3] == x[n] + 3 x[n - 1] + 3 x[n - 2] + x[n - 3], y[-1] == 0, y[-2] == 0, y[-3] == 0}, y, {n, 0, 20}]//N]img = [image];
row = ImageData[img, Interleaving -> False][[1, 100]];
ListLinePlot /@ {row, RecurrenceFilter[tfd, row]}RecurrenceFilter[tfd, img]