如何制作高通滤波器?
how to make a high pass filter?
我有一个海平面数据(时间、y、x)的 3D 数据矩阵,我通过 FFT 的平方找到了功率谱,但低频确实占主导地位。我想通过应用高通滤波器来去除那些低频……我该怎么做呢?
数据集和 structure/code 的示例如下:
这是数据集和创建数组:
Yearmin = 2018
Yearmax = 2019
year_len = Yearmax - Yearmin + 1.0 # number of years
direcInput = "filepath"
a = s.Dataset(direcInput+"test.nc", mode='r')
#creating arrays
lat = a.variables["latitude"][:]
lon = a.variables["longitude"][:]
time1 = a.variables["time"][:] #DAYS SINCE JAN 1ST 1950
sla = a.variables["sla"][:,:,:] #t, y, x
time = Yearmin + (year_len * (time1 - np.min(time1)) / ( np.max(time1) - np.min(time1)))
#detrending and normalizing data
def standardize(y, detrend = True, normalize = True):
if detrend == True:
y = signal.detrend(y, axis=0)
y = (y - np.mean(y, axis=0))
if normalize == True:
y = y / np.std(y, axis=0)
return y
sla_standard = standardize(sla)
print(sla_standard.shape) = (710, 81, 320)
#fft
fft = np.fft.rfft(sla_standard, axis=0)
spec = np.square(abs(fft))
frequencies = (0, nyquist, df)
#PLOTTING THE FREQUENCIES VS SPECTRUM FOR A FEW DIFFERENT SPATIAL LOCATIONS
plt.plot(frequencies, spec[:, 68,85])
plt.plot(frequencies, spec[:, 23,235])
plt.plot(frequencies, spec[:, 39,178])
plt.plot(frequencies, spec[:, 30,149])
plt.xlim(0,.05)
plt.show()
我的目标是对原始时间序列 (sla_standard) 进行高通滤波,以去除两个非常大的峰值。我应该使用哪种类型的过滤器?谢谢!
使用.axes.Axes.set_ylim
设置y轴范围。
Axes.set_ylim(self, left=None, right=None, emit=True, auto=False, *, ymin=None, ymax=None)
所以在你的情况下 ymin=None
并且你在开始绘图之前将 ymax 例如设置为 ymax=60000
。
因此plt.ylim(ymin=None, ymax=60000)
.
不应该在这里取出数据,因为它"falsifying results"。您真正想要的是放大图表。如果没有事先通知,独立于您阅读图表的人会错误地解释数据。超出图表的峰值是可以的,因为每个人都明白这一点。
或:
直接替换数组中的某些值(arr):
arr[arr > ori] = dest
例如你的情况 ori=60000
和 dest=1
所有大于 60k 的“>”值都替换为 1。
不同的滤波器: 正如您所说,滤波器作用于信号的频率。存在不同的滤波器形状,其中一些具有复杂的表达式,因为它们需要在实时处理(因果)中实现。但是在您的情况下,您似乎 post 处理数据。您可以使用需要所有数据(非因果关系)的傅里叶变换。
要选择的滤波器: 因此,您可以通过在频率上应用掩码来直接在傅立叶域中执行滤波操作。如果你想删除频率,我建议你使用由 0 和 1 组成的二进制掩码。为什么?因为它是您能想到的最简单的过滤器。声明您完全删除了一些频率(说出来并证明它是合理的)在科学上是相关的。然而,更难说你让一些衰减了一些,并且你任意选择了衰减因子...
Python实施
signal_fft = np.fft.rfft(sla_standard,axis=0)
mask = np.ones_like(sla_standard)
mask[freq_to_filter,...] = 0.0 # define here the frequencies to filter
filtered_signal = np.fft.irfft(mask*signal_fft,axis=0)
我有一个海平面数据(时间、y、x)的 3D 数据矩阵,我通过 FFT 的平方找到了功率谱,但低频确实占主导地位。我想通过应用高通滤波器来去除那些低频……我该怎么做呢? 数据集和 structure/code 的示例如下:
这是数据集和创建数组:
Yearmin = 2018
Yearmax = 2019
year_len = Yearmax - Yearmin + 1.0 # number of years
direcInput = "filepath"
a = s.Dataset(direcInput+"test.nc", mode='r')
#creating arrays
lat = a.variables["latitude"][:]
lon = a.variables["longitude"][:]
time1 = a.variables["time"][:] #DAYS SINCE JAN 1ST 1950
sla = a.variables["sla"][:,:,:] #t, y, x
time = Yearmin + (year_len * (time1 - np.min(time1)) / ( np.max(time1) - np.min(time1)))
#detrending and normalizing data
def standardize(y, detrend = True, normalize = True):
if detrend == True:
y = signal.detrend(y, axis=0)
y = (y - np.mean(y, axis=0))
if normalize == True:
y = y / np.std(y, axis=0)
return y
sla_standard = standardize(sla)
print(sla_standard.shape) = (710, 81, 320)
#fft
fft = np.fft.rfft(sla_standard, axis=0)
spec = np.square(abs(fft))
frequencies = (0, nyquist, df)
#PLOTTING THE FREQUENCIES VS SPECTRUM FOR A FEW DIFFERENT SPATIAL LOCATIONS
plt.plot(frequencies, spec[:, 68,85])
plt.plot(frequencies, spec[:, 23,235])
plt.plot(frequencies, spec[:, 39,178])
plt.plot(frequencies, spec[:, 30,149])
plt.xlim(0,.05)
plt.show()
我的目标是对原始时间序列 (sla_standard) 进行高通滤波,以去除两个非常大的峰值。我应该使用哪种类型的过滤器?谢谢!
使用.axes.Axes.set_ylim
设置y轴范围。
Axes.set_ylim(self, left=None, right=None, emit=True, auto=False, *, ymin=None, ymax=None)
所以在你的情况下 ymin=None
并且你在开始绘图之前将 ymax 例如设置为 ymax=60000
。
因此plt.ylim(ymin=None, ymax=60000)
.
不应该在这里取出数据,因为它"falsifying results"。您真正想要的是放大图表。如果没有事先通知,独立于您阅读图表的人会错误地解释数据。超出图表的峰值是可以的,因为每个人都明白这一点。
或:
直接替换数组中的某些值(arr):
arr[arr > ori] = dest
例如你的情况 ori=60000
和 dest=1
所有大于 60k 的“>”值都替换为 1。
不同的滤波器: 正如您所说,滤波器作用于信号的频率。存在不同的滤波器形状,其中一些具有复杂的表达式,因为它们需要在实时处理(因果)中实现。但是在您的情况下,您似乎 post 处理数据。您可以使用需要所有数据(非因果关系)的傅里叶变换。
要选择的滤波器: 因此,您可以通过在频率上应用掩码来直接在傅立叶域中执行滤波操作。如果你想删除频率,我建议你使用由 0 和 1 组成的二进制掩码。为什么?因为它是您能想到的最简单的过滤器。声明您完全删除了一些频率(说出来并证明它是合理的)在科学上是相关的。然而,更难说你让一些衰减了一些,并且你任意选择了衰减因子...
Python实施
signal_fft = np.fft.rfft(sla_standard,axis=0)
mask = np.ones_like(sla_standard)
mask[freq_to_filter,...] = 0.0 # define here the frequencies to filter
filtered_signal = np.fft.irfft(mask*signal_fft,axis=0)