原文地址: https://www.computationalimaging.cn/2018/11/matlab.html
卷积计算起来较为繁琐, 若能够用matlab辅助计算则会简单很多. 通过使用卷积定理和MATLAB符号函数, 便可以计算连续函数的卷积表达式.
本文主要包括如下几个部分:
1. 利用符号函数计算Fourier变换和Fourier反变换
2. 利用符号函数进行卷积计算
3. 以信号与系统考研题中的例题为例进行说明
1. 利用符号函数计算Fourier变换和Fourier反变换
Matlab提供了fourier 和 ifourier 两个内置函数.
下面是fourier的帮助
fourier Fourier integral transform.
F = fourier(f) is the Fourier transform of the symbolic expression
or function f with default independent variable x. If f does not
contain x, then the default variable is determined by SYMVAR.
By default, the result F is a function of w. If f = f(w), then F
is returned as a function of the variable v, F = F(v).
By definition, F(w) = c*int(f(x)*exp(s*i*w*x),x,-inf,inf).
You can set the parameters c,s to any numeric or symbolic values
by setting the preference SYMPREF('FourierParameters',[c,s]).
By default, the values are c = 1 and s = -1.
F = fourier(f,v) returns F as a function of the variable v
instead of the default variable w:
F(v) = c*int(f(x)*exp(s*i*v*x),x,-inf,inf).
F = fourier(f,u,v) treats f as a function of the variable u instead
of the default variable x:
F(v) = c*int(f(u)*exp(s*i*v*u),u,-inf,inf).
Examples:
syms t v w x f(x)
fourier(1/t) returns -pi*sign(w)*1i
fourier(exp(-x^2),x,t) returns pi^(1/2)*exp(-t^2/4)
fourier(exp(-t)*heaviside(t),v) returns 1/(1+v*1i)
fourier(diff(f(x)),x,w) returns w*fourier(f(x),x,w)*1i
以及ifourier的帮助
ifourier Inverse Fourier integral transform.
f = ifourier(F) is the inverse Fourier transform of the symbolic
expression or function F with default independent variable w. If
F does not contain w, then the default variable is determined by
SYMVAR. By default, the result f is a function of x. If F = F(x),
then f is returned as a function of the variable t, f = f(t).
By definition,
f(x) = abs(s)/(2*pi*c) * int(F(w)*exp(-s*i*w*x),w,-inf,inf).
You can set the parameters c,s to any numeric or symbolic values
by setting the preference SYMPREF('FourierParameters',[c,s]).
By default, the values are c = 1 and s = -1.
f = ifourier(F,u) returns f as a function of the variable u
instead of the default variable x:
f(u) = abs(s)/(2*pi*c) * int(F(w)*exp(-s*i*w*u),w,-inf,inf).
f = ifourier(F,v,u) treats F as a function of the variable v
instead of the default variable w:
f(u) = abs(s)/(2*pi*c) * int(F(v)*exp(-s*i*v*u),v,-inf,inf).
Examples:
syms t u v w f(x)
ifourier(w*exp(-3*w)*heaviside(w)) returns 1/(2*pi*(-3+x*1i)^2)
ifourier(1/(1 + w^2),u) returns exp(-abs(u))/2
ifourier(v/(1 + w^2),v,u) returns -(dirac(1,u)*1i)/(w^2+1)
ifourier(fourier(f(x),x,w),w,x) returns f(x)
显然, 有了这两个函数, 我们便可以利用符号变量计算Fourier变换和反变换.
例如, 对于奥本海姆<信号与系统(第2版)>习题4.1(b)
计算$f(t)=e^{-2|t-1|}$的傅里叶变换
我们便可利用MATLAB进行计算:
syms t
fourier(exp(-2*abs(t-1)))
得到如下结果:
>> fourier(exp(-2*abs(t-1)))
ans =
- exp(-w*1i)/(- 2 + w*1i) + exp(-w*1i)/(2 + w*1i)
本题的参考答案为:
对比可知, 通过MATLAB得到的结果与实际结果相同.
同理, 我们也可以计算Fourier的反变换. 如下所示
>> syms w
>> ifourier(dirac(w))
ans =
1/(2*pi)
2. 利用符号函数进行卷积计算
通过计算Fourier变换和反变换, 我们便可以结合卷积定理, 如式\ref{eq1}所示, 求出任意两个函数的卷积.
$$x(t)*h(t)=F^{-1}(X(jw)\dot H(jw)) \tag{1} \label{eq1}$$
例如, 我们用$x(t)=e^{cos(3t+\frac{1}{4}\pi)}$与$\delta(t-3)$的卷积进行验证, 有dirac函数的性质, 结果显然应为$x(t)=e^{cos(3(t-3)+\frac{1}{4}\pi)}$
syms t
>> xt = exp((cos(3*t + 0.25*pi)))
xt =
exp(cos(3*t + pi/4))
>> f_xt=fourier(xt)
f_xt =
fourier(exp(cos(3*t + pi/4)), t, w)
>> f_ht=fourier(dirac(t-3))
f_ht =
exp(-w*3i)
>> result = ifourier(f_xt * f_ht)
result =
exp(cos(3*x + pi/4 - 9))
结果正确.
3. 以信号与系统考研题中的例题为例进行说明
题目为:
若$x(t)=\frac{sint}{t}$, 则积分$\int_{-\pi}^{\pi}[x^2(t)*sin(t+\frac{\pi}{3})]dt$的值为多少?
正解为0, 方法是求出$x^2(t)$和$sin(t+\frac{\pi}{3})$的Fourier变换, 通过卷积定理, 再利用三角函数的周期性即可得到结果为0. (欢迎补充更简单的方法~)
用MATLAB则可通过如下代码求解:
syms t
ft = sin(t)*sin(t)/(t*t);
ft = sin(t)*sin(t)/(t*t);
vt = sin(t+pi/3);
f_ft = fourier(ft);
f_vt = fourier(vt);
f_anss = f_ft * f_vt;
fv = ifourier(f_anss);
result = int(fv, [-pi, pi])
输出为:
result =
0
可见, 利用MATLAB能够比较好地计算连续函数卷积结果的表达式, 并可以用来解决实际的问题. 其中, dirac函数, li表示的虚部, 以及fourier和ifourier函数都是之前没用过的新函数.