示例#1
0
文件: sound.py 项目: choldgraf/LaSP
def temporal_envelope(s, sample_rate, cutoff_freq=200.0, resample_rate=None):
    """
        Get the temporal envelope from the sound pressure waveform.

        s: the signal
        sample_rate: the sample rate of the signal
        cutoff_freq: the cutoff frequency of the low pass filter used to create the envelope

        Returns the temporal envelope of the signal, with same sample rate or downsampled.
    """

    #rectify
    srect = np.abs(s)
    #low pass filter
    if cutoff_freq is not None:
        srect = lowpass_filter(srect, sample_rate, cutoff_freq, filter_order=4)
        srect[srect < 0] = 0
        
    if resample_rate is not None:
        lensound = len(srect)
        t=(np.array(range(lensound),dtype=float))/sample_rate
        lenresampled = int(round(float(lensound)*resample_rate/sample_rate))
        (srectresampled, tresampled) = resample(srect, lenresampled, t=t, axis=0, window=None)
        return (srectresampled, tresampled)
    else:   
        return srect
示例#2
0
def bandpass_timefreq(s, frequencies, sample_rate):
    """
        Bandpass filter signal s at the given frequency bands, and then use the Hilber transform
        to produce a complex-valued time-frequency representation of the bandpass filtered signal.
    """

    freqs = sorted(frequencies)
    tf_raw = np.zeros([len(frequencies), len(s)], dtype='float')
    tf_freqs = list()

    for k,f in enumerate(freqs):
        #bandpass filter signal
        if k == 0:
            tf_raw[k, :] = lowpass_filter(s, sample_rate, f)
            tf_freqs.append( (0.0, f) )
        else:
            tf_raw[k, :] = bandpass_filter(s, sample_rate,  freqs[k-1], f)
            tf_freqs.append( (freqs[k-1], f) )

    #compute analytic signal
    tf = hilbert(tf_raw, axis=1)
    #print 'tf_raw.shape=',tf_raw.shape
    #print 'tf.shape=',tf.shape

    return np.array(tf_freqs),tf_raw,tf
示例#3
0
# Testing the coherence

import numpy as np
import matplotlib.pyplot as plt

from lasp.coherence import coherence_jn
from lasp.signal import lowpass_filter

# Make two gaussian signals
sample_rate = 1000.0
tlen = 2.0  # 2 second signal

# Make space for both signals
s1 = np.random.normal(0, 1, int(tlen*sample_rate))
s2 = lowpass_filter(s1, sample_rate, 250.0) + np.random.normal(0, 1, int(tlen*sample_rate))


freq1,c_amp,c_var_amp,c_phase,c_phase_var, cohe_unbiased, cohe_se  = coherence_jn(s1, s2, sample_rate, 0.1, 0.05)

plt.figure()

plt.plot(freq1, cohe_unbiased, 'k-', linewidth=2.0, alpha=0.9)
plt.plot(freq1, cohe_unbiased+2*(cohe_se), 'g-', linewidth=2.0, alpha=0.75)
plt.plot(freq1, cohe_unbiased-2*(cohe_se), 'c-', linewidth=2.0, alpha=0.75)

plt.show()