2016-04-27 239 views
1

我想要繪製在了下面的等式MATLAB的周期函數的:使用for循環和數組繪製傅里葉變換MATLAB

enter image description here

使用下面的代碼:

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; 

我用下面的代碼繪製的功能。爲什麼我們想象的情節看起來不同?

+0

您可以將您的繪圖代碼的例子以上?我不確定你究竟想要繪製什麼。 – nalyd88

+0

您無法繪製複雜數字(在1D中)。我會分別看餘弦和正弦分量(e(ix)= cos x + i sin x)。 – roadrunner66

回答

0

我沒有Matlab的,但在這裏,在Python相同(使用numpy的& Matplotlib這是非常相似的Matlab的):

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)) 

enter image description here

+0

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((k * pi)/ 2) PI)/ 50)* N); 結束; 結束; –

+0

如果它對你有幫助,而且你認爲它可以幫助其他人,你可以接受或者讚揚它。 – roadrunner66