Savitzky-Golay Filter - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: Savitzky-Golay Filter (/thread-8645.html) |
Savitzky-Golay Filter - muhsin - Mar-01-2018 Hello, I am trying to filter fourier amplitude using Savitzky-Golay Filter. Can you please help me with the the following code. I get an error saying "TypeError: '_io.TextIOWrapper' object is not subscriptable". The file is attached Thank you import numpy as np import plotly.plotly as py import plotly.graph_objs as go import numpy as np import pandas as pd import scipy from scipy import signal outputFile=r'C:\Users\muhsi\Desktop\data' directory=outputFile+'\\data.txt' frequency=[] amplitude=[] file=open(directory,'r') file.readline() data=file.readline().split() frequency=file[1] amplitude=file[2] trace1 = go.Scatter( x=frequency, y= signal.savgol_filter(amplitude, 53, 3), mode='markers', marker=dict( size=6, color='#C190F0', symbol='triangle' ), name='Savitzky-Golay' ) layout = go.Layout( showlegend=True ) data = [trace1] fig = go.Figure(data=data, layout=layout) py.iplot(fig, filename='smoothing-savitzky-golay-filter') RE: Savitzky-Golay Filter - ka06059 - Mar-02-2018 1st, change line 22&23 to frequency.append(data[0]) amplitude.append(data[1])does TypeError persist? RE: Savitzky-Golay Filter - muhsin - Mar-02-2018 Hello, Now I get an error "TypeError: expected x and y to have same length" RE: Savitzky-Golay Filter - ka06059 - Mar-02-2018 code failed to read & extract infos from text file correctly thats all assume frequency and amplitude is 1st & 2nd column respectively in data.txt. try replace line 18 to 23 with filz=open(directory,'r') while True: text = filz.readline() if text == '': break text = text.split() frequency.append(text[0]) amplitude.append(text[1]) filz.close()best avoid using python built-in func/keywords( 'file' in this case) as variable name. so i named it filz. |