Beispiel #1
0
def kaiser_windows_filter(tr,
                          isotr,
                          periods,
                          window=('kaiser', 9.0),
                          deltaT=PERIOD_RESAMPLE,
                          debug=False):
    """
    Apply FIR filter with kaiser window

    :type tr: `obspy.trace`
    :param tr: trace contains raw waveform
    :type isotr: numpy matrix
    :param isotr: matrix contains isolated waveform
    :type periods: numpy array
    :param periods: contains various periods
    :type window: tuple
    :param window: window for setting kaiser filter
    :type deltaT: int or float
    :param deltaT: time interval
    """
    conv_result = np.zeros(shape=(len(periods), len(tr.data)))

    nyq = tr.stats.sampling_rate / 2.0
    ntaps = 2**math.ceil(math.log(tr.stats.sac.npts, 2))
    if debug:
        print(nyq, ntaps)
    for iperiod, T0 in enumerate(periods):
        lowcut = 1.0 / (T0 + 0.2 * deltaT / 2.0)
        highcut = 1.0 / (T0 - 0.2 * deltaT / 2.0)
        width = (highcut - lowcut) / 2.0
        window = firwin(ntaps, [lowcut, highcut],
                        width=width,
                        window=window,
                        nyq=nyq,
                        pass_zero=False)
        conv_result[iperiod, :] = sig_convolve(isotr[iperiod, :],
                                               window,
                                               mode='same')
    logger.info("Kaiser window filter applied Suc.!")
    return conv_result
Beispiel #2
0
freq_signal_4096 = np.linspace(-np.pi, np.pi, len(signal1_Response_4096))

from scipy import signal
from scipy.signal import convolve as sig_convolve

## create window
# SIZE OF WINDOW IS IMPORTANT
window_Beta0_L64 = signal.kaiser(64, beta=0)

window_Beta0_L64_Response = getFFT(window_Beta0_L64)
window_Beta0_L64_Response_4096 = getFFT(window_Beta0_L64)
freq_win = np.linspace(-np.pi, np.pi, len(window_Beta0_L64_Response))

## convolve in freq domain the fft of the signal and fft of the window
# 2048
conv1_L64 = sig_convolve(signal1_Response_2048, window_Beta0_L64_Response)
conv2_L64 = sig_convolve(signal2_Response_2048, window_Beta0_L64_Response)
freq_conv_L64 = np.linspace(-np.pi, np.pi, len(conv1_L64))

#4096
conv1_L64_4096 = sig_convolve(signal1_Response_4096,
                              window_Beta0_L64_Response_4096)
conv2_L64_4096 = sig_convolve(signal2_Response_4096,
                              window_Beta0_L64_Response_4096)
freq_conv_L64_4096 = np.linspace(-np.pi, np.pi, len(conv1_L64_4096))

## Plots
# FFT of input signal
plt.figure(1)
plt.title('Input Signal: FFT')
plt.xlabel('omega')
Beispiel #3
0
fftconv_time = []
conv1d_time = []
lfilt_time = []

diff_list = []
diff2_list = []
diff3_list = []

ntaps_list = 2**np.arange(2, 14)

for ntaps in ntaps_list:
    # Create a FIR filter.
    b = firwin(ntaps, [0.05, 0.95], width=0.05, pass_zero=False)
    # Signal convolve.
    tstart = time.time()
    conv_result = sig_convolve(x, b[np.newaxis, :], mode="valid")
    conv_time.append(time.time() - tstart)
    # --- numpy.convolve ---
    tstart = time.time()
    npconv_result = np.array([np_convolve(xi, b, mode="valid") for xi in x])
    npconv_time.append(time.time() - tstart)
    # fft convolve (fast fourier transform) convolution.
    tstart = time.time()
    fftconv_result = fftconvolve(x, b[np.newaxis, :], mode="valid")
    fftconv_time.append(time.time() - tstart)
    # 1-dimensional (one-dimensional) convolution.
    tstart = time.time()
    # convolve1d doesn"t have a "valid" mode, so we expliclity slice out
    # the valid part of the result.
    conv1d_result = convolve1d(x, b)[:, (len(b) - 1) // 2:-(len(b) // 2)]
    conv1d_time.append(time.time() - tstart)
                                              [dpass, dstop],
                                              Hz=1.0)
bands *= 2.0  # above function outputs frequencies normalized from 0.0 to 0.5
#  use this tool to design a Parks-McClellan lowpass filter using pre-specified design parameters
b = scipy.signal.remez(numtaps, bands, amps, weights, Hz=2.0)

# FREQUENCY DOMAIN
# Frequency Response of filter
w_axis, h = scipy.signal.freqz(b)

# compute frequency domain convolutions
fftconv_result = fftconvolve(signal, b, mode='full')

# TIME DOMAIN
# compute time domain convolution
conv_result = sig_convolve(signal, b, mode='full')

### TIME DOMAIN PLOTS
# Plots input signal
plt.figure(1)
#plt.plot(t[:400], signal[:400], 'b');
plt.plot(t, signal, 'b')
plt.title('Input Signal')
plt.xlabel('Time')
plt.ylabel('Amplitude')
plt.ylim(min(signal), max(signal))
plt.show()

## Filter Plots
# plot impulse response of filter
plt.figure(2)
diff_list = []
diff2_list = []
diff3_list = []

ntaps_list = 2 ** np.arange(2, 13)

for ntaps in ntaps_list:
    # Create a FIR filter.
    b = firwin(ntaps, [0.05, 0.95], width=0.05, pass_zero=False)

    if ntaps <= 2 ** 9:
        # --- signal.convolve ---
        # We know this is slower than the others when ntaps is
        # large, so we only compute it for small values.
        tstart = time.time()
        conv_result = sig_convolve(x, b[np.newaxis, :], mode='valid')
        conv_time.append(time.time() - tstart)

    # --- numpy.convolve ---
    tstart = time.time()
    npconv_result = np.array([np_convolve(xi, b, mode='valid') for xi in x])
    npconv_time.append(time.time() - tstart)

    # --- signal.fftconvolve ---
    tstart = time.time()
    fftconv_result = fftconvolve(x, b[np.newaxis, :], mode='valid')
    fftconv_time.append(time.time() - tstart)

    # --- convolve1d ---
    tstart = time.time()
    # convolve1d doesn't have a 'valid' mode, so we expliclity slice out
Beispiel #6
0
def gen_convolve_scipy (input, kernel, output=None, accumulate=False):
    y = sig_convolve (input, kernel, 'valid')
    if output is None: output    = y
    elif accumulate:   output   += y
    else:              output[:] = y
    return output