Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Savitzky-Golay Filter
#1
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')

Attached Files

.txt   data.txt (Size: 197.96 KB / Downloads: 63)
Reply
#2
1st, change line 22&23 to
frequency.append(data[0])
amplitude.append(data[1])
does TypeError persist?
swallow osama bin laden
Reply
#3
Hello,
Now I get an error "TypeError: expected x and y to have same length"
Reply
#4
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.
swallow osama bin laden
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020