2016-12-07 10 views
0

プログラムの目的:信号の図形を上にプロットし、この信号のスペクトル図を下に描画する必要があります。両方の場合のyデータだけが異なります。サブプロットを使用してx軸をmatplotlibに固定した2つのアニメーショングラフィックスをプロットするにはどうすればよいですか?

入力にランダムノイズの正弦波を生成し、それを上にプロットすると、完全に動作します。

問題はスペクトルグラフをプロットするときです。それは何らかの理由で更新されていないので、私はmatplotlib.animation.FuncAnimationの機能をよく理解していませんでした。

コード:

import numpy as np 
import matplotlib.pyplot as plt 
import matplotlib.animation as animation 

dt = 0.01 
Fs = 44000.0    # sample rate 
timestep = 1.0/Fs   # sample spacing (1/sample rate) 
t = np.arange(0, 10, dt) # t range 
n = 256     # size of the array data 
w = 10000     # frequency of the input 

data = np.sin(2*np.pi*w*t) 



def update(data): 
    # update the curves with the incoming data 
    line.set_ydata(data) 
    #line2.set_ydata(magnitude) 

    return line, 

def generateData(): 
    # simulate new data coming in 
    while True: 
     nse = np.random.randn(len(t)) 
     r = np.exp(-t/0.05) 
     cnse = np.convolve(nse, r)*dt 
     cnse = cnse[:len(t)] 
     data = np.sin(2*np.pi*w*(t)) + cnse 
     magnitude = np.fft.fft(data)/n 
     magnitude = np.abs(magnitude[range(n//2)]) 
     yield data 

fig = plt.figure() 

# plot time graph axis 
timeGraph = plt.subplot(2, 1, 1) 
timeGraph.set_ylim(-0.2, 0.2) 
timeGraph.set_xlabel('Time') 
timeGraph.set_ylabel('Amplitude') 

# plot frequency graph axis 
freqGraph = plt.subplot(2, 1, 2) 
freqGraph.set_xlabel('Freq (Hz)') 
freqGraph.set_ylabel('|Y(freq)|') 

# get frequency range 
n = len(data) # length of the signal 
print(len(data)) 
k = np.arange(n) 
T = n/Fs 
freq = k/T # two sides frequency range 
freq = freq[range(n//2)] # one side frequency range 

# fft computing and normalization 
magnitude = np.fft.fft(data)/n 
magnitude = np.abs(magnitude[range(n//2)]) 


line, = timeGraph.plot(np.linspace(0, 1, len(t)), 'b') 
line2, = freqGraph.plot(freq, magnitude, 'g') 


# animate the curves 
ani = animation.FuncAnimation(fig, update, generateData, 
           interval=10, blit=True) 

plt.show() # open window 

ボーナス:どのように私は正しくデータと大きさを初期化しますか?

答えて

0

時間と周波数の両方のグラフを更新するには、update関数の両方のプロットにデータを設定する必要があります。もちろん、生成機能でこのデータを提供する必要もあります。したがって、生成関数はyield (data, magnitude)で、更新関数はこのタプルを入力として受け入れる必要があります。

頻度グラフの一部の制限を空にしないように設定することをお勧めします。freqGraph.set_ylim([0, 0.006])

の意味がわかりません。データと振幅を正しく初期化するにはどうすればよいですか?。私は彼らが最初のフレームを含むすべてのフレームについて計算されるという意味で、正しく初期化されていると思います。

ここには動作コードがあります。

import numpy as np 
import matplotlib.pyplot as plt 
import matplotlib.animation as animation 

dt = 0.01 
Fs = 44000.0    # sample rate 
timestep = 1.0/Fs   # sample spacing (1/sample rate) 
t = np.arange(0, 10, dt) # t range 
n = 256     # size of the array data 
w = 10000     # frequency of the input 

data = np.sin(2*np.pi*w*t) 



def update(data): 
    # update the curves with the incoming data 
    line.set_ydata(data[0]) 
    line2.set_ydata(data[1]) 
    return line, line2, 

def generateData(): 
    # simulate new data coming in 
    while True: 
     nse = np.random.randn(len(t)) 
     r = np.exp(-t/0.05) 
     cnse = np.convolve(nse, r)*dt 
     cnse = cnse[:len(t)] 
     data = np.sin(2*np.pi*w*(t)) + cnse 
     magnitude = np.fft.fft(data)/n 
     magnitude = np.abs(magnitude[range(n//2)]) 
     yield (data, magnitude) 

fig = plt.figure() 

# plot time graph axis 
timeGraph = plt.subplot(2, 1, 1) 
timeGraph.set_ylim(-0.2, 0.2) 
timeGraph.set_xlabel('Time') 
timeGraph.set_ylabel('Amplitude') 

# plot frequency graph axis 
freqGraph = plt.subplot(2, 1, 2) 
freqGraph.set_ylim([0, 0.006]) 
freqGraph.set_xlabel('Freq (Hz)') 
freqGraph.set_ylabel('|Y(freq)|') 

# get frequency range 
n = len(data) # length of the signal 
print(len(data)) 
k = np.arange(n) 
T = n/Fs 
freq = k/T # two sides frequency range 
freq = freq[range(n//2)] # one side frequency range 

# fft computing and normalization 
magnitude = np.fft.fft(data)/n 
magnitude = np.abs(magnitude[range(n//2)]) 


line, = timeGraph.plot(np.linspace(0, 1, len(t)),'b') 
line2, = freqGraph.plot(freq, magnitude,'g') 


# animate the curves 
ani = animation.FuncAnimation(fig, update, generateData, 
           interval = 10, blit=True) 

plt.show() # open window 
+0

ありがとうございました!私は「matplotlib.animationFuncAnimation()」がどのように動作するのかちょっと混乱していました。 –

+0

この回答があなたの問題を解決しますか?そうであれば、それを受け入れることができます。そうでない場合や、より良い/より良い回答を待っている場合は、人々があなたが探しているものを知るように洗練された要件で質問を更新することができます。 – ImportanceOfBeingErnest

+0

Sry、私はそれについて知りませんでした:D Accepted。 –

関連する問題