def test_highpassVsPitsa(self): """ Test Butterworth highpass filter against Butterworth highpass filter of PITSA. Note that the corners value is twice the value of the filter sections in PITSA. The rms of the difference between ObsPy and PITSA tends to get bigger with higher order filtering. """ # load test file file = os.path.join(self.path, 'rjob_20051006.gz') f = gzip.open(file) data = np.loadtxt(f) f.close() # parameters for the test samp_rate = 200.0 freq = 10 corners = 4 # filter trace datcorr = highpass(data, freq, df=samp_rate, corners=corners) # load pitsa file file = os.path.join(self.path, 'rjob_20051006_highpass.gz') f = gzip.open(file) data_pitsa = np.loadtxt(f) f.close() # calculate normalized rms rms = np.sqrt( np.sum((datcorr - data_pitsa)**2) / np.sum(data_pitsa**2)) self.assertEqual(rms < 1.0e-05, True)
def test_highpassVsPitsa(self): """ Test Butterworth highpass filter against Butterworth highpass filter of PITSA. Note that the corners value is twice the value of the filter sections in PITSA. The rms of the difference between ObsPy and PITSA tends to get bigger with higher order filtering. """ # load test file file = os.path.join(self.path, 'rjob_20051006.gz') f = gzip.open(file) data = np.loadtxt(f) f.close() # parameters for the test samp_rate = 200.0 freq = 10 corners = 4 # filter trace datcorr = highpass(data, freq, df=samp_rate, corners=corners) # load pitsa file file = os.path.join(self.path, 'rjob_20051006_highpass.gz') f = gzip.open(file) data_pitsa = np.loadtxt(f) f.close() # calculate normalized rms rms = np.sqrt(np.sum((datcorr - data_pitsa) ** 2) / np.sum(data_pitsa ** 2)) self.assertEqual(rms < 1.0e-05, True)
def test_highpassZPHSHVsPitsa(self): """ Test Butterworth zero-phase highpass filter against Butterworth zero-phase highpass filter of PITSA. Note that the corners value is twice the value of the filter sections in PITSA. The rms of the difference between ObsPy and PITSA tends to get bigger with higher order filtering. Note: The Zero-Phase filters deviate from PITSA's zero-phase filters at the end of the trace! The rms for the test is calculated omitting the last 200 samples, as this part of the trace is assumed to generally be of low interest/importance. """ # load test file file = os.path.join(self.path, 'rjob_20051006.gz') f = gzip.open(file) data = np.loadtxt(f) f.close() # parameters for the test samp_rate = 200.0 freq = 10 corners = 2 # filter trace datcorr = highpass(data, freq, df=samp_rate, corners=corners, zerophase=True) # load pitsa file file = os.path.join(self.path, 'rjob_20051006_highpassZPHSH.gz') f = gzip.open(file) data_pitsa = np.loadtxt(f) f.close() # calculate normalized rms rms = np.sqrt(np.sum((datcorr[:-200] - data_pitsa[:-200]) ** 2) / np.sum(data_pitsa[:-200] ** 2)) self.assertEqual(rms < 1.0e-05, True)
def plot_FilteredResult(x, Fs, c, Fr, bw, level, spec=1): from SK_process import raylinv from SK_grid import nextpow2 sig = np.median(np.abs(c)) / np.sqrt(np.pi / 2.) threshold = sig * raylinv(np.array([ .999, ]), np.array([ 1, ])) t = np.arange(len(x)) / float(Fs) tc = np.linspace(t[0], t[-1], len(c)) fig = plt.figure() ax1 = plt.subplot(2 + spec, 1, 1) Fr *= Fs filt = highpass(x, Fr - bw / 2, Fs, corners=2) filt = lowpass(filt, Fr + bw / 2, Fs, corners=2) filt /= np.max(filt) plt.plot(t, x, 'k', label='Original Signal') plt.plot(t, filt, label='Obspy Filtered Signal') plt.plot(tc, np.abs(c), 'r', label='filtered Signal') plt.legend() plt.grid(True) plt.subplot(2 + spec, 1, 2, sharex=ax1) plt.plot(tc, np.abs(c), 'k') plt.axhline(threshold, c='r') for ti in tc[np.where(np.abs(c) >= threshold)[0]]: plt.axvline(ti, c='g', zorder=-1) ax1.axvline(ti, c='g', zorder=-1) plt.xlabel('time [s]') plt.grid(True) if spec == 1: #print nextpow2(len(c)) nfft = int(nextpow2(len(c))) env = np.abs(c)**2 S = np.abs( np.fft.fft( (env.ravel() - np.mean(env)) * np.hanning(len(env)) / len(env), nfft)) f = np.linspace(0, 0.5 * Fs / 2**level, nfft / 2) plt.subplot(313) plt.plot(f, S[:nfft / 2], 'k') plt.title('Fourier transform magnitude of the squared envelope') plt.xlabel('frequency [Hz]') plt.grid(True) plt.tight_layout() plt.show()