1
votes

I am not getting the desired output, it is cutting off the lower freq. no matter if I set it to 'low' or 'high'.

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

def butter_filter(data, butterCutoffFreq, order=1):
    b, a = signal.butter(order, butterCutoffFreq, 'low', analog=True)
    y = signal.lfilter(b, a, data)
    return y

"""
    White noise
"""
N = 1024
dt = np.float64(1)
y = np.random.normal(loc=0.0, scale=1.0, size=N)
t = np.arange(start=0, stop=N, step=dt)
butterCutoffFreq = 0.5 * 1/dt

amps = np.fft.rfft(y)
freqs = np.fft.rfftfreq(y.size, dt)
#plt.plot(freqs, np.abs(amps), color='b')

ampsFiltered = butter_filter(y, butterCutoffFreq)
ampsFiltered = np.fft.rfft(ampsFiltered)

plt.plot(freqs, np.abs(ampsFiltered), color='g')
plt.show()

enter image description here

1
Your code is incomplete, what is dt? Is yWhitenoise the same as y?Dan
@Dan: fixed, tnx!user1581390
Have you tried varying values for dt? Maybe random noise at 1Hz intervals doesn't have nice properties. See if changing the frequency to something significantly higher makes the chart appear more low pass?Dan

1 Answers

0
votes

Your problem is whith the param analog=True remove it and it will work.

lfilter() can only use a digital filter. It doesn't make sense to filter a digital signal (your white noise assumed regularly sampled) with an analog filter.