Example #1
0
def timefreq_fft ( x, **kwargs ):
    '''Labeled analogue of scipy.signal.spectrogram; runs along x's 'time' axis.

    Inputs:
    x - LabeledArray containing the data; must have a 'time' axis, and probably
        shouldn't have a 'frequency' axis
    **kwargs - (Optional) Keyword arguments passed to spectrogram

    Output is a LabeledArray with the same axes as x except for:
        'time' - Determined by the window properties; uses the values returned
            by spectrogram
        'frequency' - Added based on the values returned by spectrogram
    '''
    
    # Make sure we don't override axis in the kwargs
    kwargs.pop( 'axis', None )

    time_axis = x.axis_index( 'time' )
    other_axes = [ k
                   for k in x.axes
                   if not k == 'time' ]
    
    ## Compute spectrograms
    
    # Test with first sample along each non-time axis
    test_slice = functools.reduce( lambda a, b : a + b,
                                  tuple( zip( other_axes,
                                              ( 0 for axis in other_axes ) ) ) )
    f_spec, t_spec, ret_test = sig.spectrogram( x[test_slice], **kwargs )
    
    # Form new axes
    ret_axes = OrderedDict( x.axes )
    ret_axes['time'] = t_spec + x.axes['time'][0]
    ret_axes.move_to_end( 'time', last = True )
    ret_axes['frequency'] = f_spec
    ret_axes.move_to_end( 'frequency', last = False )
    # Allocate full result
    ret = LabeledArray( axes = ret_axes )
    
    # Compute for each trial
    for x_cur, i in x.iter_over( other_axes, return_index = True ):
        cur_slice = functools.reduce( lambda a, b : a + b,
                                      tuple( zip( other_axes,
                                                  i ) ) )
        f_cur, t_cur, ret_cur = sig.spectrogram( x_cur.array, **kwargs )
        ret[cur_slice] = ret_cur
    
    return ret
Example #2
0
def spectrogram_scores(pattern_chunk, chan_sound, candidates):
    s_f = chan_sound.s_f
    n_window = 256
    n_overlap = 192
    sigma = 1. / 1000. * s_f

    spectrogram_kwargs = {'nperseg': n_window,
                          'noverlap': n_overlap,
                          'window': sg.gaussian(n_window, sigma),
                          'scaling': 'spectrum'}

    pattern_spectrogram = spectrogram(pattern_chunk.data[:, 0], s_f, **spectrogram_kwargs)

    logger.info('Getting spectrogram difference score for {} candidates'.format(len(candidates.index)))
    for (i, start) in enumerate(candidates['start'][:]):
        logger.debug('Start {0}: {1}'.format(i, start))
        motif_start = start
        series = chan_sound.get_chunk(motif_start, motif_start + pattern_chunk.samples)
        # f, t, sxx = spectrogram(bandpass_filter(series[:, 0], s_f), s_f, **spectrogram_kwargs)

        candidates.set_value(i, 'spectral_diff',
                             spectrogram_diff(series[:, 0],
                                              pattern_spectrogram[2],
                                              s_f,
                                              spectrogram_kwargs)
                             )
Example #3
0
    def process_data(self):
        """makes the spectrigrams and adds it to data"""
        self.frames_processed = 0
        window = self.window
        self.window = []

        f, t, spectrogram = signal.spectrogram(window, 30, nperseg=10, nfft=50, noverlap=5)
        # if self.show_spectrograms:
        # plt.pcolormesh(t, f, spectrogram)
        # plt.ylabel('Frequency [Hz]')
        # plt.xlabel('Time [sec]')
        # plt.show()

        # heartrate_for_window = self.heartrates[0:4].mean()
        heartrate_for_window = self.heartrates[0:4]

        # saftey measure to make sure that ffmpeg doesnt analyse a slice too much
        if len(heartrate_for_window) != TIME_SECOND_WINDOW:
            return

        self.heartrates = self.heartrates[4:]
        if self.data is None:
            self.data = (np.array([spectrogram]), np.array([heartrate_for_window]), [window])
        else:
            (spectrograms, heartrates, time_series) = self.data
            spectrograms = np.append(spectrograms, [spectrogram], axis=0)
            heartrates = np.append(heartrates, [heartrate_for_window])
            time_series += [self.window]
            self.data = (spectrograms, heartrates, time_series)
Example #4
0
    def spectrogram(self, width, overlap=0, **kwargs):
        """
        Computes the spectrogram (short-time Fourier transform) of the signal. This method uses
        the function :func:`scipy.signal.spectrogram`.

        Parameters
        ----------
        width : float
            Substitute for the argument *nperseg* in the function :func:`scipy.signal.spectrogram`.
            Here, *width* has same units as *index*.

        overlap : float, optional
            Substitute for the argument *noverlap* in the function :func:`scipy.signal.spectrogram`.
            Here, *overlap was same units as *index*.

        Returns
        -------
        : Signal2D
            A Signal2D class representing the

        Note
        ----
        For other supported keyword arguments, see the documentation of
        :func:`scipy.signal.spectrogram`. However, the two arguments *nperseg* and *noverlap*
        should not be used.
        """
        nperseg = int(width * self.fs)
        nol = int(overlap * self.fs)
        f, t, S = spectrogram(self.values, fs=self.fs, nperseg=nperseg, noverlap=nol,
                              mode='magnitude', **kwargs)
        return Signal2D(S, index=f, columns=t)
Example #5
0
def generate_chirp_spectrogram_plot():
    fs = 10e3
    N = 1e5
    amp = 2 * np.sqrt(2)
    noise_power = 0.001 * fs / 2
    time = np.arange(N) / fs
    freq = np.linspace(1e3, 2e3, N)
    x = amp * chirp(time, 1e3, 2.0, 6e3, method='quadratic') + \
        np.random.normal(scale=np.sqrt(noise_power), size=time.shape)

    f, t, Sxx = spectrogram(x, fs)

    ax = plt.subplot(211)
    ax.pcolormesh(t, f, Sxx)
    ax.set_ylabel('Frequency [Hz]')
    ax.set_xlabel('Time [sec]')

    f, t, Sxx = spectrogram_lspopt(x, fs, c_parameter=20.0)

    ax = plt.subplot(212)
    ax.pcolormesh(t, f, Sxx)
    ax.set_ylabel('Frequency [Hz]')
    ax.set_xlabel('Time [sec]')

    plt.show()
def plot_spectogram(data_in, fs):

    f, t, Sxx = sp.spectrogram(data_in, fs, nperseg=50, noverlap=49)
    plt.pcolormesh(t, f, Sxx)
    plt.ylabel('Frequency [Hz]')
    plt.xlabel('Time [sec]')
    plt.show()
def spectrograms(D, p_local, p_global):
    if p_local['eog_in']:
        D = D[p_global['eeg_chans'], :]
    C = D.shape[0]
    T = D.shape[1]
    for c in range(C)[:3]:
        f, t, Sxx = spectrogram(D[c, :], p_global['sample_freq'])
        sns.heatmap(np.log(Sxx[::-1, :]), xticklabels = t.astype(int),
            yticklabels = f.astype(int)[::-1])

        # There is probably a better way to do this
        for label in plt.gca().get_xticklabels():
            label.set_visible(False)
        for label in plt.gca().get_xticklabels()[::Sxx.shape[1] / 6]:
            label.set_visible(True)
        for label in plt.gca().get_yticklabels():
            label.set_visible(False)
        for label in plt.gca().get_yticklabels()[::Sxx.shape[0] / 6]:
            label.set_visible(True)
        cbar = plt.gca().collections[0].colorbar
        plt.title('Spectrogram for channel ' + str(c + 1))
        plt.xlabel('Time in seconds')
        plt.ylabel('Frequency')
        cbar.set_label(r"$\log(\hat{f})$", labelpad=20, rotation=270)
        path = p_global['plot_folders']['spectrogram_dir'] \
               + '/' + 'spectrogram-%03d' % (c + 1)
	if p_global['plotting']['notebook']:
	    show_and_close()
	else:
	    save_and_close(path, p_local)
Example #8
0
def lofar(data, fs, n_pts_fft=1024, n_overlap=0,
    decimation_rate=3, spectrum_bins_left=None, **tpsw_args):
    norm_parameters = {'lat_window_size': 10, 'lat_gap_size': 1, 'threshold': 1.3}
    if not isinstance(data, np.ndarray):
        raise NotImplementedError

    if decimation_rate > 1:
        dec_data = decimate(data, decimation_rate, 10, 'fir', zero_phase=True)
        Fs = fs/decimation_rate
    else:
        dec_data = data
        Fs=fs
    freq, time, power = spectrogram(dec_data,
                                    window=('hann'),
                                    nperseg=n_pts_fft,
                                    noverlap=n_overlap,
                                    nfft=n_pts_fft,
                                    fs=Fs,
                                    detrend=False,
                                    axis=0,
                                    scaling='spectrum',
                                    mode='magnitude')
    power = np.absolute(power)
    power = power / tpsw(power)#, **tpsw_args)
    power = np.log10(power)
    power[power < -0.2] = 0

    if spectrum_bins_left is None:
        spectrum_bins_left = power.shape[0]*0.8
    power = power[:spectrum_bins_left, :]
    freq = freq[:spectrum_bins_left]

    return np.transpose(power), freq, time
Example #9
0
def plot_spectrogram(raw_data, nfft, fs, channel_bottom, print_frequency_graph):
    data_shape = raw_data.shape

    print("Generating spectrogram...")
    plt_num = 1
    plt.clf()
    plt.figure(1)

    channel_data = []
    for i in range(0, data_shape[1]):
        plt.subplot(8, 2, plt_num)

        f, t, Sxx = signal.spectrogram(x=raw_data[:, i], nfft=nfft, fs=fs, noverlap=127, nperseg=128,
                                       scaling='density')  # returns PSD power per Hz
        plt.pcolormesh(t, f, Sxx)

        plt.xlabel('Time (sec)')
        plt.ylabel('Frequency (Hz)')
        plt.title('Channel %s' % (i + channel_bottom))
        plt_num += 1
        channel_data.append([f, t, Sxx])
        print("\tChannel %d spectrogram generated" % i)
    if print_frequency_graph:
        plt.show()
    return channel_data
Example #10
0
    def set_data(self, data, sf=1., method='fourier', nperseg=256, f_min=1.,
                 f_max=160., f_step=1., baseline=None, norm=None,
                 n_window=None, overlap=0., window=None, c_parameter=20,
                 clim=None, cmap='viridis', vmin=None, under=None, vmax=None,
                 over=None):
        """Compute TF and set data to the ImageObj."""
        # ======================= CHECKING =======================
        assert isinstance(data, np.ndarray) and data.ndim == 1
        assert isinstance(sf, (int, float))
        assert method in ('fourier', 'wavelet', 'multitaper')
        if not isinstance(window, str):
            window = 'hamming' if method is 'fourier' else 'flat'
        assert 0. <= overlap < 1.
        # Wavelet args :
        assert isinstance(f_min, (int, float))
        assert isinstance(f_max, (int, float))
        assert isinstance(f_step, (int, float))
        # Spectrogram and Multi-taper args :
        noverlap = int(round(overlap * nperseg))
        assert isinstance(nperseg, int)
        assert isinstance(c_parameter, int)

        # Update color arguments :
        self._update_cbar_args(cmap, clim, vmin, vmax, under, over)
        logger.info("    Compute time-frequency decomposition using the"
                    " %s method" % method)

        if method == 'fourier':
            freqs, time, tf = spectrogram(data, sf, nperseg=nperseg,
                                          noverlap=noverlap, window=window)
        if method == 'wavelet':
            n_pts = len(data)
            freqs = np.arange(f_min, f_max, f_step)
            time = np.arange(n_pts) / sf
            tf = np.zeros((len(freqs), n_pts), dtype=data.dtype)
            # Compute TF and inplace normalization :
            logger.info("    Compute the time-frequency map ("
                        "normalization=%r)" % norm)
            for i, k in enumerate(freqs):
                tf[i, :] = np.square(np.abs(morlet(data, sf, k)))
            normalization(tf, norm=norm, baseline=baseline, axis=1)

            # Averaging :
            if isinstance(n_window, int):
                logger.info("    Averaging time-frequency map using windows of"
                            " size %i with a %f overlap" % (n_window, overlap))
                kw = dict(overlap=overlap, window=window)
                tf = averaging(tf, n_window, axis=1, **kw)
                time = averaging(time, n_window, **kw)
        elif method == 'multitaper':
            is_lspopt_installed(raise_error=True)
            from lspopt import spectrogram_lspopt
            freqs, time, tf = spectrogram_lspopt(data, sf, nperseg=nperseg,
                                                 noverlap=noverlap,
                                                 c_parameter=c_parameter)

        # Set data to the image object :
        ImageObj.set_data(self, tf, xaxis=time, yaxis=freqs,
                          **self.to_kwargs())
