Exemple #1
0
def cross_corr_stack_self(stack, adj_ref = False, verbose = True, pivot_slice = 0):
    '''
    Align a stack to itself. If adj_ref is True, then align each slice to the neighboring slice preceeding it.
    Added: subpixel precision
    '''
    nz, ny, nx = stack.shape
    hy = ny//2
    hx = nx//2
    shift_coord = np.zeros([nz, 2])
    hann_w = signal.hann(nx)
    hann_h = signal.hann(ny)
    hfilter = np.outer(hann_h, hann_w)
    bpf = np.fft.fftshift(bpd(ny,nx))

    container_1 = pyfftw_container(ny, nx)
    container_2 = pyfftw_container(ny, nx)
    container_invx = pyfftw_container(ny, nx, bwd = True)
    if np.isscalar(pivot_slice):
        ref_frame = stack[pivot_slice]
    else:
        ref_frame = pivot_slice

    for ii in range(nz):
        shy, shx  = cross_corr_shift_frame(ref_frame, stack[ii], container_1, container_2, container_invx, filter_freq = 'han', filter_pattern = hfilter )[:2]
        if verbose:
            print("slice ", ii+1, '-->', shy, shx)
        shift_coord[ii] = np.array([-shy, -shx])
        # then we have to shift im 2 by (-shy, -shx)
        if adj_ref:
            # if the stack is aligned in the adjacent mode, them each slice should be updated in place; otherwise we can just return the shift coordinates.
            shifted_frame = interpolation.shift(stack[ii+1],shift = [-shy, -shx])
            ref_frame = shifted_frame
            stack[ii+1] = shifted_frame

    return shift_coord # Hmmmm, this is much nicer.
Exemple #2
0
def _get_window(start, end):
    """Return window which has length as much as parameter start - end."""
    from scipy.signal import hann
    window = 1 - np.r_[hann(4)[:2],
                       np.ones(np.abs(end - start) - 4),
                       hann(4)[-2:]].T
    return window
def window(f,start,stop,type='blackman'):
    """
    runs the data through a hamming window.
    @param f: The data matrix
    @param start: The start index of the hamming window.
    @param stop: The end index of the hamming window.
    """
    h=numpy.zeros(f.shape,dtype=float)

    if len(h.shape)==1:
        if type=='hamming':
            h[start:stop]=signal.hamming(stop-start)
        elif type=='blackman':
            h[start:stop]=signal.blackman(stop-start)
        elif type=='hann':
            h[start:stop]=signal.hann(stop-start)
        elif type=='blackmanharris':
            h[start:stop]=signal.blackmanharris(stop-start)
        elif type=='rectangular' or type=='rect' or type=='boxcar':
            h[start:stop]=signal.boxcar(stop-start)
    else:
        if type=='hamming':
            h[:,start:stop]=signal.hamming(stop-start)
        elif type=='blackman':
            h[:,start:stop]=signal.blackman(stop-start)
        elif type=='hann':
            h[:,start:stop]=signal.hann(stop-start)
        elif type=='blackmanharris':
            h[:,start:stop]=signal.blackmanharris(stop-start)
        elif type=='rectangular' or type=='rect' or type=='boxcar':
            h[:,start:stop]=signal.boxcar(stop-start)
    return numpy.multiply(f,h)
Exemple #4
0
    def __init__(self, path_to_file, scalar=1, windowed=True):
        self.path = path_to_file
        i = cv2.imread(self.path, cv2.IMREAD_UNCHANGED)
        if i is None:
            raise Exception

        if scalar != 1:
            i = cv2.resize(i, (0, 0), fx=scalar, fy=scalar)
            self.image = i.astype(float) / 255.0
        else:
            self.image = i.astype(float) / 255.0

        self.height, self.width, self.planes = np.shape(self.image)
        if self.planes < 4:
            z = np.ones((self.height, self.width, 4))
            z[:, :, 0:self.planes] = self.image
            self.image = z
            self.height, self.width, self.planes = np.shape(self.image)

        if windowed:
            win = np.vstack(hann(self.height)) * hann(self.width)
            for i in range(self.planes):
                self.image[:, :, i] *= win

        self.image[:, :, 0:3] *= 1. / linalg.norm(self.image[:, :, 0:3])  # normalize
Exemple #5
0
def basefreq(audiofile):
    """
    This function reads in the audio file and does the hann windowed fft of 
    the right input. It then smooths the output using a gaussian filter and
    then finds the peaks. It returns the peaks in the right audio channel since
    testing showed there was no significant difference in the two.
    """
    #read the data into an ndarray using scikits-audiolab        
    data, rate, enc = al.aiffread(audiofile)
    #split the left and right channel
    datar = data[:,1]
    datal = data[:,0]
    #take the fft of both of the channels with the hann window applied
    #the hann window reduces spectral leakage in the FFT     
    dftr = abs(fft.fft(datar*signal.hann(len(datar))))
    dftl = abs(fft.fft(datal*signal.hann(len(datal))))
    #compute the frequencies in the FFT
    freq = float(rate)/float(len(datar))
    freqs = np.arange(len(dftr)/2+99)*freq
    dftr = dftr[0:np.size(dftr)/2]
    dftl = dftl[0:np.size(dftr)/2]
    #smooth the fft with a gaussian
    c = signal.gaussian(100,20)
    dftr = signal.convolve(dftr,c)
    dftl = signal.convolve(dftl,c)
    #find the significant peaks in each channel
    peaksr = findpeaks(dftr,freqs)
    peaksl = findpeaks(dftl,freqs)
    #plot the output fft for testing
    #plt.plot(freqs,dftr)
    #plt.show()
    #print peaksr
    return peaksr
Exemple #6
0
def eliminate_stim_artifact(raw, events, event_id, tmin=-0.005,
                            tmax=0.01, mode='linear'):
    """Eliminates stimulations artifacts from raw data

    The raw object will be modified in place (no copy)

    Parameters
    ----------
    raw : Raw object
        raw data object.
    events : array, shape (n_events, 3)
        The list of events.
    event_id : int
        The id of the events generating the stimulation artifacts.
    tmin : float
        Start time of the interpolation window in seconds.
    tmax : float
        End time of the interpolation window in seconds.
    mode : 'linear' | 'window'
        way to fill the artifacted time interval.
        'linear' does linear interpolation
        'window' applies a (1 - hanning) window.

    Returns
    -------
    raw: Raw object
        raw data object.
    """
    if not raw._preloaded:
        raise RuntimeError('Modifying data of Raw is only supported '
                           'when preloading is used. Use preload=True '
                           '(or string) in the constructor.')
    events_sel = (events[:, 2] == event_id)
    event_start = events[events_sel, 0]
    s_start = int(np.ceil(raw.info['sfreq'] * tmin))
    s_end = int(np.ceil(raw.info['sfreq'] * tmax))

    picks = pick_types(raw.info, meg=True, eeg=True, eog=True, ecg=True,
                       emg=True, ref_meg=True, misc=True, chpi=True,
                       exclude='bads', stim=False, resp=False)

    if mode == 'window':
        window = 1 - np.r_[signal.hann(4)[:2],
                           np.ones(np.abs(s_end - s_start) - 4),
                           signal.hann(4)[-2:]].T

    for k in range(len(event_start)):
        first_samp = int(event_start[k]) - raw.first_samp + s_start
        last_samp = int(event_start[k]) - raw.first_samp + s_end
        data, _ = raw[picks, first_samp:last_samp]
        if mode == 'linear':
            x = np.array([first_samp, last_samp])
            f = interpolate.interp1d(x, data[:, (0, -1)])
            xnew = np.arange(first_samp, last_samp)
            interp_data = f(xnew)
            raw[picks, first_samp:last_samp] = interp_data
        elif mode == 'window':
            raw[picks, first_samp:last_samp] = data * window[np.newaxis, :]
    return raw
Exemple #7
0
 def test_basic(self):
     assert_allclose(signal.hann(6, sym=False),
                     [0, 0.25, 0.75, 1.0, 0.75, 0.25])
     assert_allclose(signal.hann(6, True),
                     [0, 0.3454915028125263, 0.9045084971874737,
                      0.9045084971874737, 0.3454915028125263, 0])
     assert_allclose(signal.hann(7),
                     [0, 0.25, 0.75, 1.0, 0.75, 0.25, 0])
Exemple #8
0
def eliminate_stim_artifact(raw, events, event_id, tmin=-0.005, tmax=0.01, mode="linear"):
    """Eliminates stimulations artifacts from raw data

    The raw object will be modified in place (no copy)

    Parameters
    ----------
    raw: Raw object
        raw data object
    events: array, shape (n_events, 3)
        The list of events
    event_id: int
        The id of the events generating the stimulation artifacts.
    tmin : float
        Start time before event in seconds
    tmax : float
        End time after event in seconds
    mode : 'linear' | 'window'
        way to fill the artifacted time interval
        'linear' does linear interpolation
        'window' applies a (1 - hanning) window

    Returns
    -------
    raw: Raw object
        raw data object
    """
    if not raw._preloaded:
        raise RuntimeError(
            "Modifying data of Raw is only supported "
            "when preloading is used. Use preload=True "
            "(or string) in the constructor."
        )
    events_sel = events[:, 2] == event_id
    event_start = events[events_sel, 0]
    s_start = np.ceil(raw.info["sfreq"] * np.abs(tmin))
    s_end = np.ceil(raw.info["sfreq"] * tmax)

    picks = pick_types(raw.info, meg=True, eeg=True)

    if mode == "window":
        window = 1 - np.r_[signal.hann(4)[:2], np.ones(s_end + s_start - 4), signal.hann(4)[-2:]].T

    for k in range(len(event_start)):
        first_samp = event_start[k] - raw.first_samp - s_start
        last_samp = event_start[k] - raw.first_samp + s_end
        data, _ = raw[picks, first_samp:last_samp]
        if mode == "linear":
            x = np.array([first_samp, last_samp])
            f = interpolate.interp1d(x, data[:, (0, -1)])
            xnew = np.arange(first_samp, last_samp)
            interp_data = f(xnew)
            raw[picks, first_samp:last_samp] = interp_data
        elif mode == "window":
            raw[picks, first_samp:last_samp] = data * window[np.newaxis, :]
    return raw
Exemple #9
0
 def test_basic(self):
     assert_allclose(signal.hann(6, sym=False),
                     [0, 0.25, 0.75, 1.0, 0.75, 0.25])
     assert_allclose(signal.hann(7, sym=False),
                     [0, 0.1882550990706332, 0.6112604669781572,
                      0.9504844339512095, 0.9504844339512095,
                      0.6112604669781572, 0.1882550990706332])
     assert_allclose(signal.hann(6, True),
                     [0, 0.3454915028125263, 0.9045084971874737,
                      0.9045084971874737, 0.3454915028125263, 0])
     assert_allclose(signal.hann(7),
                     [0, 0.25, 0.75, 1.0, 0.75, 0.25, 0])
Exemple #10
0
def _window_evoked(evoked, size):
    """Window evoked (size in seconds)"""
    if isinstance(size, (float, int)):
        lsize = rsize = float(size)
    else:
        lsize, rsize = size
    evoked = deepcopy(evoked)
    sfreq = float(evoked.info["sfreq"])
    lsize = int(lsize * sfreq)
    rsize = int(rsize * sfreq)
    lhann = signal.hann(lsize * 2)
    rhann = signal.hann(rsize * 2)
    window = np.r_[lhann[:lsize], np.ones(len(evoked.times) - lsize - rsize), rhann[-rsize:]]
    evoked.data *= window[None, :]
    return evoked
Exemple #11
0
def drop_regn(tvec, y, drv_th, len_th = 3, smwd = 5):

    dy = deriv(y)
    win = signal.hann(smwd)
    smdy = signal.convolve(dy, win, mode = 'same')/sum(win)

    drop = []
    for i,d in enumerate(smdy):
        if d < drv_th:
            drop.append(i)

    dif_drop = deriv(drop)
    regn = []
    for i,dd in enumerate(dif_drop):
        if dd == 1:
            regn.append(drop[i])
        else:
            if len(regn) >= len_th - 1:
                regn.append(drop[i])
                break
            else:
                regn = []
    if len(regn):
        return tvec[regn[0]],tvec[regn[-1]]
    else:
        return None
Exemple #12
0
def select_events(nevents,nfeatures):
    global groups
    fftbins = 8192
    featurewidth = 16
    print "Selecting %d random spectral features.." % nfeatures
    feature_bins = np.random.randint(featurewidth/2,(fftbins/8),nfeatures)
    print "Selecting %d random audio events.." % nevents
    events = np.random.randint(0,len(faudio)-grain_mid,nevents)
    # Initialise features array with the first variable as index
    features = np.zeros((nfeatures+1,nevents))
    features[0] = np.arange(0,nevents)
    print "Computing audio event spectrograms.."
    # For each event..
    for i in range(0,nevents):
        # Calculate spectrogram for the event
        _fftevent = faudio[events[i]:min(events[i]+grain_mid,len(faudio))]*sig.hann(grain_mid)
        mags = abs(rfft(_fftevent,fftbins))
        mags = 20*log10(mags) # dB
        mags -= max(mags) # normalise to 0dB max
        # Calculate each feature for this event
        for j in range(0,nfeatures):
            features[j+1][i] = abs(np.mean(abs(mags[(feature_bins[j]-featurewidth/2):(feature_bins[j]+featurewidth/2)])))
    print "Clustering events with K-Means algorithm.."
    groups = kmeans(np.transpose(features[1:,:]),tracks,minit='points',iter=30)[1]
    return [events,groups]
    def CalculateSpectrumBlock(self, region):
        '''Return the power spectrum of a region based on a Welch-Bartlett method.
        The block used in each FFT is half the length of the total window.
        The step size is half the size of the FFT window.
        Average over A-lines.

        This function assumes the size of the region is divisible by 4.

        It uses a zoomed in FFT to compute the power spectrum.  The zoomed in FFT is given by the
        chirpz transform.
        '''
        from scipy.signal import hann,convolve
        import numpy
        from chirpz import chirpz
        points = region.shape[0]
        points -= points%4
        points /= 2
        #######SAMPLE REGION#############
        maxDataWindow = region[0:2*points, :]

        #compute 3 fourier transforms and average them
        #Cutting off the zero-value end points of the hann window
        #so it matches Matlab's definition of the function
        windowFunc = hann(points+2)[1:-1].reshape(points,1)
        fftSample = numpy.zeros(points)
        for f in range(3):
            dataWindow = maxDataWindow[(points/2)*f:(points/2)*f + points, :]*windowFunc

            for l in range(dataWindow.shape[1]):
                fftSample += abs(chirpz(dataWindow[:,l], self.cztA, self.cztW, points))**2

        return fftSample
    def add_grain_1(self, src_path, tar_path, src_start, src_end, tar_start, tar_end):
        """
        Windows a grain of audio and adds to target buffer.
        """
        window = signal.hann(src_end - src_start)

        print window.shape
