如何修复我的低通滤波器和重采样代码

时间:2019-04-11 13:38:14

标签: python scipy signals frequency

我是信号分析和振动的新手。我有振动传感器,可以在F:65536 Hz处采集数据

我确实为Fn实现了一个低通滤波器:30000 Hz

然后,我对数据进行了重新采样,以设置新的低频。

我的代码中使用的低通滤波器来自以下链接:Creating lowpass filter in SciPy - understanding methods and units

def butter_lowpass(cutoff, fs, order=5):
    nyq = 0.5 * fs
    normal_cutoff = cutoff / nyq
    b, a = butter(order, normal_cutoff, btype='low', analog=False)
    return b, a

def butter_lowpass_filter(data, cutoff, fs, order=5):
    b, a = butter_lowpass(cutoff, fs, order=order)
    y = lfilter(b, a, data)
    return y


# Filter requirements.
order = 6
fs = 65536.0      # sample rate, Hz
cutoff = 15000  # desired cutoff frequency of the filter, Hz

data =d
length= len(d)

# Get the filter coefficients so we can check its frequency response.
b, a = butter_lowpass(cutoff, fs, order)

# Plot the frequency response.
w, h = freqz(b, a, worN=8000)
plt.subplot(2, 1, 1)
plt.plot(0.5*fs*w/np.pi, np.abs(h), 'b')
plt.plot(cutoff, 0.5*np.sqrt(2), 'ko')
plt.axvline(cutoff, color='k')
plt.xlim(0, 0.5*fs)
plt.title("Lowpass Filter Frequency Response")
plt.xlabel('Frequency [Hz]')
plt.grid()


# Demonstrate the use of the filter.
T = length * (1/ fs)         # seconds 
n = int(T * fs) # total number of samples
t = np.linspace(0, T, n, endpoint=False)


# Filter the data, and plot both the original and filtered signals.
y = butter_lowpass_filter(data, cutoff, fs, order)

plt.figure(figsize=(20,10))
plt.subplot(211)
plt.plot(t, data, 'b-', label='data')
plt.title('Sensor Data')
plt.subplot(212)
plt.plot(t, y, 'g-', linewidth=2, label='filtered data')
plt.title('filtered signal')
plt.show()

f = signal.resample (y,50000) #there is a mistake here try asking for help to figure out how this really works 

length= len(d)
fs = 65536.0
T = length * (1/ fs)         # seconds 
n = int(T * length) # total number of samples
t = np.linspace(0, T, 50000, endpoint=False)

xnew = np.linspace(0,1000,50000,endpoint=False)

plt.figure(figsize=(20,10))
plt.plot(t, f, 'o-', linewidth=2, label='filtered data')
plt.title('Filtered -Resampled signal')
plt.show()


0 个答案:

没有答案