Example #11
0
    def get_spectrogram(self, data_dict):
        #TODO
        from scipy import signal
        spectrogram_dict = OrderedDict()

        for _, data in data_dict.items():
            f, t, Sxx = signal.spectrogram(data, fs=1)
            plt.pcolormesh(t, f, Sxx)
        return spectrogram_dict
Example #12
0
    def test_short_data(self):
        x = np.random.randn(1024)
        fs = 1.0

        #for string-like window, input signal length < nperseg value gives
        #UserWarning, sets nperseg to x.shape[-1]
        f, _, p = spectrogram(x, fs, window=('tukey',0.25))  # default nperseg
        with suppress_warnings() as sup:
            sup.filter(UserWarning,
                       "nperseg = 1025 is greater than input length  = 1024, using nperseg = 1024")
            f1, _, p1 = spectrogram(x, fs, window=('tukey',0.25),
                                    nperseg=1025)  # user-specified nperseg
        f2, _, p2 = spectrogram(x, fs, nperseg=256)  # to compare w/default
        f3, _, p3 = spectrogram(x, fs, nperseg=1024)  # compare w/user-spec'd
        assert_allclose(f, f2)
        assert_allclose(p, p2)
        assert_allclose(f1, f3)
        assert_allclose(p1, p3)
 def calculates_spectrogram(data):
     nperseg = int(round(WINDOW_SIZE_MS * SAMPLE_RATE / 1e3))
     noverlap = int(round(WINDOW_STRIDE_MS * SAMPLE_RATE / 1e3))
     freqs, times, spec = signal.spectrogram(
         data, fs=SAMPLE_RATE, window='hann', nperseg=nperseg, noverlap=noverlap, detrend=False)
     spec = np.log(spec.T.astype(np.float32) + EPS)
     spec = (spec - np.min(spec)) / (np.max(spec) - np.min(spec) + EPS)
     spec = (spec - 0.5) * 2
     return spec
Example #14
0
    def test_short_data(self):
        x = np.random.randn(1024)
        fs = 1.0

        #for string-like window, input signal length < nperseg value gives
        #UserWarning, sets nperseg to x.shape[-1]
        with warnings.catch_warnings():
            warnings.simplefilter('ignore', UserWarning)
            f, _, p = spectrogram(x, fs,
                                  window=('tukey',0.25))  # default nperseg
            f1, _, p1 = spectrogram(x, fs, window=('tukey',0.25),
                                    nperseg=1025)  # user-specified nperseg
        f2, _, p2 = spectrogram(x, fs, nperseg=256)  # to compare w/default
        f3, _, p3 = spectrogram(x, fs, nperseg=1024)  # compare w/user-spec'd
        assert_allclose(f, f2)
        assert_allclose(p, p2)
        assert_allclose(f1, f3)
        assert_allclose(p1, p3)
Example #15
0
    def test_window_external(self):
        x = np.random.randn(1024)

        fs = 1.0
        window = ('tukey', 0.25)
        nperseg = 16
        noverlap = 2
        f, _, P = spectrogram(x, fs, window, nperseg, noverlap)

        win = signal.get_window(('tukey', 0.25), 16)
        fe, _, Pe = spectrogram(x, fs, win, nperseg=None, noverlap=2)
        assert_array_equal(fe.shape, (9,))  # because win length used as nperseg
        assert_array_equal(Pe.shape, (9,73))
        assert_raises(ValueError, spectrogram, x,
                      fs, win, nperseg=8)  # because nperseg != win.shape[-1]
        win_err = signal.get_window(('tukey', 0.25), 2048)
        assert_raises(ValueError, spectrogram, x,
                      fs, win_err, nperseg=None)  # win longer than signal
Example #16
0
	def get_spectrogram(self, at_second, for_seconds=4,channel=0):
		self.__load_data__()
		window_start = at_second * self.sample_rate	
		window_size = for_seconds * self.sample_rate
		f, t, Sxx = signal.spectrogram(self.audio_data[window_start:window_start + window_size,0],
					  self.sample_rate,
					  nperseg = self.freqency_resolution,
					  ) 
				#to decibel
		return f, t, 20.*np.log10(np.abs(Sxx)/10e-6)
Example #17
0
def loadfeature(method=None):
    if method is 'tbm':
        wav = Wav('./query.wav')
        f, t, Sxx = spectrogram(wav.audio, wav.sample_rate)
        tbmatrix = TransciptionMatrix()
        tbmatrix.quantize(f, t, Sxx)
        tbmatrix.normalize() 
        return np.sum(tbmatrix.mat, axis=0)
    elif method is 'onset':
        os.system('./Pre-Processing/SuperFlux.py --det_suffix .txt query.wav')
        return np.loadtxt('query.txt')
Example #18
0
def log_spectrogram(audio, sample_rate, window_size=20,
                    step_size=10, eps=1e-10):
    nperseg = int(round(window_size * sample_rate / 1e3))
    noverlap = int(round(step_size * sample_rate / 1e3))
    freqs, times, spec = signal.spectrogram(audio,
                                            fs=sample_rate,
                                            window='hann',
                                            nperseg=nperseg,
                                            noverlap=noverlap,
                                            detrend=False)
    return freqs, times, np.log(spec.T.astype(np.float32) + eps)
Example #19
0
def spectrogram2(mysignal):

    fs=1e3
    NFFT=int(fs*0.08) # 60ms windows length 
    noverlap=int(fs*0.04) # window overlap
    maxfreq=100
    minfreq=0

    f, t, Sxx = signal.spectrogram(mysignal,fs,window='hann',nperseg=NFFT,noverlap=noverlap,scaling='density',detrend='constant')
    Sxx = Sxx[np.logical_and((f >= minfreq),(f <= maxfreq))]
    return Sxx
Example #20
0
def generate_spectrogram(signal, Fs, tr_length, noverlap=0, bins_per_octave = 20*12, freq_min = 200 ,decibels=True):
    
    # window size is 1 TR x samples per seconds
    win = Fs*tr_length
    
    # find num freqs
    nfft=win
    
    # get spectrum
    freqs, times, spec = spectrogram(signal, Fs, nperseg=win, noverlap=noverlap, nfft=nfft)
    
    if decibels:
        
        # Ratio between adjacent frequencies in log-f axis
        fratio = 2**(1/bins_per_octave)
        
        # How manp.ny bins in log-f axis
        nbins = np.floor( np.log((Fs/2)/freq_min) / np.log(fratio) )
        
        # Freqs corresponding to each bin in FFT
        fftfrqs = np.arange(nfft/2)*(Fs/nfft)
        nfftbins = nfft/2;
        
        # Freqs corresponding to each bin in log F output
        logffrqs = freq_min * np.exp(np.log(2)*np.arange(nbins)/bins_per_octave);
        
        # Bandwidths of each bin in log F
        logfbws = logffrqs * (fratio - 1)
        
        ovfctr = 0.5475;   # Adjusted by hand to make sum(mx'*mx) close to 1.0
        
        # Weighting matrix mapping energy in FFT bins to logF bins
        # is a set of Gaussian profiles depending on the difference in 
        # frequencies, scaled by the bandwidth of that bin
        A = np.tile(logffrqs,(nfftbins,1)).T
        B = np.tile(fftfrqs,(nbins,1))
        C = np.tile(ovfctr*logfbws,(nfftbins,1)).T
        freqdiff = ( A - B )/C;
        
        
        # % Normalize rows by sqrt(E), so multiplying by mx' gets approx orig spectrum back
        mx = np.exp( -0.5*freqdiff**2 );
        D = np.sqrt(2*np.sum(mx**2,1))
        E = mx / np.tile(D, (nfftbins,1)).T
        Sx = spec.copy()
        Px = np.matrix(spec[1::])
        spec = np.array(np.matrix(mx) * Px)
        
        # output
        times = np.arange(spec.shape[-1])
        freqs = np.log2(logffrqs)/np.log2(10)
        
    return spec, freqs, times
Example #21
0
def spectrogram_diff(x, pattern_sxx, s_f, spectrogram_kwargs):
    """
    Get the difference between the spectrogram of a vector and a reference spectrogram
    :param x: vector
    :param pattern_sxx: spectrogram (as in the output of spectrogram(y), where y is the same length as x
    :param s_f: sampling rate
    :param spectrogram_kwargs: arguments for function spectrogram (the same as were used to get spectrogram pattern_sxx
    :return: a scalar comparison of the spectrograms.
    """
    f, t, sxx = spectrogram(x, s_f, **spectrogram_kwargs)
    assert (sxx.shape == pattern_sxx.shape)
    return compare_spectrogram(np.log(sxx), np.log(pattern_sxx))
Example #22
0
    def test_average_all_segments(self):
        x = np.random.randn(1024)

        fs = 1.0
        window = ('tukey', 0.25)
        nperseg = 16
        noverlap = 2

        f, _, P = spectrogram(x, fs, window, nperseg, noverlap)
        fw, Pw = welch(x, fs, window, nperseg, noverlap)
        assert_allclose(f, fw)
        assert_allclose(np.mean(P, axis=-1), Pw)
Example #23
0
def AverageSpectrogram(electrode_event):

    f, t, Sxx = signal.spectrogram(electrode_event, window = 'hamming', fs = 100,nperseg = 64, noverlap = 32, return_onesided =True, scaling = 'spectrum' )
    
    Sxx = np.mean(np.log(Sxx), axis = 0)
    
    upper_bound = np.argmax( f > 13.0 )

    f = [format(x, '.1f') for x in f]
            

    return f, t, Sxx, upper_bound
Example #24
0
def spec_mapping(wave) :
    wave = wave[0]
    wave = wave[:-1,:]
    window = signal.cosine(WINDOW_SIZE)

    spec = signal.spectrogram(wave, window=window, nperseg=WINDOW_SIZE, noverlap=OVERLAP, mode='psd')
    spec = np.swapaxes(spec[2], 2, 1)
    # The convnet wants a tensor4 in (batch, channel, image x, image y)
    # our batch here is the sequence in time.
    # There is only one channel
    spec = spec[:,np.newaxis,:,:]

    return (spec,)
Example #25
0
    def prepare_data(self, data, regenerate=False) :
        data = data[self.samplerate*60*2:len(data)-self.samplerate*60*2]
        window = signal.cosine(WINDOW_SIZE)

        f_data = signal.spectrogram(data, window=window, nperseg=WINDOW_SIZE, noverlap=OVERLAP, mode='complex')
        f_data = np.swapaxes(f_data[2], 0, 1)
        real = np.real(f_data)
        rmax = np.max(real) if np.absolute(np.min(real)) < np.max(real) else np.absolute(np.min(real))
        real /= rmax
        cplx = np.imag(f_data)
        cmax = np.max(cplx) if np.absolute(np.min(cplx)) < np.max(cplx) else np.absolute(np.min(cplx))
        cplx /= cmax

        return np.append(real, cplx, axis=1)
Example #26
0
def make_spectrogram(samples):
    f, t, spec = signal.spectrogram(samples, nperseg=SPECTROGRAM_HEIGHT * 2, noverlap=SPECTROGRAM_HEIGHT/2, window='hamming')

    # Remove zeroth element
    data = spec[1:]

    # Zero out low frequencies
    data[:8] = 0

    #data = whitening_filter(data)
    avg = numpy.median(data)
    data *= (.10 / avg)
    data = numpy.clip(data, 0, 1.0)
    return data
Example #27
0
def mp3ToDFT(mp3filename):
    # convert mp3, read wav
    wname = mktemp('.wav')
    FNULL = open(os.devnull, 'w')
    subprocess.call(['avconv', '-i', mp3filename, "-ss", "15", "-t", "9", wname], stdout=FNULL, stderr=subprocess.STDOUT)
    sig, fs, enc = wavread(wname)

    #todo: remove wav file
    #todo: convert to mono, averaging sig[:,0] + sig[;,1]
    os.unlink(wname)

    spectrogram = signal.spectrogram(sig[:,0], fs=fs)[2][0:62,:]

    return spectrogram