Exemple #15
0
    def plot(self):
        self.amostrasporseg=15*5 #samples/sec
        frequenciasinal=5 #Hz
        omega= frequenciasinal
        self.z=2*np.pi*np.arange(0,2*np.pi,1/self.amostrasporseg)
        y=np.sin(self.z*omega)
        k=y*signal.hann(len(y))

        ylinha= 20*np.log10(abs(np.fft.fft(k,2048)))
        ylinha=np.tile(ylinha,3)

        zlinha=np.linspace(-2,4,len(ylinha))
        self.figure.subplots_adjust(bottom=.75)
        gs = gridspec.GridSpec(5, 1,height_ratios=[0.2,1,0.25,1,0.2])
        ax =self.figure.add_subplot(gs[1])
        self.plot2,=plt.plot(zlinha,ylinha)
        plt.title('Espectro do sinal')
        plt.xlabel(r'$\omega$')
        plt.ylabel(r'|X($\omega$)|')

        plt.xticks((-2,-1,0,1,2,3,4),[r'$-2\pi$',r'$-\pi$','0',r'$\pi$',r'$2\pi$',r'$3\pi$',r'$4\pi$'])
        #ax.set_xticks([-0.0442,0.0442], minor=True)
        ax.xaxis.grid(True,which='major',linewidth=0.75,color='k',linestyle='--')
        self.beta=self.figure.add_subplot(gs[3])
        self.plot3,=plt.plot(self.z,y,label='Amostragem Correta')
        plt.plot(self.z,y,'o',label='Amostragem Fixa')
        plt.legend(loc='upper right')
        self.beta.set_xlabel(r't')
        self.beta.set_ylabel(r'x(t)')
        self.beta.set_xlim([0,2*np.pi])
        self.figure.tight_layout()
Exemple #16
0
def get_spectral_magnitude(y_data,time_data, fs):
    tStep = np.max(time_data)/len(time_data)
    timeV = np.arange(0, np.max(time_data), tStep)
    numsamp = 512 #timeDomainVectorLength(timeV)
    if (len(y_data) < numsamp):
        y_data = np.resize(y_data, (numsamp,))
    window  = hann(numsamp)
    ## setup the fft spectrum arrays
    mag_spectrum = np.zeros([numsamp,int(np.ceil(float(len(timeV))/numsamp))])
    #print 'time.len= %d, numsamp=%d, loop:%d' % (len(timeV), numsamp, int(np.ceil(float(len(timeV))/numsamp)))
    for k in range(0,int(np.ceil(float(len(timeV))/numsamp))):
        slice_dat    = y_data[k*numsamp:numsamp*(k+1)]
        
        if (len(slice_dat) < numsamp):
            if (len(slice_dat) < numsamp/2): # WE DISCARDS LAST SLICE POINTS IF < NUMSAMP/2
                break;
            slice_dat = np.resize(slice_dat,(numsamp,))
        #multiply it with the window and transform it into frequency domain
        spectrum_dat = fft(slice_dat*window);
        #get the spectrum mag @ each of the 256 frequency points and store it
        #print 'k:',k,' spectrum_dat.len:',len(spectrum_dat)
        mag_spectrum[:,k]= 20 * np.log10(abs(spectrum_dat))
        mag_spectrum[:,k]= abs(spectrum_dat)
    #print "fs= %.4g, NFFT= %ld, y_data.shape= %d, mag_spectrum= %dx%d" % (fs, numsamp,np.shape(y_data)[0], np.shape(mag_spectrum)[0],np.shape(mag_spectrum)[1])
    ## DOUBLE CHECK  THE SIZE OF THE MATRIX
    avg_fft_foreach = np.mean(mag_spectrum, axis=1)
    #    print "np.shape(avg_fft_foreach):", np.shape(avg_fft_foreach)
    return avg_fft_foreach
Exemple #17
0
    def signal(self, fs, atten, caldb, calv):
        if self._filename is None:
            # allow lack of file to not cause error, catch in GUI when necessary?
            logger = logging.getLogger('main')
            logger.warn('Vocalization signal request without a file')
            return np.array([0,0])

        if not self._findFile():
            return np.array([0,0])

        fs, wavdata = audioread(self._filename)
        if fs != fs:
            print 'specified', fs, 'wav file', fs
            raise Exception("specified samplerate does not match wav stimulus")

        #truncate to nears ms
        duration = float(len(wavdata))/fs
        # print 'duration {}, desired {}'.format(duration, np.trunc(duration*1000)/1000)
        desired_npts = int((np.trunc(duration*1000)/1000)*fs)
        # print 'npts. desired', len(wavdata), desired_npts
        wavdata = wavdata[:desired_npts]

        amp_scale = signal_amplitude(wavdata, fs)

        signal = ((wavdata/amp_scale)*self.amplitude(caldb, calv))

        if self._risefall > 0:
            rf_npts = int(self._risefall * fs) / 2
            wnd = hann(rf_npts*2) # cosine taper
            signal[:rf_npts] = signal[:rf_npts] * wnd[:rf_npts]
            signal[-rf_npts:] = signal[-rf_npts:] * wnd[rf_npts:]
            
        return signal
def average_fft(x, fft_size):
    n = len(x) // fft_size * fft_size
    tmp = x[:n].reshape(-1, fft_size) 
    tmp *= signal.hann(fft_size, sym=0)
    xf = np.abs(np.fft.rfft(tmp)/fft_size)
    avgf = np.average(xf, axis=0)
    return 20*np.log10(avgf)
Exemple #19
0
    def signal(self, fs, atten, caldb, calv):
        npts = self._duration*fs
        # start with full spectrum white noise and band-pass to get desired 
        # frequency range
        signal = self._noise[:npts]
        
        # band frequency cutoffs
        delta = 10**(3./(10.*(2*self._width)))
        low_freq = self._center_frequency / delta
        high_freq = self._center_frequency * delta
        # scipy butter function wants frequencies normalized between 0. and 1.
        nyquist = fs/2.
        low_normed = low_freq / nyquist
        high_normed = high_freq / nyquist

        order, wn = buttord([low_normed, high_normed], [low_normed-0.05, high_normed+0.05], 1, 40)

        # print 'CUTOFFS', low_freq, high_freq
        # print 'ORDER WN', order, wn, low_normed, high_normed

        b, a = butter(order, wn, btype='band')
        signal = lfilter(b, a, signal)

        if self._risefall > 0:
            rf_npts = int(self._risefall * fs) / 2
            wnd = hann(rf_npts*2) # cosine taper
            signal[:rf_npts] = signal[:rf_npts] * wnd[:rf_npts]
            signal[-rf_npts:] = signal[-rf_npts:] * wnd[rf_npts:]

        return signal
Exemple #20
0
def apodization(mzs,intensities,w_size=10):
    import scipy.signal as signal
    win = signal.hann(w_size)
    win = signal.slepian(w_size,0.3)
    intensities = signal.fftconvolve(intensities, win, mode='same') / sum(win)
    intensities[intensities<1e-6]=0
    return intensities
Exemple #21
0
def test_continuous_regression_with_overlap():
    """Test regression with overlap correction."""
    signal = np.zeros(100000)
    times = [1000, 2500, 3000, 5000, 5250, 7000, 7250, 8000]
    events = np.zeros((len(times), 3), int)
    events[:, 2] = 1
    events[:, 0] = times
    signal[events[:, 0]] = 1.
    effect = hann(101)
    signal = np.convolve(signal, effect)[:len(signal)]
    raw = RawArray(signal[np.newaxis, :], mne.create_info(1, 100, 'eeg'))

    assert_allclose(effect, linear_regression_raw(
        raw, events, {1: 1}, tmin=0)[1].data.flatten())

    # test that sklearn solvers can be used
    from sklearn.linear_model.ridge import ridge_regression

    def solver(X, y):
        return ridge_regression(X, y, alpha=0.)
    assert_allclose(effect, linear_regression_raw(
        raw, events, tmin=0, solver=solver)['1'].data.flatten())

    # test bad solvers
    def solT(X, y):
        return ridge_regression(X, y, alpha=0.).T
    assert_raises(ValueError, linear_regression_raw, raw, events, solver=solT)
    assert_raises(ValueError, linear_regression_raw, raw, events, solver='err')
    assert_raises(TypeError, linear_regression_raw, raw, events, solver=0)
Exemple #22
0
def plot_specgram(ax, data, fs, nfft=256, noverlap=128, window='hann',
                  cmap='jet', interpolation='bilinear', rasterized=True):

    if window not in SPECGRAM_WINDOWS:
        raise ValueError("Window not supported")

    elif window == "boxcar":
        mwindow = signal.boxcar(nfft)
    elif window == "hamming":
        mwindow = signal.hamming(nfft)
    elif window == "hann":
        mwindow = signal.hann(nfft)
    elif window == "bartlett":
        mwindow = signal.bartlett(nfft)
    elif window == "blackman":
        mwindow = signal.blackman(nfft)
    elif window == "blackmanharris":
        mwindow = signal.blackmanharris(nfft)

    specgram, freqs, time = mlab.specgram(data, NFFT=nfft, Fs=fs,
                                          window=mwindow,
                                          noverlap=noverlap)
    specgram = 10 * np.log10(specgram[1:, :])
    specgram = np.flipud(specgram)

    freqs = freqs[1:]
    halfbin_time = (time[1] - time[0]) / 2.0
    halfbin_freq = (freqs[1] - freqs[0]) / 2.0
    extent = (time[0] - halfbin_time, time[-1] + halfbin_time,
              freqs[0] - halfbin_freq, freqs[-1] + halfbin_freq)

    ax.imshow(specgram, cmap=cmap, interpolation=interpolation,
                            extent=extent, rasterized=rasterized)
    ax.axis('tight')
Exemple #23
0
def fft_transformation(data,scale):
	#hanning window to smooth the edge
	xs = np.multiply(data, signal.hann(DEFAULT_FFT_SIZE, sym=0))
	#fft transfer
	xf = np.abs(np.fft.rfft(xs))
	#200HZ ~ 2000HZ, 56 * 32Hz(size) , scale: 44100/16384 = 2.7
	xfp = 20*np.log10(np.clip(xf, 1e-20, 1e100))
	
	sub_fin = 0L
	sumdb=0
	num=0
	for n in  xrange(1,FIN_BIT+1):
		p1 = 0
		p2 = 0
		if len(table)< 32:
			b0 = 700*(10**((n-1)*mel/2596)-1)
			b1 = 700*(10**((n+1)*mel/2596)-1)
			table.update({n:[b0,b1]})
		else:
			b0 = table[n][0]
			b1 = table[n][1]
		for i in  xrange(int(b0),int((b1+1))):
			fp = xfp[int(i/scale)]
			p1 += fp*i
			p2 += fp
			num += 1
		sumdb += p2
		if (p1/p2-(b0+b1)/2)/(b1-b0) >= 0:
			sub_fin = sub_fin | (1<<(n-1))

	return sub_fin,sumdb/num
Exemple #24
0
def updatePic(i):
    global data, wbuffer , line
    stream.write(data)
    data = wf.readframes(CHUNK)

    data16=np.fromstring(data,dtype=np.int16)
    wbuffer[:CHUNK]=wbuffer[CHUNK:]
    wbuffer[CHUNK:]=data16
    wbuffer=np.reshape(wbuffer, CHUNK*2)
    wbuffer *= signal.hann(CHUNK*2, sym=0) #128
    n=len(data16)
    k=np.arange(n)
    T=n/SAMPLE_RATE
    frq=k/T
    frq=frq[range(int(n/2))]
    #import pdb ; pdb.set_trace()
    fftdata=np.fft.fft(data16)
    reduced=reduce_noise(fftdata)
    dispfft=(fftdata/n)[range(int(n/2))]
    dispfft_reduced=(reduced/n)[range(int(n/2))]
    #ifftdata=np.fft.ifft(fftdata).real
    ifftdata=np.fft.ifft(reduced).real
    data=ifftdata.astype(np.int16).tostring()
    

    line.set_data(frq,abs(dispfft)) # plotting the spectrum
    line1.set_data(frq, abs(dispfft_reduced))
    return line
Exemple #25
0
def select_events(nevents,nfeatures):
    global groups
    fftbins = 8192
    featurewidth = 16
    print "Selecting %d random spectral features.." % nfeatures
    feature_bins = np.random.randint(featurewidth/2,(fftbins/8),nfeatures)
    print "Selecting %d random audio events.." % nevents
    events = np.random.randint(0,len(faudio)-grain_mid,nevents)
    # Initialise features array with the first variable as index
    features = np.zeros((14,nevents))
    features[0] = np.arange(0,nevents)
    print "Computing audio event spectrograms.."
    # For each event..
    for i in range(0,nevents):
        # Calculate spectrogram for the event
        _fftevent = faudio[events[i]:min(events[i]+1000,len(faudio))]*sig.hann(1000)
        mfcc = MFCC.extract(_fftevent)
        features[:,i] = np.append(i,mfcc)
        #powerspec = abs(fft(_fftevent,fftbins)) ** 2
        #melspec = np.dot(powerspec,melFilterBank(len(_fftevent)))
        #logspec = np.log(melspec)
        #mfcc = dct(logspec,type=2)
        #print mfcc
        # Calculate each feature for this event
        #for j in range(0,nfeatures):
        #    features[j+1][i] = abs(np.mean(abs(mags[(feature_bins[j]-featurewidth/2):(feature_bins[j]+featurewidth/2)])))
    print "Clustering events with K-Means algorithm.."
    groups = kmeans(np.transpose(features),tracks,minit='points',iter=30)[1]
    return [events,groups]
Exemple #26
0
def fft_filter3(Magcom, filt_start_ind=0, filt_end_ind=0, filt_func=hann):
    """hann windows the data before filtering and then divides by hann window at the end"""
    #newMagcom=Magcom #zeros(padlen*2+len(Magcom))
    #newMagcom[padlen:-padlen]=Magcom

    filt=filt_prep(len(Magcom), filt_start_ind, filt_end_ind, filt_func=filt_func)
    return fft.fft(hann_ifft(Magcom)*filt)/hann(len(Magcom))
Exemple #27
0
def data_w_hann(dt,frame=256):
    temp = []
    _t = sig.hann(frame)
    fx = frame*0.5
    #temp = [sum(np.array(dt[x*fx:x*fx+frame]*_t)**2) for x in range(int(len(dt)/fx -1))]
    temp = [np.log(sum(np.abs(dt[x*fx:x*fx+frame]*_t))) for x in range(int(len(dt)/fx -1))]
    return temp
Exemple #28
0
def show_magnitued(file_name):
    # read audio samples
    input_data = read(file_name)
    audio = input_data[1]
    print(audio)
    # apply a Hanning window
    window = hann(1024)
    audio = audio[0:1024] * window
    # fft
    mags = abs(rfft(audio))
    # convert to dB
    mags = 20 * scipy.log10(mags)
    # normalise to 0 dB max
    # mags -= max(mags)
    file = open('tmp.txt', 'w')
    for i in mags:
        file.write(str(i) + '\n')
    file.close()
    # plot
    plt.plot(mags)
    # label the axes
    plt.ylabel("Magnitude (dB)")
    plt.xlabel("Frequency Bin")
    # set the title
    plt.title(file_name + " Spectrum")
    plt.show()
Exemple #29
0
 def __init__(self, 
              X_mean=None,
              X_std=None,
              shuffle=0,
              use_n_gram=0,
              use_window=0,
              use_spec=0,
              load_phonetic_label=0,
              load_spk_info=0,
              frame_size=200,
              file_name="_timit",
              **kwargs):
     self.X_mean = X_mean
     self.X_std = X_std
     self.shuffle = shuffle
     self.use_n_gram = use_n_gram
     self.use_window = use_window
     self.use_spec = use_spec
     self.load_phonetic_label = load_phonetic_label
     self.load_spk_info = load_spk_info
     self.frame_size = frame_size
     self.file_name = file_name
     if use_window:
         if self.use_spec:
             if not is_power2(self.frame_size):
                  raise ValueError("Provide a number which is power of 2,\
                                    for fast speed of DFT.")
         if np.mod(self.frame_size, 2)==0:
             self.overlap = self.frame_size / 2
         else:
             self.overlap = (self.frame_size - 1) / 2
         self.window = signal.hann(self.frame_size)[None, :].astype(theano.config.floatX)
     super(TIMIT_h5, self).__init__(**kwargs)
