0

import matplotlib.pyplot as plt import numpy as np import sys from scipy.interpolate import spline

    filename = sys.argv[1]

    I take data from a file, y=time,x=distance
    load it into 2 numpy arrays x and y
    x,y=np.loadtxt(filename, delimiter=',', unpack=True)
    dydx=np.zeros([1,len(x)],dtype = float)
    zero =  np.array(np.zeros(5))
    xdiff = np.array([0,len(x)-1],dtype = float)
    ydiff = np.array([0,len(x)-1],dtype = float)
    xdiff = np.diff(x)

    ydiff = np.diff(y)
    dydx = np.divide(np.diff(x),np.diff(y))
    plt.ylabel("Velocity")
    plt.xlabel("Time")

    a = np.linspace(y[0],y[len(y)-1],len(y))
    dydx = np.append(dydx, zero[0])

The graph that I get is still pointy and not smooth

This is something I did to smoothen the plot but hasn't worked, still pointy

a_smooth = np.linspace(a.min(),a.max(),len(y))
dydx_smooth = spline(a,dydx,a_smooth)

this is the normal procedure for plotting

plt.plot(a_smooth,dydx_smooth)
plt.grid()
plt.show()

My plot is pointy at the edges

I tried to use splrev,splrep but the knots are not non-decreasing, the dydx constantly goes above and below zero, so that cannot be used. I saw the technique in a youtube video,it worked for him but not for me https://www.youtube.com/watch?v=uSB8UBrbMfk

you can get my input data from here https://pastebin.com/44Kn2sm5

  • Doesn't that video already show why this is not a good way of smoothing? (not sure why the guy made the video though, showing a not so good option) In any case there are a lot of questions on smoothing here, so I think you rather want to look at those and tell in how far they are not working. Also provide a [mcve] of the problem, which people can run. – ImportanceOfBeingErnest Jun 07 '18 at 21:15
  • @ImportanceOfBeingErnest I have added a link to my input data. Please run the code. I saw other answers but it hasn't helped me so far, please help me – Rick Sanchez Jun 07 '18 at 23:22
  • sorry, but your code looks quite messy: `np.zeros` already returns a numpy array, why the cast to `np.array`? The same with the definition of xdiff and ydiff (which do not differ in a single character...) Then you redefine them - and then you do not even use these variables. Then you want to 'smoothen' something, not claryfying, what you mean by that. a rolling mean filter? that has not anything to do with a spline. Do you want to make spline fit? I guess not... Please make your request much more clearer. – SpghttCd Jun 08 '18 at 08:08

1 Answers1

1

You can filter your data in a very easy and convenient way via pandas.

I do not have access to your online data storage, so I made some sample data on my own, if that's ok...

import pandas as pd

x = np.linspace(0,25000,1000)
y = np.sin(np.linspace(0,25*np.pi,1000))*(1.2+np.cos(np.arange(-200,800)/45/4/2))+np.random.random(1000)

df = pd.DataFrame({'y': y}, index=x)
plt.plot(df)
plt.plot(df, label='raw data')
plt.plot(df.rolling(10, Center=True).mean(), label='filtered by rolling mean size 10')
plt.legend()

enter image description here

SpghttCd
  • 10,510
  • 2
  • 20
  • 25
  • Guys, thanks to your contribution but I used Savitsky Golay filter and it worked fantastically. U should definitely try that. – Rick Sanchez Jun 11 '18 at 07:32
  • @Rick Sanchez if you have such a fantastic solution: don't just mention it - post it here, so that others can take advantage of that, too. remember: https://stackoverflow.com/help/self-answer – SpghttCd Jun 11 '18 at 07:40
  • I'm sorry, you are right. I'll do it right away – Rick Sanchez Jun 12 '18 at 05:36
  • Turns out I don't have enough reputation, I can't answer my own question. But here you go, https://docs.scipy.org/doc/scipy-0.16.1/reference/generated/scipy.signal.savgol_filter.html – Rick Sanchez Jun 12 '18 at 05:39