2026a

# 计算卷积以及算法选择


# 问题描述

如何计算卷积以及算法选择。

# 解决方法

在 Syslab 中,可以使用 conv 函数计算卷积,函数提供 method 关键字参数用于设置卷积的计算方法。method 关键字参数有 "auto" 、"direct" 、 "fft" 三个选项,"direct" 对应直接卷积算法,"fft" 对应 fft 卷积算法,"auto" 会根据数据长度函数内部自动选取直接算法和 fft 算法进行计算。

构造向量 v1 和 v2:

using TyMath
rng = MT19937ar(5489)
v1 = rand(rng,5)
v2 = rand(rng,6)
v1 = 5-element Vector{Float64}:
 0.8147236863931789
 0.9057919370756192
 0.12698681629350606
 0.9133758561390194
 0.6323592462254095

v2 = 6-element Vector{Float64}:
 0.09754040499940952
 0.2784982188670484
 0.5468815192049838
 0.9575068354342976
 0.9648885351992765
 0.15761308167754828

调用 conv 函数并设置 method 关键字参数为 "direct"。

conv(v1,v2,method = "direct")
ans = 10-element Vector{Float64}:
 0.07946847833340259
 0.3152504079168516
 0.7102051139775342
 1.3999210224250587
 2.0389203846736748
 1.799609410467748
 1.4856819925570601
 1.5068089760757135
 0.7541161702260939
 0.09966808952487834

调用 conv 函数并设置 method 关键字参数为 "fft"。

conv(v1,v2,method = "fft")
ans = 10-element Vector{Float64}:
 0.07946847833340272
 0.31525040791685144
 0.7102051139775346
 1.3999210224250587
 2.0389203846736748
 1.7996094104677482
 1.4856819925570606
 1.5068089760757137
 0.7541161702260941
 0.09966808952487827