Exemple #30
0
 def __init__(self,
              X_mean=None,
              X_std=None,
              shuffle=0,
              seq_len=8000,
              use_window=0,
              use_spec=0,
              frame_size=200,
              file_name='accent_tbptt',
              batch_size=64,
              range_start=0,
              range_end=None,
              **kwargs):
     self.X_mean = X_mean
     self.X_std = X_std
     self.shuffle = shuffle
     self.seq_len = seq_len
     self.use_window = use_window
     self.use_spec = use_spec
     self.frame_size = frame_size
     self.file_name = file_name
     if self.use_window:
         if self.use_spec:
             if not is_power2(self.frame_size):
                  raise ValueError("Provide a number which is power of 2,\
                                    for fast speed of DFT.")
         if np.mod(self.frame_size, 2)==0:
             self.overlap = self.frame_size / 2
         else:
             self.overlap = (self.frame_size - 1) / 2
         self.window = signal.hann(self.frame_size)[None, :].astype(theano.config.floatX)
     self.batch_size = batch_size
     self.range_start = range_start
     self.range_end = range_end
     super(Accent_h5, self).__init__(**kwargs)
# make sure the clip is float32
clip = n.array(clip, dtype=n.float32)

N_fft = 2048

M = 1000

# fftfreq gives frequency in Hz
# fftshift orders frequencies correctly
frequencies = n.fft.fftshift(n.fft.fftfreq(N_fft, d=1.0 / sr))

N_time = 1000
#N_time = int((len(clip)-N_fft)/M)-1
#print(N_time)
wfun = s.hann(N_fft)
S = n.zeros([N_fft, N_time], dtype=n.float32)

for i in range(N_time):
    signal = clip[(i * M):(i * M + N_fft)]
    S[:, i] = n.fft.fftshift(n.abs(n.fft.fft(wfun * signal))**2.0)

time_vector = n.arange(N_time) * M / sr

plt.pcolormesh(time_vector, frequencies, 10.0 * n.log10(S))
plt.xlabel("Time (s)")
plt.ylabel("Frequency (Hz)")
cb = plt.colorbar()
cb.set_label("Power (dB)")
plt.show()
Exemple #32
0
             
#Get data for each header
data = pd.read_csv('Records/EEGLogger.csv', sep=',', header=1)
data = data.as_matrix()
data = np.delete(data, np.s_[-1:], axis=1)

#(N/fs)-second(s) domain
fs = 128
N = 1280
n1 = to_seconds(np.arange(0, N), fs)
n2 = fftfreq(N, float(1)/fs)
nfreq = n2[:N/2]

#Highpass filter
##h = ifft(kaiser(N, 5.4))
h = np.concatenate([np.zeros(30), hann(N-60), np.zeros(30)]) 

#Sample data
s_data = to_volts(data[512:1792,idx_dict['IED_AF4']])
##s_data = np.concatenate([s_data, np.zeros(N-len(s_data))])
##print s_data

#Transform data
t_data = fft(s_data, N)
t_data /= float(N)

#Apply highpass filter to data
t_data_filtered = np.multiply(t_data, h)

