分段-2pi到2pi的傅里叶级数与sympy

问题描述 投票:0回答:1

我正在尝试分段找到以下的傅里叶级数

f_t = sym.Piecewise(
    (sym.sin(t), (t >= -2 * sym.pi) & (t <= 2 * sym.pi))
)

这是我已经拥有的代码,但我无法获得一些术语,因为它被卡住了。也许还有另一种方法来进行计算并获得级数的一些项?

import sympy as sym

t = sym.symbols('t', real = True)

#piecewise function
f_t = sym.Piecewise(
    (sym.sin(t), (t >= -2 * sym.pi) & (t <= 2 * sym.pi))
)

#3 terms
num_terms = 3
approx_fourier_series = sym.fourier_series(f_t, (t, -2 * sym.pi, 2 * sym.pi)).truncate(n=num_terms)

#result
print("Fourier Series with", num_terms, "terms:")
print(approx_fourier_series)

我尝试过执行此代码,但我无法获取这些术语,因为它似乎卡住了,所以也许还有另一种方法来计算傅里叶级数。

python sympy fft calculus piecewise
1个回答
0
投票

我的代码在 Julia 中

使用 Heaviside 定义分段函数

t = sympy.symbols("t", real = true)
f_t = sympy.sin(t)*(Heaviside(t+sympy.pi) - Heaviside(t-sympy.pi)  ) #piecewise using Heaviside, from -pi to pi 
#3 terms
num_terms = 3
approx_fourier_series = sympy.fourier_series(f_t, (t, -2 * sympy.pi, 2 * sympy.pi)).truncate(n=num_terms)


#result
println("Fourier Series with ", num_terms, " terms:")
println(approx_fourier_series)

输出

Fourier Series with 3 terms:
4*sin(t/2)/(3*pi) + sin(t)/2 + 4*sin(3*t/2)/(5*pi)
© www.soinside.com 2019 - 2024. All rights reserved.