|
scipy中signal模块之IIR滤波器(低通)的使用
- # -*- coding: utf-8 -*-
- __author__ = u'东方耀 微信:dfy_88888'
- __date__ = '2021/9/17 上午11:41'
- __product__ = 'PyCharm'
- __filename__ = '00_重新理解一下复信号'
- from scipy.fftpack import fft, ifft, fftshift
- import numpy as np
- from scipy import signal
- import random
- import matplotlib.pyplot as plt
- import matplotlib.ticker as mticker
- from matplotlib.font_manager import FontProperties
- font_fname = "/usr/share/fonts/wps-office/simfang.ttf"
- font = FontProperties(fname=font_fname)
- # plt.rcParams['font.sans-serif']=['simfang']#设置作图中文显示
- # scipy中signal模块之IIR滤波器(低通)的使用
- continue_time = 2e-6
- fs = 100e6
- f = [10e6, 30e6, 40e6]
- sample_num = int(np.ceil(continue_time * fs))
- print("每个子脉冲的采样点数(采样速率有关)=", sample_num)
- print("多个频率分量的信号=", f)
- x_time = np.linspace(0, continue_time, sample_num, endpoint=True)
- RCS = 1
- Rx_signal_1 = RCS * np.cos(2 * np.pi * f[0] * x_time)
- Rx_signal_2 = RCS * np.cos(2 * np.pi * f[1] * x_time)
- Rx_signal_3 = RCS * np.cos(2 * np.pi * f[2] * x_time)
- Rx_signal_all = Rx_signal_1 + Rx_signal_2 + Rx_signal_3
- Rx_y_fft_1 = fftshift(fft(Rx_signal_1))
- Rx_y_fft_2 = fftshift(fft(Rx_signal_2))
- Rx_y_fft_3 = fftshift(fft(Rx_signal_3))
- Rx_y_fft_all = fftshift(fft(Rx_signal_all))
- x_f = np.linspace(0, int(fs), sample_num, endpoint=True) - int(fs/2)
- # 开始滤波 何为0相位滤波器?
- # Wn:归一化截止频率。计算公式Wn=2*截止频率/采样频率 截止频率=35e6 20e6
- # b,a: IIR滤波器的分子(b)和分母(a)多项式系数向量。output='ba'
- b, a = signal.butter(N=7, Wn=0.7, btype="lowpass") # N表示滤波器的阶数 7 9 11阶
- print("IIR滤波器的分子b:", len(b), b)
- print("IIR滤波器的分母a:", len(a), a)
- filtedData = signal.filtfilt(b, a, Rx_signal_all) #data为要过滤的信号
- print("滤波之后的数据:", filtedData.shape, filtedData[:3])
- Rx_y_fft_filtedData = fftshift(fft(filtedData))
- # 画图展示
- plt.figure(figsize=(14, 12))
- plt.figure(1)
- plt.subplot(5, 1, 1)
- # plt.plot(np.arange(sample_num), Rx_signal_1, marker='o', markersize=6, markerfacecolor='r')
- # plt.title('信号_时域', fontproperties=font, fontsize=16)
- # plt.xlabel('time/sample_num')
- plt.plot(x_f, np.abs(Rx_y_fft_filtedData), marker='o', markersize=6, markerfacecolor='r')
- plt.title('滤波后_幅度谱', fontproperties=font, fontsize=16)
- plt.xlabel('freq')
- plt.subplot(5, 1, 2)
- plt.plot(x_f, np.abs(Rx_y_fft_1), marker='o', markersize=6, markerfacecolor='b')
- plt.title('信号1_幅度谱', fontproperties=font, fontsize=16)
- # plt.axis([0, 500, -3, 3])
- plt.xlabel('freq')
- plt.subplot(5, 1, 3)
- plt.plot(x_f, np.abs(Rx_y_fft_2), marker='o', markersize=6, markerfacecolor='r')
- plt.title('信号2_幅度谱', fontproperties=font, fontsize=16)
- # plt.axis([0, 500, -3, 3])
- plt.xlabel('freq')
- plt.subplot(5, 1, 4)
- plt.plot(x_f, np.abs(Rx_y_fft_3), marker='o', markersize=6, markerfacecolor='b')
- plt.title('信号3_幅度谱', fontproperties=font, fontsize=16)
- # plt.axis([0, 500, -3, 3])
- plt.xlabel('freq')
- plt.subplot(5, 1, 5)
- plt.plot(x_f, np.abs(Rx_y_fft_all), marker='o', markersize=6, markerfacecolor='r')
- plt.title('信号all_幅度谱', fontproperties=font, fontsize=16)
- # plt.axis([0, 500, -3, 3])
- plt.xlabel('freq')
- plt.show()
复制代码
|
|