Python matplotli.psd fitting

雨燕双飞 提交于 2019-12-11 05:33:59

问题


I'm trying to make a fit using matplotlib.psd function. My datafile has 8 columns with displacement and speed for a particle (positionX, positionY, positionZ, AveragePositionXYZ, speedX, speedY, speedZ, AverageSpeedXYZ). Using the positionX for example, I try to get the Power Spectrum with matplotlib.psd:

power, freqs = plt.psd(data, len(data), Fs = 256, scale_by_freq=True, return_line=0)

Then, I try to make a curve fitting using linear regression with scipy stas.linregress:

slope, inter, r2, p, stderr = stats.linregress(x, y)

However, my results are very bad. I try to plot with:

line = (inter + slope * (10 * np.log10(freqs))) 
plt.semilogx(freqs, line)    
plt.show()

And get the following image:

I know that I have a lot of mistakes, and I try to get some solutions in the web. However, I have not had much success. So, I'm asking if there's someone here that could help me.

The datafile has the following format (first 10 lines):

1.50000000,0.00000000,0.00000000,0.50000000,0.00000000,0.00000000,0.00000000,0.00000000
1.49788889,0.00000000,0.00000000,0.49929630,-0.06333333,0.00000000,0.00000000,-0.02111111
1.49367078,0.00000005,0.00000000,0.49789028,-0.12654314,0.00000165,0.00000000,-0.04218050
1.48735391,0.00000027,0.00000000,0.49578473,-0.18950635,0.00000659,0.00000000,-0.06316659
1.47895054,0.00000082,0.00000000,0.49298379,-0.25210085,0.00001647,0.00000000,-0.08402813
1.46847701,0.00000192,0.00000000,0.48949298,-0.31420588,0.00003296,0.00000000,-0.10472431
1.45595360,0.00000385,0.00000000,0.48531915,-0.37570257,0.00005769,0.00000000,-0.12521496
1.44140445,0.00000692,0.00000000,0.48047046,-0.43647431,0.00009232,0.00000000,-0.14546066
1.42485754,0.00001154,0.00000000,0.47495636,-0.49640723,0.00013851,0.00000000,-0.16542291
1.40634452,0.00001814,0.00000000,0.46878755,-0.55539066,0.00019789,0.00000000,-0.18506426

My complete Python code is as follows:

import matplotlib.pyplot as plt
from scipy import stats
import numpy as np

filename = 'datafile.txt'

# Load data
file = np.genfromtxt(filename,
                 skip_header = 0,
                 skip_footer = 0,
                 delimiter = ',',
                 dtype = 'float32',
                 filling_values = 0,
                 usecols = (0, 1, 2, 3, 4, 5, 6, 7),
                 names = ['posX', 'posY', 'posZ', 'posMedias', 'velX', 'velY', 'velZ', 'velMedias'])

# Map values                        
posX = file['posX']
posY = file['posY']
posZ = file['posZ']
posMedia = file['posMedias']
velX = file['velX']
velY = file['velY']
velZ = file['velZ']
velMedia = file['velMedias']

# Column data that will be used
data = posMedia

# PSD calculation
power, freqs = plt.psd(data, len(data), Fs = 256, scale_by_freq=True, return_line=0)

# Linear fit
x = np.log10(freqs[1:])
y = np.log10(power[1:])

slope, inter, r2, p, stderr = stats.linregress(x, y)

print(slope, inter)

# Plot   
line = (inter + slope * (10 * np.log10(freqs))) 
plt.semilogx(freqs, line)    
plt.show()

Thank you so much!

来源:https://stackoverflow.com/questions/39443715/python-matplotli-psd-fitting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!