Example #28
0
def test_spectrogram_method():
    """Test the spectrogram method's functionality."""
    fs = 10e3
    N = 1e5
    amp = 2 * np.sqrt(2)
    noise_power = 0.001 * fs / 2
    time = np.arange(N) / fs
    freq = np.linspace(1e3, 2e3, N)
    x = amp * chirp(time, 1e3, 2.0, 6e3, method='quadratic') + \
        np.random.normal(scale=np.sqrt(noise_power), size=time.shape)

    f, t, Sxx = spectrogram_lspopt(x, fs, c_parameter=20.0)
    f_sp, t_sp, Sxx_sp = spectrogram(x, fs)

    assert True
Example #29
0
    def run(self):
        waveform = b''.join(self.frames)
        f, t, Sxx = signal.spectrogram(map(ord, waveform), fs=RATE)
        LSxx = spectrogram_log_frequency_scale(f, Sxx)

        # Convert from 128x201 to 128x196 as the model expects
        LSxx = numpy.delete(LSxx, [0,1,2,3,4], axis=1)

        LSxx = numpy.expand_dims(LSxx, axis=0) # Make into tensor with depth 1, as that's the input to a ConvNet
        LSxx = numpy.expand_dims(LSxx, axis=0) # Make into data set with size 1
        prediction = model.predict(LSxx)[0][0]
        print prediction
        print '\r\n'
        if prediction > 0.99:
            PlayStopWhistling().start()
Example #30
0
    def test_fftgram(self, losc):
        fgram = losc.fftgram(1)
        fs = int(losc.sample_rate.value)
        f, t, sxx = signal.spectrogram(
            losc, fs,
            window='hann',
            nperseg=fs,
            mode='complex',
        )
        utils.assert_array_equal(losc.t0.value + t, fgram.xindex.value)
        utils.assert_array_equal(f, fgram.yindex.value)
        utils.assert_array_equal(sxx.T, fgram)

        fgram = losc.fftgram(1, overlap=0.5)
        f, t, sxx = signal.spectrogram(
            losc, fs,
            window='hann',
            nperseg=fs,
            noverlap=fs//2,
            mode='complex',
        )
        utils.assert_array_equal(losc.t0.value + t, fgram.xindex.value)
        utils.assert_array_equal(f, fgram.yindex.value)
        utils.assert_array_equal(sxx.T, fgram)
Example #31
0
 def __wavelet_transform(self):
     f, t, Sxx = signal.spectrogram(self.data, self.sampling_frequency)
     f = np.arange(1, max(f))
     cwtmatr = signal.cwt(self.data, signal.ricker, f)
     return cwtmatr, max(f)
from playsound import playsound
import os as os
from scipy.io import wavfile
import matplotlib.pyplot as plt
from scipy import signal
import numpy as np

playcount = 0
for sounds in os.listdir("testsounds"):
    fs, WaveFile = wavfile.read(os.path.join("testsounds",sounds))
    N = 500
    a, b, c = signal.spectrogram(WaveFile,fs,window=signal.blackman(N),nfft=N)
    plt.pcolormesh(b,c,10*np.log10(c))
    plt.plot(WaveFile)
    plt.ylabel('Frequency [Hz]')
    plt.xlabel('Time [seg]')
    plt.title('Spectrogram with scipy.signal',size=16)
    plt.show()
    playcount = playcount+1
print("Done!!!")
Example #33
0
 def __spectrogram_transform(self):
     f, t, Sxx = signal.spectrogram(self.data, self.sampling_frequency)
     return f, t, Sxx
