[MATLAB]:傅里叶级数 - 无法从sym转换为逻辑

时间:2017-04-06 00:26:07

标签: matlab while-loop fft

我试图在MATLAB中计算/绘制函数的傅里叶级数。功能是

f(t) = 1-cos(t) for [-pi,0] and f(t) = t/pi for [0,pi].

我需要找到系列中需要多少项才能用99%的平均能量逼近原始信号。我尝试使用while循环来完成它,但似乎无法使比较条件起作用。完整代码如下。

clear all

n = 0;
syms t;

f1 = 1-cos(t);
f2 = t/pi;

e1 = int((abs(f1))^2,-pi,0);
e2 = int((abs(f2))^2, 0, pi);

e_av = (1/(2*pi)) * (e1 + e2);

for k = 1 : 11
c_n(k) = subs((1/(2*pi)) * (int((f1 * exp(-1j*n*2*pi*t)),-pi,0) + int((f2 * exp(-1j*n*2*pi*t)), 0, pi)));
n=n+1;
end

sum = c_n(1).^2;
i = 2;

while (sum < 0.9075)
    sum = sum + 2*c_n(i).^2;
    i = i+1;
end

display(i)
display(sum)

0.9075是e_av的99%。

我理解错误意味着什么,我假设MATLAB由于某种原因将sum作为符号变量读取,并且你不能将sym与逻辑进行比较。但是,当第一次定义时,sum = c_n(1).^2;,它是一个值,而不是sym(c_n(1)是cn0,第一个傅立叶系数)。即使在集成之后,符号变量是否以某种方式进入c_n?任何帮助将非常感激。

三江源

0 个答案:

没有答案