I want to use the Butterworth filter in python on my data set. I want to plot it and compare it to the original line. This is my code:

import numpy as np
import scipy.io as spio
import scipy as sc
from scipy import signal
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import pandas as pd
import classmax
from numpy.random import randn
from numpy.fft import rfft
from scipy.signal import butter, lfilter
from matplotlib.pyplot import loglog

a = spio.loadmat("Sub21.mat")
b = a['data_mat']

cone = pd.DataFrame(b[0])
cone.columns = ["No experience1 (NREM)", "No experience2 (NREM)", "Dream + Recall3 (NREM)", "Dream + Recall4 (REM)", "No experience5 (NREM)", "No experience6 (NREM)", "No experience7 (NREM)", "Dream + Recall8 (REM)", "Dream + Recall9 (NREM)", "Dream + Recall10 (REM)", "Dream + Recall11 (REM)"]

fs = 500
signala = pd.DataFrame(cone[["Dream + Recall3 (NREM)"]])
#t = np.arange(500) / fs
#plt.plot(t, np.ravel(signala), label="No Experience1")

fc = 50
w = fc / (fs/2)
b, a = signal.butter(2, w)
z, h = signal.freqs(b,a)
plt.semilogx(z, 20 * np.log10(abs(h)))
#output = signal.filtfilt(b, a, np.ravel(signala), axis=0)
#plt.plot(t, output, label = 'filtered')


This code computes a graph but I don't think it is a graph that necessarily corresponds to my data. I think that the graph with its values is just something randomly created through the code I have. Could you tell me how I can adjust this code to get the Butterworth filter to work with the specific data I am giving it? Please be sure to explain throughly what is going on with the code and what the parameters mean seeing as I am still confused here too. Thanks.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.