#Power terms
t_data_conjugates = np.conjugate(t_data_filtered)
def restSign(sign, frameLen, noise_signal, parm=1):
    Ln = len(sign)
    Flen = frameLen // 2
    wnd = signal.hamming(frameLen)
    wnd2 = signal.hann(2047)
    U = wnd[frameLen // 2:] + wnd[:frameLen // 2]
    Out = zeros((Ln))
    Four = zeros((Ln))
    a = 0.5
    sign_ = np.concatenate((sign[:Flen], sign, sign[-Flen:]))
    max_val = max(sign)
    mean_val = mean(sign)
    Rn = autocorrelation(noise_signal - mean_val, frameLen,
                         max_val) * wnd * 0.5  #/1024
    plt.plot(Rn)
    plt.cla()
    hann2 = sgn.hann(2 * frameLen - 1)
    for Ind in range(0, Ln - frameLen, Flen):
        Span = np.float32(sign[Ind:Ind + frameLen])  #*wnd
        Span_ = np.float32(sign_[Ind:Ind + 2 * frameLen - 1])  #*hann2
        Ry = np.correlate(
            (Span_ - mean(Span)) / max_val,
            (Span - mean(Span)) / max_val, 'valid') * wnd * 0.5  #/1024
        k = 1
        Ry /= k
        plt.plot(Ry)
        plt.cla()
        if Ry.shape != Rn.shape:
            print(Ind, Ln, Ry.shape, Rn.shape)
        Rx = Ry - Rn
        plt.plot(Rx)
        plt.cla()
        RFy = fft(Ry)
        RFx = fft(Rx)
        H = RFx / RFy
        plt.plot(abs(H))
        plt.cla()

        FSpan = fft(Span * wnd)
        plt.plot(FSpan)
        plt.cla()
        AFSpan = abs(FSpan)
        NFour = AFSpan * abs(H)
        for LInd in range(frameLen):
            NFour[LInd] *= FSpan[LInd] / AFSpan[LInd]
        plt.plot(abs(NFour))
        plt.cla()
        for LInd in range(1, Flen):
            NFour[frameLen - LInd] = conj(NFour[LInd])
        NFour[0] = 0
        INFour = real(ifft(NFour))
        Out[Ind + Flen:Ind + frameLen] = INFour[Flen:]
        if Ind != 0:
            Out[Ind:Ind + Flen] = (
                INFour[:Flen] + Out[Ind:Ind + Flen]
            ) / U  # custom_hamm(Out[Ind:Ind + Flen], INFour[:Flen], frameLen)
        else:
            Out[Ind:Ind + Flen] = INFour[:Flen]
    # plt.figure()
    plt.plot(Out)
    plt.show()
    # plt.figure()
    return real(Out)
Exemple #34
0
    Train.loc[i,'MA_400MA_BB_high_mean'] = (Train.loc[i, 'Moving_average_700_mean'] + no_of_std * Train.loc[i, 'MA_400MA_std_mean']).mean()
    Train.loc[i,'MA_400MA_BB_low_mean'] = (Train.loc[i, 'Moving_average_700_mean'] - no_of_std * Train.loc[i, 'MA_400MA_std_mean']).mean()
    Train.loc[i, 'MA_1000MA_std_mean'] = x.rolling(window=1000).std().mean()
    Train.drop('Moving_average_700_mean', axis=1, inplace=True)
    
    Train.loc[i, 'q999'] = np.quantile(x,0.999)
    Train.loc[i, 'q001'] = np.quantile(x,0.001)
    
    Train.loc[i,'iqr'] = np.subtract(*np.percentile(x, [75, 25]))
    Train.loc[i,'iqr1'] = np.subtract(*np.percentile(x, [95, 5]))
    Train.loc[i,'ave10'] = stats.trim_mean(x, 0.1)
    
    
    hann_windows = [50, 150, 1500, 15000]
    for hw in hann_windows:
        Train.loc[i,f'Hann_window_mean_'+str(hw)] = (convolve(x, hann(hw), mode='same') / sum(hann(hw))).mean()

    Train.loc[i,'classic_sta_lta1_mean'] = classic_sta_lta(x, 500, 10000).mean()
    Train.loc[i,'classic_sta_lta2_mean'] = classic_sta_lta(x, 5000, 100000).mean()
    Train.loc[i,'classic_sta_lta3_mean'] = classic_sta_lta(x, 3333, 6666).mean()
    Train.loc[i,'classic_sta_lta4_mean'] = classic_sta_lta(x, 10000, 25000).mean()
    Train.loc[i,'classic_sta_lta5_mean'] = classic_sta_lta(x, 50, 1000).mean()
    Train.loc[i,'classic_sta_lta6_mean'] = classic_sta_lta(x, 100, 5000).mean()
    Train.loc[i,'classic_sta_lta7_mean'] = classic_sta_lta(x, 333, 666).mean()
    Train.loc[i,'classic_sta_lta8_mean'] = classic_sta_lta(x, 4000, 10000).mean()
    
    autocorr_lags = [1, 5, 10, 50, 100, 500, 1000, 5000, 10000]
    autoc = cross_corr(x,autocorr_lags)
    j=0
    for lag in autocorr_lags:
        Train.loc[i,'autocorr_'+str(lag)] = autoc[j][0]
Exemple #35
0
 def __init__(self, *args, **kwargs):
     self.windows = dict(hamming_asymmetric=lambda sz: scp_sig.hamming(sz, sym=False),
                         hamming_symmetric=lambda sz: scp_sig.hamming(sz, sym=True),
                         hann_asymmetric=lambda sz: scp_sig.hann(sz, sym=False),
                         hann_symmetric=lambda sz: scp_sig.hann(sz, sym=True))
Exemple #36
0
def create_features(seg_id, seg, X):
    xc = pd.Series(seg['acoustic_data'].values)
    zc = np.fft.fft(xc)

    X.loc[seg_id, 'mean'] = xc.mean()
    X.loc[seg_id, 'std'] = xc.std()
    X.loc[seg_id, 'max'] = xc.max()
    X.loc[seg_id, 'min'] = xc.min()

    # FFT transform values
    realFFT = np.real(zc)
    imagFFT = np.imag(zc)
    X.loc[seg_id, 'Rmean'] = realFFT.mean()
    X.loc[seg_id, 'Rstd'] = realFFT.std()
    X.loc[seg_id, 'Rmax'] = realFFT.max()
    X.loc[seg_id, 'Rmin'] = realFFT.min()
    X.loc[seg_id, 'Imean'] = imagFFT.mean()
    X.loc[seg_id, 'Istd'] = imagFFT.std()
    X.loc[seg_id, 'Imax'] = imagFFT.max()
    X.loc[seg_id, 'Imin'] = imagFFT.min()
    X.loc[seg_id, 'Rmean_last_5000'] = realFFT[-5000:].mean()
    X.loc[seg_id, 'Rstd__last_5000'] = realFFT[-5000:].std()
    X.loc[seg_id, 'Rmax_last_5000'] = realFFT[-5000:].max()
    X.loc[seg_id, 'Rmin_last_5000'] = realFFT[-5000:].min()
    X.loc[seg_id, 'Rmean_last_15000'] = realFFT[-15000:].mean()
    X.loc[seg_id, 'Rstd_last_15000'] = realFFT[-15000:].std()
    X.loc[seg_id, 'Rmax_last_15000'] = realFFT[-15000:].max()
    X.loc[seg_id, 'Rmin_last_15000'] = realFFT[-15000:].min()

    X.loc[seg_id, 'mean_change_abs'] = np.mean(np.diff(xc))
    X.loc[seg_id, 'mean_change_rate'] = np.mean(np.nonzero((np.diff(xc) / xc[:-1]))[0])
    X.loc[seg_id, 'abs_max'] = np.abs(xc).max()
    X.loc[seg_id, 'abs_min'] = np.abs(xc).min()

    X.loc[seg_id, 'std_first_50000'] = xc[:50000].std()
    X.loc[seg_id, 'std_last_50000'] = xc[-50000:].std()
    X.loc[seg_id, 'std_first_10000'] = xc[:10000].std()
    X.loc[seg_id, 'std_last_10000'] = xc[-10000:].std()

    X.loc[seg_id, 'avg_first_50000'] = xc[:50000].mean()
    X.loc[seg_id, 'avg_last_50000'] = xc[-50000:].mean()
    X.loc[seg_id, 'avg_first_10000'] = xc[:10000].mean()
    X.loc[seg_id, 'avg_last_10000'] = xc[-10000:].mean()

    X.loc[seg_id, 'min_first_50000'] = xc[:50000].min()
    X.loc[seg_id, 'min_last_50000'] = xc[-50000:].min()
    X.loc[seg_id, 'min_first_10000'] = xc[:10000].min()
    X.loc[seg_id, 'min_last_10000'] = xc[-10000:].min()

    X.loc[seg_id, 'max_first_50000'] = xc[:50000].max()
    X.loc[seg_id, 'max_last_50000'] = xc[-50000:].max()
    X.loc[seg_id, 'max_first_10000'] = xc[:10000].max()
    X.loc[seg_id, 'max_last_10000'] = xc[-10000:].max()

    X.loc[seg_id, 'max_to_min'] = xc.max() / np.abs(xc.min())
    X.loc[seg_id, 'max_to_min_diff'] = xc.max() - np.abs(xc.min())
    X.loc[seg_id, 'count_big'] = len(xc[np.abs(xc) > 500])
    X.loc[seg_id, 'sum'] = xc.sum()

    X.loc[seg_id, 'mean_change_rate_first_50000'] = np.mean(np.nonzero((np.diff(xc[:50000]) / xc[:50000][:-1]))[0])
    X.loc[seg_id, 'mean_change_rate_last_50000'] = np.mean(np.nonzero((np.diff(xc[-50000:]) / xc[-50000:][:-1]))[0])
    X.loc[seg_id, 'mean_change_rate_first_10000'] = np.mean(np.nonzero((np.diff(xc[:10000]) / xc[:10000][:-1]))[0])
    X.loc[seg_id, 'mean_change_rate_last_10000'] = np.mean(np.nonzero((np.diff(xc[-10000:]) / xc[-10000:][:-1]))[0])

    X.loc[seg_id, 'q95'] = np.quantile(xc, 0.95)
    X.loc[seg_id, 'q99'] = np.quantile(xc, 0.99)
    X.loc[seg_id, 'q05'] = np.quantile(xc, 0.05)
    X.loc[seg_id, 'q01'] = np.quantile(xc, 0.01)

    X.loc[seg_id, 'abs_q95'] = np.quantile(np.abs(xc), 0.95)
    X.loc[seg_id, 'abs_q99'] = np.quantile(np.abs(xc), 0.99)
    X.loc[seg_id, 'abs_q05'] = np.quantile(np.abs(xc), 0.05)
    X.loc[seg_id, 'abs_q01'] = np.quantile(np.abs(xc), 0.01)

    X.loc[seg_id, 'trend'] = add_trend_feature(xc)
    X.loc[seg_id, 'abs_trend'] = add_trend_feature(xc, abs_values=True)
    X.loc[seg_id, 'abs_mean'] = np.abs(xc).mean()
    X.loc[seg_id, 'abs_std'] = np.abs(xc).std()

    X.loc[seg_id, 'mad'] = xc.mad()
    X.loc[seg_id, 'kurt'] = xc.kurtosis()
    X.loc[seg_id, 'skew'] = xc.skew()
    X.loc[seg_id, 'med'] = xc.median()

    X.loc[seg_id, 'Hilbert_mean'] = np.abs(hilbert(xc)).mean()
    X.loc[seg_id, 'Hann_window_mean'] = (convolve(xc, hann(150), mode='same') / sum(hann(150))).mean()
    X.loc[seg_id, 'classic_sta_lta1_mean'] = classic_sta_lta(xc, 500, 10000).mean()
    X.loc[seg_id, 'classic_sta_lta2_mean'] = classic_sta_lta(xc, 5000, 100000).mean()
    X.loc[seg_id, 'classic_sta_lta3_mean'] = classic_sta_lta(xc, 3333, 6666).mean()
    X.loc[seg_id, 'classic_sta_lta4_mean'] = classic_sta_lta(xc, 10000, 25000).mean()
    X.loc[seg_id, 'Moving_average_700_mean'] = xc.rolling(window=700).mean().mean(skipna=True)
    X.loc[seg_id, 'Moving_average_1500_mean'] = xc.rolling(window=1500).mean().mean(skipna=True)
    X.loc[seg_id, 'Moving_average_3000_mean'] = xc.rolling(window=3000).mean().mean(skipna=True)
    X.loc[seg_id, 'Moving_average_6000_mean'] = xc.rolling(window=6000).mean().mean(skipna=True)
    ewma = pd.Series.ewm
    X.loc[seg_id, 'exp_Moving_average_300_mean'] = (ewma(xc, span=300).mean()).mean(skipna=True)
    X.loc[seg_id, 'exp_Moving_average_3000_mean'] = ewma(xc, span=3000).mean().mean(skipna=True)
    X.loc[seg_id, 'exp_Moving_average_30000_mean'] = ewma(xc, span=6000).mean().mean(skipna=True)
    no_of_std = 2
    X.loc[seg_id, 'MA_700MA_std_mean'] = xc.rolling(window=700).std().mean()
    X.loc[seg_id, 'MA_700MA_BB_high_mean'] = (
                X.loc[seg_id, 'Moving_average_700_mean'] + no_of_std * X.loc[seg_id, 'MA_700MA_std_mean']).mean()
    X.loc[seg_id, 'MA_700MA_BB_low_mean'] = (
                X.loc[seg_id, 'Moving_average_700_mean'] - no_of_std * X.loc[seg_id, 'MA_700MA_std_mean']).mean()
    X.loc[seg_id, 'MA_400MA_std_mean'] = xc.rolling(window=400).std().mean()
    X.loc[seg_id, 'MA_400MA_BB_high_mean'] = (
                X.loc[seg_id, 'Moving_average_700_mean'] + no_of_std * X.loc[seg_id, 'MA_400MA_std_mean']).mean()
    X.loc[seg_id, 'MA_400MA_BB_low_mean'] = (
                X.loc[seg_id, 'Moving_average_700_mean'] - no_of_std * X.loc[seg_id, 'MA_400MA_std_mean']).mean()
    X.loc[seg_id, 'MA_1000MA_std_mean'] = xc.rolling(window=1000).std().mean()

    X.loc[seg_id, 'iqr'] = np.subtract(*np.percentile(xc, [75, 25]))
    X.loc[seg_id, 'q999'] = np.quantile(xc, 0.999)
    X.loc[seg_id, 'q001'] = np.quantile(xc, 0.001)
    X.loc[seg_id, 'ave10'] = stats.trim_mean(xc, 0.1)

    for windows in [10, 100, 1000]:
        x_roll_std = xc.rolling(windows).std().dropna().values
        x_roll_mean = xc.rolling(windows).mean().dropna().values

        X.loc[seg_id, 'ave_roll_std_' + str(windows)] = x_roll_std.mean()
        X.loc[seg_id, 'std_roll_std_' + str(windows)] = x_roll_std.std()
        X.loc[seg_id, 'max_roll_std_' + str(windows)] = x_roll_std.max()
        X.loc[seg_id, 'min_roll_std_' + str(windows)] = x_roll_std.min()
        X.loc[seg_id, 'q01_roll_std_' + str(windows)] = np.quantile(x_roll_std, 0.01)
        X.loc[seg_id, 'q05_roll_std_' + str(windows)] = np.quantile(x_roll_std, 0.05)
        X.loc[seg_id, 'q95_roll_std_' + str(windows)] = np.quantile(x_roll_std, 0.95)
        X.loc[seg_id, 'q99_roll_std_' + str(windows)] = np.quantile(x_roll_std, 0.99)
        X.loc[seg_id, 'av_change_abs_roll_std_' + str(windows)] = np.mean(np.diff(x_roll_std))
        X.loc[seg_id, 'av_change_rate_roll_std_' + str(windows)] = np.mean(
            np.nonzero((np.diff(x_roll_std) / x_roll_std[:-1]))[0])
        X.loc[seg_id, 'abs_max_roll_std_' + str(windows)] = np.abs(x_roll_std).max()

        X.loc[seg_id, 'ave_roll_mean_' + str(windows)] = x_roll_mean.mean()
        X.loc[seg_id, 'std_roll_mean_' + str(windows)] = x_roll_mean.std()
        X.loc[seg_id, 'max_roll_mean_' + str(windows)] = x_roll_mean.max()
        X.loc[seg_id, 'min_roll_mean_' + str(windows)] = x_roll_mean.min()
        X.loc[seg_id, 'q01_roll_mean_' + str(windows)] = np.quantile(x_roll_mean, 0.01)
        X.loc[seg_id, 'q05_roll_mean_' + str(windows)] = np.quantile(x_roll_mean, 0.05)
        X.loc[seg_id, 'q95_roll_mean_' + str(windows)] = np.quantile(x_roll_mean, 0.95)
        X.loc[seg_id, 'q99_roll_mean_' + str(windows)] = np.quantile(x_roll_mean, 0.99)
        X.loc[seg_id, 'av_change_abs_roll_mean_' + str(windows)] = np.mean(np.diff(x_roll_mean))
        X.loc[seg_id, 'av_change_rate_roll_mean_' + str(windows)] = np.mean(
            np.nonzero((np.diff(x_roll_mean) / x_roll_mean[:-1]))[0])
        X.loc[seg_id, 'abs_max_roll_mean_' + str(windows)] = np.abs(x_roll_mean).max()
Exemple #37
0
def gerchberg2d(interferogram, mask_where_fringes_are, N_iter_max):
    """
    Extrapolates fringe pattern beyond mask, following Gerchberg algorithm.
    """

    ref = interferogram
    refh = interferogram * mask_where_fringes_are
    interf = mask_where_fringes_are

    ft_ref = np.fft.rfft2(ref)
    ft_refh = np.fft.rfft2(refh)

    S = ref.shape
    S = S[0]

    # k0x and R_in_k_space determination by gaussian fir
    y = (np.abs(ft_refh[0, :]))
    y = y / np.max(y)
    x = np.linspace(0, (len(y) - 1), len(y))
    maxInd = argrelextrema(y, np.greater)
    x, y = x[maxInd], y[maxInd]
    n = len(x)
    w = hann(n)
    y = y * w
    index_mean = np.argwhere(y == np.max(y))[0, 0]
    mean = maxInd[0][index_mean]
    sigma = np.sum(y * (x - mean)**2) / n
    try:
        popt, pcov = curve_fit(gaus,
                               x,
                               y,
                               p0=[y[index_mean], mean, sigma],
                               maxfev=1100)

        # popt, pcov = curve_fit(gaus, x, y, p0 = [1, mean, sigma],maxfev=1100)
    except:
        popt, pcov = curve_fit(gaus, x, y, maxfev=1100)
    '''
    try:
        popt, pcov = curve_fit(gaus, x, y)
    except RuntimeError:
        try:
            popt, pcov = curve_fit(gaus, x, y, p0 = [1, mean, sigma])
        except:
            try:
                popt, pcov = curve_fit(gaus, x[1:], y[1:], p0 = [1, mean, sigma])
            except:
                popt, pcov = curve_fit(gaus, x[1:], y[1:])
        #popt, pcov = curve_fit(gaus, x[5:], y[5:])#, p0 = [1, mean, sigma])
        #popt, pcov = curve_fit(gaus, x, y, p0 = [1, mean, sigma])
    #except RuntimeError:
        #popt, pcov = curve_fit(gaus, x[5:], y[5:])#, p0 = [1, mean, sigma])
    #except RuntimeError:
        #popt = [mean, sigma]
    '''

    k0x, k0y = popt[1], 0
    R_in_k_space = popt[2]  #*2.5

    kx, ky = np.meshgrid(range(int(S / 2 + 1)), range(S))

    # lugar_a_conservar son dos cuartos de circulo
    # centrados en 0,0 y en 0,1024
    cuarto_superior = ((kx - k0x)**2 + (ky - (S - k0y))**2 <= R_in_k_space**2)
    cuarto_inferior = ((kx - k0x)**2 + (ky - (0 - k0y))**2 <= R_in_k_space**2)
    lugar_a_conservar = cuarto_inferior + cuarto_superior
    lugar_a_anular = 1 - lugar_a_conservar

    # non-fancy indexing es mejor
    lugar_a_anular = lugar_a_anular.nonzero()
    interf = interf.nonzero()

    En = np.zeros(N_iter_max + 1)

    ii = 0
    while ii <= N_iter_max:
        # print(ii)
        ft_refh[lugar_a_anular] = 0
        refhc = np.fft.irfft2(ft_refh)
        refhc[interf] = refh[interf]
        ft_refh = np.fft.rfft2(refhc)
        En[ii] = np.sum(np.abs(ft_refh))
        if ii > 0 and En[ii - 1] < En[ii]:
            break
        ii += 1
    En = En[0:ii]

    refhc = np.real(refhc)
    refhc[interf] = ref[interf]

    return refhc
def voltage_scan_animation_driver(folderpath,
                                  filename,
                                  dec,
                                  big=False,
                                  subtitles=False):
    '''
    Create a deluxe visualization based on the given voltage scan:
        - Voltage trace animated based on input voltage
        - Bifurcation diagram
        - Frequency spectrum animated based on input voltage
        - Frequency waterfall diagram
    Arguments:
        folderpath -- path to voltage scan results folder
        filename   -- name of voltage scan to analyze (.npz)
        dec        -- the decimation at which the scan was taken
        big        -- if `True`, produces 1080p graphic; 
                      else creates standard 5"x3" graphic
        subtitles  -- if `True`, put descriptive titles on the subfigures
    Effects:
        produce the animation;
        save said animation to disk (.mp4)
    '''
    with np.load(folderpath + filename) as scan:
        V, s = scan['V'], scan['s']
        infreq = int(filename.split('.')[0][1:])

        d = dec / (125e6)  # sample length (s)
        t = np.arange(0, 2**14 * d, d)

        ffts = np.fft.rfft(s, axis=1)
        fft_freqs = np.fft.rfftfreq(s.shape[1], d)

        bif_x, bif_y = get_bifurcation_diagram(V, s, 400 // dec)

        f, _, S = sps.spectrogram(np.ravel(s),
                                  fs=(1 / d),
                                  nfft=2**14,
                                  noverlap=0,
                                  window=sps.hann(2**14))

        #        for i in range(0, len(V), len(V)//20):
        #            trace = s[i,:]
        ##            plt.figure()
        ##            plt.plot(t, trace)
        ##            plt.show()
        #            plt.figure()
        #            plt.title(V[i])
        #            plt.plot(np.fft.rfftfreq(len(trace), d),
        #                     np.abs(np.fft.rfft(trace)))
        #            plt.xlim(0, infreq * 5)
        #            plt.show()

        fig = plt.figure(figsize=(20, 11.25) if big else (5, 3), dpi=96)
        fig.suptitle(f"$f$={infreq}Hz, $V$={V[0]}")
        ax1 = fig.add_subplot(221)
        ax1.set(xlim=(0.001, 0.002) if dec == 64 else None,
                ylim=(np.min(s), np.max(s)),
                xlabel="$t$ (s)",
                ylabel="$V$ (V)",
                title="Trace" if subtitles else "")
        ax2 = fig.add_subplot(222)
        ax2.set(xlim=(V[0], V[-1]),
                xlabel="$V_{\mathrm{in}}$ (V)",
                ylabel="$V_{\mathrm{out}}$ (V)",
                title="Bifurcaion Diagram" if subtitles else "")
        ax3 = fig.add_subplot(223)
        ax3.set(xlim=(0, infreq * 2),
                ylim=(np.min(np.abs(ffts)), np.max(np.abs(ffts))),
                yticks=[],
                yticklabels=[],
                xlabel="$f$ (Hz)",
                title="Spectrum" if subtitles else "")
        ax4 = fig.add_subplot(224)
        ax4.set(xlabel="$V_{\mathrm{in}}$ (V)",
                ylabel="$f$ (kHz)",
                ylim=(0, 2 * (infreq / 1_000)),
                title="Waterfall" if subtitles else "")
        trace = ax1.plot(t, s[0, :])[0]
        bif = ax2.plot(bif_x, bif_y, '.', alpha=1 / dec)
        line1 = ax2.axvline(V[0], color='r')
        fft = ax3.plot(fft_freqs, np.abs(ffts[0, :]))[0]
        wfall = ax4.pcolormesh(V, f / 1_000, S, norm=colors.LogNorm())
        line2 = ax4.axvline(V[0], color='r', alpha=0.3)

        def animate(i):
            fig.suptitle(f"$f$={infreq}Hz, $V$={V[i]}")
            trace.set_ydata(s[i, :])
            line1.set_xdata([V[i]] * 2)
            fft.set_ydata(np.abs(ffts[i, :]))
            line2.set_xdata([V[i]] * 2)

        anim = anm.FuncAnimation(fig, animate, interval=500, frames=len(V))
        anim.save(folderpath + filename.split(".")[0] + "-anim.mp4")
        #        anim.save(folderpath + filename.split(".")[0] + "-anim-small.gif",
        #                  anm.FFMpegWriter(codec="gif", fps=1)
        #                  )

        plt.draw()
        plt.show()
def multifrequency_voltage_scan_dual_animation_driver(folderpath,
                                                      dec,
                                                      alpha,
                                                      forward=True,
                                                      colorbar=True,
                                                      big=False):
    '''
    Create a visualization of both a bifurcation diagram and frequency 
    waterfall spectrum, animated based on input frequeny, from the given
    voltage scans
    Arguments:
        folderpath -- path to voltage scan results folder
        dec        -- the decimation at which the scans were taken
        forward    -- if `True`, selects forward scans; 
                      else selects reverse scans
        colorbar   -- if `True`, includes a colorbar scale
        big        -- if `True`, produces 1080p graphic; 
                      else creates standard 5"x3" graphic
    Effects:
        produce the animation;
        save the animation to disk
    '''
    fsorted = sorted([
        (int(f.split('.')[0].split('-')[0][1:]), f)
        for f in os.listdir(folderpath)
        if f.startswith("f" if forward else "r") and f.endswith(".npz")
    ])

    width = 400 // dec
    d = dec / (125e6)  # sample length (s)
    xx, yy, SS = [], [], []
    for infreq, filepath in fsorted:
        with np.load(folderpath + filepath) as scan:
            V, s = scan['V'], scan['s']
            x, y = get_bifurcation_diagram(V, s, width)
            f, _, S = sps.spectrogram(np.ravel(s),
                                      fs=(1 / d),
                                      nfft=2**14,
                                      noverlap=0,
                                      window=sps.hann(2**14))
            SS.append(S)
            xx.append(x)
            yy.append(y)

    infreqs, _ = zip(*fsorted)

    fig = plt.figure(figsize=(20, 11.25) if big else (5, 3), dpi=96)
    fig.suptitle(f"$f$={infreqs[0]}Hz")

    ax1 = fig.add_subplot(211)
    ax1.set(title="Bifurcation diagram",
            xlim=(0., V[-1]),
            ylim=(np.min(np.concatenate(yy)), 1.),
            xlabel=r"$V_{\mathrm{in}}$ (V)",
            ylabel=r"$V_{\mathrm{out}}$ (V)")
    bifdiag = ax1.plot(xx[0], yy[0], '.', alpha=alpha)[0]

    ax2 = fig.add_subplot(212)
    spectrum = ax2.pcolormesh(V, f / 1_000, SS[0], norm=colors.LogNorm())
    if colorbar:
        cb = plt.colorbar(spectrum)
        cb.set_label = ("$I$ (W/Hz)")
    ax2.set(title="Spectrum",
            xlabel="$V_{\mathrm{in}}$ (V)",
            ylabel="$f$ (kHz)",
            ylim=(0, 2 * (infreqs[0] / 1_000)))

    def animate(i):
        fig.suptitle(f"$f$={infreqs[i]}Hz")
        bifdiag.set_data(xx[i], yy[i])
        spectrum = ax2.pcolormesh(V, f / 1_000, SS[i], norm=colors.LogNorm())
        ax2.set_ylim(0, 2 * (infreqs[i] / 1_000))
        return bifdiag, spectrum

    anim = anm.FuncAnimation(fig, animate, interval=1000, frames=len(infreqs))
    anim.save(folderpath + "dual-animation.mp4")

    plt.close()
    X_tr.loc[segment, 'abs_q05'] = np.quantile(np.abs(x), 0.05)
    X_tr.loc[segment, 'abs_q01'] = np.quantile(np.abs(x), 0.01)

    X_tr.loc[segment, 'trend'] = add_trend_feature(x)
    X_tr.loc[segment, 'abs_trend'] = add_trend_feature(x, abs_values=True)
    X_tr.loc[segment, 'abs_mean'] = np.abs(x).mean()
    X_tr.loc[segment, 'abs_std'] = np.abs(x).std()

    X_tr.loc[segment, 'mad'] = x.mad()
    X_tr.loc[segment, 'kurt'] = x.kurtosis()
    X_tr.loc[segment, 'skew'] = x.skew()
    X_tr.loc[segment, 'med'] = x.median()

    X_tr.loc[segment, 'Hilbert_mean'] = np.abs(hilbert(x)).mean()
    X_tr.loc[segment,
             'Hann_window_mean'] = (convolve(x, hann(150), mode='same') /
                                    sum(hann(150))).mean()
    X_tr.loc[segment,
             'classic_sta_lta1_mean'] = classic_sta_lta(x, 500, 10000).mean()
    X_tr.loc[segment,
             'classic_sta_lta2_mean'] = classic_sta_lta(x, 5000,
                                                        100000).mean()
    X_tr.loc[segment,
             'classic_sta_lta3_mean'] = classic_sta_lta(x, 3333, 6666).mean()
    X_tr.loc[segment,
             'classic_sta_lta4_mean'] = classic_sta_lta(x, 10000,
                                                        25000).mean()
    X_tr.loc[segment, 'classic_sta_lta5_mean'] = classic_sta_lta(x, 50,
                                                                 1000).mean()
    X_tr.loc[segment, 'classic_sta_lta6_mean'] = classic_sta_lta(x, 100,
                                                                 5000).mean()
    def structure_function(self,
                           varname='sst',
                           maskname='hdf_file',
                           qualname='qual',
                           lonrg=(154.9, 171.7),
                           latrg=(30, 45.4),
                           q=2,
                           rand=10000,
                           MAX_GAP=0.5,
                           detre=True,
                           windw=True,
                           iso=False,
                           lin_near=True):
        """Calculate a structure function of Matlab variable 'varname'
           in the box defined by lonrange and latrange.
        """
        nc_qual = xray.open_dataset(maskname, engine='pynio')
        quality = nc_qual[qualname]
        meta = self.nc[varname].where(quality > 3.).sel(
            lat=slice(latrg[1], latrg[0]), lon=slice(lonrg[0], lonrg[1]))
        lon = meta.lon
        lat = meta.lat

        ############
        # load data
        ############
        T = meta.values

        # define variables
        Ny, Nx = T.shape
        n = np.arange(0, np.log2(3 * Nx / 4.), dtype='i4')
        ndel = len(n)
        # Spatial step
        dx = gsw.earth.distance([lon[int(Nx / 2)], lon[int(Nx / 2) + 1]],
                                [lat[int(Ny / 2)], lat[int(Ny / 2)]])
        dy = gsw.earth.distance([lon[int(Nx / 2)], lon[int(Nx / 2)]],
                                [lat[int(Ny / 2)], lat[int(Ny / 2) + 1]])
        # dx = gsw.earth.distance([.5*(lonrg[0]+lonrg[1])-.5, .5*(lonrg[0]+lonrg[1])+.5],
        #                        [.5*(latrg[0]+latrg[1]), .5*(latrg[0]+latrg[1])])
        # dy = gsw.earth.distance([.5*(lonrg[0]+lonrg[1]), .5*(lonrg[0]+lonrg[1])],
        #                       [.5*(latrg[0]+latrg[1])-.5, .5*(latrg[0]+latrg[1])+.5])

        ############
        # Figure out if there is too much gaps in the box
        # default: MAX_GAP = 0.5 (only allow up to 50% of gap)
        ############
        mask_domain = np.ma.masked_invalid(T).mask
        gap_fraction = mask_domain.sum().astype('f8') / (Ny * Nx)
        # step 5: If gap_fraction is larger than critical values, give an warning
        if gap_fraction > MAX_GAP:
            # crit = 'false'
            # errstr = 'The sector has too much land or mask. masked_fraction = ' + str(gap_fraction)
            # warnings.warn(errstr)
            # sys.exit(0)
            # raise ValueError('The sector has too much land or mask. masked_fraction = ' + str(gap_fraction))
            return None
        else:
            # no problem
            # step 6: detrend the data in two dimensions (least squares plane fit)
            Ti = np.ma.masked_invalid(T.copy())
            interpolate_2Djit = jit(interpolate_2d)
            if lin_near:
                interpTi = interpolate_2Djit(np.ma.masked_invalid(T.copy()))
                interpTi = interpolate_2Djit(np.ma.masked_invalid(interpTi),
                                             meth='nearest')
            else:
                interpTi = interpolate_2Djit(np.ma.masked_invalid(T.copy()),
                                             meth='nearest')
            trend_2Djit = jit(trend_2d)
            trendTi = trend_2Djit(interpTi)
            Ti -= trendTi

            # window the data
            # Hanning window
            if windw:
                windowx = sig.hann(Nx)
                windowy = sig.hann(Ny)
                window = windowx * windowy[:, np.newaxis]
                Ti *= window

            if iso:
                ##################
                # Calculate structure functions isotropically
                #
                # Due to difference between meridional and zonal distance
                # the length scale will only be defined as the grid points
                # in between the two points
                ##################
                L = 2**n
                S = np.empty(ndel)
                S[:] = np.nan
                #dT = np.zeros(rand)
                dT = 0.
                for m in range(ndel):
                    for n in range(rand):
                        i = np.random.randint(0, Nx)
                        j = np.random.randint(0, Ny)
                        angle = 2. * np.pi * np.random.uniform(0., 1.)
                        r = 2**m
                        di = r * np.cos(angle)
                        dj = r * np.sin(angle)
                        i2 = round(i + di)
                        j2 = round(j + dj)
                        if i2 >= Nx or i2 < 0:
                            i2 = round(i - di)
                            #i2 -= Nx
                        if j2 >= Ny or j2 < 0:
                            j2 = round(j - dj)
                            #j2 -= Ny

                        #dT[n] = np.abs( Ti[j,i] - Ti[j2,i2] )**q
                        dT += np.abs(Ti[j, i] - Ti[j2, i2])**q
                    #H[m] = dT.mean()
                    S[m] = dT / rand

                return dx, dy, L, S, lon, lat, gap_fraction

            else:
                ##################
                # Calculate structure functions along each x-y axis
                ##################
                Li = 2.**n * dx
                Lj = 2.**n * dy
                diff_twopoints_jit = jit(diff_twopoints)
                Si, Sj = diff_twopoints_jit(Ti, ndel)

                return dx, dy, Li, Lj, Si, Sj, lon, lat, gap_fraction
Coe = 0.65  # Во сколько раз увеличиться pitch
Ln = len(Dat)
Out = np.zeros(Ln)


def stretch(In, Coe):
    LnFrame = len(In)
    LnOut = int(LnFrame * Coe)
    Out = np.zeros(LnOut)
    # print(In)
    for I in range(LnOut):
        Indx = int(I / Coe)
        Out[I] = In[Indx]
    return Out


Beg = 0
End = Beg + Len
Out = np.zeros(Ln)
R = int(Len * Coe)
wnd = sgn.hann(R)
Shift = int((Ln - R) * Len / (Ln - Len))
Pos = 0
while End <= Ln:
    Frame = np.float_(Dat[Beg:End])
    Res = stretch(Frame, Coe)
    Out[Pos:Pos + R] += .5 * Res * wnd
    Pos += Shift
    Beg += Len
    End = Beg + Len
wfl.write('Out.wav', int(Fr * 1), np.int16(Out))
Exemple #43
0
def wavplot(data,
            title='Title',
            file=None,
            segmentsize=512,
            overlap=8,
            Fs=48000,
            vmin=-160,
            vmax=0,
            normalize=False):
    cmap = smooth_colormap([(0, '#000000'), (1 / 9, '#010325'),
                            (2 / 9, '#130246'), (3 / 9, '#51026e'),
                            (4 / 9, '#9e0379'), (5 / 9, '#d6033e'),
                            (6 / 9, '#fc4d21'), (7 / 9, '#fdc967'),
                            (8 / 9, '#f3fab8'), (1, '#ffffff')])

    if not isinstance(data, (list, tuple, np.ndarray)):
        w = wave.open(data, 'rb')
        Fs = w.getframerate()
        data = np.fromstring(w.readframes(w.getnframes()),
                             dtype=np.int32) / 2147483647.0
    data = np.array(data)
    if normalize:
        maxitem = max(abs(np.max(data)), abs(np.min(data)))
        if maxitem > 0:
            data = data / maxitem

    datalen = len(data)

    def fast_resample(data, newlen):
        oldlen = len(data)
        result = []
        for i in range(newlen):
            result.append(data[i * oldlen // newlen])
        return np.array(result)

    datalen = len(data)
    segments = datalen // segmentsize - 1

    im = []

    window = signal.hann(segmentsize * overlap)

    np.seterr(all='ignore')

    for segm in range(segments - overlap):
        r = range(segm * datalen // segments,
                  segm * datalen // segments + segmentsize * overlap)
        subdata = data[r]
        subdata = subdata * window
        n = len(subdata)
        Y = np.fft.fft(subdata) / n
        Y = Y[range(len(Y) // 2)]
        Yfreq = 20 * np.log10(np.absolute(Y))
        Yfreq = signal.resample(Yfreq, 512)
        Yfreq = np.fmax(-300, Yfreq)
        im.append(Yfreq)

    im = np.transpose(im)

    plt.imshow(im,
               cmap=cmap,
               aspect='auto',
               vmin=vmin,
               vmax=vmax,
               origin='lower',
               extent=[0, datalen / Fs, 0, Fs / 2],
               interpolation='bicubic')
    plt.colorbar()

    if not file:
        plt.show()
    else:
        plt.savefig(file)
from scipy import signal

M = 200

# generate triangle wave
data = []
for i in range(0, 10):
    temp = np.linspace(0, 1, 20)
    data = np.r_[data, temp]

# fft transform
theta = np.angle(np.fft.fft(data))
data_fft = abs(np.fft.fft(data))

# add window
window = signal.hann(M)
data_win = window * data[:M]
theta_win = np.angle(np.fft.fft(data_win))
data_win_fft = abs(np.fft.fft(data_win))

# draw
plt.subplot(321)
plt.plot(data)
plt.title('Triangle wave')
plt.subplot(323)
plt.plot(data_fft)
plt.subplot(325)
plt.plot(theta)

plt.subplot(322)
plt.plot(data_win)
Exemple #45
0
def linear_filter1D(sin, sout, lag=0, debug=0):
    """ Estimates the linear filter  between sin and sout which are both one dimensional arrays of
    equal length. Estimation based on the normal equation in the Fourier Domain.
    lags is the number of points in the past of the filter.
    signals are zeroed but the bias term is not returned.
    returns the weights of the filter."""

    assert len(sin) == len(sout), "Signals must be same length! len(sin)=%d, len(sout)=%d" % (len(sin), len(sout))
    assert np.sum(np.isnan(sin)) == 0, "There are NaNs in sin"
    assert np.sum(np.isnan(sout)) == 0, "There are NaNs in sout"

    lags = np.asarray(range(-lag, lag+1, 1))
    corrSinSout = correlation_function(sin, sout, lags, mean_subtract=True, normalize=False)
    corrSinSin = correlation_function(sin, sin, lags, mean_subtract=True, normalize=False)
    corrSoutSout = correlation_function(sout, sout, lags, mean_subtract=True, normalize=False)
    win = hann(2*lag+1)


    if lag == 0:
        h = corrSinSout/corrSinSin
        fvals = 0
        gf = corrSinSout**2/(corrSinSin*corrSoutSout)
    else:
        # Normalize in the frequency domain
        corrSinSoutF = fft(corrSinSout*win)
        corrSinSinF = fft(corrSinSin*win)
        corrSoutSoutF = fft(corrSoutSout*win)
        hF = corrSinSoutF/np.abs(corrSinSinF)
        gf = np.abs(corrSinSoutF*corrSinSoutF.conj())/(np.abs(corrSinSinF)*np.abs(corrSoutSoutF))
        fvals = fftfreq(len(corrSinSout))
        h = ifft(hF)

# Plots for debugging/analyzing
    if debug:
        # Time domain plots
        plt.figure()
        plt.subplot(141)
        plt.plot(lags, corrSinSout*win)
        plt.title('Cross-Corr')
        plt.subplot(142)
        plt.plot(lags, corrSinSin*win)
        plt.title('Auto-Corr Input')
        plt.subplot(143)
        plt.plot(lags, corrSoutSout*win)
        plt.title('Auto-Corr Output')
        plt.subplot(144)
        plt.plot(lags, h)
        plt.title('Filter')

        # Frequency domain plots
        plt.figure()
        fmid = len(fvals)/2
        plt.subplot(131)
        plt.plot(fvals[0:fmid], abs(corrSinSinF[0:fmid]) )
        plt.title('Input Power')
        plt.subplot(132)
        plt.plot(fvals[0:fmid], abs(corrSoutSoutF[0:fmid]) )
        plt.title('Output Power')
        plt.subplot(133)
        plt.plot(fvals[0:fmid], gf[0:fmid])
        plt.title('Coherence')

    return h, lags, gf, fvals
from scipy import fft

data = CppSimData('test.tr0')
dout = data.evalsig('out')
t = data.evalsig('TIME')
t_delta = lfilter([1, -1], [1], t)
Ts = mean(t_delta[1:-1])
fs = 1 / Ts

# assume dout corresponds to 1-bit Delta-Sigma sequence alternating between 0 and 1
dout_mean = mean(dout)
dout[dout < dout_mean] = 0
dout[dout > dout_mean] = 1

N = len(dout)
hwin = hann(N)

dout_win = dout * hwin
# remove low frequency spectral bleeding
dout_minus_mean = dout - mean(dout_win) / mean(hwin)
dout_win = dout_minus_mean * hwin

s_out = (4 / sum(hwin) * absolute(fft(dout_win, N)))**2
freq = arange(N) * 1 / N

# look at frequencies in range of input sine wave
bw = 1 / (100 * 2)  # oversampling of such that 100Hz BW with 20kHz clk assumed
f_ind_bw = freq[:] <= bw
freq_bw = freq[f_ind_bw]
s_out_bw = s_out[f_ind_bw]
s_ind_max = argmax(s_out_bw)
Exemple #47
0
def smooth(y):
    return np.convolve(y, hann(50), mode='same')
Exemple #48
0
    def init(self, img, init_rect):
        init_rect = np.array(init_rect)
        self.frame_i = 0
        self.wsize = np.array((init_rect[3], init_rect[2]))
        position = (init_rect[1] + np.floor(self.wsize[0] / 2),
                    init_rect[0] + np.floor(self.wsize[1] / 2))
        self._rect_pos = init_rect

        self._position = np.floor(position)
        target_pix_sz = np.floor(self.wsize)
        self.target_sz = target_pix_sz

        # h*w*search_area_scale
        search_area = np.prod(self.target_sz / self.feature_ratio *
                              self.search_area_scale)

        # When the number of cells are small, choose a smaller cell size
        if search_area < self.cell_selection_thresh * self.filter_max_area:
            tmp_cell_size = max(
                1,
                np.ceil(
                    np.sqrt(
                        np.prod(self.target_sz * self.search_area_scale) /
                        (self.cell_selection_thresh * self.filter_max_area))))
            self.feature_ratio = int(min(self.feature_ratio, tmp_cell_size))
            search_area = np.prod(self.target_sz / self.feature_ratio *
                                  self.search_area_scale)

        if self._fixed_size is not None:
            # Scale factor of an extracted pixel area to the fixed size
            fixed_size = np.array(self._fixed_size)
            scale_factor = np.sqrt(search_area /
                                   np.prod(fixed_size / self.feature_ratio))

            # Target size, or bounding box size at the initial scale
            base_target_pix_sz = target_pix_sz / scale_factor

            self.search_pix_sz = fixed_size
        else:
            if search_area > self.filter_max_area:
                scale_factor = np.sqrt(search_area / self.filter_max_area)
            else:
                scale_factor = 1.0

            # Target size, or bounding box size at the initial scale
            base_target_pix_sz = target_pix_sz / scale_factor

            # Window size, taking padding into account
            if self.search_area_shape == 'proportional':
                # proportional area, same aspect ratio as the target
                search_pix_sz = np.floor(base_target_pix_sz *
                                         self.search_area_scale)
            elif self.search_area_shape == 'square':
                # square area, ignores the target aspect ratio
                search_pix_sz = np.tile(
                    np.sqrt(
                        np.prod(base_target_pix_sz * self.search_area_scale)),
                    2)
            elif self.search_area_shape == 'fix_padding':
                search_pix_sz = base_target_pix_sz \
                     + np.sqrt(np.prod(base_target_pix_sz * self.search_area_scale) \
                               + (base_target_pix_sz[0]) - base_target_pix_sz[1] / 4) \
                     - sum(base_target_pix_sz) / 2  # const padding
            else:
                print('Unknown "params.search_area_shape". Must be '
                      'proportional'
                      ', '
                      'square'
                      ' or '
                      'fix_padding'
                      '')

            # Set the size to exactly match the cell size
            self.search_pix_sz = np.round(
                search_pix_sz / self.feature_ratio) * self.feature_ratio

        self._scale_factor = scale_factor

        pixel = get_pixel(img, self._position,
                          np.round(self.search_pix_sz * scale_factor),
                          self.search_pix_sz)
        features = self._get_features(pixel)
        # The 2-D size of extracted feature
        self.feature_sz = np.float32(features.shape[:2])

        # Construct the label function
        output_sigma = np.sqrt(
            np.prod(np.floor(base_target_pix_sz /
                             self.feature_ratio))) * self.output_sigma_factor
        rg = np.roll(
            np.arange(-np.floor((self.feature_sz[0] - 1) / 2) - 1,
                      np.ceil((self.feature_sz[0] - 1) / 2)), -np.floor(
                          (self.feature_sz[0] - 1) / 2).astype(np.int64)) + 1
        cg = np.roll(
            np.arange(-np.floor((self.feature_sz[1] - 1) / 2) - 1,
                      np.ceil((self.feature_sz[1] - 1) / 2)), -np.floor(
                          (self.feature_sz[1] - 1) / 2).astype(np.int64)) + 1
        [cs, rs] = np.meshgrid(cg, rg)
        y = np.exp(-0.5 * (((rs**2 + cs**2) / output_sigma**2)))
        self.yf = fft2(y)

        # Construct cosine window
        self.cos_window = np.dot(
            hann(int(self.feature_sz[0])).reshape(int(self.feature_sz[0]), 1),
            hann(int(self.feature_sz[1])).reshape(1, int(self.feature_sz[1])))
        self.multi_cos_window = np.tile(
            self.cos_window[:, :, np.newaxis, np.newaxis],
            (1, 1, self.dim_feature, self.n_scales))

        if self.n_scales > 0:
            scale_exp = np.arange(-np.floor((self.n_scales - 1) / 2),\
                                  np.ceil((self.n_scales - 1) / 2 + 1))
            self.scale_factors = np.power(self.scale_step, scale_exp)

            # Force reasonable scale changes
            self.min_scale_factor = self.scale_step ** np.ceil(np.log(np.max(5 / self.search_pix_sz)) \
                                                                 / np.log(self.scale_step))
            self.max_scale_factor = self.scale_step ** \
                               np.floor(np.log(np.min(img.shape[0:2] / base_target_pix_sz)) \
                                        / np.log(self.scale_step))
        else:
            raise NotImplementedError

        if self.interpolate_response >= 3:
            # Pre-computes the grid that is used for socre optimization
            self.ky = np.roll(
                np.arange(-np.floor((self.feature_sz[0] - 1) / 2.),
                          np.ceil((self.feature_sz[0] - 1) / 2. + 1)),
                -np.floor((self.feature_sz[0] - 1) / 2).astype(np.int32),
                axis=0)
            self.kx = np.roll(
                np.arange(-np.floor((self.feature_sz[1] - 1) / 2.),
                          np.ceil((self.feature_sz[1] - 1) / 2. + 1)),
                -np.floor((self.feature_sz[1] - 1) / 2).astype(np.int32),
                axis=0)
            self.newton_iterations = self.newton_iterations
        else:
            raise NotImplementedError

        # Allocate memory for multi-scale tracking
        self.multires_pixel_template = np.zeros(
            (int(self.search_pix_sz[0]), int(self.search_pix_sz[1]),
             img.shape[2], self.n_scales))
        self.small_filter_sz = np.floor(base_target_pix_sz /
                                        self.feature_ratio)
        self.base_target_pix_sz = base_target_pix_sz
        # Initialization of inner parameters
        initial_g_f = np.zeros(features.shape)

        self._train(img, initial_g_f)

        self.frame = img
        self.pixel = pixel

        return pixel
# just compute one period
period = samplerate / float(frequency) # in sample points
omega = 2*pi / period # 2*pi*frequency / samplerate
xaxis = N.arange(int(period),dtype = N.float) * omega
ydata = 16384 * N.sin(xaxis)

# and then repeat it
signal = N.resize(ydata, (samples,))
	
## Appendix 2: Hanning windows

# http://en.wikipedia.org/wiki/Window_function#Hann_.28Hanning.29_window

from scipy.signal import hann
audio = getwavdata('flute.wav')
window = hann(len(audio))
audio = audio * window

## Appendix 3: Labeling graphs

# If you want to have labels for your graphs...

# the graph
pyplot.plot(audio)
# label the axes
pyplot.ylabel("Amplitude")
pyplot.xlabel("Time (samples)")
# set the title
pyplot.title("Flute Sample")

## Appendix 4: useful terms to look up
def identify_motor_acc(dt, dq, ddq, current, tau, Kt_p, Kv_p,
                       ZERO_VELOCITY_THRESHOLD_SMALL, ZERO_JERK_THRESHOLD,
                       SHOW_THRESHOLD_EFFECT):
    #Filter current*****************************************************
    win = signal.hann(10)
    filtered_current = signal.convolve(current, win, mode='same') / sum(win)
    current = filtered_current

    # Mask valid data***************************************************
    #~ # remove high jerk
    dddq = np.gradient(ddq, 1) / dt
    maskConstAcc = (abs(dddq) < ZERO_JERK_THRESHOLD)
    #~ # erode to get only steady phases where acceleration is constant
    maskConstAcc = ndimage.morphology.binary_erosion(maskConstAcc, None, 100)
    maskPosVel = (dq > ZERO_VELOCITY_THRESHOLD_SMALL)
    maskNegVel = (dq < -ZERO_VELOCITY_THRESHOLD_SMALL)
    maskConstPosAcc = np.logical_and(maskConstAcc, maskPosVel)
    maskConstNegAcc = np.logical_and(maskConstAcc, maskNegVel)

    if SHOW_THRESHOLD_EFFECT:
        plt.figure()
        plt.plot(ddq)
        plt.ylabel('ddq')
        ddq_const = ddq.copy()
        ddq_const[np.logical_not(maskConstAcc)] = np.nan
        plt.plot(ddq_const)
        plt.ylabel('ddq_const')
        plt.show()

    #~ y              = a. x   +  b
    #~ i-Kt.tau-Kv.dq = Ka.ddq +  Kf
    #~
    # Identification ***************************************************
    y = current - Kt_p * tau - Kv_p * dq
    y[maskConstPosAcc] = current[maskConstPosAcc] - Kt_p * tau[
        maskConstPosAcc] - Kv_p * dq[maskConstPosAcc]
    y[maskConstNegAcc] = current[maskConstNegAcc] - Kt_p * tau[
        maskConstNegAcc] - Kv_p * dq[maskConstNegAcc]
    y_label = r'$i(t)-{K_t}{\tau(t)}-{K_v}{\dot{q}(t)}$'
    x = ddq
    x_label = r'$\ddot{q}(t)$'
    (Kap, Kfp) = solve1stOrderLeastSquare(x[maskConstPosAcc],
                                          y[maskConstPosAcc])
    (Kan, b) = solve1stOrderLeastSquare(x[maskConstNegAcc], y[maskConstNegAcc])
    Kfn = -b

    # Plot *************************************************************
    plt.figure()
    plt.axhline(0, color='black', lw=1)
    plt.axvline(0, color='black', lw=1)
    plt.plot(x, y, '.', lw=3, markersize=1, c='0.5')
    plt.plot(x[maskConstPosAcc], y[maskConstPosAcc], 'rx', lw=3, markersize=1)
    plt.plot(x[maskConstNegAcc], y[maskConstNegAcc], 'bx', lw=3, markersize=1)

    #plot identified lin model
    plt.plot([min(x), max(x)], [Kap * min(x) + Kfp, Kap * max(x) + Kfp],
             'g:',
             lw=3)
    plt.plot([min(x), max(x)], [Kan * min(x) - Kfn, Kan * max(x) - Kfn],
             'g:',
             lw=3)
    plt.ylabel(y_label)
    plt.xlabel(x_label)
    plt.show()

    return (Kap, Kan, Kfp, Kfn)
Exemple #51
0
import scipy
from scipy.io.wavfile import read
from scipy.signal import hann
from scipy.fftpack import rfft
import matplotlib.pyplot as plt

# read audio samples
input_data = read("Sound/tabByAnkur1.wav")
audio = input_data[1]

# apply a Hanning window
window = hann(1024)
print(window)
audio = audio[0:1024] * window[1]

# fft
mags = abs(rfft(audio))

# convert to dB
mags = 20 * scipy.log10(mags)

# normalise to 0 dB max
mags -= max(mags)

# plot
plt.plot(mags.any())

# label the axes
plt.ylabel("Magnitude (dB)")
plt.xlabel("Frequency Bin")
    def feat_extra(self):
        def feature_gen(df1):
            print(df1)

            # statistische Berechnung
            df_max = max(df1)
            df_min = min(df1)
            df_peak = df_max-df_min
            df_mean = np.mean(df1)
            df_var = np.var(df1)
            df_std = np.std(df1)
            df_max_pos, _ = find_peaks(df1, height=df_max)

            # uni_vec = np.ones(0, df_num, float)
            # print(df_max, df_min, df_mean, df_var, df_std)

            # check for bad reading
            if df_mean > bad_read_value or df_num != np.size(df1):
                print("bad reading of the value")

            # Offset substraction
            df1 = df1 - df_mean

            # praparation fft analyses

            # max value in center of fft
            df_high_peak = np.argmax(df1)
            fft_start = df_high_peak - int(num_fft / 2)
            fft_end = df_high_peak + int(num_fft / 2)

            # protection for limet of dataframe value fft_end >= df_num
            if fft_end > int(df_num):
                dif_fft = fft_end - int(df_num)
                fft_start = fft_start - dif_fft
                fft_end = fft_end - dif_fft

            if fft_start < 0:
                dif_fft = fft_end - int(df_num)
                fft_start = fft_start - dif_fft
                fft_end = fft_end - dif_fft

            x_fft = np.fft.rfftfreq(num_fft, d=del_time)
            # x_fft = x_fft[fft_start:fft_end]

            # db_fft = np.log10(fft_df/np.argmax(fft_df))
            # plot for x axis fft
            # plt.plot(x_fft)
            # plt.show()

            # FFT
            df_new = df1[fft_start:fft_end] * window
            fft_df = abs(np.fft.rfft(df_new))
            fft_plot = np.copy(fft_df)

            mag = abs(20 * np.log10(fft_df))
            max_fft = max(fft_df)
            max_fft_pos = np.argmax(fft_df)
            fft_peak, _ = find_peaks(fft_df, height=max_fft)
            # fft_sample = fft_peak + fft_start
            # feature

            # distance
            distance = 18500 * del_time * (df_max_pos)

            # bandwith and center frequency
            f_center = x_fft[max_fft_pos]
            for i in range(num_fft):
                if 0.5 * max_fft > fft_df[i + max_fft_pos]:
                    f_band = x_fft[i + max_fft_pos]
                    f_band -= f_center
                    f_band = 2 * f_band
                    break

            # print "f_center = %d und f_0 = %d" % (f_center, f_band)

            # THD
            total_rms = np.sqrt(np.mean(np.abs(df1[fft_start:fft_end]) ** 2))
            under_five = np.argwhere(fft_df < 2)
            for i in range(len(under_five)):
                if under_five[i] > max_fft_pos:
                    uppermin = under_five[i]
                    lowermin = under_five[i - 1]
                    break

            fft_plot[int(lowermin):int(uppermin)] = 0.0
            noise_df = np.fft.irfft(fft_df)
            noise_rms = np.sqrt(np.mean(np.abs(noise_df) ** 2))
            THDN = noise_rms / total_rms
            print("THD+N:     %.4f%% or %.1f dB" % (THDN * 100, 20 * np.log10(THDN)))

            #

            # Num of Peaks
            num_peak, _ = find_peaks(df1, height=peak_high)
            num_peak_num = np.size(num_peak)

            return (THDN, f_center, f_band, max_fft, df_var, df_std, df_peak)
        # define Variabels
        df_num = 16384.0
        sam_fre = 1900000.0
        del_time = 1.0 / sam_fre
        sum_df = 0.0
        num_fft = 4098
        peak_high = 0.05
        end_x_axis = del_time * df_num
        x_time = np.linspace(0.0, end_x_axis, df_num)
        bad_read_value = 0.05
        delay = 4000  # sampels
        window = hann(num_fft)
        # window = hann(int(df_num))

        path = self.ui.ad_meas.text()

        df_ob = np.loadtxt(path, delimiter=";")
        size_ob = df_ob.shape
        feature_ob = np.zeros((size_ob[0], 7))

        for i in range(size_ob[0]):
            df1 = df_ob[i]
            feature_ob[i:] = feature_gen(df1)
        colum_one = np.ones(size_ob[0])
        data_ob = np.column_stack((feature_ob, colum_one))

        fileName, _ = QFileDialog.getSaveFileName(self, "QFileDialog.getSaveFileName()", "",
                                                      "All Files (*);;csv Files (*.csv)")
        np.savetxt(str(fileName), data_ob, delimiter=";")


        path_per = self.ui.ad_meas_3.text()


        df_per = np.loadtxt(path_per, delimiter=";")

        size_per = df_per.shape
        feature_per = np.zeros((size_per[0], 7))
        for i in range(size_per[0]):
            df1 = df_per[i]
            feature_per[i:] = feature_gen(df1)
        colum_zero = np.zeros(size_per[0])
        data_per = np.column_stack((feature_per, colum_zero))

        fileName_2, _ = QFileDialog.getSaveFileName(self, "QFileDialog.getSaveFileName()", "",
                                                  "All Files (*);;csv Files (*.csv)")
        np.savetxt(str(fileName_2), data_per, delimiter=";")


        data_all = np.concatenate((data_ob, data_per), axis=0)
        fileName_3, _ = QFileDialog.getSaveFileName(self, "QFileDialog.getSaveFileName()", "",
                                                    "All Files (*);;csv Files (*.csv)")
        np.savetxt(str(fileName_3), data_all, delimiter=";")


        print(feature_per, feature_ob)
Exemple #53
0
def pitch_estimation(x, fs, cfg=None):
    """
    Estimates pitch for a time-series, implements SWIPE algorithm
    see [1] for more details

    Args:
        x   (array): real-valued numpy array (e.g., speech signal)
        fs  (float): sampling frequency of x
        cfg (psola.experiment_config.ExperimentConfig instance),
            None is default (will use default params if cfg instance
            not provided)

    Returns:
        pitch    (array): pitch corresponding to times
        t        (array): times
        strength (array): strength of pitch corresponding to times

    References:
        [1] Camacho, A., & Harris, J. G. (2008). A sawtooth waveform
            inspired pitch estimator for speech and music. The Journal
            of the Acoustical Society of America, 124(3), 1638–1652.
            https://doi.org/10.1121/1.2951592
    """
    # supply default params if user does not provide specified config file
    if cfg is None:
        from psola.experiment_config import ExperimentConfig
        cfg = ExperimentConfig()

    dt = cfg.frame_step  # define time resolution of pitch estimation
    times = np.arange(0, x.size / np.float_(fs), dt)

    # define pitch candidates, lp := log2(pitch)
    lp_min, lp_max = np.log2(cfg.min_pitch), np.log2(cfg.max_pitch)
    lp_step = cfg.dlog2p
    lp_candidates = np.arange(lp_min, lp_max, lp_step)
    pitch_candidates = 2**lp_candidates.T

    # pitch strength matrix
    S = np.zeros((pitch_candidates.size, times.size))

    # determine power-of-2 window-sizes (P2-WSs)
    lws = np.round(np.log2(8 * fs / np.array([cfg.min_pitch, cfg.max_pitch])))
    wss = 2**np.arange(lws[0], lws[1] - 1, -1)  # note the -1 for inclusion
    p0s = 8 * fs / wss  # optimal pitches for P2-WSs

    # determine window sizes used by each pitch candidate
    window_size = 1 + lp_candidates - np.log2(8 * fs / wss[0])

    # create ERB-scale uniformly-spaced freqs (Hz)
    min_hz = hz2erbs(pitch_candidates.min() / 4)  # TODO: why divide by 4?
    max_hz = hz2erbs(fs / 2)  # Nyquist freq
    hz_scale = np.arange(min_hz, max_hz, cfg.dERBs)
    f_erbs = erbs2hz(hz_scale)

    for i, (ws, p0) in enumerate(zip(wss, p0s)):

        hop_size = max(1, np.round(8 * (1 - cfg.window_overlap) * fs / p0))

        # zero pad signal
        before = np.zeros(int(ws / 2))
        after = np.zeros(int(ws / 2 + hop_size))
        xzp = np.concatenate((before, x, after))

        w = signal.hann(int(ws))
        noverlap = int(max(
            0, np.round(ws - hop_size)))  # window overlap for spectrogram

        # Note that there MATLAB and Scipy implement specgram diff,
        # so values will NOT be equivalent (there are no options in Python's
        # implementation to make the two funcs equal).
        spec_kwargs = dict(x=xzp,
                           fs=fs,
                           window=w,
                           nperseg=w.size,
                           noverlap=noverlap,
                           nfft=int(ws),
                           scaling='spectrum',
                           mode='complex')
        freqs, ti, X = signal.spectrogram(**spec_kwargs)

        # Note: this is very opaque, learn what this is doing and provide explanation
        # select candidates that use this window size
        # np.squeeze used to remove single-dimension entries from array
        ii = i + 1
        if wss.size == 1:
            j = pitch_candidates.T
            k = np.array([])
        elif wss.size == ii:
            j = find(window_size - ii > -1)
            k = find(window_size[j] - ii < 0)
        elif ii == 1:
            j = find(window_size - ii < 1)
            k = find(window_size[j] - ii > 0)
        else:
            j = find(np.abs(window_size - ii) < 1)
            k = np.arange(0, j.size)

        # compute loudness at ERBs uniformly-spaced freqs
        idx = find(f_erbs > pitch_candidates[j[0]] / 4)[0]
        f_erbs = f_erbs[idx:]
        f = interp1d(freqs, np.abs(X), axis=0, kind='cubic',
                     fill_value=0)  # interp on columns
        interpd_X = f(f_erbs)
        interpd_X[interpd_X < 0] = 0
        L = np.sqrt(interpd_X)

        # compute pitch strength
        Si_ = pitch_strength_all_candidates(f_erbs, L, pitch_candidates[j])

        # replicate matlab behavior with ti, default extends one elem too far and doesn't include 0
        # default ti makes the interp1d stage not work, since 0 is not included
        ti = np.concatenate((np.zeros(1), ti[:-1]))
        # interpolate pitch strength at desired times
        if Si_.shape[1] > 1:
            f = interp1d(ti, Si_.T, axis=0, kind='linear', fill_value=np.nan)
            Si = f(times).T
        else:
            Si = np.empty(
                (Si_.shape[0], times.size)) * np.nan  # TODO: test this line

        # add pitch strength to combination
        lambda_ = window_size[j[k]] - (i + 1)
        mu = np.ones(j.shape)
        mu[k] = 1 - np.abs(lambda_)
        S[j, :] = S[j, :] + np.tile(mu, (Si.shape[1], 1)).T * Si

    # fine-tune pitch using parabolic interp
    pitch = np.empty(S.shape[1]) * np.nan
    strength = np.empty(S.shape[1]) * np.nan
    for j in range(S.shape[1]):
        strength[j] = np.nanmax(S[:, j])
        i = np.nanargmax(S[:, j])
        if strength[j] < cfg.pitch_strength_thresh:
            continue
        if i == 0 or i == pitch_candidates.size - 1:
            pitch[j] = pitch_candidates[i]
        else:
            I = np.arange(i - 1, i + 2)  # funky additions to mimic MATLAB
            tc = 1 / pitch_candidates[I]
            ntc = (tc / tc[1] - 1) * 2 * np.pi
            c = np.polyfit(ntc, S[I, j], 2)
            # TODO: why are these params hardcoded and what is meaning?
            ftc_low = np.log2(pitch_candidates[I[0]])
            ftc_high = np.log2(pitch_candidates[I[2]])
            ftc_step = 1 / 12 / 100
            ftc = 1 / 2**np.arange(ftc_low, ftc_high, ftc_step)
            nftc = (ftc / tc[1] - 1) * 2 * np.pi
            polyfit_nftc = np.polyval(c, nftc)
            strength[j] = np.nanmax(polyfit_nftc)
            k = np.argmax(polyfit_nftc)
            pitch[j] = 2**(ftc_low + (k - 1) / 12 / 100)

    return pitch, times, strength
Exemple #54
0
import matplotlib.pyplot as plt
import numpy as np
import scipy.signal as sig
from scipy.fftpack import fft, fftshift

N = 1000
fs = 1000
Ts = 1 / fs

tt = np.linspace(0, (N - 1) * Ts, N)

ventanas = [
    1,
    sig.boxcar(N),
    sig.bartlett(N),
    sig.hann(N),
    sig.blackman(N),
    sig.flattop(N)
]
ventanas_names = [
    "No window", "Rectangular", "Bartlett", "Hanning", "Blackman", "Flattop"
]
V = len(ventanas_names)

f1 = fs / 4
f2 = f1 + (10 * fs / N)

a2dB = -250
a2 = 10**(a2dB / 20)

x1 = np.sin(2 * np.pi * f1 * tt)
Exemple #55
0
for cen in cen_types:
    print "*** Trying centering: ", cen
    lattice_cen = lattice[cen_ind[cen]]
    lattice_type = [bravais[0][k] for k in cen_ind[cen]]
    unique_axis = [bravais[2][k] for k in cen_ind[cen]]

    uc=collections.defaultdict(list)
    for i in range(len(myUC)):
        print "max: ", np.max(lattice_cen[:,i])
        hist,bin_edges=np.histogram(lattice_cen[:,i],bins=np.arange(0,np.max(lattice_cen[:,i]),binSize))
        uc[myUC[i]].append(hist)

    ## 1D histogram peak finding
    foundPeaks = True
    possible = []
    win=signal.hann(15)
    for j in range(3):
        conv=signal.convolve(uc[myUC[j]][0],win,mode='same')/sum(win)
        peakind = signal.find_peaks(uc[myUC[j]][0], prominence=10, distance=15)
        if doPlot:
            plt.subplot(211); plt.plot(uc[myUC[j]][0],'b-'); plt.title(myUC[j])
            plt.subplot(212); plt.plot(conv,'g-'); plt.plot(peakind[0],conv[peakind[0]],'ro'); plt.title("conv and peaks found"); plt.show()
        if len(peakind[0]) == 0:
            print "Unable to find peak in unitcell "+myUC[j]+" for "+cen+". Try again after indexing more data."
            foundPeaks = False
        possible.append(np.array([p*binSize for p in peakind[0]]))
    print "possible uc values: ", possible
    if not foundPeaks:
        continue

    # Select lattices that appear at the possible peaks 
def calAdjCCTTFromTrace(nt, dt, tStartIn, tEndIn, dataIn, synthIn):
    """ calculate the cross correlation traveltime adjoint sources for one seismogram
    IN:
        nt          : number of timesteps in each seismogram
        dt          : timestep of seismograms
        tStartIn      : float starting time for trace
        tEndIn        : float end time for trace
    OUT:
        fBar        : array containing the adjoint seismogram for the trace
        t           : ndarray containing the time steps
    """
    isCalculateWeights = False
    if isCalculateWeights:
        dSeism = np.zeros(nt)
        weight = 0

    # -- time vector
    t = np.ogrid[0:(nt - 1) * dt:nt * 1j]
    # -- the norm
    norm = 0

    # -- numpy arrays initialisation
    velSynth = np.zeros(nt)
    accSynth = np.zeros(nt)
    timeWind = np.zeros(nt)
    fBar = np.zeros(nt)

    # -- calculate time time-window
    tStart = tStartIn
    tEnd = tEndIn
    # -- the starting and ending sample numbers
    iStart = int(np.floor(tStart / dt))
    iEnd = int(np.ceil(tEnd / dt))
    # -- sample length of the window
    iWind = iEnd - iStart
    #print iStart,iEnd,iWind
    timeWind[iStart:iEnd] = sgnl.hann(iWind)

    # -- calculate the adjoint
    synth = synthIn
    interpTrc = interp.InterpolatedUnivariateSpline(t, synth)
    velSynth = interpTrc(t, 1)
    accSynth = interpTrc(t, 2)

    integrArgument = timeWind * synth * accSynth
    # -- calculating the norm
    norm = integr.simps(integrArgument, dx=dt, axis=-1, even='last')

    # -- divide every trace (row in matrices) by their norm (row in vector norm)
    fBar = timeWind * velSynth / norm
    if isCalculateWeights:
        # -- read in the data seismograms
        data = dataIn
        # -- calculate the difference between data and synthetics (amplitude) per trace
        dSeism = data - synth
        # -- calculate the weight per trace
        integrArgument = timeWind * velSynth * dSeism
        weight = integr.simps(integrArgument, dx=dt, axis=-1, even='last')
        print "weight", weight / norm
        # -- multiply weight with every adj trace
        fBar = fBar * weight
        print weight
    return [fBar, t]
    def features(self, x, y, seg_id):
        feature_dict = dict()
        feature_dict['target'] = y
        feature_dict['seg_id'] = seg_id

        # create features here

        # lists with parameters to iterate over them
        percentiles = [
            1, 5, 10, 20, 25, 30, 40, 50, 60, 70, 75, 80, 90, 95, 99
        ]
        hann_windows = [50, 150, 1500, 15000]
        spans = [300, 3000, 30000, 50000]
        windows = [10, 50, 100, 500, 1000, 10000]
        borders = list(range(-4000, 4001, 1000))
        peaks = [10, 20, 50, 100]
        coefs = [1, 5, 10, 50, 100]
        lags = [10, 100, 1000, 10000]
        autocorr_lags = [5, 10, 50, 100, 500, 1000, 5000, 10000]

        # basic stats
        feature_dict['mean'] = x.mean()
        feature_dict['std'] = x.std()
        feature_dict['max'] = x.max()
        feature_dict['min'] = x.min()

        # basic stats on absolute values
        feature_dict['mean_change_abs'] = np.mean(np.diff(x))
        feature_dict['abs_max'] = np.abs(x).max()
        feature_dict['abs_mean'] = np.abs(x).mean()
        feature_dict['abs_std'] = np.abs(x).std()

        # geometric and harminic means
        feature_dict['hmean'] = stats.hmean(np.abs(x[np.nonzero(x)[0]]))
        feature_dict['gmean'] = stats.gmean(np.abs(x[np.nonzero(x)[0]]))

        # k-statistic and moments
        for i in range(1, 5):
            feature_dict[f'kstat_{i}'] = stats.kstat(x, i)
            feature_dict[f'moment_{i}'] = stats.moment(x, i)

        for i in [1, 2]:
            feature_dict[f'kstatvar_{i}'] = stats.kstatvar(x, i)

        # aggregations on various slices of data
        for agg_type, slice_length, direction in product(
            ['std', 'min', 'max', 'mean'], [1000, 10000, 50000],
            ['first', 'last']):
            if direction == 'first':
                feature_dict[
                    f'{agg_type}_{direction}_{slice_length}'] = x[:
                                                                  slice_length].agg(
                                                                      agg_type)
            elif direction == 'last':
                feature_dict[f'{agg_type}_{direction}_{slice_length}'] = x[
                    -slice_length:].agg(agg_type)

        feature_dict['max_to_min'] = x.max() / np.abs(x.min())
        feature_dict['max_to_min_diff'] = x.max() - np.abs(x.min())
        feature_dict['count_big'] = len(x[np.abs(x) > 500])
        feature_dict['sum'] = x.sum()

        feature_dict['mean_change_rate'] = calc_change_rate(x)
        # calc_change_rate on slices of data
        for slice_length, direction in product([1000, 10000, 50000],
                                               ['first', 'last']):
            if direction == 'first':
                feature_dict[
                    f'mean_change_rate_{direction}_{slice_length}'] = calc_change_rate(
                        x[:slice_length])
            elif direction == 'last':
                feature_dict[
                    f'mean_change_rate_{direction}_{slice_length}'] = calc_change_rate(
                        x[-slice_length:])

        # percentiles on original and absolute values
        for p in percentiles:
            feature_dict[f'percentile_{p}'] = np.percentile(x, p)
            feature_dict[f'abs_percentile_{p}'] = np.percentile(np.abs(x), p)

        feature_dict['trend'] = add_trend_feature(x)
        feature_dict['abs_trend'] = add_trend_feature(x, abs_values=True)

        feature_dict['mad'] = x.mad()
        feature_dict['kurt'] = x.kurtosis()
        feature_dict['skew'] = x.skew()
        feature_dict['med'] = x.median()

        feature_dict['Hilbert_mean'] = np.abs(hilbert(x)).mean()

        for hw in hann_windows:
            feature_dict[f'Hann_window_mean_{hw}'] = (
                convolve(x, hann(hw), mode='same') / sum(hann(hw))).mean()

        feature_dict['classic_sta_lta1_mean'] = classic_sta_lta(x, 500,
                                                                10000).mean()
        feature_dict['classic_sta_lta2_mean'] = classic_sta_lta(
            x, 5000, 100000).mean()
        feature_dict['classic_sta_lta3_mean'] = classic_sta_lta(x, 3333,
                                                                6666).mean()
        feature_dict['classic_sta_lta4_mean'] = classic_sta_lta(
            x, 10000, 25000).mean()
        feature_dict['classic_sta_lta5_mean'] = classic_sta_lta(x, 50,
                                                                1000).mean()
        feature_dict['classic_sta_lta6_mean'] = classic_sta_lta(x, 100,
                                                                5000).mean()
        feature_dict['classic_sta_lta7_mean'] = classic_sta_lta(x, 333,
                                                                666).mean()
        feature_dict['classic_sta_lta8_mean'] = classic_sta_lta(
            x, 4000, 10000).mean()

        # exponential rolling statistics
        ewma = pd.Series.ewm
        for s in spans:
            feature_dict[f'exp_Moving_average_{s}_mean'] = (ewma(
                x, span=s).mean(skipna=True)).mean(skipna=True)
            feature_dict[f'exp_Moving_average_{s}_std'] = (ewma(
                x, span=s).mean(skipna=True)).std(skipna=True)
            feature_dict[f'exp_Moving_std_{s}_mean'] = (ewma(
                x, span=s).std(skipna=True)).mean(skipna=True)
            feature_dict[f'exp_Moving_std_{s}_std'] = (ewma(
                x, span=s).std(skipna=True)).std(skipna=True)

        feature_dict['iqr'] = np.subtract(*np.percentile(x, [75, 25]))
        feature_dict['iqr1'] = np.subtract(*np.percentile(x, [95, 5]))
        feature_dict['ave10'] = stats.trim_mean(x, 0.1)

        for slice_length, threshold in product([50000, 100000, 150000],
                                               [5, 10, 20, 50, 100]):
            feature_dict[f'count_big_{slice_length}_threshold_{threshold}'] = (
                np.abs(x[-slice_length:]) > threshold).sum()
            feature_dict[
                f'count_big_{slice_length}_less_threshold_{threshold}'] = (
                    np.abs(x[-slice_length:]) < threshold).sum()

        # tfresh features take too long to calculate, so I comment them for now

#         feature_dict['abs_energy'] = feature_calculators.abs_energy(x)
#         feature_dict['abs_sum_of_changes'] = feature_calculators.absolute_sum_of_changes(x)
#         feature_dict['count_above_mean'] = feature_calculators.count_above_mean(x)
#         feature_dict['count_below_mean'] = feature_calculators.count_below_mean(x)
#         feature_dict['mean_abs_change'] = feature_calculators.mean_abs_change(x)
#         feature_dict['mean_change'] = feature_calculators.mean_change(x)
#         feature_dict['var_larger_than_std_dev'] = feature_calculators.variance_larger_than_standard_deviation(x)
        feature_dict['range_minf_m4000'] = feature_calculators.range_count(
            x, -np.inf, -4000)
        feature_dict['range_p4000_pinf'] = feature_calculators.range_count(
            x, 4000, np.inf)

        for i, j in zip(borders, borders[1:]):
            feature_dict[f'range_{i}_{j}'] = feature_calculators.range_count(
                x, i, j)

#         feature_dict['ratio_unique_values'] = feature_calculators.ratio_value_number_to_time_series_length(x)
#         feature_dict['first_loc_min'] = feature_calculators.first_location_of_minimum(x)
#         feature_dict['first_loc_max'] = feature_calculators.first_location_of_maximum(x)
#         feature_dict['last_loc_min'] = feature_calculators.last_location_of_minimum(x)
#         feature_dict['last_loc_max'] = feature_calculators.last_location_of_maximum(x)

#         for lag in lags:
#             feature_dict[f'time_rev_asym_stat_{lag}'] = feature_calculators.time_reversal_asymmetry_statistic(x, lag)
        for autocorr_lag in autocorr_lags:
            feature_dict[
                f'autocorrelation_{autocorr_lag}'] = feature_calculators.autocorrelation(
                    x, autocorr_lag)
            feature_dict[f'c3_{autocorr_lag}'] = feature_calculators.c3(
                x, autocorr_lag)


#         for coeff, attr in product([1, 2, 3, 4, 5], ['real', 'imag', 'angle']):
#             feature_dict[f'fft_{coeff}_{attr}'] = list(feature_calculators.fft_coefficient(x, [{'coeff': coeff, 'attr': attr}]))[0][1]

#         feature_dict['long_strk_above_mean'] = feature_calculators.longest_strike_above_mean(x)
#         feature_dict['long_strk_below_mean'] = feature_calculators.longest_strike_below_mean(x)
#         feature_dict['cid_ce_0'] = feature_calculators.cid_ce(x, 0)
#         feature_dict['cid_ce_1'] = feature_calculators.cid_ce(x, 1)

        for p in percentiles:
            feature_dict[
                f'binned_entropy_{p}'] = feature_calculators.binned_entropy(
                    x, p)

        feature_dict['num_crossing_0'] = feature_calculators.number_crossing_m(
            x, 0)

        for peak in peaks:
            feature_dict[
                f'num_peaks_{peak}'] = feature_calculators.number_peaks(
                    x, peak)

        for c in coefs:
            feature_dict[f'spkt_welch_density_{c}'] = list(
                feature_calculators.spkt_welch_density(x, [{
                    'coeff': c
                }]))[0][1]
            feature_dict[
                f'time_rev_asym_stat_{c}'] = feature_calculators.time_reversal_asymmetry_statistic(
                    x, c)

        # statistics on rolling windows of various sizes
        for w in windows:
            x_roll_std = x.rolling(w).std().dropna().values
            x_roll_mean = x.rolling(w).mean().dropna().values

            feature_dict[f'ave_roll_std_{w}'] = x_roll_std.mean()
            feature_dict[f'std_roll_std_{w}'] = x_roll_std.std()
            feature_dict[f'max_roll_std_{w}'] = x_roll_std.max()
            feature_dict[f'min_roll_std_{w}'] = x_roll_std.min()

            for p in percentiles:
                feature_dict[
                    f'percentile_roll_std_{p}_window_{w}'] = np.percentile(
                        x_roll_std, p)

            feature_dict[f'av_change_abs_roll_std_{w}'] = np.mean(
                np.diff(x_roll_std))
            feature_dict[f'av_change_rate_roll_std_{w}'] = np.mean(
                np.nonzero((np.diff(x_roll_std) / x_roll_std[:-1]))[0])
            feature_dict[f'abs_max_roll_std_{w}'] = np.abs(x_roll_std).max()

            feature_dict[f'ave_roll_mean_{w}'] = x_roll_mean.mean()
            feature_dict[f'std_roll_mean_{w}'] = x_roll_mean.std()
            feature_dict[f'max_roll_mean_{w}'] = x_roll_mean.max()
            feature_dict[f'min_roll_mean_{w}'] = x_roll_mean.min()

            for p in percentiles:
                feature_dict[
                    f'percentile_roll_mean_{p}_window_{w}'] = np.percentile(
                        x_roll_mean, p)

            feature_dict[f'av_change_abs_roll_mean_{w}'] = np.mean(
                np.diff(x_roll_mean))
            feature_dict[f'av_change_rate_roll_mean_{w}'] = np.mean(
                np.nonzero((np.diff(x_roll_mean) / x_roll_mean[:-1]))[0])
            feature_dict[f'abs_max_roll_mean_{w}'] = np.abs(x_roll_mean).max()

        return feature_dict
Exemple #58
0
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 22 00:54:03 2019

@author: fede
"""
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
from numpy.fft import fft, fftshift
import scipy.signal as sig

N = 60 # muestras
fftSize = 2048

ventanas = [sig.boxcar(N), sig.bartlett(N), sig.hann(N), sig.blackman(N), sig.flattop(N)]
ventanas_names = ["rectangular", "bartlett", "hanning", "blackman", "flattop"]
V = len(ventanas_names)
plt.figure("Ventanas", figsize = (10,10))

for (vv, this_win) in zip(ventanas_names, ventanas):
       plt.plot(this_win, label=vv)
plt.legend()
plt.grid()       
plt.xlabel("Numero de muestra")
plt.ylabel("Amplitud de la ventana")
plt.title("Forma de ventanas")


complexMat = np.transpose(np.vstack([fft(thisWin,fftSize, axis=0) for thisWin in ventanas]))
#fft(signal, size) Si size > len(signal) la FFT le hace zero padding automaticamente :)
def multifrequency_voltage_scan_spectrum_driver(folderpath,
                                                dec,
                                                forward=True,
                                                colorbar=True,
                                                big=False):
    '''
    Create a frequency waterfall visualization of the spectra of voltage traces
    as a function of input voltage, animated based on input frequency
    NB: unlike traditional waterfalls, these flow from left to right to match
    the corresponding bifurcation diagrams
    Arguments:
        folderpath -- path to voltage scan results folder
        dec        -- the decimation at which the scans were taken
        forward    -- if `True`, selects forward scans; 
                      else selects reverse scans
        colorbar   -- if `True`, includes a colorbar scale
        big        -- if `True`, produces 1080p graphic; 
                      else creates standard 5"x3" graphic
    Effects:
        produce the waterfall animation;
        save said animation to disk (.mp4)
    '''
    fsorted = sorted([
        (int(f.split('.')[0].split('-')[0][1:]), f)
        for f in os.listdir(folderpath)
        if f.startswith("f" if forward else "r") and f.endswith(".npz")
    ])

    SS = []
    for infreq, filename in fsorted:
        with np.load(folderpath + filename) as scan:
            V, s = scan['V'], scan['s']
            d = dec / (125e6)  # sample length (s)
            f, _, S = sps.spectrogram(np.ravel(s),
                                      fs=(1 / d),
                                      nfft=2**14,
                                      noverlap=0,
                                      window=sps.hann(2**14))
            SS.append(S)

    infreqs, _ = zip(*fsorted)

    fig, ax = plt.subplots(figsize=(20, 11.25) if big else (5, 3), dpi=96)
    pc = ax.pcolormesh(V, f / 1_000, SS[0], norm=colors.LogNorm())
    if colorbar:
        cb = plt.colorbar(pc)
        cb.set_label = ("$I$ (W/Hz)")
    ax.set(title=f"$f$={infreqs[0]}Hz",
           xlabel="$V_{\mathrm{in}}$ (V)",
           ylabel="$f$ (kHz)",
           ylim=(0, 2 * (infreqs[0] / 1_000)))

    def animate(i):
        pc = ax.pcolormesh(V, f / 1_000, SS[i], norm=colors.LogNorm())
        ax.set(
            title=f"$f$={infreqs[i]}Hz",
            ylim=(0, 2 * (infreqs[i] / 1_000)),
        )
        return pc,

    anim = anm.FuncAnimation(fig, animate, interval=1000, frames=len(infreqs))
    anim.save(folderpath + "spec-animation.mp4")

    plt.draw()
    plt.show()
    def plot(self, tab1, tab2, canal_1, canal_2, canal_3, win_var=1):
        num_datos = len(canal_1)
        X = range(0, num_datos, 1)

        # Scale the signal in g's
        for indice in X:
            canal_1[indice] *= g_scale
            canal_2[indice] *= g_scale
            canal_3[indice] *= g_scale

        # Calculates medium value for each channel.
        vdc_canal_1 = 0
        vdc_canal_2 = 0
        vdc_canal_3 = 0
        for indice in X:
            vdc_canal_1 += canal_1[indice]
            vdc_canal_2 += canal_2[indice]
            vdc_canal_3 += canal_3[indice]
        vdc_canal_1 = vdc_canal_1 / num_datos
        vdc_canal_2 = vdc_canal_2 / num_datos
        vdc_canal_3 = vdc_canal_3 / num_datos
        #print("Vdc Channel 1: {0}, Vdc Channel 2: {1}".format(vdc_canal_1, vdc_canal_2))

        # Substract DC offset
        for indice in X:
            canal_1[indice] -= vdc_canal_1
            canal_2[indice] -= vdc_canal_2
            canal_3[indice] -= vdc_canal_3

        #----------------- Plotting ----------
        X1 = np.linspace(0, num_datos / 5,
                         num=num_datos)  # X axis, 5000 sps, 1/5 ms.

        # Figure 1. Sampled signals.
        #Channel X
        ax_11, ax_12, ax_13 = self.canvas2.figure.get_axes()
        ax_11.clear()
        ax_11.plot(X1, canal_1)
        ax_11.set_title("Channel X")
        ax_11.set_ylabel('g')
        ax_11.grid()  #Shows grid.

        #Channel Y
        ax_12.clear()
        ax_12.plot(X1, canal_2)
        ax_12.set_title("Channel Y")
        #ax_12.set_xlabel('ms')
        ax_12.set_ylabel('g')
        ax_12.grid()  #Shows grid.

        #Channel Z
        ax_13.clear()
        ax_13.plot(X1, canal_3)
        ax_13.set_title("Channel Z")
        ax_13.set_xlabel('ms')
        ax_13.set_ylabel('g')
        ax_13.grid()  #Shows grid.

        # Figure 2. FFT from signals.
        #Channel X
        canal_fft = []
        canal_fft = canal_1

        N = len(canal_fft)  # length of the signal

        #Window function
        if (win_var == 2):
            w = signal.hann(N, sym=False)  #Hann (Hanning) window
        elif (win_var == 3):
            w = signal.flattop(N, sym=False)  #Flattop window
        else:
            w = 1  #Rectangular window

        T = 1.0 / sample_rate
        y = canal_fft
        yf = fftpack.fft(y * w)
        xf = np.linspace(0.0, 1.0 / (2.0 * T), N / 2)

        ax_21, ax_22, ax_23 = self.canvas1.figure.get_axes()
        ax_21.clear()
        ax_21.plot(xf, 2.0 / N * np.abs(yf[:N / 2]))
        ax_21.grid()
        ax_21.set_title("Channel X")
        ax_21.set_ylabel('g')
        ax_21.set_xlim(xmax=max_freq)

        #Channel Y
        canal_fft = []
        canal_fft = canal_2

        N = len(canal_fft)  # length of the signal
        T = 1.0 / sample_rate
        y = canal_fft
        yf = fftpack.fft(y * w)
        xf = np.linspace(0.0, 1.0 / (2.0 * T), N / 2)

        ax_22.clear()
        ax_22.plot(xf, 2.0 / N * np.abs(yf[:N / 2]))
        ax_22.grid()
        ax_22.set_title("Channel Y")
        #ax_22.set_xlabel('Hz')
        ax_22.set_xlim(xmax=max_freq)
        ax_22.set_ylabel('g')

        #Channel Z
        canal_fft = []
        canal_fft = canal_3

        N = len(canal_fft)  # length of the signal
        T = 1.0 / sample_rate
        y = canal_fft
        yf = fftpack.fft(y * w)
        xf = np.linspace(0.0, 1.0 / (2.0 * T), N / 2)

        ax_23.clear()
        ax_23.plot(xf, 2.0 / N * np.abs(yf[:N / 2]))
        ax_23.grid()
        ax_23.set_title("Channel Z")
        ax_23.set_xlabel('Hz')
        #ax_23.set_xlim(xmax=max_freq)
        ax_23.set_xlim(xmax=max_freq_z)
        ax_23.set_ylabel('g')

        self.canvas1.draw()
        self.canvas2.draw()