2016-09-02 41 views
0

我只是在與竇性波的調製作鬥爭。 我得到了一個頻率(來自數據 - 隨時間變化),現在我需要繪製頻率相應的正弦波。得到了頻率,需要在python中的竇性竇波

real data and sinus

藍線是剛剛繪製的實際數據點和綠色是我做的到現在,但它不會與真實數據corespond可言。

代碼繪製正弦波是底部:

def plotmodulsin(): 
    n = 530 
    f1, f2 = 16, 50 # frequency 

    t = linspace(6.94,8.2,530) 
    dt = t[1] - t[0] # needed for integration 
    print t[1] 
    print t[0] 
    f_inst = logspace(log10(f1), log10(f2), n) 
    phi = 2 * pi * cumsum(f_inst) * dt # integrate to get phase 
    pylab.plot(t, 5*sin(phi)) 

振幅矢量:

[2.64,-2.64,6.14,-6.14,9.56,-9.56,12.57,-12.57, 15.55,-15.55,18.04,-18.04,21.17,-21.17,23.34,-23.34,25.86,-25.86,28.03,-28.03,30.49,-30.49,33.28,-33.28,35.36,-35.36,36.47,-36.47, 38.86,-38.86,41.49,-41.49,42.91,42.91,44.41,-44.41,45.98,-45.98,47.63,-47.63,47.63,-47.63,51.23,-51.23,51.23,-51.23,53.18,-53.18, 55.24,-55.24,55.24,-55.24,55.24,-55.24,57.43,-57.43,57.43,-57 -19.75,59.75,59.75,59.75,59.75,59.75,59.75,62.22,-62.22,59.75,59.75,62.22,62.22,59.75,59.75,62.22, -62.22,59.75,59.75,62.22,-62.22,62.22,-62.22,59.75,59.75,62.22,-62.22,62.22,-62.22,62.22,62.22,59.75,59.75,62.22,62.22,59.75, -59.75,62.22,-62.22,59.75,-59.75,59.75]

時間矢量爲真實的數據:

[6.954,6.985,7.016,7.041,7.066,7.088,7.11,7.13, 7.149,7.167,7.186,7.202,7.219,7.235,7.251,7.266,7.282,7.296,7.311,7.325,7.339,7.352,7.366,7.379,7.392,7.404,7.417,7.43,7.442,7.454, 7.501,7.513,7.524,7.536,7.547,7.558, 7.56,7.569,7.58,7.591,7.602,7.613,7.624,7.634,7.645,7.655,7.666,7.676,7.686,7.697,7.707,7.717,7.728, 7.828,7.838,7.848,7.858,7.868,7.877,7.858,7.868,7.877,7.887,7.897,7.907,7​​.917,7.927,7.937,7.946,7.956,7.966,7.976,7.986,7.996,8.006,8.016,8.026,8.035,8.045,8.055,8.065, 8.075,8.084,8.094,8.104,8.114,8.124,8.134,8.144,8.154,8.164,8.174,8.184,8.194,8.20]

所以我需要產生具有恆定幅度和下列頻率竇:

[10.5,16.03,20.0,22.94,25.51,27.47,29.76,31.25,32.89,34.25,35.71,37.31,38.46,39.06,40.32,41.67,42.37,4 3.1,43.86,44.64,44.64,46.3,46.3,47.17,48.08,48.08,48.08,49.02,49.02,50.0,50.0,50.0,50.0]

+0

您繪製恆幅正弦(5)。因此,你不能指望它匹配你的數據,它有一個振幅,似乎或多或少線性增加,直到它達到60.但沒有你的數據的形式和它所代表的信息,人們不能真正決定如何適應你的數據。最好的做法是繪製一些定性上看起來相似的東西...... – jotasi

+0

現在我需要的是將數據olny與它們的頻率進行匹配。振幅可以作爲下一步。這就是爲什麼我乘以常數。 –

+0

我加了我的載體,如果有人可以處理呢? –

回答

0

你可以嘗試匹配你的東西正弦函數或通過從數據中提取頻率和振幅的估計值,實際上就像餘弦一樣。如果我正確地理解了你,你的數據是最大值和最小值,並且你想要一個類似於它的三角函數。如果您的數據保存在兩個陣列timevalue中,則幅度估計值僅由np.abs(value)給出。頻率以最大值和最小值之間的時間差的兩倍的倒數給出。 freq = 0.5/(time[1:]-time[:-1])爲您提供每個時間間隔中點的頻率估計值。因此相應的時間爲freqTimes = (time[1:]+time[:-1])/2.

爲了獲得更平滑的曲線,現在可以對這些幅度和頻率值進行插值,以獲得其中間值的估計值。一個非常簡單的方法是使用np.interp,它將執行簡單的線性插值。您將必須指定在哪些時間點插值。我們將構建一個數組爲,然後通過插值:

n = 10000 
timesToInterpolate = np.linspace(time[0], time[-1], n, endpoint=True) 
freqInterpolated = np.interp(timesToInterpolate, freqTimes, freq) 
amplInterpolated = np.interp(timesToInterpolate, time, np.abs(value)) 

現在你可以做集成,你已經有了你的例子做:

phi = (2*np.pi*np.cumsum(freqInterpolated) 
     *(timesToInterpolate[1]-timesToInterpolate[0])) 

現在你可以繪製。所以,把他們放在一起給你:

import numpy as np 
import matplotlib.pyplot as plt 

time = np.array([6.954, 6.985, 7.016, 7.041, 7.066, 7.088, 7.11, 7.13]) #... 
value = np.array([2.64, -2.64, 6.14, -6.14, 9.56, -9.56, 12.57, -12.57]) #... 

freq = 0.5/(time[1:]-time[:-1]) 
freqTimes = (time[1:]+time[:-1])/2. 

n = 10000 
timesToInterpolate = np.linspace(time[0], time[-1], n, endpoint=True) 
freqInterpolated = np.interp(timesToInterpolate, freqTimes, freq) 
amplInterpolated = np.interp(timesToInterpolate, time, np.abs(value)) 

phi = (2*np.pi*np.cumsum(freqInterpolated) 
     *(timesToInterpolate[1]-timesToInterpolate[0])) 

plt.plot(time, value) 
plt.plot(timesToInterpolate, amplInterpolated*np.cos(phi)) #or np.sin(phi+np.pi/2) 
plt.show() 

結果看起來是這樣的(如果包括全陣列):

enter image description here

+0

哇...謝謝你...這是美好的:D –