def data_preprocess(wav_files, number_of_classes):
    data_x = []
    data_y = []
    sample_frequencies = []
    segment_times = []
    begin_time = time.time()
    for i, onewav in enumerate(wav_files):
        if i % 5 == 4:  # 运行5个路径名后。
            gaptime = time.time() - begin_time
            percent = float(i) * 100 / len(wav_files)
            eta_time = gaptime * 100 / (percent + 0.01) - gaptime
            strprogress = "[" + "=" * int(
                percent // 2) + ">" + "-" * int(50 - percent // 2) + "]"
            str_log = (
                "%.2f %% %s %s/%s \t used:%d s  eta:%d s" %
                (percent, strprogress, i, len(wav_files), gaptime, eta_time))
            sys.stdout.write('\r' + str_log)

        elements = onewav.split(splitchar)
        for x in elements:
            if x == '01 diode':
                label = 0
            elif x == '02 metalnode':
                label = 1
            elif x == '03 qiangkaiguan':
                label = 2
            elif x == '04 mouse':
                label = 3
            elif x == '05 dianluban':
                label = 4
            elif x == '06 libattery':
                label = 5
            elif x == '07 charger':
                label = 6
            elif x == '08 A-wav':
                label = 7
            elif x == '09 qiangchazuo':
                label = 8
            elif x == '10 netport':
                label = 9

        (rate, data) = wav.read(onewav)
        # 注意!考虑到所有音频数据左声道信号非常清晰,而右声道信号很弱很难分辨,因此此处仅采用左声道的数据
        data = np.transpose(data)[0]
        '''正向取3秒:
        for j in range(len(data)):  # len(aud)是统计出二元数组aud的行数,len(aud[0])则是统计数组列数。如果多维,每一维是len(A[i])。
            if data[j] > 10 or data[j] < -10:
                data = data[j:(j + 132400)].copy()
                break
        '''
        '''反向取3秒:132400'''
        data = data[-132400:-1].copy()

        sample_frequency, segment_time, spectrogram = signal.spectrogram(data)
        sample_frequencies.append(sample_frequency)
        segment_times.append(segment_time)

        data_x.append(spectrogram)
        data_y.append(label)

    max_freq = len(sample_frequencies[0])
    max_time = len(segment_times[0])
    # data_x = [np.concatenate([i, np.zeros((max_freq, max_time - i.shape[1]))], axis=1) for i in data_x]

    data_x = np.asarray(data_x)
    # data_x = np.transpose(data_x, axes=(0, 2, 1))
    # data_x=np.expand_dims(data_x,axis=3)
    data_y = to_categorical(data_y, num_classes=number_of_classes)
    return data_x, data_y, max_freq, max_time
def main(argv):
    SBJ = ''
    RUN = ''
    WIN_LENGTH = 30
    WIN_OVERLAP = 29
    NFFT = 32
    SCALING = 'density'
    DETREND = 'linear'
    FS = 1 / 2  # Sampling Frequency 1/TR

    try:
        opts, args = getopt.getopt(argv, "hs:d:r:w:p:",
                                   ["subject=", "run=", "wdir="])
    except getopt.GetoptError:
        print('ExtractROIs.py -s <subject> -d <run> -w <working_dir>')
        sys.exit(2)
    for opt, arg in opts:
        if opt == '-h':
            print('ExtractROIs.py -s <subject> -d <run> -w <working_dir>')
            sys.exit()
        elif opt in ("-s", "--subject"):
            SBJ = arg
        elif opt in ("-d", "--run"):
            RUN = arg
        elif opt in ("-w", "--wdir"):
            PRJDIR = arg
    print(
        '++ WARNING: We assume data has a TR of 1s. Please ensure that is correct'
    )
    print(
        '++ ====================================================================='
    )
    print('++ Working on %s' % SBJ)
    print(' + Run Dir       -->  %s' % RUN)
    print(' + Data Dir      -->  %s' % PRJDIR)
    print(' + Win Length    -->  %d TRs' % WIN_LENGTH)
    print(' + Win Overlap   -->  %d TRs' % WIN_OVERLAP)
    print(' + Detrend       -->  %s' % str(DETREND))
    print(' + Sampling Freq -->  %f Hz' % FS)

    #Inputs
    #======
    RUND_Path = osp.join(PRJDIR, SBJ, 'D03_4thVent')
    roits_path = osp.join(
        RUND_Path, '{SBJ}.{RUN}.volreg.Signal.V4.1D'.format(SBJ=SBJ, RUN=RUN))

    print('++ Loading ROI timeseries into memory from [%s]' % roits_path)
    roits = np.loadtxt(roits_path)

    # Compute Spectrogram
    # ===================
    print('++ Computing Spectrogram...')
    f, t, Sxx = spectrogram(roits,
                            FS,
                            window=get_window(('tukey', 0.25), WIN_LENGTH),
                            noverlap=WIN_OVERLAP,
                            scaling=SCALING,
                            nfft=NFFT,
                            detrend=DETREND,
                            mode='psd')
    spectrogram_df = pd.DataFrame(Sxx, index=f, columns=t)

    # Compute Average Power/Time in Sleep Band
    # ========================================
    print('++ Computing Summary Statistics of Power in Sleep Band...')
    band_lim_spect_df = generate_sleep_psd(f, t, Sxx)

    # Outputs
    # =======
    SPECTROGRAM_FILE = '{SBJ}.{RUN}.volreg.Signal.V4.Spectrogram.pkl'.format(
        SBJ=SBJ, RUN=RUN)
    BANDLIMITED_FILE = '{SBJ}.{RUN}.volreg.Signal.V4.Spectrogram_BandLimited.pkl'.format(
        SBJ=SBJ, RUN=RUN)
    SPECTROGRAM_Path = osp.join(RUND_Path, SPECTROGRAM_FILE)
    BANDLIMITED_Path = osp.join(RUND_Path, BANDLIMITED_FILE)

    # Save Results
    # ============
    print('++ Saving Power Spectrum do disk: [%s]' % SPECTROGRAM_Path)
    spectrogram_df.to_pickle(SPECTROGRAM_Path)

    print('++ Saving Average Power in Sleep Band: [%s]' % BANDLIMITED_Path)
    band_lim_spect_df.to_pickle(BANDLIMITED_Path)
Example #36
0
  x = x[..., np.newaxis]
  # prediction = [ [0.1, 0.2, ...] ]
  predictions = model.predict(x) # X -> (1, 130, 13, 1) 
  # extract index with max value
  predicted_index = np.argmax( predictions, axis = -1) # [4]
  print( "Expected index: {}, Predicted index: {}".format(y, predicted_index) )


  cols = 4
  rows = np.ceil(len(x)/cols)
  
  for i in range(len(x)):
    pltpath.title("Normal" if np.argmax(predicted_index[i]) == 0 else "Pnuemonia")
    pltpath.axis('off')
    
    
    
from google.colab import files
uploads = files.upload()
uploads.keys()

# running our model
eval_model = model
eval_model.load_weights("model.tf")

for ima in uploads.keys():
    data, samplerate = sf.read(ima)
    frequencies, times, spectrogram = signal.spectrogram(data, samplerate)
    
    #make prediction on a sample
    predict( model , spectrogram,  spectrogram )
Example #37
0
        print(intervals)
        print("median beat:", np.median(intervals))

    if False:
        # plot raw spectrogram (this doesn't seem as useful as the chroma plot)
        fig, ax = plt.subplots(nrows=len(raws), sharex=True, sharey=True)
        for i, raw in enumerate(raws):
            M = 1024
            dt = 1 / samples[i].frame_rate
            sync_ms = clap_offset[i]
            rate = samples[i].frame_rate
            trimval = int(round(sync_ms * rate / 1000))
            freqs, times, Sx = signal.spectrogram(np.array(raw[trimval:]),
                                                  fs=dt,
                                                  window='hanning',
                                                  nperseg=M,
                                                  noverlap=M - 100,
                                                  detrend=False,
                                                  scaling='spectrum')
            ax[i].pcolormesh(times, freqs, 10 * np.log10(Sx), cmap='viridis')
            ax[i].set_title("Accelerometer Spectogram")
            ax[i].set_ylabel('Frequency [Hz]')
            ax[i].set_xlabel('Time [s]')

    fig, ax = plt.subplots(nrows=len(raws), sharex=True, sharey=True)
    for i in range(len(raws)):
        trimval = int(round(clap_offset[i] * rate / 1000))
        librosa.display.waveplot(np.array(raws[i][trimval:]).astype('float'),
                                 sr=samples[i].frame_rate,
                                 ax=ax[i])
        ax[i].set(title=args.videos[i])
Example #38
0
File: vad.py Project: zhucq/vad
def detect_spoken_frames(sound_data, sampling_frequency, params=DEFAULT_PARAMS_OF_ADAPT_ALG, sound_name=None):
    assert sampling_frequency >= 8000, 'Sampling frequency is inadmissible!'
    n_data = len(sound_data)
    assert (n_data > 0) and ((n_data % 2) == 0), 'Sound data are wrong!'
    frame_size = int(round(FRAME_DURATION * float(sampling_frequency)))
    if (sound_name is None) or (sound_name not in global_features_cache):
        n_fft_points = 2
        while n_fft_points < frame_size:
            n_fft_points *= 2
        sound_signal = numpy.empty((int(n_data / 2),))
        for ind in range(sound_signal.shape[0]):
            sound_signal[ind] = float(struct.unpack('<h', sound_data[(ind * 2):(ind * 2 + 2)])[0])
        frequencies_axis, time_axis, spectrogram = signal.spectrogram(
            sound_signal, fs=sampling_frequency, window='hamming', nperseg=frame_size, noverlap=0, nfft=n_fft_points,
            scaling='spectrum', mode='psd'
        )
        spectrogram = spectrogram.transpose()
        if spectrogram.shape[0] <= INIT_SILENCE_FRAMES:
            return []
        if (sound_signal.shape[0] % frame_size) == 0:
            sound_frames = numpy.reshape(sound_signal, (spectrogram.shape[0], frame_size))
        else:
            sound_frames = numpy.reshape(sound_signal[0:int(sound_signal.shape[0] / frame_size) * frame_size],
                                         (spectrogram.shape[0], frame_size))
        features = calculate_features_for_VAD(sound_frames, frequencies_axis, spectrogram)
        del sound_frames
        del spectrogram
        del frequencies_axis
        del time_axis
        if sound_name is not None:
            global_features_cache[sound_name] = features.copy()
    else:
        features = global_features_cache[sound_name]
    [min_energy, min_sfm, min_freq] = features[0:INIT_SILENCE_FRAMES].min(axis=0).tolist()
    energy_th = params['Energy_PrimThresh'] * math.log(min_energy)
    sfm_th = params['SF_PrimThresh']
    freq_th = params['F_PrimThresh']
    spoken_frames = []
    number_of_silence_frames = 0
    for ind in range(features.shape[0]):
        counter = 0
        if (features[ind][0] - min_energy) >= energy_th:
            counter += 1
        if (features[ind][1] - min_sfm) >= sfm_th:
            counter += 1
        if (features[ind][2] - min_freq) >= freq_th:
            counter += 1
        if counter > 1:
            spoken_frames.append(True)
        else:
            spoken_frames.append(False)
            min_energy = (features[ind][0] + number_of_silence_frames * min_energy) / (number_of_silence_frames + 1)
            energy_th = params['Energy_PrimThresh'] * math.log(min_energy)
            number_of_silence_frames += 1
    del features
    min_frames_in_silence = int(round(params['Min_Silence'] * float(sampling_frequency) / frame_size))
    if min_frames_in_silence < 0:
        min_frames_in_silence = 0
    min_frames_in_speech = int(round(params['Min_Speech'] * float(sampling_frequency) / frame_size))
    if min_frames_in_speech < 0:
        min_frames_in_speech = 0
    sound_duration = (n_data - 2.0) / (2.0 * float(sampling_frequency))
    for cur_speech_frame in smooth_spoken_frames(spoken_frames, min_frames_in_silence, min_frames_in_speech):
        init_time = cur_speech_frame[0] * FRAME_DURATION
        fin_time = cur_speech_frame[1] * FRAME_DURATION
        if fin_time > sound_duration:
            fin_time = sound_duration
        yield (init_time, fin_time)
    del spoken_frames
 def time_spectrogram(self):
     signal.spectrogram(self.x)
Example #40
0
MagFreq = np.abs(AudioFreq)  # Magnitude
MagFreq = MagFreq / float(n)
# power spectrum
MagFreq = MagFreq**2
if n % 2 > 0:  # ffte odd
    MagFreq[1:len(MagFreq)] = MagFreq[1:len(MagFreq)] * 2
else:  # fft even
    MagFreq[1:len(MagFreq) - 1] = MagFreq[1:len(MagFreq) - 1] * 2

plt.figure()
freqAxis = np.arange(0, int(np.ceil((n + 1) / 2.0)), 1.0) * (fs / n)
plt.plot(freqAxis / 1000.0, 10 * np.log10(MagFreq))  #Power spectrum
plt.xlabel('Frequency (kHz)')
plt.ylabel('Power spectrum (dB)')

#Spectrogram
from scipy import signal
N = 512  #Number of point in the fft
f, t, Sxx = signal.spectrogram(Audiodata,
                               fs,
                               window=signal.blackman(N),
                               nfft=N)
plt.figure()
plt.pcolormesh(t, f, 10 * np.log10(Sxx))  # dB spectrogram
#plt.pcolormesh(t, f,Sxx) # Lineal spectrogram
plt.ylabel('Frequency [Hz]')
plt.xlabel('Time [seg]')
plt.title('Spectrogram with scipy.signal', size=16)

plt.show()
Example #41
0
def generate_spectrogram(signal,
                         Fs,
                         tr_length,
                         noverlap=0,
                         bins_per_octave=20 * 12,
                         freq_min=200,
                         decibels=True):

    # window size is 1 TR x samples per seconds
    win = Fs * tr_length

    # find num freqs
    nfft = win

    # get spectrum
    freqs, times, spec = spectrogram(signal,
                                     Fs,
                                     nperseg=int(win),
                                     noverlap=noverlap,
                                     nfft=nfft)

    if decibels:  # pragma: no cover

        # Ratio between adjacent frequencies in log-f axis
        fratio = 2**(1 / bins_per_octave)

        # How manp.ny bins in log-f axis
        nbins = np.floor(np.log(
            (Fs / 2) / freq_min) / np.log(fratio)).astype(int)

        # Freqs corresponding to each bin in FFT
        fftfrqs = np.arange(nfft / 2) * (Fs / nfft)
        nfftbins = np.int(nfft / 2)

        # Freqs corresponding to each bin in log F output
        logffrqs = freq_min * np.exp(
            np.log(2) * np.arange(nbins) / bins_per_octave)

        # Bandwidths of each bin in log F
        logfbws = logffrqs * (fratio - 1)

        ovfctr = 0.5475
        # Adjusted by hand to make sum(mx'*mx) close to 1.0

        # Weighting matrix mapping energy in FFT bins to logF bins
        # is a set of Gaussian profiles depending on the difference in
        # frequencies, scaled by the bandwidth of that bin
        A = np.tile(logffrqs, (nfftbins, 1)).T
        B = np.tile(fftfrqs, (nbins, 1))
        C = np.tile(ovfctr * logfbws, (nfftbins, 1)).T
        freqdiff = (A - B) / C

        # % Normalize rows by sqrt(E), so multiplying by mx' gets approx orig spectrum back
        mx = np.exp(-0.5 * freqdiff**2)
        D = np.sqrt(2 * np.sum(mx**2, 1))
        E = mx / np.tile(D, (nfftbins, 1)).T
        Sx = spec.copy()
        Px = np.matrix(spec[1::])
        spec = np.array(np.matrix(mx) * Px)

        # output
        times = np.arange(spec.shape[-1])
        freqs = np.log2(logffrqs) / np.log2(10)

    return spec, freqs, times
Example #42
0
from scipy import signal
from scipy.fft import fftshift
import matplotlib.pyplot as plt

 fs = 10e3
 N = 1e5
 amp = 2 * np.sqrt(2)
 noise_power = 0.01 * fs / 2
 time = np.arange(N) / float(fs)
 mod = 500*np.cos(2*np.pi*0.25*time)
 carrier = amp * np.sin(2*np.pi*3e3*time + mod)
 noise = np.random.normal(scale=np.sqrt(noise_power), size=time.shape)
 noise *= np.exp(-time/5)
 x = carrier + noise

f, t, Sxx = signal.spectrogram(x, fs)
plt.pcolormesh(t, f, Sxx)
 plt.ylabel('Frequency [Hz]')
 plt.xlabel('Time [sec]')
 plt.show()
Example #43
0
def plotLFP (electrodes = ['avg', 'all'], plots = ['timeSeries', 'PSD', 'spectrogram', 'locations'], timeRange=None, NFFT=256, noverlap=128, 
    nperseg=256, minFreq=1, maxFreq=100, stepFreq=1, smooth=0, separation=1.0, includeAxon=True, logx=False, logy=False, normSignal=False, normPSD=False, normSpec=False, dpi=200, overlay=False, filtFreq = False, filtOrder=3, detrend=False, transformMethod='morlet', fontSize=14, colors = None, maxPlots=8, lineWidth=1.5, figSize = (8,8), saveData = None, saveFig = None, showFig = True): 
    ''' 
    Plot LFP
        - electrodes (list): List of electrodes to include; 'avg'=avg of all electrodes; 'all'=each electrode separately (default: ['avg', 'all'])
        - plots (list): list of plot types to show (default: ['timeSeries', 'PSD', 'timeFreq', 'locations']) 
        - timeRange ([start:stop]): Time range of spikes shown; if None shows all (default: None)
        - NFFT (int, power of 2): Number of data points used in each block for the PSD and time-freq FFT (default: 256)
        - noverlap (int, <nperseg): Number of points of overlap between segments for PSD and time-freq (default: 128)
        - minFreq (float)
        - maxFreq (float): Maximum frequency shown in plot for PSD and time-freq (default: 100 Hz)
        - stepFreq (float)
        - nperseg (int): Length of each segment for time-freq (default: 256)
        - smooth (int): Window size for smoothing LFP; no smoothing if 0 (default: 0)
        - separation (float): Separation factor between time-resolved LFP plots; multiplied by max LFP value (default: 1.0)
        - includeAxon (boolean): Whether to show the axon in the location plot (default: True)
        - logx (boolean)
        - logy (boolean)
        - normSignal (boolean)
        - normPSD (boolean)
        - filtFreq (float)
        - filtOrder (int)
        - detrend (false)
        - transformMethod ('morlet'|'fft')
        - overlay (boolean)
        - dpi (int) 
        - colors
        - maxPlots
        - lineWidth
        - figSize ((width, height)): Size of figure (default: (10,8))
        - saveData (None|True|'fileName'): File name where to save the final data used to generate the figure; 
            if set to True uses filename from simConfig (default: None)
        - saveFig (None|True|'fileName'): File name where to save the figure;
            if set to True uses filename from simConfig (default: None)
        - showFig (True|False): Whether to show the figure or not (default: True)

        - Returns figure handles
    
    Note: should probably split funcs for signal, psd, spectrogram and locs
    '''

    from .. import sim
    from ..support.scalebar import add_scalebar

    print('Plotting LFP ...')

    if not colors: colors = colorList
    
    # set font size
    plt.rcParams.update({'font.size': fontSize})
    
    # time range
    if timeRange is None:
        timeRange = [0,sim.cfg.duration]

    lfp = np.array(sim.allSimData['LFP'])[int(timeRange[0]/sim.cfg.recordStep):int(timeRange[1]/sim.cfg.recordStep),:]

    if filtFreq:
        from scipy import signal
        fs = 1000.0/sim.cfg.recordStep
        nyquist = fs/2.0    
        if isinstance(filtFreq, list): # bandpass
            Wn = [filtFreq[0]/nyquist, filtFreq[1]/nyquist]
            b, a = signal.butter(filtOrder, Wn, btype='bandpass')
        elif isinstance(filtFreq, Number): # lowpass
            Wn = filtFreq/nyquist
            b, a = signal.butter(filtOrder, Wn)
        for i in range(lfp.shape[1]):
            lfp[:,i] = signal.filtfilt(b, a, lfp[:,i])

    if detrend:
        from scipy import signal
        for i in range(lfp.shape[1]):
            lfp[:,i] = signal.detrend(lfp[:,i])

    if normSignal:
        for i in range(lfp.shape[1]):
            offset = min(lfp[:,i])
            if offset <= 0:
                lfp[:,i] += abs(offset)
            lfp[:,i] /= max(lfp[:,i])

    # electrode selection
    if 'all' in electrodes:
        electrodes.remove('all')
        electrodes.extend(list(range(int(sim.net.recXElectrode.nsites))))

    # plotting
    figs = []
    #maxPlots = 8.0
    
    data = {'lfp': lfp}  # returned data


    # time series -----------------------------------------
    if 'timeSeries' in plots:
        ydisp = np.absolute(lfp).max() * separation
        offset = 1.0*ydisp
        t = np.arange(timeRange[0], timeRange[1], sim.cfg.recordStep)

        if figSize:
            figs.append(plt.figure(figsize=figSize))

        for i,elec in enumerate(electrodes):
            if elec == 'avg':
                lfpPlot = np.mean(lfp, axis=1)
                color = 'k'
                lw=1.0
            elif isinstance(elec, Number) and elec <= sim.net.recXElectrode.nsites:
                lfpPlot = lfp[:, elec]
                color = colors[i%len(colors)]
                lw=1.0
            plt.plot(t, -lfpPlot+(i*ydisp), color=color, linewidth=lw)
            if len(electrodes) > 1:
                plt.text(timeRange[0]-0.07*(timeRange[1]-timeRange[0]), (i*ydisp), elec, color=color, ha='center', va='top', fontsize=fontSize, fontweight='bold')

        ax = plt.gca()

        data['lfpPlot'] = lfpPlot
        data['ydisp'] =  ydisp
        data['t'] = t

        # format plot
        if len(electrodes) > 1:
            plt.text(timeRange[0]-0.14*(timeRange[1]-timeRange[0]), (len(electrodes)*ydisp)/2.0, 'LFP electrode', color='k', ha='left', va='bottom', fontSize=fontSize, rotation=90)
            plt.ylim(-offset, (len(electrodes))*ydisp)
        else:       
            plt.suptitle('LFP Signal', fontSize=fontSize, fontweight='bold')
        ax.invert_yaxis()
        plt.xlabel('time (ms)', fontsize=fontSize)
        ax.spines['top'].set_visible(False)
        ax.spines['right'].set_visible(False)
        ax.spines['left'].set_visible(False)
        plt.subplots_adjust(bottom=0.1, top=1.0, right=1.0)

        # calculate scalebar size and add scalebar
        round_to_n = lambda x, n, m: int(np.ceil(round(x, -int(np.floor(np.log10(abs(x)))) + (n - 1)) / m)) * m 
        scaley = 1000.0  # values in mV but want to convert to uV
        m = 10.0
        sizey = 100/scaley
        while sizey > 0.25*ydisp:
            try:
                sizey = round_to_n(0.2*ydisp*scaley, 1, m) / scaley
            except:
                sizey /= 10.0
            m /= 10.0
        labely = '%.3g $\mu$V'%(sizey*scaley)#)[1:]
        if len(electrodes) > 1:
            add_scalebar(ax,hidey=True, matchy=False, hidex=False, matchx=False, sizex=0, sizey=-sizey, labely=labely, unitsy='$\mu$V', scaley=scaley, 
                loc=3, pad=0.5, borderpad=0.5, sep=3, prop=None, barcolor="black", barwidth=2)
        else:
            add_scalebar(ax, hidey=True, matchy=False, hidex=True, matchx=True, sizex=None, sizey=-sizey, labely=labely, unitsy='$\mu$V', scaley=scaley, 
                unitsx='ms', loc=3, pad=0.5, borderpad=0.5, sep=3, prop=None, barcolor="black", barwidth=2)
        # save figure
        if saveFig: 
            if isinstance(saveFig, basestring):
                filename = saveFig
            else:
                filename = sim.cfg.filename+'_'+'lfp.png'
            plt.savefig(filename, dpi=dpi)

    # PSD ----------------------------------
    if 'PSD' in plots:
        if overlay:
            figs.append(plt.figure(figsize=figSize))
        else:
            numCols = 1# np.round(len(electrodes) / maxPlots) + 1
            figs.append(plt.figure(figsize=(figSize[0]*numCols, figSize[1])))
            #import seaborn as sb

        allFreqs = []
        allSignal = []
        data['allFreqs'] = allFreqs
        data['allSignal'] = allSignal

        for i,elec in enumerate(electrodes):
            if elec == 'avg':
                lfpPlot = np.mean(lfp, axis=1)
            elif isinstance(elec, Number) and elec <= sim.net.recXElectrode.nsites:
                lfpPlot = lfp[:, elec]
            
            # Morlet wavelet transform method
            if transformMethod == 'morlet':
                from ..support.morlet import MorletSpec, index2ms

                Fs = int(1000.0/sim.cfg.recordStep)

                #t_spec = np.linspace(0, index2ms(len(lfpPlot), Fs), len(lfpPlot))
                morletSpec = MorletSpec(lfpPlot, Fs, freqmin=minFreq, freqmax=maxFreq, freqstep=stepFreq)
                freqs = F = morletSpec.f
                spec = morletSpec.TFR
                signal = np.mean(spec, 1)
                ylabel = 'Power'

            # FFT transform method
            elif transformMethod == 'fft':
                Fs = int(1000.0/sim.cfg.recordStep)
                power = mlab.psd(lfpPlot, Fs=Fs, NFFT=NFFT, detrend=mlab.detrend_none, window=mlab.window_hanning, 
                    noverlap=noverlap, pad_to=None, sides='default', scale_by_freq=None)

                if smooth:
                    signal = _smooth1d(10*np.log10(power[0]), smooth)
                else:
                    signal = 10*np.log10(power[0])
                freqs = power[1]
                ylabel = 'Power (dB/Hz)'

            allFreqs.append(freqs)
            allSignal.append(signal)

        # ALTERNATIVE PSD CALCULATION USING WELCH
        # from http://joelyancey.com/lfp-python-practice/
        # from scipy import signal as spsig
        # Fs = int(1000.0/sim.cfg.recordStep)
        # maxFreq=100
        # f, psd = spsig.welch(lfpPlot, Fs, nperseg=100)
        # plt.semilogy(f,psd,'k')
        # sb.despine()
        # plt.xlim((0,maxFreq))
        # plt.yticks(size=fontsiz)
        # plt.xticks(size=fontsiz)
        # plt.ylabel('$uV^{2}/Hz$',size=fontsiz)

        if normPSD:
            vmax = np.max(allSignal)
            for i, s in enumerate(allSignal):
                allSignal[i] = allSignal[i]/vmax

        for i,elec in enumerate(electrodes):
            if not overlay:
                plt.subplot(np.ceil(len(electrodes)/numCols), numCols,i+1)
            if elec == 'avg':
                color = 'k'
            elif isinstance(elec, Number) and elec <= sim.net.recXElectrode.nsites:
                color = colors[i % len(colors)]
            freqs = allFreqs[i]
            signal = allSignal[i]
            plt.plot(freqs[freqs<maxFreq], signal[freqs<maxFreq], linewidth=lineWidth, color=color, label='Electrode %s'%(str(elec)))
            plt.xlim([0, maxFreq])
            if len(electrodes) > 1 and not overlay:
                plt.title('Electrode %s'%(str(elec)), fontsize=fontSize)
            plt.ylabel(ylabel, fontsize=fontSize)
            

        # format plot
        plt.xlabel('Frequency (Hz)', fontsize=fontSize)
        if overlay:
            plt.legend(fontsize=fontSize)
        plt.tight_layout()
        plt.suptitle('LFP Power Spectral Density', fontsize=fontSize, fontweight='bold') # add yaxis in opposite side
        plt.subplots_adjust(bottom=0.08, top=0.92)

        if logx:
            pass
        #from IPython import embed; embed()

        # save figure
        if saveFig: 
            if isinstance(saveFig, basestring):
                filename = saveFig
            else:
                filename = sim.cfg.filename+'_'+'lfp_psd.png'
            plt.savefig(filename, dpi=dpi)

    # Spectrogram ------------------------------
    if 'spectrogram' in plots:
        import matplotlib.cm as cm
        numCols = 1 #np.round(len(electrodes) / maxPlots) + 1
        figs.append(plt.figure(figsize=(figSize[0]*numCols, figSize[1])))
        #t = np.arange(timeRange[0], timeRange[1], sim.cfg.recordStep)
        
        # Morlet wavelet transform method
        if transformMethod == 'morlet':
            from ..support.morlet import MorletSpec, index2ms

            spec = []
            
            for i,elec in enumerate(electrodes):
                if elec == 'avg':
                    lfpPlot = np.mean(lfp, axis=1)
                elif isinstance(elec, Number) and elec <= sim.net.recXElectrode.nsites:
                    lfpPlot = lfp[:, elec]
                fs = int(1000.0 / sim.cfg.recordStep)
                t_spec = np.linspace(0, index2ms(len(lfpPlot), fs), len(lfpPlot))
                spec.append(MorletSpec(lfpPlot, fs, freqmin=minFreq, freqmax=maxFreq, freqstep=stepFreq))
                
            f = np.array(range(minFreq, maxFreq+1, stepFreq))  # only used as output for user

            vmin = np.array([s.TFR for s in spec]).min()
            vmax = np.array([s.TFR for s in spec]).max()
            for i,elec in enumerate(electrodes):
                plt.subplot(np.ceil(len(electrodes) / numCols), numCols, i + 1)
                T = timeRange
                F = spec[i].f
                if normSpec:
                    spec[i].TFR = spec[i].TFR / vmax
                    S = spec[i].TFR
                    vc = [0, 1]
                else:
                    S = spec[i].TFR
                    vc = [vmin, vmax]

                plt.imshow(S, extent=(np.amin(T), np.amax(T), np.amin(F), np.amax(F)), origin='lower', interpolation='None', aspect='auto', vmin=vc[0], vmax=vc[1], cmap=plt.get_cmap('viridis'))
                plt.colorbar(label='Power')
                plt.ylabel('Hz')
                plt.tight_layout()                
                if len(electrodes) > 1:
                    plt.title('Electrode %s' % (str(elec)), fontsize=fontSize - 2)
        
        # FFT transform method
        elif transformMethod == 'fft':

            from scipy import signal as spsig
            spec = []
            
            for i,elec in enumerate(electrodes):
                if elec == 'avg':
                    lfpPlot = np.mean(lfp, axis=1)
                elif isinstance(elec, Number) and elec <= sim.net.recXElectrode.nsites:
                    lfpPlot = lfp[:, elec]
                # creates spectrogram over a range of data 
                # from: http://joelyancey.com/lfp-python-practice/
                fs = int(1000.0/sim.cfg.recordStep)
                f, t_spec, x_spec = spsig.spectrogram(lfpPlot, fs=fs, window='hanning',
                detrend=mlab.detrend_none, nperseg=nperseg, noverlap=noverlap, nfft=NFFT,  mode='psd')
                x_mesh, y_mesh = np.meshgrid(t_spec*1000.0, f[f<maxFreq])
                spec.append(10*np.log10(x_spec[f<maxFreq]))

            vmin = np.array(spec).min()
            vmax = np.array(spec).max()
            for i,elec in enumerate(electrodes):
                plt.subplot(np.ceil(len(electrodes)/numCols), numCols, i+1)
                plt.pcolormesh(x_mesh, y_mesh, spec[i], cmap=cm.viridis, vmin=vmin, vmax=vmax)
                plt.colorbar(label='dB/Hz', ticks=[np.ceil(vmin), np.floor(vmax)])
                if logy:
                    plt.yscale('log')
                    plt.ylabel('Log-frequency (Hz)')
                    if isinstance(logy, list):
                        yticks = tuple(logy)
                        plt.yticks(yticks, yticks)
                else:
                    plt.ylabel('(Hz)')
                if len(electrodes) > 1:
                    plt.title('Electrode %s'%(str(elec)), fontsize=fontSize-2)

        plt.xlabel('time (ms)', fontsize=fontSize)
        plt.tight_layout()
        plt.suptitle('LFP spectrogram', size=fontSize, fontweight='bold')
        plt.subplots_adjust(bottom=0.08, top=0.90)
        
        # save figure
        if saveFig: 
            if isinstance(saveFig, basestring):
                filename = saveFig
            else:
                filename = sim.cfg.filename+'_'+'lfp_timefreq.png'
            plt.savefig(filename, dpi=dpi)

    # locations ------------------------------
    if 'locations' in plots:
        cvals = [] # used to store total transfer resistance

        for cell in sim.net.compartCells:
            trSegs = list(np.sum(sim.net.recXElectrode.getTransferResistance(cell.gid)*1e3, axis=0)) # convert from Mohm to kilohm
            if not includeAxon:
                i = 0
                for secName, sec in cell.secs.items():
                    nseg = sec['hObj'].nseg #.geom.nseg
                    if 'axon' in secName:
                        for j in range(i,i+nseg): del trSegs[j] 
                    i+=nseg
            cvals.extend(trSegs)  
            
        includePost = [c.gid for c in sim.net.compartCells]
        fig = sim.analysis.plotShape(includePost=includePost, showElectrodes=electrodes, cvals=cvals, includeAxon=includeAxon, dpi=dpi,
        fontSize=fontSize, saveFig=saveFig, showFig=showFig, figSize=figSize)[0]
        figs.append(fig)


    outputData = {'LFP': lfp, 'electrodes': electrodes, 'timeRange': timeRange, 'saveData': saveData, 'saveFig': saveFig, 'showFig': showFig}

    if 'PSD' in plots:
        outputData.update({'allFreqs': allFreqs, 'allSignal': allSignal})
    
    if 'spectrogram' in plots:
        outputData.update({'spec': spec, 't': t_spec*1000.0, 'freqs': f[f<=maxFreq]})

    #save figure data
    if saveData:
        figData = outputData
    
        _saveFigData(figData, saveData, 'lfp')


    # show fig 
    if showFig: _showFigure()

    return figs, outputData
Example #44
0
fp = 10  #frequence de la porteuse
fm = 1  #frequence du modulant
B = 5  #indice de modulation
n = 10 * fs  # nombre d'échantillons. Ici le signal dure 10 secondes
t = linspace(0, (n - 1) / fs, n)
x = cos(2 * pi * fp * t + B * sin(2 * pi * fm * t))

figure()
subplot(121)
plot(t, x)
xlabel('Temps en secondes')
ylabel('Amplitude')
title('Représentation temporelle du signal')

f, psd = periodogram(x, fs)
subplot(122)
plot(f, psd)
xlabel('fréquence [Hz]')
ylabel('densité spectrale de puissance [V^2/Hz]')
title('Représentation spectrale du signal')

#%%Spectrogramme
nperseg = int(fs * 5)
f, t, Sxx = spectrogram(x, fs, nperseg=nperseg, noverlap=nperseg // 2)
figure()
pcolormesh(t, f, Sxx)
ylabel('Frequence en Hz')
xlabel('Temps en secondes')
colorbar(label='Densité spectrale de puissance [V^2/Hz]')
grid('on')
title('Représentation temps-fréquence du signal')
Example #45
0
for i in range(snippet[0], snippet[1]):
    ds_target1 = ds_band1[i, :] + ds_target1
    ds_target3 = ds_band3[i, :] + ds_target3
ds_target1 = ds_target1 / (snippet[1] - snippet[0])
ds_target3 = ds_target3 / (snippet[1] - snippet[0])
N = len(ds_target1)
length = N * 5
fs = N / length

x = np.linspace(0, length, N)
xf = np.linspace(0, length * fs / 2, N // 2) / length
fy = fftpack.fft(ds_target1)[:N // 2] / N

j = ds_target1 - ds_target3
nper = 64
f, t, Z = ss.spectrogram(ds_target1, fs, nperseg=nper)
ff, tt, ZZ = ss.spectrogram(j, fs, nperseg=nper)

plt.figure(figsize=(15, 10))
plt.subplot(4, 2, 1)
plt.plot(x, np.log(ds_target1) * 10)
plt.plot(x, np.log(ds_target3) * 10)
plt.xlabel("[m]")
plt.ylabel("[dB]")
plt.subplot(4, 2, 2)
plt.xlabel("Frequency [Hz]")
plt.ylabel("..")
plt.plot(xf, np.abs(fy))
plt.subplot(4, 2, 3)
plt.pcolormesh(t, f, np.log(Z) * 10, vmin=-50, vmax=0)
plt.ylabel('[Hz]')
def SegmentS(signal,
             srate,
             tdb=90,
             fdb=90,
             printprocess='NO',
             prefilter='NO',
             postfilter='NO'):

    itn1 = []
    itn2 = []
    ifn1 = []
    ifn2 = []
    if (prefilter == 'YES'):
        signal = butter_bandp(signal, 500, 10000, srate)
    t = np.arange(0, float(len(signal) / srate), 1.0 / srate)

    freqs, times, spec = spectrogram(signal, srate)
    specdb = 20 * np.log10(spec)

    tsize = spec.shape[1]
    fsize = spec.shape[0]
    ifn, itn = np.where(specdb == np.amax(specdb))[0][0], np.where(
        specdb == np.amax(specdb))[1][0]
    #obtain the coordenates of the max value in dB
    pmax = specdb[ifn, itn]
    maxiter = int(t[t.shape[0] - 1] * 0.8)
    for f in range(0, maxiter):
        ifn, itn = np.where(specdb == np.amax(specdb))[0][0], np.where(
            specdb == np.amax(specdb))[1][0]
        #obtain the coordenates of the max value in dB
        powermaxdB = specdb[ifn, itn]
        #gets the maximum power in the spectrogram
        if (powermaxdB < pmax - 20):
            break

        #obtaining the section in time of a syllable
        #seccion   |----------!-----------------------------|
        #          0          max                           tsize
        #          |-----|----|------|----------------------|
        #          |   itn-i  itn  itn+i
        i = 0
        for i in range(0, tsize - itn):
            if (specdb[ifn, itn + i] < (powermaxdB - tdb)):
                #when the power drops tdB then cut (forward)
                break
        itnv2 = i + itn
        i = 0
        for i in range(0, itn):
            if (specdb[ifn, itn - i] < (powermaxdB - tdb)):
                #when the power drops tdB then cut (backward)
                break
        itnv1 = itn - i

        #saving the indices of the time for the secction

        itt1 = np.abs(t - times[itnv1]).argmin()
        itt2 = np.abs(t - times[itnv2]).argmin()
        itn1.append(itt1)
        itn2.append(itt2)

        if (postfilter == 'YES'):
            #segmenting in frequency
            i = 0
            for i in range(0, fsize - ifn):
                if (specdb[ifn + i, itn] < (powermaxdB - fdb)):
                    #when the power drops tdB then cut (Upper)
                    break
            ifnv2 = i + ifn
            i = 0
            for i in range(0, ifn):
                if (specdb[ifn - i, itn] < (powermaxdB - fdb)):
                    #when the power drops tdB then cut (Lower)
                    break
            ifnv1 = ifn - i

            #saving the inces of the freq seccion
            ifn1.append(ifnv1)
            ifn2.append(ifnv2)

            prevsmax = specdb[ifn, itn]

            #print(freqs[ifnv1],freqs[ifnv2],times[itnv1],times[itnv2])
            specdb[ifnv1:ifnv2, itnv1:itnv2] = -np.inf
            if (printprocess == 'YES'):
                #plt.pcolormesh(times,freqs,specdb)
                #plt.show()
                print(powermaxdB)
        else:
            prevsmax = specdb[ifn, itn]

            #print(freqs[ifnv1],freqs[ifnv2],times[itnv1],times[itnv2])
            specdb[:, itnv1:itnv2] = -np.inf
            if (printprocess == 'YES'):
                #plt.pcolormesh(times,freqs,specdb)
                #plt.show()
                print(powermaxdB)

    if (postfilter == 'YES'):
        it1 = np.array(itn1)
        it2 = np.array(itn2)
        if1 = np.array(ifn1)
        if2 = np.array(ifn2)
        it2 = it2[np.argsort(itn1)]
        if1 = if1[np.argsort(itn1)]
        if2 = if2[np.argsort(itn1)]
        it1 = it1[np.argsort(itn1)]
        ifh = int(np.mean(if2))
        ifl = int(np.mean(if1))
        #filtering
        if (freqs[ifl] == 0):
            signalfil = butter_bandp(signal, 500, freqs[ifh], srate)
        else:
            signalfil = butter_bandp(signal, freqs[ifl] + 500,
                                     freqs[ifh] + 1000, srate)
        if (printprocess == 'YES'):
            print('Filtering')
            f, ti, sp = spectrogram(signalfil, srate)
            sp = 20 * np.log10(sp)
            plt.pcolormesh(ti, f, sp)
    else:
        it1 = np.array(itn1)
        it2 = np.array(itn2)
        it2 = it2[np.argsort(itn1)]
        it1 = it1[np.argsort(itn1)]
        signalfil = signal

    count = 0
    #segments
    segments = np.array([0])
    for i in range(0, it1.shape[0]):
        segments = np.concatenate((segments, signalfil[it1[i]:it2[i]]), axis=0)
        #print(segments.shape[0])

    return segments
Example #47
0
def analyze_pred_vs_actual(args):
    """Generate plots to analyze the predicted signal vs the actual
    signal.

    Inputs:
        output_wav : The actual signal, by default will use y_test.wav from the test.py output
        pred_wav : The predicted signal, by default will use y_pred.wav from the test.py output
        input_wav : The pre effect signal, by default will use x_test.wav from the test.py output
        model_name : Used to add the model name to the plot .png filename
        show_plots : Default is 1 to show plots, 0 to only generate .png files and suppress plots

    1. Plots the two signals
    2. Calculates Error to signal ratio the same way Pedalnet evauluates the model for training
    3. Plots the absolute value of pred_signal - actual_signal  (to visualize abs error over time)
    4. Plots the spectrogram of (pred_signal - actual signal)
         The idea here is to show problem frequencies from the model training
    """
    output_wav = args.output_wav
    pred_wav = args.pred_wav
    input_wav = args.input_wav
    model_name = args.model_name
    show_plots = args.show_plots

    # Read the input wav file
    signal3, fs3 = read_wave(input_wav)

    # Read the output wav file
    signal1, fs = read_wave(output_wav)

    Time = np.linspace(0, len(signal1) / fs, num=len(signal1))
    fig, (ax3, ax1, ax2) = plt.subplots(3, sharex=True, figsize=(13, 8))
    fig.suptitle("Predicted vs Actual Signal")
    ax1.plot(Time, signal1, label=output_wav, color="red")

    # Read the predicted wav file
    signal2, fs2 = read_wave(pred_wav)

    Time2 = np.linspace(0, len(signal2) / fs2, num=len(signal2))
    ax1.plot(Time2, signal2, label=pred_wav, color="green")
    ax1.legend(loc="upper right")
    ax1.set_xlabel("Time (s)")
    ax1.set_ylabel("Amplitude")
    ax1.set_title("Wav File Comparison")
    ax1.grid("on")

    error_list = []
    for s1, s2 in zip(signal1, signal2):
        error_list.append(abs(s2 - s1))

    # Calculate error to signal ratio with pre-emphasis filter as
    #    used to train the model
    e2s = error_to_signal(signal1, signal2)
    e2s_no_filter = error_to_signal(signal1, signal2, use_filter=0)
    print("Error to signal (with pre-emphasis filter): ", e2s)
    print("Error to signal (no pre-emphasis filter): ", e2s_no_filter)
    fig.suptitle("Predicted vs Actual Signal (error to signal: " +
                 str(round(e2s, 4)) + ")")
    # Plot signal difference
    signal_diff = signal2 - signal1
    ax2.plot(Time2, error_list, label="signal diff", color="blue")
    ax2.set_xlabel("Time (s)")
    ax2.set_ylabel("Amplitude")
    ax2.set_title("abs(pred_signal-actual_signal)")
    ax2.grid("on")

    # Plot the original signal
    Time3 = np.linspace(0, len(signal3) / fs3, num=len(signal3))
    ax3.plot(Time3, signal3, label=input_wav, color="purple")
    ax3.legend(loc="upper right")
    ax3.set_xlabel("Time (s)")
    ax3.set_ylabel("Amplitude")
    ax3.set_title("Original Input")
    ax3.grid("on")

    # Save the plot
    plt.savefig(model_name + "_signal_comparison_e2s_" + str(round(e2s, 4)) +
                ".png",
                bbox_inches="tight")

    # Create a zoomed in plot of 0.01 seconds centered at the max input signal value
    sig_temp = signal1.tolist()
    plt.axis([
        Time3[sig_temp.index((max(sig_temp)))] - 0.005,
        Time3[sig_temp.index((max(sig_temp)))] + 0.005,
        min(signal2),
        max(signal2),
    ])
    plt.savefig(model_name + "_Detail_signal_comparison_e2s_" +
                str(round(e2s, 4)) + ".png",
                bbox_inches="tight")

    # Reset the axis
    plt.axis([0, Time3[-1], min(signal2), max(signal2)])

    # Plot spectrogram difference
    plt.figure(figsize=(12, 8))
    print("Creating spectrogram data..")
    frequencies, times, spectrogram = signal.spectrogram(signal_diff, 44100)
    plt.pcolormesh(times, frequencies, 10 * np.log10(spectrogram))
    plt.colorbar()
    plt.title("Diff Spectrogram")
    plt.ylabel("Frequency [Hz]")
    plt.xlabel("Time [sec]")
    plt.savefig(model_name + "_diff_spectrogram.png", bbox_inches="tight")

    if show_plots == 1:
        plt.show()
Example #48
0
        str_log = (
            "%.2f %% %s %s/%s \t used:%d s  eta:%d s" %
            (percent, strprogress, i, len(wav_files), gaptime, eta_time))
        sys.stdout.write('\r' + str_log)

    elements = onewav.split("\\")
    for x in elements:
        if x == 'diode-wav':
            label = 0
        elif x == 'metalnode-wav':
            label = 1

    (rate, data) = wav.read(onewav)
    #注意!考虑到所有音频数据左声道信号非常清晰,而右声道信号很弱很难分辨,因此此处仅采用左声道的数据
    data = np.transpose(data)[0]
    sample_frequency, segment_time, spectrogram = signal.spectrogram(data)
    sample_frequencies.append(sample_frequency)
    segment_times.append(segment_time)

    data_x.append(spectrogram)
    data_y.append(label)

len_freq = []
len_time = []
for i in sample_frequencies:
    len_freq.append(len(i))
for i in segment_times:
    len_time.append(len(i))

# #结果:129 129 1429 833
max_time = max(len_time)
elif (test_type == "mou"):
    test_type = 2
elif (test_type == "mod"):
    test_type = 3
elif (test_type == "r"):
    test_type = 4

data = removeDC(data)
sub_signals = downSampling(data, 5)

Fs = 128.0 / 5  # esto es porque fue submuestreado a 2
ts = 1 / Fs
time = np.arange(0, len(data[0][0]) * ts, ts)
f, t, S = signal.spectrogram(sub_signals[0][0],
                             fs=Fs,
                             nperseg=32,
                             nfft=32,
                             noverlap=10)
ff = sub_signals.shape  # Tamaño del array
Sxx = np.zeros((ff[0] * ff[1], len(f) + 3))
i = 0
for electrode in range(0, len(sub_signals)):
    for trial in range(0, len(sub_signals[electrode])):
        x = sub_signals[electrode][trial]
        _, _, S = signal.spectrogram(x,
                                     fs=Fs,
                                     nperseg=32,
                                     nfft=32,
                                     noverlap=10)
        Sxx[i, 0:3] = [electrode, test_type, trial]
        Sxx[i, 3::] = np.mean(S, axis=1)
Example #50
0
def compare_cwt_example(x, t, fs=128, sLog=False):
    t1 = t
    t0 = 0

    #print('Gauss')
    f0 = np.linspace(0.1, 40, 100)
    Q = np.linspace(0.1, 5, 100)  #[:,None]
    XW1, S1 = ScalogramCWT(x,
                           t,
                           wType='Gauss',
                           fs=fs,
                           PlotW=False,
                           PlotPSD=False,
                           fftMeth=True,
                           f0=f0,
                           Q=Q)

    #print('Morlet')
    sig = np.linspace(0.1, 10, 100)
    f = np.linspace(-10, 10, 2 * len(x) - 1)
    XW2, S2 = ScalogramCWT(x,
                           t,
                           wType='Morlet',
                           fs=fs,
                           PlotW=False,
                           PlotPSD=False,
                           fftMeth=True,
                           sig=sig,
                           f=f)

    #print('Gabor')
    f0 = np.linspace(1, 40, 100)
    a = 0.5
    XW3, S3 = ScalogramCWT(x,
                           t,
                           wType='Gabor',
                           fs=fs,
                           PlotW=False,
                           PlotPSD=False,
                           fftMeth=True,
                           f0=f0,
                           a=a)

    #print('Poisson')
    n = np.arange(100)
    XW4, S4 = ScalogramCWT(x,
                           t,
                           wType='Poisson',
                           fs=fs,
                           PlotW=False,
                           PlotPSD=False,
                           fftMeth=True,
                           n=n,
                           method=3)

    #print('cMaxican')
    f0 = np.linspace(0, 40, 80)  #[:,None]
    a = 0.005
    XW5, S5 = ScalogramCWT(x,
                           t,
                           wType='cMaxican',
                           fs=fs,
                           PlotW=False,
                           PlotPSD=False,
                           fftMeth=True,
                           f0=f0,
                           a=a)

    #print('cShannon')
    f0 = np.linspace(0, 40, 40)  #[:,None]
    fw = 5
    XW6, S6 = ScalogramCWT(x,
                           t,
                           wType='cShannon',
                           fs=128,
                           PlotW=False,
                           PlotPSD=False,
                           fftMeth=True,
                           f0=f0,
                           fw=fw)

    N = 32
    win = signal.get_window('hann', N)
    fx, tx, Sxx = signal.spectrogram(x,
                                     fs=fs,
                                     nfft=2 * N,
                                     nperseg=N,
                                     noverlap=N // 2,
                                     window=win)

    plt.figure(figsize=(15, 15))
    plt.subplot(811)
    #print(x.shape,t.shape)
    plt.plot(t, x)
    plt.xlim([t[0], t[-1]])
    plt.grid()

    plt.subplot(812)
    plt.imshow(abs(XW1),
               aspect='auto',
               origin='lower',
               cmap=plt.cm.jet,
               extent=[t[0], t[-1], S1[0], S1[-1]],
               interpolation='sinc')
    plt.ylabel('Gauss')

    plt.subplot(813)
    plt.imshow(abs(XW2),
               aspect='auto',
               origin='lower',
               cmap=plt.cm.jet,
               extent=[t[0], t[-1], S2[0], S2[-1]],
               interpolation='sinc')
    plt.ylabel('Morlet')

    plt.subplot(814)
    plt.imshow(abs(XW3),
               aspect='auto',
               origin='lower',
               cmap=plt.cm.jet,
               extent=[t[0], t[-1], S3[0], S3[-1]],
               interpolation='sinc')
    plt.ylabel('Gabor')

    plt.subplot(815)
    plt.imshow(abs(XW4),
               aspect='auto',
               origin='lower',
               cmap=plt.cm.jet,
               extent=[t[0], t[-1], S4[0], S4[-1]],
               interpolation='sinc')
    plt.ylabel('Poisson')

    plt.subplot(816)
    plt.imshow(abs(XW5),
               aspect='auto',
               origin='lower',
               cmap=plt.cm.jet,
               extent=[t[0], t[-1], S5[0], S5[-1]],
               interpolation='sinc')
    plt.ylabel('cMaxican')

    plt.subplot(817)
    plt.imshow((abs(XW6)),
               aspect='auto',
               origin='lower',
               cmap=plt.cm.jet,
               extent=[t[0], t[-1], S6[0], S6[-1]],
               interpolation='sinc')
    plt.ylabel('cShannon')

    plt.subplot(818)
    if sLog:
        plt.imshow(np.log10(Sxx),
                   aspect='auto',
                   origin='lower',
                   cmap=plt.cm.jet,
                   extent=[t[0], t[-1], fx[0], fx[-1]],
                   interpolation='sinc')
    else:
        plt.imshow(Sxx,
                   aspect='auto',
                   origin='lower',
                   cmap=plt.cm.jet,
                   extent=[t[0], t[-1], fx[0], fx[-1]],
                   interpolation='sinc')
    plt.ylabel('Spectrogram')

    plt.subplots_adjust(hspace=0.05)
    plt.show()
Example #51
0
nspecs = fftsize / 2
siglen_input = fftsize * nspecs
rand_signal = np.random.normal(size=siglen_input)  # gaussian: mu=0, sigma=1

# time-varying fir filter parameter
ntaps = 1001
siglen_output = siglen_input - ntaps + 1

frb_cfreq = np.linspace(0.8, 0.3, siglen_output)
frb_width = np.linspace(0.005, 0.02, siglen_output)

# apply time-varying fir filter
filt_signal = []
start_time = time()
for i, cfreq, width in zip(range(siglen_output), frb_cfreq, frb_width):
    fir = signal.firwin(ntaps, [cfreq - width / 2, cfreq + width / 2.0],
                        pass_zero=False,
                        nyq=fs)
    a = np.dot(fir, rand_signal[i:i + ntaps])
    filt_signal.append(a)
print "Time: " + str(time() - start_time)
filt_signal = np.array(filt_signal)

# get spectrogram and plot
f, t, Sxx = signal.spectrogram(filt_signal, nperseg=fftsize, fs=fs)
plt.pcolormesh(t, f, 10 * np.log10(Sxx))
plt.ylabel('Frequency [Hz]')
plt.xlabel('Time [sec]')
plt.colorbar()
plt.show()
import numpy as np

from scipy.io import wavfile
from scipy.signal import spectrogram
from bokeh.plotting import *

fs, raw_data = wavfile.read('testaudio/testaudio.wav')

f, t, Sxx = spectrogram(raw_data, fs)
i = 0
for freq in range(f.shape[0]):
    for time in range(t.shape[0]):
        df_spectogram.loc[i] = [f[freq], t[time], Sxx[freq][time]]
        i = i + 1

TOOLS = "hover,save,pan,box_zoom,reset,wheel_zoom"
PALETTE = [
    '#081d58', '#253494', '#225ea8', '#1d91c0', '#41b6c4', '#7fcdbb',
    '#c7e9b4', '#edf8b1', '#ffffd9'
]
mapper = LinearColorMapper(palette=PALETTE, low=np.min(Sxx), high=np.max(Sxx))
spectogram_figure = figure(title="Spectogram",
                           x_axis_location="below",
                           plot_width=900,
                           plot_height=400,
                           tools=TOOLS)
spectogram_figure.background_fill_color = "#eaeaea"
spectrogram_source = ColumnDataSource(
    data=dict(Sxx=df_spectogram['Sxx'],
              Frequency=df_spectogram['Frequency'],
              Time=df_spectogram['Time']))
Example #53
0
def compute_spectrum_welch(sig, fs, avg_type='mean', window='hann',
                           nperseg=None, noverlap=None,
                           f_range=None, outlier_percent=None):
    """Compute the power spectral density using Welch's method.

    Parameters
    ----------
    sig : 1d or 2d array
        Time series.
    fs : float
        Sampling rate, in Hz.
    avg_type : {'mean', 'median'}, optional
        Method to average across the windows:

        * 'mean' is the same as Welch's method, taking the mean across FFT windows.
        * 'median' uses median across FFT windows instead of the mean, to minimize outlier effects.
    window : str or tuple or array_like, optional, default: 'hann'
        Desired window to use. See scipy.signal.get_window for a list of available windows.
        If array_like, the array will be used as the window and its length must be nperseg.
    nperseg : int, optional
        Length of each segment, in number of samples.
        If None, and window is str or tuple, is set to 1 second of data.
        If None, and window is array_like, is set to the length of the window.
    noverlap : int, optional
        Number of points to overlap between segments.
        If None, noverlap = nperseg // 8.
    f_range : list of [float, float], optional
        Frequency range to sub-select from the power spectrum.
    outlier_percent : float, optional
        The percentage of outlier values to be removed. Must be between 0 and 100.

    Returns
    -------
    freqs : 1d array
        Frequencies at which the measure was calculated.
    spectrum : 1d or 2d array
        Power spectral density.

    Notes
    -----
    - Welch's method ([1]_) computes a power spectra by averaging over windowed FFTs.

    References
    ----------
    .. [1] Welch, P. (1967). The use of fast Fourier transform for the estimation of power
           spectra: A method based on time averaging over short, modified periodograms.
           IEEE Transactions on Audio and Electroacoustics, 15(2), 70–73.
           DOI: https://doi.org/10.1109/TAU.1967.1161901

    Examples
    --------
    Compute the power spectrum of a simulated time series using Welch's method:

    >>> from neurodsp.sim import sim_combined
    >>> sig = sim_combined(n_seconds=10, fs=500,
    ...                    components={'sim_powerlaw': {}, 'sim_oscillation': {'freq': 10}})
    >>> freqs, spec = compute_spectrum_welch(sig, fs=500)
    """

    # Calculate the short time Fourier transform with signal.spectrogram
    nperseg, noverlap = check_spg_settings(fs, window, nperseg, noverlap)
    freqs, _, spg = spectrogram(sig, fs, window, nperseg, noverlap)

    # Throw out outliers if indicated
    if outlier_percent is not None:
        spg = discard_outliers(spg, outlier_percent)

    # Average across windows
    spectrum = get_avg_func(avg_type)(spg, axis=-1)

    # Trim spectrum, if requested
    if f_range:
        freqs, spectrum = trim_spectrum(freqs, spectrum, f_range)

    return freqs, spectrum
Example #54
0
np.save(os.path.join(save_path, 'rate_time_series_V1.npy'), rate_binned)
np.save(os.path.join(save_path, 'rate_time_series_time.npy'), time)

# Parameters for Welch Power Spectral density and spectrogram
noverlap = 1000
nperseg = 1024
window = 'boxcar'
fs = 1.e3
"""
Compute spectrogram for panel A
"""
pop_rate = centralize(pop_rate, units=True)
f, t, Sxx = spectrogram(pop_rate,
                        fs=fs,
                        nperseg=4096,
                        noverlap=0,
                        window=window)

np.save(os.path.join(save_path, 'spectrogram_time.npy'), t)
np.save(os.path.join(save_path, 'spectrogram_freq.npy'), f)
np.save(os.path.join(save_path, 'spectrogram_Sxx.npy'), Sxx)
"""
Detect phases of low and high fluctuations:
Criterium: Integrated power for f in [0, 40] Hz
See Methods section in the paper.
"""
f_crit = 40.
ind = np.where(f <= f_crit)[0]
threshold = 125.
ind_raw = np.where(f <= f_crit)[0]
Example #55
0
        #df.iloc[get_outliers_mask(df[channels], std=3, iter_numb=3)] = 0
        #channels = channels[:32]

        channels = channels[:32]
        if fs == 1000:
            df = df.iloc[::2]
            fs = 500

        df = df.iloc[:13 * fs * 60]

        right = np.load(wdir + desc + '-RIGHT.npy')[0]
        left = np.load(wdir + desc + '-LEFT.npy')[0]

        x = np.dot(df[channels], left)

        f, t, Sxx = signal.spectrogram(x, fs, scaling='spectrum', nperseg=fs*10, nfft=fs*10, noverlap=10*int(fs*0.8))
        Sxx = Sxx**0.5
        print(f.shape, t.shape, Sxx.shape)


        norm = np.median(Sxx[:, t <= 120], 1)
        bandpow = (Sxx/norm[:, None] - 1)*100

        # mot = np.median(Sxx[:, (t <= 240) & (t > 120)], 1)
        specs.append(bandpow)

        # fig = plt.figure()
        # ax = plt.pcolormesh(t / 60, f, (bandpow ** 0.5), cmap='RdBu_r', vmin=0.5, vmax=1.5)
        #
        # plt.xlabel('Time [s]')
        # plt.ylabel('Frequency [Hz]')
Example #56
0
def get_step_spectograms():
    global CURRENT_INDEX
    global SPECTOGRAMS, LABELS

    # CLear the RAM.
    SPECTOGRAMS = []

    n = len(FILE_NAMES)
    # Keep openning the connection to make sure it is not lost.
    # Keep openning the connection to make sure it is not lost.
    client = paramiko.SSHClient()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    client.connect(MACHINE_NAME, username=USERNAME, password=PASSWORD)
    sftp = client.open_sftp()
    # Read .wav files from STEPS folders form teh DIRECTORY_NAME.
    for i in range(CURRENT_INDEX, CURRENT_INDEX + STEPS):

        # Make sure that you dont go beyong the number of folders found in the
        #   DIRECOTRY_NAME.
        if (i >= n):
            break

        # The command is listing all the .wav files on the folder FILE_NAMES[i]
        cmd = ('ls ' + DIRECTORY_NAME + '/' + FILE_NAMES[i] + '/' +
               FILE_NAMES[i][:4] + '_audio_formatted_and_segmented_downloads/')
        stdin, stdout, stderr = client.exec_command(cmd)
        wav_files_in_directory = (
            stdout.read().decode('utf-8')).split("\n")[:-1]

        count = 0
        check = 10
        for wav_filename in wav_files_in_directory:
            # print count

            if (count == check):
                print("Finished reading", count, ".wav files from",
                      FILE_NAMES[i][:4])
                check += 10
                #count = 0

            # Read the audiofile
            wave_file = sftp.open(
                '/home/sshaar/' + FILE_NAMES[i] + '/' + FILE_NAMES[i][:4] +
                '_audio_formatted_and_segmented_downloads/' + wav_filename,
                'r')

            rate, data = wavfile.read(wave_file)

            # Specification for the spectorgram. You may change this function if wanted.
            #   I think we need to modify this more.
            ##            if ("xaa" in FILE_NAMES[i]):
            ##                fs = 1600
            ##            else:
            fs = 16000
            #fs = 1600
            f, t, Sxx = signal.spectrogram(data,
                                           fs=fs,
                                           window=signal.get_window(
                                               "boxcar", int(fs * 0.025)),
                                           nperseg=int(fs * .025),
                                           noverlap=int(fs * .01),
                                           nfft=2048)

            Sxx = np.resize(Sxx, (1025, 600))
            #Sxx = Sxx.reshape((1,) + Sxx.shape)
            print("SPECTO SHAPE", Sxx.shape)
            # Add the spectogram of the audiofile to the list.
            SPECTOGRAMS.append(Sxx)  # I am not sure anout this!
            #print (SPECTOGRAMS)
            print("file", wav_filename)
            t = wav_filename.split(".wav")
            t = t[0]
            t = t.split("@")[1]
            if (t not in UNIQUETAGS):
                UNIQUETAGS.append(t)
            #print (t)
            LABELS.append(t)
            #print ("TAG", t)
            #print ("SPECTO SHAPE", Sxx.shape)
            #print( "ALL SHAPE", SPECTOGRAMS.shape)
            wave_file.close()

            count += 1
            print(count)
            print(len(SPECTOGRAMS))

            if (count == 50):
                break

        print('Finished reading all .wav files in ' + FILE_NAMES[i][:4])

    client.close()
    CURRENT_INDEX += STEPS
Example #57
0
def spectrogram(path):
    data, sample_rate = sf.read(path)
    frequencies, times, spectrogram = signal.spectrogram(data, sample_rate)
    return spectrogram
Example #58
0
def full_analysis(radius_arr, dens_prof, f_sampling=125e6, sweep_time=25e-6,
                  f_probe_limits=(1, 1e11), full_output=False,
                  antenna_side='hfs', reflect_at_wall=True, pos_antenna=1.15,
                  vacuum_distance=0.1, num_points_delay=16):
    """
    TODO
    Parameters
    ----------
    radius_arr
    dens_prof
    f_sampling
    sweep_time
    f_probe_limits
    full_output
    antenna_side
    reflect_at_wall
    vacuum_distance
    num_points_delay

    Returns
    -------

    """

    n_points_fs = int(sweep_time * f_sampling)

    sweep_rate = (f_probe_limits[1] - f_probe_limits[0]) / sweep_time
    f_probe = np.linspace(f_probe_limits[0], f_probe_limits[1], n_points_fs)

    refract_index = refractive_matrix_O(dens_prof, f_probe)

    phi = phase_delay(f_probe, radius_arr, refract_index,
                      antenna_side=antenna_side,
                      reflect_at_wall=reflect_at_wall)
    tau_g = group_delay(f_probe, phi)

    beat_sig = beat_signal(f_probe, tau_g)

    f_spectrum, t_spectrum, spectrum = spectrogram(
        beat_sig, fs=f_sampling, nperseg=136, nfft=2048, noverlap=128
    )

    f_probe_spect, tau_g_spect = group_delay_from_spectrogram_O(
        f_spectrum, t_spectrum, spectrum, sweep_rate, f_probe_limits
    )

    f_probe, tau_g = initialize_group_delay(
        f_probe, tau_g,
        vacuum_distance=vacuum_distance, num_points_delay=num_points_delay
    )

    f_probe_spect, tau_g_spect = initialize_group_delay(
        f_probe_spect, tau_g_spect,
        vacuum_distance=vacuum_distance, num_points_delay=num_points_delay
    )

    radius_original, dens_original = abel_inversion(
        f_probe, tau_g, pos_antenna=pos_antenna, other_method=True
    )
    radius_spect, dens_spect = abel_inversion(
        f_probe_spect, tau_g_spect, pos_antenna=pos_antenna, other_method=True
    )

    if full_output:
        return {
            'f_sampling': f_sampling,
            'sweep_rate': sweep_rate,
            'f_probe': f_probe,
            'refract_index': refract_index,
            'phi': phi,
            'tau_g': tau_g,
            'beat_sig': beat_sig,
            'radius_calc': radius_original,
            'dens_calc': dens_original,
            'spectrum_data': {
                'f': f_spectrum, 't': t_spectrum, 'signal': spectrum,
                'tau_g': tau_g_spect, 'f_probe': f_probe_spect,
                'radius_calc': radius_spect, 'dens_calc': dens_spect
            }
        }
    else:
        return f_sampling, f_probe, beat_sig, sweep_rate, \
               radius_original, dens_original, radius_spect, dens_spect
Example #59
0
def _spectrogram_scipy_fourier(data,
                               fs,
                               nt,
                               nch,
                               fmin=None,
                               window=('tukey', 0.25),
                               deg=False,
                               nperseg=None,
                               noverlap=None,
                               detrend='linear',
                               stft=False,
                               boundary='constant',
                               padded=True,
                               warn=True):
    """ Return a spectrogram for each channel, and a common frequency vector

    The min frequency of interest fmin fixes the nb. of pt. per seg. (if None)
    The number of overlapping points is set to nperseg-1 if None
    The choice of the window type is a trade-off between:
        Spectral resolution between similar frequencies/amplitudes:
            =>
        Dynamic range (lots of != frequencies of != amplitudes):
            =>
        Compromise:
            => 'hann'
    """

    # Check inputs
    if nperseg is None and fmin is None:
        fmin = _fmin_coef * (fs / nt)
        if warn:
            msg = "nperseg and fmin were not provided\n"
            msg += "    => fmin automatically set to 10.*fs/nt:\n"
            msg += "       fmin = 10.*{0} / {1} = {2} Hz".format(fs, nt, fmin)
            warnings.warn(msg)

    # Format inputs
    if nperseg is None:
        assert fmin > fs / nt
        nperseg = int(np.ceil(fs / fmin))

    if nperseg % 2 == 1:
        nperseg = nperseg + 1
    if noverlap is None:
        noverlap = nperseg - 1
    n = int(np.ceil(np.log(nperseg) / np.log(2)))
    nfft = 2**n

    # Prepare output
    if stft:
        f, tf, ssx = scpsig.stft(data,
                                 fs=fs,
                                 window=window,
                                 nperseg=nperseg,
                                 noverlap=noverlap,
                                 nfft=nfft,
                                 detrend=detrend,
                                 return_onesided=True,
                                 boundary=boundary,
                                 padded=padded,
                                 axis=0)
    else:
        f, tf, ssx = scpsig.spectrogram(data,
                                        fs=fs,
                                        window=window,
                                        nperseg=nperseg,
                                        noverlap=noverlap,
                                        nfft=nfft,
                                        detrend=detrend,
                                        return_onesided=True,
                                        scaling='density',
                                        axis=0,
                                        mode='complex')

    # Split in list (per channel)
    lssx = np.split(ssx, np.arange(1, nch), axis=1)
    lssx = [ss.squeeze().T for ss in lssx]
    lpsd = [np.abs(ss)**2 for ss in lssx]
    lang = [np.angle(ss, deg=deg) for ss in lssx]

    return f, tf, lpsd, lang
Example #60
0
                       0.0001, 0.02, 1.3, 1.6)
    aqpq5Shimmer = call([sound, pointProcess], "Get shimmer (apq5)", 0, 0,
                        0.0001, 0.02, 1.3, 1.6)
    apq11Shimmer = call([sound, pointProcess], "Get shimmer (apq11)", 0, 0,
                        0.0001, 0.02, 1.3, 1.6)
    ddaShimmer = call([sound, pointProcess], "Get shimmer (dda)", 0, 0, 0.0001,
                      0.02, 1.3, 1.6)
    voice_report = call([sound, pitch, pointProcess], "Voice report", 0.0, 0.0,
                        f0min, f0max, 1.3, 1.6, 0.03, 0.45)

    return meanF0, stdevF0, localJitter, localabsoluteJitter, rapJitter, ppq5Jitter, ddpJitter, localShimmer, localdbShimmer, apq3Shimmer, aqpq5Shimmer, apq11Shimmer, ddaShimmer, voice_report


AudioFile = "/home/subhranil/Downloads/61e50f62_nohash_1.wav"
sample_rate, samples = wavfile.read(AudioFile)
frequencies, times, spectogram = signal.spectrogram(samples, sample_rate)
sound = parselmouth.Sound(AudioFile)
DFA = nolds.dfa(times)
PPE = entropy.shannon_entropy(times)
(meanF0, stdevF0, localJitter, localabsoluteJitter, rapJitter, ppq5Jitter,
 ddpJitter, localShimmer, localdbShimmer, apq3Shimmer, aqpq5Shimmer,
 apq11Shimmer, ddaShimmer,
 voice_report) = measurePitch(sound, 75, 500, "Hertz")

voice_report = voice_report.strip()

hnr = voice_report[984:989]
nhr = voice_report[941:953]

df = pd.DataFrame(np.column_stack([
    localJitter, localabsoluteJitter, rapJitter, ppq5Jitter, ddpJitter,