【问题标题】:Using for loops and an array to plot Fourier Transform's on matlab使用 for 循环和数组在 matlab 上绘制傅里叶变换
【发布时间】:2016-04-27 19:07:50
【问题描述】:

我正在尝试在 matlab 中绘制以下方程的周期函数:

使用以下代码:

tn= 25;
kn= 7;

time=0:1:200;
f=0;

for t= (-tn):1:(tn) 

for k = (-kn):1:(kn)

        s1=((1j)*k*exp(-abs(k)));
        s2=exp((1j)*k*((2*pi)/50));

        f=f+(s1*s2);
end
           tval = (f*exp(t));
        fs(1,t+1) = tval;
end

我无法理解为什么我看不到情节。有没有另一种方法可以在 Matlab 中绘制复数?

我是不是走错了路?我只是想根据时间绘制 fs (我关心的可能是 0-200 或 -100 - 100)并查看周期函数,以便我可以继续操作它,但我似乎无法得到正确的情节。

我尝试在 matlab 中使用 symsum 函数,但无法弄清楚。我了解 C 和 C++,感觉这种方法对我来说更直观。

编辑:

x(1:101)=0;
t(1:101)=0;

for n=0:1:100
    t(n+1)=n;

    for k=-100:1:100
        x(n+1)=x(n+1)+abs(sin((k*pi)/2))*exp(1j*k*((2*pi)/50)*n);
    end;
end;

我使用以下代码绘制了函数。为什么我们想象的情节看起来不一样?

【问题讨论】:

  • 你能把你的绘图代码添加到上面的例子中吗?我不确定你到底想画什么。
  • 您不能绘制复数(一维)。我会分别查看余弦和正弦分量( e(ix) = cos x + i sin x) 。

标签: arrays matlab plot transform fft


【解决方案1】:

我没有Matlab,但在Python中也一样(使用与Matlab非常相似的Numpy和Matplotlib):

import numpy as np
import matplotlib.pyplot as p
%matplotlib inline

t= np.arange(0,100,0.1)
s=np.zeros(len(t))


for k in range(40):
    s=s+  np.abs(np.sin (k*np.pi/2)) * np.exp( 1j*k *2*np.pi/50.0*t)   # pos k
    s=s+  np.abs(np.sin (-k*np.pi/2)) * np.exp(- 1j*k *2*np.pi/50.0*t)   # negative k 
  # the cosine is symmetric so it survives adding the negatives, 
  # the sines get cancelled as they are antisymmetric for the negative k,
  # so the imaginary part is identically zero.

p.subplot(311)    
p.plot(np.real(s))
p.subplot(312) 
p.plot(np.imag(s),'r')
p.subplot(313)
p.plot(np.abs(s))

【讨论】:

  • x(1:101)=0; t(1:101)=0;对于 n=0:1:100 t(n+1)=n;对于 k=-100:1:100 x(n+1)=x(n+1)+abs(sin((kpi)/2))*exp(1jk*(( 2*pi)/50)*n);结尾;结束;
  • 如果它对您有帮助,并且您认为它可能对其他人有帮助,您可以接受或点赞。
猜你喜欢
  • 1970-01-01
  • 2016-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-26
  • 2014-11-02
相关资源
最近更新 更多