def test_decimate():
    from numpy import arange,sin
    from scipy.signal import decimate, resample, cheby1, lfilter, filtfilt
    t = arange(0,30)
    q = 2
    n = 8
    print(t)
    print(decimate(t,2))
    print(resample(t, len(t)/2))
    t2 = sin(t)
    print(t2)
    print(len(t2))
    d2 = decimate(t2,2, ftype='fir')
    print(d2)
    print(len(d2))
    b, a = cheby1(n, 0.05, 0.8 / q)
    print(b,a)
    y = filtfilt(b, a, t2)
    print(y)
    sl = [slice(None)] * y.ndim

    sl[-1] = slice(None, None, -q)
    print(sl)
    print(y[sl])
    print(t2[sl])
    #r2 = resample(t2, len(t2)/2)
    #print(r2)
    #print(len(r2))
    assert(False)
示例#2
0
def demodulate(args):
    samples = args[0]
    decim_r1 = args[1]
    decim_r2 = args[2]
    # DEMODULATION CODE

    # LIMITER goes here

    # low pass & down sampling
    lp_samples = signal.decimate(lowpass_filter(samples,32),int(decim_r1))


# polar discriminator

    A = lp_samples[1:lp_samples.size]
    B = lp_samples[0:lp_samples.size-1]

    dphase = ( A * np.conj(B) )

    dphase.resize(dphase.size+1)
    dphase[dphase.size-1] = dphase[dphase.size-2]

    rebuilt = signal.medfilt(np.angle(dphase)/np.pi,15) #  np.cos(dphase)

    output = signal.decimate(rebuilt,int(decim_r2))

    return np.real(output)
示例#3
0
 def test_shape(self):
     # Regression test for ticket #1480.
     z = np.zeros((10, 10))
     d0 = signal.decimate(z, 2, axis=0)
     assert_equal(d0.shape, (5, 10))
     d1 = signal.decimate(z, 2, axis=1)
     assert_equal(d1.shape, (10, 5))
示例#4
0
文件: arrays.py 项目: dblalock/dig
def downsampleMat(A, rowsBy=1, colsBy=1):
	if len(A.shape) == 1:
		return signal.decimate(A, rowsBy, n=(rowsBy-1))
	if rowsBy != 1:
		A = signal.decimate(A, rowsBy, n=(rowsBy-1), axis=0)
	if colsBy != 1:
		A = signal.decimate(A, rowsBy, n=(colsBy-1), axis=1)
	return A
def sf_anal(infile, chunk_rate=80.0, n_steps=12, base_freq=440.0, min_level=0.001, cutoff=None):
    min_sq_level = min_level**2
    if cutoff is None: cutoff = chunk_rate

    sr, wav = load_non_wav(infile)
    chunk_size = int(round(float(sr) / chunk_rate))
    wav = high_passed(sr, wav)
    wav2 = wav * wav
    freqs = 2**(np.linspace(0.0, n_steps, num=n_steps, endpoint=False)/n_steps) * base_freq

    amp2 = wav2
    rel_cutoff = 2.0/(float(sr)/cutoff)  # relative to nyquist freq, not samplerate
    b, a = RC(Wn=rel_cutoff)
    for i in xrange(4):
        amp2 = filtfilt(b, a, amp2)

    mask = amp2>min_sq_level
    little_amp2 = decimate(
        amp2, chunk_size, ftype='fir'
    )
    little_mask = mask[np.arange(0,little_amp2.size,1)*chunk_size]

    little_corrs = []
    for freq in freqs:
        # For now, offset is rounded to the nearest sample
        offset = int(round(float(sr)/freq))
        cov = np.zeros_like(wav)
        cov[:-offset] = wav[offset:]*wav[:-offset]
        # repeatedly filter; this is effectively an 8th-order lowpass now
        smooth_cov = cov
        for i in xrange(4):
            smooth_cov = filtfilt(b, a, smooth_cov)
        
        # technically the correlation should be taken wrt the harmonic mean of the variances at
        # the two times, but we assume autocorrelation lag << smooth time
        little_corrs.append(
            decimate(
                mask * smooth_cov/np.maximum(amp2, min_sq_level),
                chunk_size,
                ftype='fir' #FIR is needed to be stable at haptic rates
            )
        )
    
    
    all_corrs = np.vstack(little_corrs)
    sample_times = (np.arange(0,little_amp2.size,1)*chunk_size).astype(np.float)/sr

    #trim "too quiet" stuff
    all_corrs = all_corrs[:,np.where(little_mask)[0]]
    sample_times = sample_times[np.where(little_mask)[0]]
    little_amp2 = little_amp2[np.where(little_mask)[0]]
    
    return dict(
        all_corrs=all_corrs,
        sample_times=sample_times,
        amp=np.sqrt(little_amp2), #RMS amp is more usual
    )
示例#6
0
文件: FEE.py 项目: jjgomezcadenas/IC
def DownScaleSignal(signal_t, signal, scale):
 	"""
 	downscales the time and signal vectors
 	"""
	signal_d=SGN.decimate(signal,scale,ftype='fir')
	signal_t_d = SGN.decimate(signal_t,scale,ftype='fir')

	#pulse_plot(signal_t_d, signal_d)
	return signal_t_d, signal_d
def subsample_basis_data(data, oldFreq, newFreq, filterType='Default', filterOrder=None):
    if int((oldFreq / newFreq) % int(oldFreq / newFreq)) is not 0:
        raise ArithmeticError("Subsampling can be done only with integer factors of Old Frequency / New Frequency")
    if filterType == 'Default':
        subData = signal.decimate(data, int(oldFreq / newFreq))
    else:
        if filterOrder == None:
            raise ArithmeticError("A filter order is required if the filter is to be specified")
        subData = signal.decimate(data, int(oldFreq / newFreq), filterOrder, filterType)
    return subData
示例#8
0
    def detect_pitch_hps(self, data, sampling_rate):
        freq_spectrum = np.fft.rfft(data)
        freq_spectrum = np.absolute(freq_spectrum)
        fft1 = freq_spectrum / float(100000)
        fft2 = signal.decimate(fft1, 2)
        fft3 = signal.decimate(fft1, 3)
        fft4 = signal.decimate(fft1, 4)
        fft5 = signal.decimate(fft1, 5)
        fft6 = signal.decimate(fft1, 6)

        max1 = self.detect_max_idx(fft1)
        max2 = self.detect_max_idx(fft2)
        sec_max1 = self.detect_second_max(fft1)
        sec_max2 = self.detect_second_max(fft2)

        print "maxes"
        print max1 * float(sampling_rate) / data.size
        print max2

        print "second maxes"
        print sec_max1
        print sec_max2
        
        plt.plot(fft1)
        plt.plot(fft2)
        plt.plot(fft3)
        

        n = fft6.size
        hps = np.zeros(n)

        for i in range(n):
            hps[i] = fft1[i] * fft2[i] * fft3[i] * fft4[i] * fft5[i] * fft6[i]

        #plt.plot(hps)
        plt.axis([0, 300, 0, 30])
        plt.show()

        maxFreqIdx1 = 0
        maxFreqIdx2 = 0
        maxFreqAmp1 = 0
        maxFreqAmp2 = 0
        
        for i in range(n):
            if hps[i] > maxFreqAmp1:
                maxFreqIdx2 = maxFreqIdx1
                maxFreqAmp2 = maxFreqAmp1
                maxFreqIdx1 = i
                maxFreqAmp1 = hps[i]

        #N = data.size
        return sampling_rate / maxFreqIdx1
示例#9
0
def load_continuous_tsd(paths, t_min=None, t_max=None, downsample=None,
                        columns=None):
    """
    read data for a specific time interval from a list of files (or ContinuousFile objects)
    Args:
        paths: a list of pathnames or ContinuousFile objects
        t_min: the low end of the time interval to read
        t_max: the high end of the time interval to read
        downsample: if not None, it should be an integer and acts as a downsampling factor (useful to get e.g. LFP)
        columns: a list of column names for the resulting TsdFrame. If None, the labels from the ContinuousFile objects 
        are used

    Returns:
        a TsdFrame with the data 
    """
    import scipy.signal as ss
    if isinstance(paths, str):
        paths = (paths,)
    elif not is_sequence(paths):
        raise TypeError("paths must be a string or list of strings.")

    if isinstance(paths[0], str):
        cf = [ContinuousFile(p) for p in paths]
    else:
        cf = paths

    data, tstamps = cf[0].read_interval(t_min, t_max)
    if downsample:
        data = ss.decimate(data, downsample, zero_phase=True)
    data = data.reshape((-1, 1))
    columns_from_files = False
    if columns is None:
        columns = [cf[0].label]
        columns_from_files = True
    if isinstance(columns, tuple):
        columns = list(columns)
    for f in cf[1:]:
        d, ts1 = f.read_interval(t_min, t_max)
        assert len(ts1) == len(tstamps)
        if downsample:
            d = ss.decimate(d, downsample, zero_phase=True)
        data = np.hstack((data, d.reshape((-1, 1))))
        if columns_from_files:
            columns.append(f.label)

    if downsample:
        tstamps = tstamps[::downsample]
        data = data[:, :len(tstamps)]

    cont_tsd = nts.TsdFrame(tstamps, data, columns=columns)

    return cont_tsd
示例#10
0
def loadITANfolder(folder,save_folder = None,q=25,overwrite=False):
    with Timer.Timer(folder):
        if type(save_folder) is 'NoneType':
           save_folder = folder  
        save_file = save_folder + folder[-14:-1] + '.nex'
        #get files in the folder
        files = getFileList(folder)
        # load info
        fid = open(folder+files['info'][0], 'rb')
        info = read_header(fid)
        sys.stdout.flush()
        #Sampling Rate
        sample_rate = info['sample_rate']
        time_vec = openDATfile(folder+files['time'][0],'time',sample_rate)
        time_vec = time_vec[0:-1:q]
        amp_unit = '$\mu V$'
        labels = []
        nch = len(files['amp']) + len(files['adc']) # +len(files['aux'])
        data = np.zeros([time_vec.shape[0],nch])
        eng = matlab.engine.start_matlab()
        eng.cd(folder,nargout=0)
        count = 0
        for f in files['amp']:
            sys.stdout.flush()
            with Timer.Timer(f):     
                name = f[:-4]
                labels.append(name)
                aux_data = openDATfile(folder+f,'amp',sample_rate)
                data[:,count] = sig.decimate(aux_data,q)
                count +=1
                if not overwrite:
                    if os.path.isfile(folder+name+'.mat'):
                        continue
                tfile = open(folder + 'Files.txt', 'w')
                tfile.write(name +'\n')
                tfile.close()
                sio.savemat(folder+name+'.mat', {'data':aux_data})
                eng.Get_spikes_alt(sample_rate,nargout=0)
                eng.close('all', nargout=0)
        eng.save_NEX(sample_rate,labels,int(time_vec[0]),save_file,nargout=0)
        for f in files['adc']:
            sys.stdout.flush()
            with Timer.Timer(f):  
                labels.append(f[:-4])
                aux_data = openDATfile(folder+f,'adc',sample_rate)
                data[:,count] = sig.decimate(aux_data,q)
                count +=1
        Data = DataObj.DataObj(data,sample_rate/q,amp_unit,labels,time_vec,[])
    Data.save(folder+'downsampled.h5','data')
    eng.quit()    
    return Data
def load_downsample_and_dump(base_dir, N, movie_name, arr_name):
    MOV_ARR_DIRS= [os.path.join(base_dir, str(i)) for i in range(N)]
    movies = [np.load(os.path.join(d, arr_name + '.npy')) for d in MOV_ARR_DIRS]
    for i_p in range(5):
        p = i_p + 1
        print 'Downsampling factor %d:' % 2**p
        for i, movie in enumerate(movies):
            m = decimate(decimate(movie, 2**p, axis=0), 2**p, axis=1)

            print 'Saving %s %s %d...' % (movie_name, arr_name, i)
            if not os.path.isdir(os.path.join(base_dir,str(i),
                                              arr_name+'_down')):
                os.makedirs(os.path.join(base_dir,str(i),arr_name+'_down'))
            np.save(os.path.join(base_dir,str(i),arr_name+'_down/%d'%2**p),m)
            print 'Saved.'
示例#12
0
def preprocess(filename):
    # takes in a filename (.wav format usually), and converts it from audio
    # to a data string. (this may not be needed?) Assuming a sample rate of
    # 44.1 kHz, preprocess the audio into a 252 x n matrix, where n is the
    # number of frames using the CQT transform with 36 bins per octave, and with
    # a hopsize of 512

    #read in audio file, convert to 16-bit int
    audiofile = AudioSegment.from_file(filename)
    data = np.fromstring(audiofile._data, np.int16)

    # downsample to 16kHz
    data = sig.decimate(data, 88200/16000)
    data = data.reshape(data.size,1)

    # assume 44.1 kHz, might downsample later
    sample_rate = 44100
    bins = 36
    # should min, max frequency be limited by piano-produced ranges?
    fmax = 4186.01
    fmin = 27.5
    cqt_audio = CQT(fmin, fmax, bins, sample_rate)
    cqt_audio.gen_cqt_kernel()
    cqt_audio.calc_CQT(data)
    cqt_audio.conv_sparse()

    print cqt_audio.sparse_cqt_coeff.shape

    return cqt_audio.sparse_cqt_coeff
def freq_from_hps(signal, fs):
    """
    Estimate frequency using harmonic product spectrum

    Low frequency noise piles up and overwhelms the desired peaks

    Doesn't work well if signal doesn't have harmonics
    """
    signal = asarray(signal) + 0.0

    N = len(signal)
    signal -= mean(signal)  # Remove DC offset

    # Compute Fourier transform of windowed signal
    windowed = signal * kaiser(N, 100)

    # Get spectrum
    X = log(abs(rfft(windowed)))

    # Remove mean of spectrum (so sum is not increasingly offset
    # only in overlap region)
    X -= mean(X)

    # Downsample sum logs of spectra instead of multiplying
    hps = copy(X)
    for h in range(2, 9):  # TODO: choose a smarter upper limit
        dec = decimate(X, h, zero_phase=True)
        hps[:len(dec)] += dec

    # Find the peak and interpolate to get a more accurate peak
    i_peak = argmax(hps[:len(dec)])
    i_interp = parabolic(hps, i_peak)[0]

    # Convert to equivalent frequency
    return fs * i_interp / N  # Hz
示例#14
0
文件: FEE.py 项目: jjgomezcadenas/IC
def down_scale_signal_(signal, scale):
 	"""
 	downscales the signal vector. Re-scale the energy
 	"""
	signal_d=SGN.decimate(signal,scale,ftype='fir')
	
	return signal_d*scale
示例#15
0
    def DistortionProducts(self, signal, reverbShift=1., reverbTau=1., echos=5, distortFactor=4., bandpass=True):
        """ Generate distortions to reverberated signal.
        :param signal: Signal
        :type signal: numpy arrray
        :param reverbShift: Delay between reverberations
        :type reverbShift: float
        :param reverbTau: Exponential decay constant
        :type reverbTau: float
        :param echos:  Number of reverberations (~5 is usually sufficient)
        :type echos: int
        :param distortFactor: Inverse scaling factor for signal in Boltzman filter (large value is small distortion)
        :type distortFactor: float
        :returns: numpy array with distorted signal
        """
        shift = reverbShift
        tau = reverbTau
        N = echos
        sigReverb = self.signalReverb(signal, shift, tau, N)

        resampleFactor = 4.
        sigReverb = resample(sigReverb, resampleFactor*len(sigReverb))
        self.Fs = resampleFactor*self.Fs

        sigReverbD = self.boltz(sigReverb/distortFactor)
        sigReverbDn = (sigReverbD - np.mean(sigReverbD[:100]))/np.max(np.abs(sigReverbD - np.mean(sigReverbD[:100])))
        if bandpass: 
        	sigReverbD_F = self.BandPassFilter(sigReverbDn, set_numtaps=501)
        else: sigReverbD_F = sigReverbDn

        resampleFactor = 4
        sigReverbD_F = decimate(sigReverbD_F, resampleFactor)
        self.Fs = self.Fs/float(resampleFactor)
        return sigReverbD_F
示例#16
0
def square_lock_in_decimate(time, bolo, f_lock, factor):
    # These phases produce approximately the same results as I =
    # sign(cos) and Q = sign(sin).
    f_nyquist = 0.5 / np.mean(np.diff(time))
    I = band_limited_square(time, f_lock, f_nyquist, np.pi/2)
    Q = band_limited_square(time, f_lock, f_nyquist, 0)
    return decimate(bolo * (I + 1j * Q), factor) #, 1023, 'fir')
示例#17
0
def downSample(data, sampleRate = 20000, dsType = 'mean'):
    """ Function that downsamples data.
    
    :param data: list including syllables with sample data
    :param sampleRate: desired samplerate
    :param dsType: Type of interpolating used for downsampling.
                   Can be mean or IIR, which uses an order 8 Chebyshev type 1 filter (default = mean)
    
    :returns syllables: downsampled data, in same format as input data
    """

    syllables = []
    for syllable in data:
        samples = []
        for sample in syllable:
            SR = int(np.round(sample[1]/float(sampleRate)))
            if dsType == 'mean':
                pad_size = int(math.ceil(float(sample[0].size)/SR)*SR - sample[0].size)
                s_padded = np.append(sample[0], np.zeros(pad_size)*np.NaN)
                s_new = sp.nanmean(s_padded.reshape(-1,SR), axis=1)
            elif dsType == 'IIR':
                s_new = ss.decimate(sample[0],SR)
            samples.append([s_new, sampleRate])
        syllables.append(samples)
    return syllables
示例#18
0
def decimate_labeled ( x, q, **kwargs ):
    '''Labeled analogue of scipy.signal.decimate; runs along x's 'time' axis.

    Inputs:
    x - LabeledArray containing the data; must have a 'time' axis
    q - Decimation factor passed to decimate
    **kwargs - (Optional) Keyword arguments passed to decimate

    Output is a LabeledArray with the same axes as x except for 'time', which
    is subsampled accordingly.
    '''
    
    # Make sure we don't override axis in the kwargs
    kwargs.pop( 'axis', None )

    time_axis = x.axis_index( 'time' )
    
    # Decimate
    ret_array = sig.decimate( x.array, q,
                              axis = time_axis,
                              **kwargs )
    
    # Form new axes
    ret_axes = OrderedDict( x.axes )
    ret_time = np.linspace( x.axes['time'][0], x.axes['time'][-1], ret_array.shape[time_axis] )
    ret_axes['time'] = ret_time
    
    return LabeledArray( array = ret_array, axes = ret_axes )
示例#19
0
文件: cq.py 项目: erikvdp/Thesis
def audio_to_pitch_given_boundaries(audio, filter_bank, window_boundaries):
    """
    like audio_to_pitch, but takes an iterable (window_boundaries) that contains
    2-tuples of the start and end of each window.
    """
    output = {} # output will be a dictionary indexed by pitch.
    for ratio, filters in filter_bank.items():
        if ratio == 1.0:
            resampled_audio = audio # decimate with ftype='fir' bitches when the ratio is 1. So do nothing in this case.
        else:
            resampled_audio = sig.decimate(audio, ratio, ftype='fir') # scipy's resample is different from matlab's resample...
            # use ftype='fir' instead of the default 'iir' to avoid warnings and bogus values.
        
        for pitch, filter_coeffs in filters.items():
            coeff_b, coeff_a = filter_coeffs
            out = sig.filtfilt(coeff_b, coeff_a, resampled_audio)
            out_squared = out ** 2 # energy
            
            # summarise over windows:
            summarised_output = np.zeros(len(window_boundaries))
            for (i, (start, end)) in enumerate(window_boundaries):
                summarised_output[i] = np.mean(out_squared[int(start/ratio):int(end/ratio)])
            
            output[pitch] = summarised_output

    return output
示例#20
0
文件: cq.py 项目: erikvdp/Thesis
def audio_to_pitch(audio, filter_bank, window_size=800, window_shift=400):
    # window size: number of samples per window. at 16kHz, a window of 800 samples spans 50ms.
    output = {} # output will be a dictionary indexed by pitch.
    for ratio, filters in filter_bank.items():
        if ratio == 1.0:
            resampled_audio = audio # decimate with ftype='fir' bitches when the ratio is 1. So do nothing in this case.
        else:
            resampled_audio = sig.decimate(audio, ratio, ftype='fir') # scipy's resample is different from matlab's resample...
            
        current_window_size = int(window_size / ratio)
        current_window_shift = int(window_shift / ratio)
            
        # use ftype='fir' instead of the default 'iir' to avoid warnings and bogus values.
        for pitch, filter_coeffs in filters.items():
            coeff_b, coeff_a = filter_coeffs
            out = sig.filtfilt(coeff_b, coeff_a, resampled_audio)
            out_squared = out ** 2 # energy
            
            # summarise over windows:
            windows = np.arange(0, len(out_squared), current_window_shift)
            summarised_output = np.zeros(len(windows))
            for i, s in enumerate(np.arange(0, len(out_squared), current_window_shift)):
                # summarised_output[i] = ratio * np.sum(out_squared[s:s+current_window_size])
                summarised_output[i] = np.mean(out_squared[s:s+current_window_size])
            
            output[pitch] = summarised_output

    return output
示例#21
0
def process_wav_files(wav_file_paths = example_wav_paths ,
                      output_dir = example_seg_paths,
                      savefig = False):

    for wav_file_path in wav_file_paths:

        # import data and down sample
        raw_sample_rate, raw_wav_data = wavfile.read( wav_file_path )
        downsample_factor = 4
        wav_data = decimate(raw_wav_data, downsample_factor)
        sample_rate = raw_sample_rate/downsample_factor

        # segment data (obtain segments and figure)
        segments, f, ax = simple_segmentation(wav_data, sample_rate)

        # read attributes from xml
        mediaID, classID = xml_attributes(wav_file_path.replace('.wav','.xml'))
 
        # add title and labels to plot
        ax.set_title(" MediaID: {0} ClassID: {1} ".format(mediaID, classID))
        ax.set_xlabel("Time (seconds)")
        ax.set_ylabel("Frequency (Hz)")

        basename = os.path.basename(wav_file_path.replace('.wav',''))

        if savefig:
            # save figure to output dir
            f.savefig(os.path.join(output_dir, basename+".pdf"))
        
        # save segments to csv
        np.savetxt(os.path.join(output_dir, basename+".csv"),
                   segments, delimiter = ",", fmt = '%3.4f')

    return None
示例#22
0
def freq_from_hps(signal, fs):
    """Estimate frequency using harmonic product spectrum
    
    Low frequency noise piles up and overwhelms the desired peaks
    """
    N = len(signal)
    signal -= mean(signal) # Remove DC offset
    
    # Compute Fourier transform of windowed signal
    windowed = signal * kaiser(N, 100)
    
    # Get spectrum
    X = log(abs(rfft(windowed)))
    
    # Downsample sum logs of spectra instead of multiplying
    hps = copy(X)
    for h in arange(2, 9): # TODO: choose a smarter upper limit
        dec = decimate(X, h)
        hps[:len(dec)] += dec
    
    # Find the peak and interpolate to get a more accurate peak
    i_peak = argmax(hps[:len(dec)])
    i_interp = parabolic(hps, i_peak)[0]
    
    # Convert to equivalent frequency
    return fs * i_interp / N # Hz
示例#23
0
文件: gabor.py 项目: mekman/popeye
def compute_model_ts(x, y, sigma, hrf_delay, theta, phi, cpd,
                     deg_x, deg_y, stim_arr, tr_length, 
                     frames_per_tr, norm_func=utils.zscore):
    
    # otherwise generate a prediction
    ts_stim = MakeFastGaborPrediction(deg_x,
                                      deg_y,
                                      stim_arr,
                                      x,
                                      y,
                                      sigma,
                                      theta,
                                      phi,
                                      cpd)
    
    # convolve it
    hrf = utils.double_gamma_hrf(hrf_delay, tr_length, frames_per_tr)
    
    # normalize it
    model = norm_func(ss.fftconvolve(ts_stim, hrf)[0:len(ts_stim)])

    # decimate it
    model = ss.decimate(model, int(frames_per_tr), 1)
    
    return model
示例#24
0
def lofar(data, fs, n_pts_fft=1024, n_overlap=0,
    decimation_rate=3, spectrum_bins_left=None, **tpsw_args):
    norm_parameters = {'lat_window_size': 10, 'lat_gap_size': 1, 'threshold': 1.3}
    if not isinstance(data, np.ndarray):
        raise NotImplementedError

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

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

    return np.transpose(power), freq, time
示例#25
0
文件: hps.py 项目: roim/PyTranscribe
def hps(x, fs=44100, lf=255, harmonics=3, precision=2, window=lambda l:_np.kaiser(l, 7.14285)):
    """ Estimates the pitch (fundamental frequency) of the given sample array by a standard HPS implementation. """
    x -= _np.mean(x)
    N = x.size
    w = x*window(N)

    # Append zeros to the end of the window so that each bin has at least the desired precision.
    if fs/N > precision:
        delta = int(fs/precision) - N
        w = _np.append(w, _np.zeros(delta))
        N = w.size

    X = _np.log(_np.abs(_np.fft.rfft(w)))

    # Sequentially decimate 'X' 'harmonics' times and add it to itself.
    # 'precision < fs/N' must hold, lest the decimation loses all the precision we'd gain.
    hps = _np.copy(X)
    for h in range(2, 2 + harmonics):
        dec = _sig.decimate(X, h)
        hps[:dec.size] += dec*(0.8**h)

    # Find the bin corresponding to the lowest detectable frequency.
    lb = lf*N/fs

    # And then the bin with the highest spectral content.
    arg_peak = lb + _np.argmax(hps[lb:dec.size])

    # TODO: Return the full array? A ranked list of identified notes?
    return fs*arg_peak/N
    def process(self, mode = 'downsample', debug = False):
        """Generate amplitude envelopes from a full path to a .wav, following
        Lewandowski (2012).

        Parameters
        ----------
        filename : str
            Full path to .wav file to process.
        freq_lims : tuple
            Minimum and maximum frequencies in Hertz to use.
        num_bands : int
            Number of frequency bands to use.
        win_len : float, optional
            Window length in seconds for using windows. By default, the
            envelopes are resampled to 120 Hz instead of windowed.
        time_step : float
            Time step in seconds for windowing. By default, the
            envelopes are resampled to 120 Hz instead of windowed.

        Returns
        -------
        2D array
            Amplitude envelopes over time.  If using windowing, the first
            dimension is the time in frames, but by default the first
            dimension is time in samples with a 120 Hz sampling rate.
            The second dimension is the amplitude envelope bands.

        """
        self._sr, proc = preproc(self._filepath,alpha=0.97)

        proc = proc / 32768 #hack!! for 16-bit pcm
        proc = proc/sqrt(mean(proc**2))*0.03;
        bandLo = [ self._freq_lims[0]*exp(log(
                                    self._freq_lims[1]/self._freq_lims[0]
                                    )/self._num_bands)**x
                                    for x in range(self._num_bands)]
        bandHi = [ self._freq_lims[0]*exp(log(
                                    self._freq_lims[1]/self._freq_lims[0]
                                    )/self._num_bands)**(x+1)
                                    for x in range(self._num_bands)]

        envs = []
        for i in range(self._num_bands):
            b, a = butter(2,(bandLo[i]/(self._sr/2),bandHi[i]/(self._sr/2)), btype = 'bandpass')
            env = filtfilt(b,a,proc)
            env = abs(hilbert(env))
            if mode == 'downsample':
                #env = resample(env,int(ceil(len(env)/int(ceil(self._sr/120)))))
                print(int(ceil(self._sr/120)))
                env = decimate(env,int(ceil(self._sr/120)))
            envs.append(env)
        envs = array(envs).T
        if mode == 'downsample':
            self._sr = 120
        self._rep = dict()
        for i in range(envs.shape[0]):
            self._rep[i/self._sr] = envs[i,:]
        #Don't know if this is the best way to do it
        if debug:
            return proc
示例#27
0
def downsample(samples, samplerate, max_frequency):
    # First find minimum samplerate multiplier to satisfy nyquist
    q = 1
    while samplerate / (q + 1) > (max_frequency * 2):
        q += 1

    logger.debug("[DECIMATE] Found decimation factor: %s (%s -> %s)" % (q, samplerate, samplerate / q))
    decimated_samples = signal.decimate(samples, q).astype(samples.dtype)
    return decimated_samples, samplerate / q
 def filterSig(self, order, lowcut, highcut, decimationFactor, method='lfilter'):
     """
     if method equals 'default' or 'filtfilt' do forward-backward filter method
     else do lfilter
     """
     
     if self.a==None:
         self._createFilter(lowcut, highcut, order=order)
     
     #Filter and decimate signal
     if method.lower() in ('default','filtfilt'):
         self.mainSignal = decimate(filtfilt(self.b, self.a, self.mainSignal), decimationFactor)
     else:
         self.mainSignal = decimate(lfilter(self.b, self.a, self.mainSignal), decimationFactor)
     
     self.fs = self.fs//decimationFactor
     self.numPoints = len(self.mainSignal)
     return self.mainSignal
示例#29
0
def downsampleBy(v, factor):
	order = 8 	# default value for decimate() anyway
	minSamples = 5
	if len(v) < (minSamples * factor):
		factor = int(len(v) / minSamples)
	v = signal.decimate(v, factor, n=order)
	if len(v) >= (order + minSamples):
		return v[(order - 1):]
	return v
def read_data_thread(rf_q,ad_rdy_ev,audio_q):
    print("read data thread start")
    pre_data=0
    #filter design
    #FIR_LP = signal.firwin(19,17e3,1e3,window='hamming',True,False,RFRATE/2)
    #FIR_LP = signal.firwin(19,400e3/(RFRATE/2))
    #FIR_LP = signal.firwin(9,10e3/(AUDIORATE/2))
    rfwindow = signal.hamming(RFSIZE)
    audiowindow = signal.hamming(AUDIOSIZE)
    fftwindow = signal.hamming(512)

    ad_rdy_ev.wait(timeout=5000)
    np.save("data",rf_q.get())
    while 0:
        ad_rdy_ev.wait(timeout=1000)
        while not rf_q.empty():
            #process data here
            data=rf_q.get()
            #data=signal.decimate(data,DOWN_FACTOR,ftype="fir")

            #data = signal.lfilter(FIR_LP,1.0,data)

            #demod method 1
            angle_data=np.angle(data)
            audioda=np.diff(angle_data)
            audiodata=np.insert(audioda,0,angle_data[0]-pre_data)
            pre_data=angle_data[-1]
            audiodata=np.unwrap(audiodata,np.pi)


            #demod method 2
            #data_delay=np.insert(data,0,pre_data)
            #pre_data = data_delay[-1]
            #data_delay=np.delete(data_delay,-1)
            #diff_data=data*np.conj(data_delay)
            #audiodata=np.angle(diff_data)
            #audiodata=np.unwrap(audiodata)


            #demod method 3
            #diff_data=np.diff(data)
            #diff_data=np.insert(diff_data,0,data[0]-pre_data)
            #pre_data=data[-1]
            #audiodata=data.real*diff_data.imag-data.imag*diff_data.real
            #audiodata=audiodata/(np.power(data.real,2)+np.power(data.imag,2))
            #audiodata=audiodata*10

            audiodata=signal.decimate(audiodata,DOWN_FACTOR,ftype="fir")

            #audiodata = signal.lfilter(FIR_LP,1.0,audiodata)

            audiodata_amp=audiodata*1e4
            snd_data = audiodata_amp.astype(np.dtype('<i2')).tostring()
            audio_q.put(snd_data)
        ad_rdy_ev.clear()
    print("read data thread ended")
示例#31
0
def returnStability(eegdata):
    decimated = decimate(eegdata, 1000, ftype='fir')
    return decimated
示例#32
0
        emg = pd.read_csv("training_data/features/" + str(feature) +
                          "/instance_" + str(instance) + "/emg.csv")
        instances[feature][instance] = [
            np.array(acc['Acc_X']),  # 0
            np.array(acc['Acc_Y']),  # 1
            np.array(acc['Acc_Z']),  # 2
            np.array(gyro['Gyro_X']),  # 3
            np.array(gyro['Gyro_Y']),  # 4
            np.array(gyro['Gyro_Z']),  # 5
            np.array(orien['Roll']),  # 6
            np.array(orien['Pitch']),  # 7
            np.array(orien['Yaw']),  # 8
            np.array(orien['Orien_X']),  # 9
            np.array(orien['Orien_Y']),  # 10
            np.array(orien['Orien_Z']),  # 11
            np.array(sp.decimate(emg['EMG1'], 4, None, 'fir', -1, True)),  # 12
            np.array(sp.decimate(emg['EMG2'], 4, None, 'fir', -1, True)),  # 13
            np.array(sp.decimate(emg['EMG3'], 4, None, 'fir', -1, True)),  # 14
            np.array(sp.decimate(emg['EMG4'], 4, None, 'fir', -1, True)),  # 15
            np.array(sp.decimate(emg['EMG5'], 4, None, 'fir', -1, True)),  # 16
            np.array(sp.decimate(emg['EMG6'], 4, None, 'fir', -1, True)),  # 17
            np.array(sp.decimate(emg['EMG7'], 4, None, 'fir', -1, True)),  # 18
            np.array(sp.decimate(emg['EMG8'], 4, None, 'fir', -1, True))  # 19
        ]

# delete the first rows in IMU (IMU length = EMG length) and finding the max length
max_length = 0
for feature in range(number_of_features):
    for instance in range(number_of_instances):
        for column in range(0, 12):
            delete = instances[feature][instance][column].__len__(
示例#33
0
def turbo_spin_echo(self, plotSeq):

    init_gpa = True
    lo_freq = self.lo_freq
    rf_amp = self.rf_amp
    #    trs=self.trs
    rf_pi_duration = None
    rf_pi2_duration = self.rf_pi2_duration
    echo_duration = self.echo_duration * 1e3
    tr_duration = self.tr_duration * 1e3
    BW = self.BW
    shim_x: float = self.shim[0]
    shim_y: float = self.shim[1]
    shim_z: float = self.shim[2]
    nScans = self.nScans
    fov_x: int = self.fov_rd * 1e-2
    fov_y: int = self.fov_ph * 1e-2
    fov_z: int = self.fov_sl * 1e-2
    trap_ramp_duration = self.trap_ramp_duration
    phase_grad_duration = self.phase_grad_duration
    echos_per_tr = self.echos_per_tr
    rd_preemph_factor: float = self.preemph_factor
    sweep_mode = self.sweep_mode
    par_acq_factor = self.par_acq_factor
    n_rd = self.n_rd
    n_ph = self.n_ph
    n_sl = self.n_sl
    x = self.x
    y = self.y
    z = self.z
    oversampling_factor = self.oversampling_factor

    BW = BW * 1e-3
    #    trap_ramp_pts=np.int32(trap_ramp_duration*0.2)    # 0.2 puntos/ms
    trap_ramp_pts = 10
    grad_readout_delay = 9  #8.83    # readout amplifier delay
    grad_phase_delay = 9  #8.83
    grad_slice_delay = 9  #8.83
    rx_period = 1 / (BW * oversampling_factor)
    """
    readout gradient: x
    phase gradient: y
    slice/partition gradient: z
    """

    expt = ex.Experiment(lo_freq=lo_freq,
                         rx_t=rx_period,
                         init_gpa=init_gpa,
                         gpa_fhdo_offset_time=(1 / 0.2 / 3.1))
    true_rx_period = expt.get_rx_ts()[0]
    true_BW = 1 / true_rx_period
    true_BW = true_BW / oversampling_factor
    readout_duration = n_rd / true_BW

    # We calculate here the realtive sequence efficiency
    alphaRO = fov_x / n_rd * np.sqrt(np.float(n_rd) / true_BW)
    alphaPH = fov_y / n_ph * np.sqrt(np.float(echos_per_tr))
    alphaSL = fov_z / n_sl * np.sqrt(
        np.float(n_sl) / (np.float(n_sl) / 2 + np.float(par_acq_factor)))
    alpha = alphaRO * alphaPH * alphaSL * 10000
    print('alpha:%f' % (alpha))

    if rf_pi_duration is None:
        rf_pi_duration = 2 * rf_pi2_duration

    # Calibration constans to change from T/m to DAC amplitude

    gammaB = 42.56e6  # Gyromagnetic ratio in Hz/T
    # Get readout, phase and slice amplitudes
    # Readout gradient amplitude
    Grd = true_BW * 1e6 / (gammaB * fov_x)
    # Phase gradient amplitude
    if (n_ph == 1):
        Gph = 0
    else:
        Gph = n_ph / (2 * gammaB * fov_y * phase_grad_duration * 1e-6)
    # Slice gradient amplitude
    if (n_sl == 1):
        Gsl = 0
    else:
        Gsl = n_sl / (2 * gammaB * fov_z * phase_grad_duration * 1e-6)

    # Get the phase gradient vector
    if (n_ph > 1):
        phase_amps = np.linspace(-Gph, Gph, n_ph)
    else:
        phase_amps = np.linspace(-Gph, Gph, n_ph)
    ind = getIndex(self, phase_amps, echos_per_tr, n_ph, sweep_mode)
    phase_amps = phase_amps[ind]

    # Get the slice gradient vector
    if (n_sl > 1):
        slice_amps = np.linspce(-Gsl, Gsl, n_sl + 1)
        slice_amps = slice_amps[1:n_sl + 1]
    else:
        slice_amps = np.linspace(-Gsl, Gsl, n_sl)

#*********************************************************************************
#*********************************************************************************
#*********************************************************************************

    def rf_wf(tstart, echo_idx):
        pi2_phase = 1  # x
        pi_phase = 1j  # y
        if echo_idx == 0:
            # do pi/2 pulse, then start first pi pulse
            return np.array([
                tstart + (echo_duration - rf_pi2_duration) / 2,
                tstart + (echo_duration + rf_pi2_duration) / 2,
                tstart + echo_duration - rf_pi_duration / 2
            ]), np.array([pi2_phase * rf_amp, 0, pi_phase * rf_amp])
#        elif echo_idx == self.echos_per_tr and ph==n_ph and sl==n_sl-1:
#            # last echo of the full sequence
#            return np.array([tstart + rf_pi_duration/2, tr_duration-echo_duration*echos_per_tr]), np.array([0, 0])
        elif echo_idx == self.echos_per_tr:
            # last echo on any other echo train
            return np.array([tstart + rf_pi_duration / 2]), np.array([0])
        else:
            # finish last pi pulse, start next pi pulse
            return np.array([
                tstart + rf_pi_duration / 2,
                tstart + echo_duration - rf_pi_duration / 2
            ]), np.array([0, pi_phase * self.rf_amp])

#*********************************************************************************
#*********************************************************************************
#*********************************************************************************

    def tx_gate_wf(tstart, echo_idx, ph, sl):
        tx_gate_pre = 15  # us, time to start the TX gate before each RF pulse begins
        tx_gate_post = 1  # us, time to keep the TX gate on after an RF pulse ends

        if echo_idx == 0:
            # do pi/2 pulse, then start first pi pulse
            return np.array([tstart + (echo_duration - rf_pi2_duration)/2 - tx_gate_pre,
                             tstart + (echo_duration + rf_pi2_duration)/2 + tx_gate_post,
                             tstart + echo_duration - rf_pi_duration/2 - tx_gate_pre]), \
                             np.array([1, 0, 1])
        elif echo_idx == self.echos_per_tr and ph == n_ph and sl == n_sl - 1:
            return np.array([
                tstart + rf_pi_duration / 2 + tx_gate_post,
                tstart + tr_duration - echo_duration * echos_per_tr
            ]), np.array([0, 0])
        elif echo_idx == echos_per_tr:
            # finish final RF pulse
            return np.array([tstart + rf_pi_duration / 2 + tx_gate_post
                             ]), np.array([0])
        else:
            # finish last pi pulse, start next pi pulse
            return np.array([tstart + rf_pi_duration/2 + tx_gate_post, tstart + self.echo_duration - rf_pi_duration/2 - tx_gate_pre]), \
                np.array([0, 1])

#*********************************************************************************
#*********************************************************************************
#*********************************************************************************

    def readout_wf(tstart, echo_idx):
        if echo_idx == 0:
            return np.array([tstart]), np.array([0])  # keep on zero otherwise
#        elif echo_idx==echos_per_tr:
#            return np.array([tstart + (echo_duration - readout_duration)/2, tstart + (echo_duration + readout_duration)/2, tstart+echo_duration+tr_duration-echos_per_tr*echo_duration ]), np.array([1, 0, 0])
        else:
            return np.array([
                tstart + (echo_duration - readout_duration) / 2,
                tstart + (echo_duration + readout_duration) / 2
            ]), np.array([1, 0])

#*********************************************************************************
#*********************************************************************************
#*********************************************************************************

    def readout_grad_wf(tstart, echo_idx):
        if echo_idx == 0:
            #            return trap_cent(tstart+echo_duration/2+rf_pi2_duration/2+trap_ramp_duration+readout_duration/2-grad_readout_delay, Grd/2*rd_preemph_factor, readout_duration+trap_ramp_duration,
            #                             trap_ramp_duration, trap_ramp_pts)
            return rect_cent(
                tstart + echo_duration / 2 + rf_pi2_duration / 2 +
                trap_ramp_duration + readout_duration / 2 - grad_readout_delay,
                Grd / 2.0 * rd_preemph_factor, readout_duration,
                trap_ramp_duration)
        else:
            #            return trap_cent(tstart + echo_duration/2-grad_readout_delay, Grd, readout_duration+trap_ramp_duration,
            #                             trap_ramp_duration, trap_ramp_pts)
            return rect_cent(tstart + echo_duration / 2 - grad_readout_delay,
                             Grd, readout_duration, trap_ramp_duration)

#*********************************************************************************
#*********************************************************************************
#*********************************************************************************

    def phase_grad_wf(tstart, echo_idx, ph):
        #        t1, a1 = trap_cent(tstart + (rf_pi_duration+phase_grad_duration-trap_ramp_duration)/2+trap_ramp_duration-grad_phase_delay,
        #                            phase_amps[ph-1], phase_grad_duration, trap_ramp_duration, trap_ramp_pts)
        #        t2, a2 = trap_cent(tstart + echo_duration/2 + readout_duration/2+phase_grad_duration/2+trap_ramp_duration/2-grad_phase_delay,
        #                            -phase_amps[ph-1], phase_grad_duration, trap_ramp_duration, trap_ramp_pts)
        t1, a1 = rect_cent(
            tstart + rf_pi_duration / 2 + phase_grad_duration / 2 +
            trap_ramp_duration - grad_phase_delay, phase_amps[ph - 1],
            phase_grad_duration, trap_ramp_duration)
        t2, a2 = rect_cent(
            tstart + echo_duration / 2 + readout_duration / 2 +
            trap_ramp_duration + phase_grad_duration / 2 - grad_phase_delay,
            -phase_amps[ph - 1], phase_grad_duration, trap_ramp_duration)
        if echo_idx == 0:
            return np.array([tstart]), np.array([0])  # keep on zero otherwise
        elif echo_idx == echos_per_tr:  # last echo, don't need 2nd trapezoids
            return t1, a1
        else:  # otherwise do both trapezoids
            return np.hstack([t1, t2]), np.hstack([a1, a2])

#*********************************************************************************
#*********************************************************************************
#*********************************************************************************

    def slice_grad_wf(tstart, echo_idx, sl):
        #        t1, a1 = trap_cent(tstart + (rf_pi_duration+phase_grad_duration-trap_ramp_duration)/2+trap_ramp_duration-grad_phase_delay, slice_amps[sl], phase_grad_duration,
        #                           trap_ramp_duration, trap_ramp_pts)
        #        t2, a2 = trap_cent(tstart +echo_duration/2 + readout_duration/2+phase_grad_duration/2+trap_ramp_duration/2-grad_slice_delay, -slice_amps[sl], phase_grad_duration,
        #                           trap_ramp_duration, trap_ramp_pts)
        t1, a1 = rect_cent(
            tstart + rf_pi_duration / 2 + trap_ramp_duration +
            phase_grad_duration / 2 - grad_slice_delay, slice_amps[sl],
            phase_grad_duration, trap_ramp_duration)
        t2, a2 = rect_cent(
            tstart + echo_duration / 2 + readout_duration / 2 +
            trap_ramp_duration + phase_grad_duration / 2 - grad_slice_delay,
            -slice_amps[sl], phase_grad_duration, trap_ramp_duration)
        if echo_idx == 0:
            return np.array([tstart]), np.array([0])  # keep on zero otherwise
        elif echo_idx == echos_per_tr:  # last echo, don't need 2nd trapezoids
            return t1, a1
        else:  # otherwise do both trapezoids
            return np.hstack([t1, t2]), np.hstack([a1, a2])

#*********************************************************************************
#*********************************************************************************
#*********************************************************************************

    global_t = 20  # start the first TR at 20us
    #    for nS in range(nScans):
    if par_acq_factor == 0:
        n_sl_par = n_sl
    else:
        n_sl_par = np.int32(n_sl / 2) + par_acq_factor
    for sl in range(n_sl_par):
        for ph_block in range(np.int32(n_ph / echos_per_tr)):
            for echo_idx in range(1 + echos_per_tr):
                tx_t, tx_a = rf_wf(global_t, echo_idx)
                tx_gate_t, tx_gate_a = tx_gate_wf(
                    global_t, echo_idx, ph_block * echos_per_tr + echo_idx, sl)
                readout_t, readout_a = readout_wf(global_t, echo_idx)
                rx_gate_t, rx_gate_a = readout_wf(global_t, echo_idx)
                readout_grad_t, readout_grad_a = readout_grad_wf(
                    global_t, echo_idx)
                phase_grad_t, phase_grad_a = phase_grad_wf(
                    global_t, echo_idx, ph_block * echos_per_tr + echo_idx)
                slice_grad_t, slice_grad_a = slice_grad_wf(
                    global_t, echo_idx, sl)

                expt.add_flodict({
                    'tx0': (tx_t, tx_a),
                    'grad_vx':
                    (eval('%s_grad_t' % (x)),
                     eval('%s_grad_a/(Gx_factor/1000)/10+shim_x' % (x))),
                    'grad_vy':
                    (eval('%s_grad_t' % (y)),
                     eval('%s_grad_a/(Gy_factor/1000)/10+shim_y' % (y))),
                    'grad_vz':
                    (eval('%s_grad_t' % (z)),
                     eval('%s_grad_a/(Gz_factor/1000)/10+shim_z' % (z))),
                    'rx0_en': (readout_t, readout_a),
                    'tx_gate': (tx_gate_t, tx_gate_a),
                    'rx_gate': (rx_gate_t, rx_gate_a),
                })

                global_t += echo_duration

            global_t += tr_duration - echo_duration * echos_per_tr

    expt.add_flodict({
        'grad_vx': (np.array([global_t + echo_duration]), np.array([0])),
        'grad_vy': (np.array([global_t + echo_duration]), np.array([0])),
        'grad_vz': (np.array([global_t + echo_duration]), np.array([0])),
    })

    if plotSeq == 1:  # What is the meaning of plotSeq??
        expt.plot_sequence()
        plt.show()
        expt.__del__()
    elif plotSeq == 0:
        for nS in range(nScans):
            print('nScan=%s' % (nS))
            rxd, msgs = expt.run()
            #            data_nodecimate = rxd['rx0']
            #Decimate
            rxd['rx0'] = sig.decimate(rxd['rx0'],
                                      oversampling_factor,
                                      ftype='fir',
                                      zero_phase=True)
            rxd['rx0'] = rxd[
                'rx0'] * 13.788  # Here I normalize to get the result in mV
            if nS == 0:
                n_rxd = rxd['rx0']
#                n_nodecimate = data_nodecimate
            else:
                n_rxd = np.concatenate((n_rxd, rxd['rx0']), axis=0)
#                n_nodecimate=np.concatenate((n_nodecimate, data_nodecimate), axis=0)
        if par_acq_factor == 0 and nScans > 1:
            n_rxd = np.reshape(n_rxd, (nScans, n_sl * n_ph * n_rd))
            data_avg = np.average(n_rxd, axis=0)
#            n_nodecimate=np.reshape(n_nodecimate, (nScans, n_sl*n_ph*n_rd))
#            data_nodecimate = np.average(n_nodecimate, axis=0)
        elif par_acq_factor > 0 and nScans > 1:
            n_rxd = np.reshape(n_rxd, (nScans, n_sl_par * n_ph * n_rd))
            data_avg = np.average(n_rxd, axis=0)
#            n_nodecimate=np.reshape(n_nodecimate, (nScans, n_sl_par*n_ph*n_rd))
#            data_nodecimate = np.average(n_nodecimate, axis=0)
        else:
            data_avg = n_rxd

        expt.__del__()

        # Reorganize the data matrix according to the sweep mode
        rxd_temp1 = np.reshape(data_avg, (n_sl_par, n_ph, n_rd))
        rxd_temp2 = rxd_temp1 * 0
        for ii in range(n_ph):
            ind_temp = ind[ii]
            rxd_temp2[:, ind_temp, :] = rxd_temp1[:, ii, :]

        rxd_temp3: complex = np.zeros((n_sl, n_ph, n_rd)) + 1j * np.zeros(
            (n_sl, n_ph, n_rd))
        rxd_temp3[0:n_sl_par, :, :] = rxd_temp2
        data_avg = np.reshape(rxd_temp3, -1)  # -1 means reshape to 1D array

        return n_rxd, msgs, data_avg
示例#34
0
def HPS(f, c, fwin_pick, plot=False, nharms=6, f_prop=-1):
    c_prod = np.copy(c[0:int(len(c) / nharms)])
    c_prod = convolve(c_prod, [0.25, 0.5, 0.25], mode='same') * 2

    if plot:
        fig = plt.figure()
        ax = fig.add_subplot(111)
        ax.plot(f, c, label='Main mode')

    for i in range(2, nharms + 1):
        a = decimate(c, i)
        c_prod += a[0:len(c_prod)]
        if plot:
            ax.plot(f[0:len(a)], a, label='Harm %d (a)' % i)

    f_red = f[0:len(c_prod)]
    b = np.array([f_red > fwin_pick[0], f_red < fwin_pick[1]]).all(axis=0)

    # Reduce to values in frequency range
    f_prod = f_red[b]
    c_prod = c_prod[b]

    # detrend c_prod
    c_prod_det = detrend(c_prod)

    # pick maximum value
    i = np.argmax(c_prod_det)
    f_peak_initial = f_prod[i]
    p_peak_initial = c_prod_det[i]

    # if a frequency value has been proposed, override the maximum
    if fwin_pick[0] < f_prop < fwin_pick[1]:
        f_peak_initial = f_prop

    # Pick maximum by fitting Gaussian
    # Set starting values
    x0 = np.asarray((f_peak_initial, p_peak_initial, 0.004))

    # minimize
    res = minimize(fun=_misfit,
                   x0=x0,
                   args=(f_prod, c_prod_det),
                   bounds=((0, None), (None, None), (0, None)))
    f_peak = res['x'][0]
    p_peak = res['x'][1]
    sigma = res['x'][2]

    if plot:
        # ax.plot(f_prod,
        #         c_prod,
        #         'k',
        #         label='Product', LineWidth=3)
        ax.plot(f_prod, c_prod_det - 140, 'k', label='detrend', LineWidth=3)
        ax.plot(f_prod,
                _gaussfun(f_prod, *x0) - 140,
                'g',
                Linewidth=3,
                label='X0')
        ax.plot(f_prod,
                _gaussfun(f_prod, *res['x']) - 140,
                'r',
                Linewidth=3,
                label='fit')
        ax.set_xlim(f[0], f[-1] / nharms)
        ax.legend()
        ax.vlines(fwin_pick[0], -150, -90, 'r')
        ax.vlines(fwin_pick[1], -150, -90, 'r')
        ax.vlines(f_peak, -150, -90, 'k')
        ax.vlines(f_peak_initial, -150, -90)
        ax.set_ylim((-150, -90))
        plt.show()
    return f_peak, p_peak, sigma
示例#35
0
    # Integrate
    if integrate:
        dat_output, i_zi = EEGsynth.online_filter(1, [1, -1],
                                                  dat_output,
                                                  axis=0,
                                                  zi=integrate_zi)

    # Rectifying
    if rectify:
        dat_output = np.absolute(dat_output)

    # Downsampling
    if not (downsample is None):
        dat_output = decimate(dat_output,
                              downsample,
                              ftype='iir',
                              axis=0,
                              zero_phase=True)
        window_new = int(window / downsample)
        if debug > 1:
            print("downsampled ", window, "samples in",
                  (time.time() - start) * 1000, "ms")
    else:
        window_new = window

    # Smoothing
    if not (smoothing is None):
        for t in range(window):
            dat_output[t, :] = smoothing * dat_output[t, :] + (
                1. - smoothing) * previous
            previous = copy(dat_output[t, :])
示例#36
0
文件: fm.py 项目: simontking/Project
    print(  np.angle(tmp));
    result[k] = (np.abs(fftinfo[I])/(fs/fc)) + 1j*np.angle(fftinfo[I])
    
print(result)
fc1 = np.exp(-1j*2*np.pi*(fc)*time)
#print(fc1)
x2 = np.array(product * fc1).astype("complex64")
lpfr = remez(64, [0,fbw,fbw+(fs/2 - fbw)/4, fs/2], [1,0], Hz=fs)
x3 = lfilter(lpfr,1.0,x2)

dec_rate = 5
print('dec',dec_rate)
x4 = x3[0::dec_rate]
fs_y = fs/dec_rate
print('fsy',fs_y)
x42 = decimate(x2, dec_rate)
plt.figure()
plt.subplot(4, 1, 1)
plt.plot(product_demod)
plt.subplot(4, 1, 2)
plt.plot(x2)
plt.subplot(4, 1, 3)
plt.plot(x3)
plt.subplot(4, 1,4)
plt.plot(x4)

tmp = np.array(x4[1:]*np.conj(x4[:-1])).astype("complex64")
x5=np.angle(tmp)

d = fs_y * 75e-6
x = np.exp(-1/d)
def downsample10x(ts):
    return signal.decimate(ts, 10, axis=0)
示例#38
0
    def on_cross_save_btn(self, event):
        """
        save tie-line cross-over differences in csv file
        file includes XY coordinate of crossings and cross-over differences of selected parameters
        """
        self.log_text.AppendText(
            f'{datetime.now()}: save cross-over differences to .csv file..\n')

        if self.data is None:
            self.log_text.AppendText(f'{datetime.now()}: Load data first\n')
            return

        if self.index_table is None:
            self.log_text.AppendText(f'{datetime.now()}: Find turns first\n')
            return

        if self.f_lines is None:
            self.log_text.AppendText(
                f'{datetime.now()}: Split into lines first\n')
            return

        if self.crossing_idx is None:
            self.log_text.AppendText(
                f'{datetime.now()}: Calculate crossings first\n')
            return

        p = self.cross_stats_select.GetString(
            self.cross_stats_select.GetSelection())
        if p is '':
            self.log_text.AppendText(f'{datetime.now()}: Nothing selected\n')
            return

        try:
            float(self.data[p][0])
        except ValueError:
            self.log_text.AppendText(
                f'{datetime.now()}: Selected parameter does not contain numbers\n'
            )
            return

        self.on_cross_stats_btn(self)

        # get parameters for cross-over differences from a multi choice dialog
        choices = self.data.dtype.names

        with wx.MultiChoiceDialog(None, 'Select variables',
                                  'Cross-differences', choices) as dlg:
            if dlg.ShowModal() == wx.ID_CANCEL:
                return  # user changed their mind
            selection = dlg.GetSelections()

        # iterate through selected parameters
        differences, names = [], []
        for var in selection:
            signal_name = self.data.dtype.names[var]

            # check if all signals are valid
            try:
                float(self.data[signal_name][0])
            except ValueError:
                self.log_text.AppendText(
                    f'{datetime.now()}: Parameter {signal_name} does not contain numbers\n'
                )
                return

            # decimate signal if q != 1
            if self.q != 1:
                signal = decimate(self.data[signal_name], q=self.q)
            else:
                signal = self.data[signal_name]

            # apply LP filter if values for lp_freq and sampling_rate are set
            self.lp = False
            lp_cut = float(self.cross_lp_freq.GetValue())
            sampling_rate = int(self.cross_sampling.GetValue())
            if lp_cut == 0 and sampling_rate == 0:
                pass
            elif lp_cut == 0 or sampling_rate == 0:
                self.log_text.AppendText(
                    f'{datetime.now()}: If you want to apply a LP filter, cutting frequency and'
                    f'sampling rate must be non-zero!\n')
                return
            else:
                signal = filter.filter_lp_no_detrend(data=signal,
                                                     freq=lp_cut,
                                                     tap_p=0.2,
                                                     df=sampling_rate)
                self.lp = True

            # compute cross-over differences
            diff = cross.crossing_diffs(signal, self.crossing_idx)

            names.append(signal_name)
            differences.append(diff)

        self.log_text.AppendText(
            f'{datetime.now()}: selected variables: {",".join(names)}\n')

        differences = np.array(differences)

        # create header
        header = f'TIE-LINE CROSS-DIFFERENCES\nfile: {self.filename}\nvariables: {",".join(names)}\n{datetime.now()}'
        if self.q != 1:
            header += f'\ndecimated with factor={self.q}'
        if self.lp:
            header += f'\nlow-pass filtered with frequency={self.cross_lp_freq.GetValue()}, ' \
                      f'sampling rate={self.cross_sampling.GetValue()}'
        header += '\nX,Y,' + ','.join(names)

        # create data array
        xy = np.column_stack((self.cross_x, self.cross_y))
        data_to_save = np.concatenate((xy, differences.T), axis=1)

        # save data to csv file
        with wx.FileDialog(self,
                           message='Save cross-over differences to file',
                           defaultDir="",
                           defaultFile="",
                           wildcard="csv files (*.csv)|*.csv",
                           style=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT) as dlg:
            if dlg.ShowModal() == wx.ID_CANCEL:
                return  # user changed their mind

            cross_diff_fname = dlg.GetPath()
            if not cross_diff_fname.endswith('.csv'):
                cross_diff_fname += '.csv'

            try:
                np.savetxt(cross_diff_fname,
                           data_to_save,
                           fmt='%.5f',
                           delimiter=',',
                           newline='\n',
                           header=header)
                self.log_text.AppendText(
                    f'{datetime.now()}: Saved tie-line cross-over differences of {",".join(names)}'
                    f'to {cross_diff_fname}\n')
            except IOError:
                self.log_text.AppendText(
                    f'{datetime.now()}: Cannot save cross-over differences in file '
                    f'{cross_diff_fname}\n')
示例#39
0
def decimate(x, q):
    '''decimate x by a factor of q with some settings that I like'''
    return signal.decimate(x, q, 20 * q, ftype='fir', axis=0, zero_phase=False)
## A. (0.5 pt)
# If the vocal tract length is 15cm, and you discretize it with a 48 kHz sampling rate, 
# how many discrete sampling periods it does take for a sound wave (340 m/s) to travel the vocal tract?
#Ans:vocal tract length in meters:15*10^(-2)=0.15m
#the time that takes to travel to the vocal tract is: t=0.15/340=0.00044117647058823526s
#the period T=1/f=1/48000
#so the final answer is t/T=(0.15/340)/T=21.176470588235293
## B. (0.5 pt)
# What is the reflection coefficient k when a sound passes from section with area 1cm^2 to 2cm^2?
#k=(Z2-Z1)/(Z2+Z1), so according to this formula, we get (2-1)/(2+1)=1/3
# Q2: programming exercise (1 pt in total)

# read in the audio file
# fs,x = wavfile.read('rhythm_birdland.wav')
fs,x = wavfile.read('oboe59.wav')
x = signal.decimate(x,4,ftype='iir')
fs=fs/4


# normalize x so that its value is between [-1.00, 1.00] (0.1 pt)
x = x.astype('float64') / float(numpy.max(numpy.abs(x)))


## A. (0.5 pt)
# MFCCs are useful features in many speech applications.
# Follow instructions below to practice your skills in feature extraction.
# use librosa to extract 13 MFCCs
mfccs = librosa.feature.mfcc(y=x, sr=fs, S=None, n_mfcc=13)
# Visualize the MFCC series
plt.figure(figsize=(10, 4))
plt.pcolormesh(mfccs)
示例#41
0
    def on_cross_stats_btn(self, event):
        if self.data is None:
            self.log_text.AppendText(f'{datetime.now()}: Load data first\n')
            return

        if self.index_table is None:
            self.log_text.AppendText(f'{datetime.now()}: Find turns first\n')
            return

        if self.f_lines is None:
            self.log_text.AppendText(
                f'{datetime.now()}: Split into lines first\n')
            return

        if self.crossing_idx is None:
            self.log_text.AppendText(
                f'{datetime.now()}: Calculate crossings first\n')
            return

        p = self.cross_stats_select.GetString(
            self.cross_stats_select.GetSelection())
        if p is '':
            self.log_text.AppendText(f'{datetime.now()}: Nothing selected\n')
            return

        try:
            float(self.data[p][0])
        except ValueError:
            self.log_text.AppendText(
                f'{datetime.now()}: Selected parameter does not contain numbers\n'
            )
            return

        # down-sampling
        if self.q != 1:
            signal = decimate(self.data[p], q=self.q)
        else:
            signal = self.data[p]

        # apply LP filter if values for lp_freq and sampling_rate are set
        self.lp = False
        lp_cut = float(self.cross_lp_freq.GetValue())
        sampling_rate = int(self.cross_sampling.GetValue())
        if lp_cut == 0 and sampling_rate == 0:
            pass
        elif lp_cut == 0 or sampling_rate == 0:
            self.log_text.AppendText(
                f'{datetime.now()}: If you want to apply a LP filter, cutting frequency and'
                f'sampling rate must be non-zero!\n')
            return
        else:
            signal = filter.filter_lp_no_detrend(data=signal,
                                                 freq=lp_cut,
                                                 tap_p=0.2,
                                                 df=sampling_rate)
            self.lp = True

        # compute differences at crossings
        self.diff = cross.crossing_diffs(signal, self.crossing_idx)

        # compute rms and std of crossing differences
        rms = np.sqrt(np.mean(np.square(self.diff)))
        std = np.std(abs(self.diff))

        if self.lp:
            self.log_text.AppendText(
                f'{datetime.now()}: Stats for parameter {p} after LP filter: '
                f'rms = {rms:.4f}, std = {std:.4f}\n')
        else:
            self.log_text.AppendText(
                f'{datetime.now()}: Stats for parameter {p}: rms = {rms:.4f}, std = {std:.4f}\n'
            )
示例#42
0
def extract_ecg_segments_v3(peaks, data, data_label=None, max_length=1000):
    #
    # read data as float32, labels as int32, sequence length as int32
    data_org = data
    # clean the data first
    data = ecg_preprocessing(data, 'sym8', 8, 3)
    #
    num_of_peaks = len(peaks)
    no_channels = 12
    data_length = np.expand_dims(data.shape[1], axis=0)
    #
    peak_to_peak = np.concatenate([
        np.expand_dims(peaks[0], axis=0),
        np.diff(peaks), data_length - peaks[-1]
    ])
    #
    if num_of_peaks <= 4:
        data = (data - np.expand_dims(np.mean(data, axis=1), axis=1)) / (
            np.expand_dims(np.std(data, axis=1), axis=1) + EPS)
        data = sig.decimate(data,
                            n=60,
                            q=7,
                            ftype='fir',
                            axis=1,
                            zero_phase=True)
        ecg_segments = np.expand_dims(set_to_desired_length(
            data, max_len=max_length),
                                      axis=0)  # Rank-3 array
        # ecg_segments = ecg_preprocessing(ecg_segments, 'sym8', 8, 3)
        if data_label is not None:
            ecg_labels = np.expand_dims(data_label,
                                        axis=0).astype(int)  # Rank-1 array
        else:
            ecg_labels = None
        ecg_seq_length = np.expand_dims(data.shape[1],
                                        axis=0).astype(int)  # Rank-1 array
        np.place(ecg_seq_length, ecg_seq_length > max_length, max_length)
    #
    if num_of_peaks > 4:
        # initialize
        no_of_segments = max(1, num_of_peaks - 3)
        # no_of_segments = max(1, num_of_peaks - 7)
        ecg_segments = np.zeros([no_of_segments, 12, max_length])
        if data_label is not None:
            ecg_labels = np.zeros([no_of_segments])
        else:
            ecg_labels = None
        ecg_seq_length = np.zeros([no_of_segments])
        #
        for p in np.arange(1, num_of_peaks - 2):
            start = int(peaks[p] - peak_to_peak[p] -
                        round(0.9 * peak_to_peak[p - 1]))
            end = int(peaks[p] + peak_to_peak[p] + peak_to_peak[p + 1] +
                      round(0.9 * peak_to_peak[p + 2]))
            segment = data[:, start:end + 1]
            segment = (
                segment - np.expand_dims(np.mean(segment, axis=1), axis=1)) / (
                    np.expand_dims(np.std(segment, axis=1), axis=1) + EPS)
            # segment = ecg_preprocessing(segment, 'sym8', 8, 3)
            segment = sig.decimate(segment,
                                   n=60,
                                   q=7,
                                   ftype='fir',
                                   axis=1,
                                   zero_phase=True)
            seq_length = segment.shape[1]
            segment = set_to_desired_length(segment, max_len=max_length)
            ecg_segments[p - 1, :, :] = segment
            #
            ecg_seq_length[p - 1] = seq_length
            np.place(ecg_seq_length, ecg_seq_length > max_length, max_length)
            #
            if ecg_labels is not None:
                if data_label == 2 or data_label == 6 or data_label == 7:
                    #
                    feature_vector = extract_features(peaks[p - 1:p + 3])
                    #
                    if np.amin(feature_vector) / np.amax(
                            feature_vector) >= .80:
                        ecg_labels[p - 1] = 1
                    else:
                        ecg_labels[p - 1] = data_label
                else:
                    ecg_labels[p - 1] = data_label
        #
        if data_label == 7:
            # print('here')
            _, PVC_segments = special_PVC(peaks, data_org)
            # print(PVC_segments)
            if len(PVC_segments) < no_of_segments:
                # print('here')
                ecg_labels[PVC_segments] = data_label
    # reshape
    ecg_segments = np.transpose(ecg_segments, axes=(0, 2, 1))
    if ecg_labels is not None:
        ecg_labels = ecg_labels.astype(int) - 1
    return ecg_segments, ecg_labels, ecg_seq_length.astype(int)
示例#43
0
def extract_ecg_segments(peaks, data, data_label, max_length):
    #
    # read data as float32, labels as int32, sequence length as int32
    data = data.astype(np.float32)
    # clean the data first
    data = ecg_preprocessing(data, 'sym8', 8, 3)
    #
    num_of_peaks = len(peaks)
    no_channels = 12
    data_length = np.expand_dims(data.shape[1], axis=0)
    #
    if num_of_peaks == 5:
        peak_to_peak = np.concatenate(
            [np.diff(peaks), data_length - peaks[-1]])
    else:
        peak_to_peak = np.diff(peaks)
    #
    #
    if num_of_peaks <= 4:
        ecg_segments = np.expand_dims(set_to_desired_length(
            data, max_len=max_length),
                                      axis=0)  # Rank-3 array
        # ecg_segments = ecg_preprocessing(ecg_segments, 'sym8', 8, 3)
        ecg_segments = sig.decimate(ecg_segments,
                                    n=60,
                                    q=7,
                                    ftype='fir',
                                    axis=2,
                                    zero_phase=True)
        ecg_labels = np.expand_dims(data_label,
                                    axis=0).astype(int)  # Rank-1 array
        ecg_seq_length = np.expand_dims(data.shape[1],
                                        axis=0).astype(int)  # Rank-1 array
        np.place(ecg_seq_length, ecg_seq_length > max_length, max_length)
    #
    if num_of_peaks == 5 or num_of_peaks == 6:
        #
        p = 2
        start = int(peaks[p] - peak_to_peak[p - 1] -
                    round(0.9 * peak_to_peak[p - 2]))
        end = int(peaks[p] + peak_to_peak[p] + peak_to_peak[p + 1] +
                  round(0.9 * peak_to_peak[p + 2]))
        #
        segment = data[:, start:end + 1]
        # segment = ecg_preprocessing(segment, 'sym8', 8, 3)
        segment = sig.decimate(segment,
                               n=60,
                               q=7,
                               ftype='fir',
                               axis=1,
                               zero_phase=True)
        ecg_segments = np.expand_dims(set_to_desired_length(
            segment, max_len=max_length),
                                      axis=0).astype(int)  # Rank-3 array
        #
        ecg_seq_length = np.expand_dims(segment.shape[1],
                                        axis=0)  # Rank-1 array
        np.place(ecg_seq_length, ecg_seq_length > max_length, max_length)
        #
        if data_label == 2 or data_label == 6 or data_label == 7:
            #
            feature_vector = extract_features(peaks[p - 1:p + 3])
            #
            if np.amin(feature_vector) / np.amax(feature_vector) >= .80:
                data_label = 1
        ecg_labels = np.expand_dims(data_label,
                                    axis=0).astype(int)  # Rank-1 array
    #
    if num_of_peaks >= 7:
        # initialize
        # no_of_segments = max(1, num_of_peaks - 3)
        no_of_segments = max(1, num_of_peaks - 7)
        ecg_segments = np.zeros([no_of_segments, 12, max_length])
        ecg_labels = np.zeros([no_of_segments])
        ecg_seq_length = np.zeros([no_of_segments])
        #
        for p in np.arange(3, max(num_of_peaks - 4, 4)):
            start = int(peaks[p] - peak_to_peak[p - 1] -
                        round(0.9 * peak_to_peak[p - 2]))
            end = int(peaks[p] + peak_to_peak[p] + peak_to_peak[p + 1] +
                      round(0.9 * peak_to_peak[p + 2]))
            segment = data[:, start:end + 1]
            # segment = ecg_preprocessing(segment, 'sym8', 8, 3)
            segment = sig.decimate(segment,
                                   n=60,
                                   q=7,
                                   ftype='fir',
                                   axis=1,
                                   zero_phase=True)
            seq_length = segment.shape[1]
            segment = set_to_desired_length(segment, max_len=max_length)
            ecg_segments[p - 3, :, :] = segment
            #
            ecg_seq_length[p - 3] = seq_length
            np.place(ecg_seq_length, ecg_seq_length > max_length, max_length)
            #
            if data_label == 2 or data_label == 6 or data_label == 7:
                #
                feature_vector = extract_features(peaks[p - 1:p + 3])
                #
                if np.amin(feature_vector) / np.amax(feature_vector) >= .80:
                    ecg_labels[p - 3] = 1
                else:
                    ecg_labels[p - 3] = data_label
            else:
                ecg_labels[p - 3] = data_label

    #
    return ecg_segments, ecg_labels.astype(int) - 1, ecg_seq_length.astype(int)
def overview_plot(channel):
    """
    Plot sections of the raw signal in order to give a first impression
    about what's going on during the recording
    Needs the ImageMagick program 'montage' (available even for window$)
    """
    figs = []
    plots = []
    fignames = []

    print('Opening %s' % channel)
    fid = NcsFile(channel)
    timestep = fid.timestep
    total_time = fid.num_recs * 512 * timestep # in seconds

    n_sessions = int(np.ceil(total_time/(60 * EVERY_MINS_MIN)))

    if n_sessions < 10:
        n_sessions = 10

    #basename = os.path.splitext(channel)[0]

    #if not os.path.isdir(basename):
    #    os.mkdir(basename)

    if not os.path.isdir(OVERVIEW_NAME):
        os.mkdir(OVERVIEW_NAME)

    overview_tmp = OVERVIEW_NAME
    #if not os.path.isdir(overview_tmp):
    #    os.mkdir(overview_tmp)

    timefactor = (512*timestep)/60

    voltfactor = fid.header['ADBitVolts'] * 1e6
    entname = fid.header['AcqEntName']
    cscname = os.path.basename(channel)[:-4]
    sessionstarts = np.array(np.linspace(0, fid.num_recs, n_sessions), dtype=int)
    fignames = []
    myfilter = DefaultFilter(timestep)
    n_recs_load = int(MINS*60/(512*timestep))

    for i in range(3):
        fig = mpl.figure(figsize=FIGSIZE)
        plots.append(fig.add_subplot(GRID[0]))
        figs.append(fig)

    for sescount in range(n_sessions):

        start = sessionstarts[sescount]
        stop = start + n_recs_load
        if stop >= fid.num_recs:
            start = fid.num_recs - n_recs_load - 1
            stop = fid.num_recs - 1
        data = fid.read(start, stop, 'data').astype(np.float32)
        data *= voltfactor

        for i in range(3):
            plot = plots[i]
            plot.cla()
            ptime = PLOTTIMES[i]
            n_samp = int(ptime/timestep)

            if i == 0:
                pdata = data[:n_samp]

            elif i == 1:
                pdata = myfilter.filter_detect(data[:n_samp])

            elif i == 2:
                pdata = sig.decimate(data, Q, 4, zero_phase=False)

            x = np.arange(pdata.shape[0])*timestep

            if i == 2:
                x *= Q/60

            plot.plot(x, pdata)

            plot.set_xlim((x[0], x[-1]))

            if i == 1:
                ylim = 100
            else:
                ylim = 300

            plot.set_ylim((-ylim, ylim))
            plot.set_xticklabels([])
            plot.grid(True, axis='x')
            plot.set_yticks((-ylim, 0, ylim))

            if sescount == 0:
                if i == 0:
                    text = '{} {} {:.2f} s raw'.format(entname,
                                                       cscname,
                                                       PLOTTIMES[0])
                else:
                    text = '{:.0f} s'.format(PLOTTIMES[i])

                if i == 1:
                    text += ' bandpassed'
                elif i == 2:
                    text += ' raw'

                xpos = x[-1]
                ypos = ylim*.96
                for sign in (1, -1):
                    plot.text(xpos, sign*ypos, str(sign*ylim) + u' µV', fontsize=FONTSIZE,
                              bbox=TEXT_BBOX, ha='right', va=VERT_ALIGN[sign])
            else:
                plot.set_yticklabels([])
                text = '{:.0f} min'.format(start * timefactor)

            xpos = x[-1]*.02
            ypos = ylim * .55
            plot.text(xpos, ypos, text, fontsize=FONTSIZE,
                      bbox=TEXT_BBOX)


            figname = '{}_{:1d}_{:03d}.png'.format(entname, i, sescount)
            figpath = os.path.join(overview_tmp, figname)
            fignames.append(figpath)
            figs[i].savefig(figpath, DPI=DPI)

    mpl.close('all')

    arg = ['montage', '-tile', '3', '-geometry',
           '{}x{}'.format(FIGSIZE[0]*DPI, FIGSIZE[1]*DPI)] + fignames

    outname = os.path.join('overview',
                           'overview_{}_{}.png'.format(entname, cscname))
    arg.append(outname)
    subprocess.call(arg)
    for figname in fignames:
        os.remove(figname)
    print('Completed ' + entname)
示例#45
0

def tock():
    elapsed = time.time() - start_time
    print "  running for %.2f s" % elapsed


# load data
print "Load training data"
X = np.load(DATA_PATH)
Y = np.load(LABEL_PATH)
tock()

# downsample
print "Downsample"
X_downsampled = sig.decimate(X, 2, axis=1).astype(X.dtype)
tock()

# normalise
if settings['normalise_volume']:
    print "Normalise volume"
    X_downsampled -= X_downsampled.mean(1).reshape(-1, 1)
    X_downsampled /= X_downsampled.std(1).reshape(-1, 1)
    tock()

# compute spectrograms
print "Compute spectrograms"
nfft = settings['specgram_num_components']
noverlap = nfft * (1 - 1. / settings['specgram_redundancy'])
log_scale = settings['log_scale']
示例#46
0
def find_channel_offset(s1, s2, nd, nl):
    '''use cross-correlation to find channel offset in samples'''
    B1 = signal.decimate(s1, nd)
    B2 = np.pad(signal.decimate(s2, nd), (nl, nl), 'constant')
    xc = np.abs(signal.correlate(B1, B2, mode='valid'))
    return (np.argmax(xc) - nl) * nd
示例#47
0
 def time_decimate(self, q, ftype, zero_phase):
     decimate(self.sig, q, ftype=ftype, zero_phase=zero_phase)
示例#48
0
def add_data(h5_file, inputfiles, args, save_examples=False):
    # Make a list of all files to be processed
    file_list = []
    file_list2 = []
    zname_list = []
    file_extensions = set(['.wav'])
    with open(inputfiles) as f:
        shuffle_list = []
        for line in f:
            filename = line.strip()
            shuffle_list.append(
                [filename.split(' ')[0],
                 filename.split(' ')[1]])
        random.shuffle(shuffle_list)

        for line_0, line_1 in shuffle_list:
            # filename = line.strip()
            # filename, zname = filename.split(' ')[0], filename.split(' ')[1]
            filename, zname = line_0, int(line_1)
            ext = os.path.splitext(filename)[1]
            if ext in file_extensions:
                file_list.append(os.path.join(args.in_dir, filename))
                file_list2.append(os.path.join(args.in_dir2, filename))
                zname_list.append(zname)
    num_files = len(file_list)

    # patches to extract and their size
    if args.interpolate:
        d, d_lr = args.dimension, args.dimension
        s, s_lr = args.stride, args.stride
    else:
        d, d_lr = args.dimension, args.dimension / args.scale
        s, s_lr = args.stride, args.stride / args.scale
    hr_patches, lr_patches, patches_class = list(), list(), list()

    for j, file_path in enumerate(file_list):
        if j % 10 == 0: print('%d/%d' % (j, num_files))

        # print(file_path)
        # load audio file
        x, fs = librosa.load(file_path, sr=args.sr)
        x_hr, fs_hr = librosa.load(file_list2[j], sr=args.sr)

        # crop so that it works with scaling ratio
        x_len = len(x)
        x = x[:x_len - (x_len % args.scale)]
        x_hr = x_hr[:x_len - (x_len % args.scale)]

        # generate low-res version
        if args.low_pass:
            # x_bp = butter_bandpass_filter(x, 0, args.sr / args.scale / 2, fs, order=6)
            # x_lr = np.array(x[0::args.scale])
            #x_lr = decimate(x, args.scale, zero_phase=True)
            x_lr = decimate(x, args.scale)
        else:
            x_lr = np.array(x[0::args.scale])

        if args.interpolate:
            x_lr = upsample(x_lr, args.scale)
            assert len(x) % args.scale == 0
            assert len(x_lr) == len(x)
        else:
            assert len(x) % args.scale == 0
            assert len(x_lr) == len(x) / args.scale

        # generate patches
        max_i = len(x) - d + 1
        for i in range(0, max_i, s):
            # keep only a fraction of all the patches
            u = np.random.uniform()
            if u > args.sam: continue

            if args.interpolate:
                i_lr = i
            else:
                i_lr = int(i // args.scale)

            hr_patch = np.array(x_hr[i:i + d])
            lr_patch = np.array(x_lr[i_lr:i_lr + int(d_lr)])

            # print 'a', hr_patch
            # print 'b', lr_patch

            assert len(hr_patch) == d
            assert len(lr_patch) == d_lr

            # print hr_patch

            hr_patches.append(hr_patch.reshape((d, 1)))
            lr_patches.append(lr_patch.reshape((int(d_lr), 1)))
            patches_class.append([zname_list[j]])

            # if j == 1: exit(1)

    # crop # of patches so that it's a multiple of mini-batch size
    num_patches = len(hr_patches)
    print('---------------------------------')
    print(num_patches)
    print('---------------------------------')
    num_to_keep = int(
        np.floor(num_patches / args.batch_size) * args.batch_size)
    hr_patches = np.array(hr_patches[:num_to_keep])
    lr_patches = np.array(lr_patches[:num_to_keep])
    patches_class = np.array(patches_class[:num_to_keep])

    if save_examples:
        librosa.output.write_wav('example-hr.wav',
                                 hr_patches[0],
                                 int(fs),
                                 norm=False)
        librosa.output.write_wav('example-lr.wav',
                                 lr_patches[0],
                                 int(fs // args.scale),
                                 norm=False)
        print(hr_patches[0].shape)
        print(lr_patches[0].shape)
        print(patches_class[:3])
        print(patches_class.shape)
        print(patches_class[0].shape)
        print(patches_class[0])
        print(hr_patches[0][0][:10])
        print(lr_patches[0][0][:10])
        print('two examples saved')

    print(hr_patches.shape)

    # create the hdf5 file
    data_set = h5_file.create_dataset('data', lr_patches.shape, np.float32)
    label_set = h5_file.create_dataset('label', hr_patches.shape, np.float32)
    label_class_set = h5_file.create_dataset('label_class',
                                             patches_class.shape, np.float32)

    data_set[...] = lr_patches
    label_set[...] = hr_patches
    label_class_set[...] = patches_class
示例#49
0
def decimate_signal(msignal, mdecimate_factor):
    return decimate(msignal, mdecimate_factor)
示例#50
0
def smooth():

	lengthSmoothed = length
	for i in range(0, 2):
	    lengthSmoothed = signal.savgol_filter(x=lengthSmoothed, window_length=115, polyorder=2, deriv=0, mode='nearest')

	plt.plot(length)
	plt.plot(lengthSmoothed, color='r')
	plt.show()

	b = signal.get_window('triang', 1000, False)
	print(len(b))

	lengthDeriv = signal.savgol_filter(length, 285, 4, 1)
	lengthDeriv = signal.savgol_filter(lengthDeriv, 255, 1, mode='nearest')

	flagus = 0
	pnts = []
	for i in range(len(lengthDeriv)):
	    if lengthDeriv[i] < 0 and flagus == 0:
	        flagus = 1
	        tmpList = []
	        tmpList.append(i)
	    if lengthDeriv[i] > 0 and flagus == 1:
	        flagus = 0
	        if lengthSmoothed[tmpList[0]] - lengthSmoothed[i] >= 0.02:
	            tmpList.append(i)
	            pnts.append(tmpList)

	for pnt in pnts:
	    plt.plot(pnt[0], lengthDeriv[pnt[0]], 'ro')
	    plt.plot(pnt[1], lengthDeriv[pnt[1]], 'ro', color = 'g')
	plt.plot(lengthDeriv)
	plt.show()

	for pnt in pnts:
	    plt.plot(pnt[0], length[pnt[0]], 'ro')
	    plt.plot(pnt[1], length[pnt[1]], 'ro', color = 'g')
	plt.plot(length)
	plt.show()


	'''
	### 03 -- B ###
	# Baseline correction #
	'''

	def baseline_als(y, lam, p, niter=10):
		L = len(y)
		D = sparse.csc_matrix(np.diff(np.eye(L), 2))
		w = np.ones(L)
		for i in xrange(niter):
			W = sparse.spdiags(w, 0, L, L)
			Z = W + lam * D.dot(D.transpose())
			z = spsolve(Z, w*y)
			w = p * (y > z) + (1-p) * (y < z)
	  return z

	lengthDecimated = lengthSmoothed

	for i in range(0, 11):
	    lengthDecimated = signal.decimate(lengthDecimated, 2, zero_phase=True)

	baseline4correction = baseline_als(lengthDecimated, 10000, 0.01)
	print("base")
	#baseline4correction = signal.resample(baseline4correction, len(length))

	#baseline4correction = signal.resample_poly(baseline4correction, len(lengthSmoothed), len(baseline4correction))
	#baseline4correction = signal.savgol_filter(length, 121, 1)
	print("resamp")
	#length4peaks = lengthSmoothed - baseline4correction
	length4peaks = lengthDecimated - baseline4correction


	#print np.mean(length4peaks)
	#print np.median(length4peaks)
	#print np.max(length4peaks)
	#print np.min(length4peaks)

	plt.plot(lengthDecimated)
	plt.plot(baseline4correction, color='r')
	plt.plot(length4peaks, color='g')
	plt.show()

	minimal_length = np.mean(length4peaks)-np.median(length4peaks)
	minimal_length2 = (np.max(length4peaks)-np.min(length4peaks))/2
	#print minimal_length, minimal_length2

	points = pnts

	for i in range(len(points)):
	    points[i][0] = points[i][0] + int((points[i][1] - points[i][0]) * 0.05)
	    #points[i][1] = points[i][1] + int((points[i][1] - points[i][0]) * 0.01)

	for point in points:
	    plt.plot(point[0], length[point[0]], 'ro')
	    plt.plot(point[1], length[point[1]], 'ro', color = 'g')
	plt.plot(length)
	plt.show()

	finalTable = fit(points=points, current=current, length=length, voltage=voltage, conc_KCL=conc_KCL, gain=gain)

	return finalTable
示例#51
0
from scipy import signal
import numpy as np

x = np.array([
    1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
    22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40
])
q = 3
d = signal.decimate(x, q, n=None, ftype='iir', axis=-1)

print("x", len(x))
print("d", len(d))
                output = True)

b,a = signal.iirdesign(4./8,5./8,1,40)
FIR_LPF = signal.firwin(2,2./16000)
data_rx = np.empty(frame_len*osr, dtype = complex)
##pre_data = 0

while not rx_q.empty():
    data_buf = rx_q.get()
    ok_separater = C_lib.CSM_data_separater(data_buf,pInput_real,pInput_imag,input_sample_len)

    #decimation
    data_rx.real = np.ctypeslib.as_array(pInput_real).astype(np.int16)
    data_rx.imag = np.ctypeslib.as_array(pInput_imag).astype(np.int16)

    dec_data = signal.decimate(data_rx,120,ftype='fir')

##    out = signal.lfilter(b,a,dec_data)
    out = signal.fftconvolve(FIR_LPF,dec_data)
    #demodulation
    dem_data = np.unwrap(np.diff(np.angle(out)))*1e4
    plt_q.put(dem_data)
##    dem_data = np.unwrap(np.diff(np.angle(dec_data)))*1e4
    play_data = dem_data.astype(np.int16).tostring()
    
##    data_delay = np.insert(dec_data,0,pre_data)
##    pre_data = data_delay[-1]
##    data_delay = np.delete(data_delay,-1)
##    diff_data = dec_data*np.conj(data_delay)
##    ang_data = np.angle(diff_data)
##    audio_data = np.unwrap(ang_data)*1e4
from numpy.random import normal
from numpy import log10, finfo, arange
import matplotlib.pyplot as plt
from scipy.fftpack import fft
from scipy.signal.windows import get_window
from scipy.signal import decimate, resample
eps = finfo("float").eps

def normalise(x):
	return (x-min(x))/(max(x)-min(x))

sr = 44100
mean = 0
std = 1 
N = 2**14
noise = normal(mean, std, size=N)
noisedec = resample(decimate(noise, 2), N)
window = get_window('blackmanharris', N, fftbins=False)

noisefft = abs(fft(noise * window))[:int(N/2)]
noisefftdec = abs(fft(noisedec * window))[:int(N/2)]
noisefft = 20*log10(normalise(noisefft) + eps)
noisefftdec = 20*log10(normalise(noisefftdec) + eps)

f = arange(int(N/2))/N*sr

fig, ax = plt.subplots(2, 1, figsize=(16,9))
ax[0].plot(f,noisefft)
ax[1].plot(f,noisefftdec)
plt.show()
示例#54
0
def add_data(h5_file, inputfiles, args, save_examples=False):
    # Make a list of all files to be processed
    file_list = []
    ID_list = []
    file_extensions = set(['.wav'])
    with open(inputfiles) as f:
        for line in f:
            filename = line.strip()
            ext = os.path.splitext(filename)[1]
            if ext in file_extensions:
                file_list.append(os.path.join(args.in_dir, filename))

    num_files = len(file_list)

    # patches to extract and their size
    if args.dimension is not -1:
        if args.interpolate:
            d, d_lr = args.dimension, args.dimension
            s, s_lr = args.stride, args.stride
        else:
            d, d_lr = args.dimension, args.dimension / args.scale
            s, s_lr = args.stride, args.stride / args.scale
    hr_patches, lr_patches = list(), list()

    print(len(file_list))
    for j, file_path in enumerate(file_list):
        if j % 10 == 0:
            print('%d/%d' % (j, num_files))

        ID = int(re.search('p\d\d\d/', file_path).group(0)[1:-1])

        # load audio file
        x, fs = librosa.load(file_path, sr=args.sr)

        # crop so that it works with scaling ratio
        x_len = len(x)
        x = x[:x_len - (x_len % args.scale)]

        # generate low-res version
        if args.low_pass:
            x_lr = decimate(x, args.scale)
        else:
            x_lr = np.array(x[0::args.scale])

        if args.interpolate:
            x_lr = upsample(x_lr, args.scale)
            assert len(x) % args.scale == 0
            assert len(x_lr) == len(x)
        else:
            assert len(x) % args.scale == 0
            assert len(x_lr) == len(x) / args.scale

        if args.dimension is not -1:
            # generate patches
            max_i = len(x) - d + 1
            for i in range(0, max_i, s):
                # keep only a fraction of all the patches
                u = np.random.uniform()
                if u > args.sam: continue

                if args.interpolate:
                    i_lr = i
                else:
                    i_lr = i / args.scale

                hr_patch = np.array(x[i:i + d])
                lr_patch = np.array(x_lr[i_lr:i_lr + d_lr])

                assert len(hr_patch) == d
                assert len(lr_patch) == d_lr

                hr_patches.append(hr_patch.reshape((d, 1)))
                lr_patches.append(lr_patch.reshape((d_lr, 1)))
                ID_list.append(ID)
        else:  # for full snr
            # append the entire file without patching
            x = x[:, np.newaxis]
            x_lr = x_lr[:, np.newaxis]
            hr_patches.append(x[:len(x) // 256 * 256])
            lr_patches.append(x_lr[:len(x_lr) // 256 * 256])
            ID_list.append(ID)

    if args.dimension is not -1:
        # crop # of patches so that it's a multiple of mini-batch size
        num_patches = len(hr_patches)
        num_to_keep = int(
            np.floor(num_patches / args.batch_size) * args.batch_size)
        hr_patches = np.array(hr_patches[:num_to_keep])
        lr_patches = np.array(lr_patches[:num_to_keep])
        ID_list = ID_list[:num_to_keep]
    if save_examples:
        librosa.output.write_wav('example-hr.wav',
                                 hr_patches[40][0],
                                 fs,
                                 norm=False)
        librosa.output.write_wav('example-lr.wav',
                                 lr_patches[40][0],
                                 fs / args.scale,
                                 norm=False)

    if args.dimension is not -1:
        # create the hdf5 file
        data_set = h5_file.create_dataset('data', lr_patches.shape, np.float32)
        label_set = h5_file.create_dataset('label', hr_patches.shape,
                                           np.float32)

        data_set[...] = lr_patches
        label_set[...] = hr_patches
        print(len(ID_list))
        pickle.dump(
            ID_list,
            open('ID_list_patches_' + str(d) + '_' + str(args.scale), 'wb'))
    else:
        # pickle the data
        pickle.dump(hr_patches, open('full-label-' + args.out[:-7], 'wb'))
        pickle.dump(lr_patches, open('full-data-' + args.out[:-7], 'wb'))
        pickle.dump(ID_list, open('ID_list', 'wb'))
示例#55
0
sdr.setFrequency(SOAPY_SDR_RX, 0, 434e6)
# sdr.setGain('auto')
sdr.setBandwidth(SOAPY_SDR_RX, 0, 1e6)

#setup a stream (complex floats)
rxStream = sdr.setupStream(SOAPY_SDR_RX, SOAPY_SDR_CF32)
sdr.activateStream(rxStream)  #start streaming

#create a re-usable buffer for rx samples
buff = numpy.array([0] * 1024 * 256, numpy.complex64)

#receive some samples
sr = sdr.readStream(rxStream, [buff], len(buff))
print(buff[0], buff[100], buff[200], buff[2000])
samples_sq = [sqrt(i.real * i.real + i.imag * i.imag) for i in buff]
samples_sq = decimate(samples_sq, 50)

sample_filtered = []
for i in samples_sq:
    if i < 0.2:
        sample_filtered.append(int(0))
    elif i > 0.4:
        sample_filtered.append(float(0.6))
    else:
        sample_filtered.append(int(0))

extracted_data_starts = []
for i in range(0, len(sample_filtered)):
    if sample_filtered[i] > 0.4 and sample_filtered[i + 1] < 0.2:
        extracted_data_starts.append(i)
start_time = time.time()
def tock():
    elapsed = time.time() - start_time
    print "  running for %.2f s" % elapsed


# load data
print "Load data"
X = np.load(DATA_PATH)
Y = np.load(LABEL_PATH)
tock()


# downsample
print "Downsample"
X_downsampled = sig.decimate(X, 2, axis=1)
tock()


# normalise
if settings['normalise_volume']:
    print "Normalise volume"
    X_downsampled -= X_downsampled.mean(1).reshape(-1, 1)
    X_downsampled /= X_downsampled.std(1).reshape(-1, 1)
    tock()


# compute spectrograms
print "Compute spectrograms"
nfft = settings['specgram_num_components']
noverlap = nfft * (1 - 1. / settings['specgram_redundancy'])
示例#57
0
def rare_standalone(
    init_gpa=False, # Starts the gpa
    nScans = 1, # NEX
    larmorFreq = 3.07454, # MHz, Larmor frequency
    rfExAmp = 0.05, # a.u., rf excitation pulse amplitude
    rfReAmp = 0.1, # a.u., rf refocusing pulse amplitude
    rfExTime = 190, # us, rf excitation pulse time
    rfReTime = 190, # us, rf refocusing pulse time
    echoSpacing = 6., # ms, time between echoes
    inversionTime = 0., # ms, Inversion recovery time
    repetitionTime = 10000., # ms, TR
    fov = np.array([120., 120., 120.]), # mm, FOV along readout, phase and slice
    dfov = np.array([0., 0., 0.]), # mm, displacement of fov center
    nPoints = np.array([60, 10, 1]), # Number of points along readout, phase and slice
    slThickness = 15, # mm, slice thickness
    etl = 60, # Echo train length
    acqTime = 2, # ms, acquisition time
    axes = np.array([1, 0, 2]), # 0->x, 1->y and 2->z defined as [rd,ph,sl]
    axesEnable = np.array([1, 1, 0]), # 1-> Enable, 0-> Disable
    sweepMode = 1, # 0->k2k (T2),  1->02k (T1),  2->k20 (T2), 3->Niquist modulated (T2)
    rdGradTime = 2.5,  # ms, readout gradient time
    rdDephTime = 1,  # ms, readout dephasing time
    phGradTime = 1, # ms, phase and slice dephasing time
    rdPreemphasis = 1.005, # readout dephasing gradient is multiplied by this factor
    ssPreemphasis = 0.45, # ssGradAmplitue is multiplied by this number for rephasing
    drfPhase = 0, # degrees, phase of the excitation pulse
    dummyPulses = 1, # number of dummy pulses for T1 stabilization
    shimming = np.array([-70., -90., 10.]), # a.u.*1e4, shimming along the X,Y and Z axes
    parAcqLines = 0 # number of additional lines, Full sweep if 0
    ):
    
    ssDelay = 190
    freqCal = 0
    
    # rawData fields
    rawData = {}
    
    # Conversion of variables to non-multiplied units
    larmorFreq = larmorFreq*1e6
    rfExTime = rfExTime*1e-6
    rfReTime = rfReTime*1e-6
    fov = np.array(fov)*1e-3
    dfov = np.array(dfov)*1e-3
    echoSpacing = echoSpacing*1e-3
    acqTime = acqTime*1e-3
    shimming = shimming*1e-4
    repetitionTime= repetitionTime*1e-3
    inversionTime = inversionTime*1e-3
    rdGradTime = rdGradTime*1e-3
    rdDephTime = rdDephTime*1e-3
    phGradTime = phGradTime*1e-3
    slThickness = slThickness*1e-3
    
    # Inputs for rawData
    rawData['nScans'] = nScans
    rawData['larmorFreq'] = larmorFreq      # Larmor frequency
    rawData['rfExAmp'] = rfExAmp             # rf excitation pulse amplitude
    rawData['rfReAmp'] = rfReAmp             # rf refocusing pulse amplitude
    rawData['rfExTime'] = rfExTime          # rf excitation pulse time
    rawData['rfReTime'] = rfReTime            # rf refocusing pulse time
    rawData['echoSpacing'] = echoSpacing        # time between echoes
    rawData['inversionTime'] = inversionTime       # Inversion recovery time
    rawData['repetitionTime'] = repetitionTime     # TR
    rawData['fov'] = fov           # FOV along readout, phase and slice
    rawData['dfov'] = dfov            # Displacement of fov center
    rawData['nPoints'] = nPoints                 # Number of points along readout, phase and slice
    rawData['etl'] = etl                    # Echo train length
    rawData['acqTime'] = acqTime             # Acquisition time
    rawData['axesOrientation'] = axes       # 0->x, 1->y and 2->z defined as [rd,ph,sl]
    rawData['axesEnable'] = axesEnable # 1-> Enable, 0-> Disable
    rawData['sweepMode'] = sweepMode               # 0->k2k (T2),  1->02k (T1),  2->k20 (T2), 3->Niquist modulated (T2)
    rawData['rdPreemphasis'] = rdPreemphasis
    rawData['ssPreemphasis'] = ssPreemphasis
    rawData['drfPhase'] = drfPhase 
    rawData['dummyPulses'] = dummyPulses                    # Dummy pulses for T1 stabilization
    rawData['partialAcquisition'] = parAcqLines
    rawData['rdDephTime'] = rdDephTime
    rawData['sliceThickness'] = slThickness
    
    # Miscellaneous
    blkTime = 10             # Deblanking time (us)
    larmorFreq = larmorFreq*1e-6
    gradRiseTime = 400e-6       # Estimated gradient rise time
    gSteps = int(gradRiseTime*1e6/5)*0+1
    gradDelay = 9            # Gradient amplifier delay
    addRdPoints = 10             # Initial rd points to avoid artifact at the begining of rd
    gammaB = 42.56e6            # Gyromagnetic ratio in Hz/T
    oversamplingFactor = 6
    randFactor = 0e-3                        # Random amplitude to add to the phase gradients
    if rfReAmp==0:
        rfReAmp = rfExAmp
    if rfReTime==0:
        rfReTime = 2*rfExTime
    resolution = fov/nPoints
    rawData['resolution'] = resolution
    rawData['gradDelay'] = gradDelay*1e-6
    rawData['gradRiseTime'] = gradRiseTime
    rawData['oversamplingFactor'] = oversamplingFactor
    rawData['randFactor'] = randFactor
    rawData['addRdPoints'] = addRdPoints
    
    # Matrix size
    nRD = nPoints[0]+2*addRdPoints
    nPH = nPoints[1]*axesEnable[1]+(1-axesEnable[1])
    nSL = nPoints[2]*axesEnable[2]+(1-axesEnable[2])
    
    # ETL if nPH = 1
    if etl>nPH:
        etl = nPH
    
    # parAcqLines in case parAcqLines = 0
    if parAcqLines==0:
        parAcqLines = int(nSL/2)
    
    # BW
    BW = nPoints[0]/acqTime*1e-6
    BWov = BW*oversamplingFactor
    samplingPeriod = 1/BWov
    
    # Readout gradient time
    if rdGradTime>0 and rdGradTime<acqTime:
        rdGradTime = acqTime
    rawData['rdGradTime'] = rdGradTime
    
    # Phase and slice de- and re-phasing time
    if phGradTime==0 or phGradTime>echoSpacing/2-rfExTime/2-rfReTime/2-2*gradRiseTime:
        phGradTime = echoSpacing/2-rfExTime/2-rfReTime/2-2*gradRiseTime
    rawData['phGradTime'] = phGradTime
    
    # Max gradient amplitude
    rdGradAmplitude = nPoints[0]/(gammaB*fov[0]*acqTime)*axesEnable[0]
    phGradAmplitude = nPH/(2*gammaB*fov[1]*(phGradTime+gradRiseTime))*axesEnable[1]
    slGradAmplitude = nSL/(2*gammaB*fov[2]*(phGradTime+gradRiseTime))*axesEnable[2]
    rawData['rdGradAmplitude'] = rdGradAmplitude
    rawData['phGradAmplitude'] = phGradAmplitude
    rawData['slGradAmplitude'] = slGradAmplitude

    # Slice selection gradient
    if slThickness!=0:
        ssGradAmplitude = 1/(gammaB*slThickness*rfExTime)
    else:
        ssGradAmplitude = 0
    print(ssGradAmplitude*1e3, ' mT/m')

    # Readout dephasing amplitude
    rdDephAmplitude = 0.5*rdGradAmplitude*(gradRiseTime+rdGradTime)/(gradRiseTime+rdDephTime)
    rawData['rdDephAmplitude'] = rdDephAmplitude

    # Get factors to OCRA1 units
    gFactor = reorganizeGfactor(axes)
    rawData['gFactor'] = gFactor
    
    # Phase and slice gradient vector
    phGradients = np.linspace(-phGradAmplitude,phGradAmplitude,num=nPH,endpoint=False)
    slGradients = np.linspace(-slGradAmplitude,slGradAmplitude,num=nSL,endpoint=False)
    
    # Now fix the number of slices to partailly acquired k-space
    nSL = (int(nPoints[2]/2)+parAcqLines)*axesEnable[2]+(1-axesEnable[2])
    
    # Add random displacemnt to phase encoding lines
    for ii in range(nPH):
        if ii<np.ceil(nPH/2-nPH/20) or ii>np.ceil(nPH/2+nPH/20):
            phGradients[ii] = phGradients[ii]+randFactor*np.random.randn()
    kPH = gammaB*phGradients*(gradRiseTime+phGradTime)
    rawData['phGradients'] = phGradients
    rawData['slGradients'] = slGradients
    
    # Change units to OCRA1 board
    rdGradAmplitude = rdGradAmplitude/gFactor[0]*1000/5
    rdDephAmplitude = rdDephAmplitude/gFactor[0]*1000/5
    phGradients = phGradients/gFactor[1]*1000/5
    slGradients = slGradients/gFactor[2]*1000/5
    ssGradAmplitude = ssGradAmplitude/gFactor[2]*1000/5
    
    # Set phase vector to given sweep mode
    ind = getIndex(phGradients, etl, nPH, sweepMode)
    rawData['sweepOrder'] = ind
    phGradients = phGradients[ind]

    # Create functions
    def rfPulse(tStart,rfTime,rfAmplitude,rfPhase):
        txTime = np.array([tStart+blkTime,tStart+blkTime+rfTime])
        txAmp = np.array([rfAmplitude*np.exp(1j*rfPhase),0.])
        txGateTime = np.array([tStart,tStart+blkTime+rfTime])
        txGateAmp = np.array([1,0])
        expt.add_flodict({
            'tx0': (txTime, txAmp),
            'tx_gate': (txGateTime, txGateAmp)
            })

    def rxGate(tStart,gateTime):
        rxGateTime = np.array([tStart,tStart+gateTime])
        rxGateAmp = np.array([1,0])
        expt.add_flodict({
            'rx0_en':(rxGateTime, rxGateAmp), 
            'rx_gate': (rxGateTime, rxGateAmp), 
            })

    def gradTrap(tStart, gTime, gAmp, gAxis):
        tUp = np.linspace(tStart, tStart+gradRiseTime, num=gSteps, endpoint=False)
        tDown = tUp+gradRiseTime+gTime
        t = np.concatenate((tUp, tDown), axis=0)
        dAmp = gAmp/gSteps
        aUp = np.linspace(dAmp, gAmp, num=gSteps)
        aDown = np.linspace(gAmp-dAmp, 0, num=gSteps)
        a = np.concatenate((aUp, aDown), axis=0)
        if gAxis==0:
            expt.add_flodict({'grad_vx': (t, a+shimming[0])})
        elif gAxis==1:
            expt.add_flodict({'grad_vy': (t, a+shimming[1])})
        elif gAxis==2:
            expt.add_flodict({'grad_vz': (t, a+shimming[2])})
    
    def gradPulse(tStart, gTime, gAmp,  gAxes):
        t = np.array([tStart, tStart+gradRiseTime+gTime])
        for gIndex in range(np.size(gAxes)):
            a = np.array([gAmp[gIndex], 0])
            if gAxes[gIndex]==0:
                expt.add_flodict({'grad_vx': (t, a+shimming[0])})
            elif gAxes[gIndex]==1:
                expt.add_flodict({'grad_vy': (t, a+shimming[1])})
            elif gAxes[gIndex]==2:
                expt.add_flodict({'grad_vz': (t, a+shimming[2])})
    
    def endSequence(tEnd):
        expt.add_flodict({
                'grad_vx': (np.array([tEnd]),np.array([0]) ), 
                'grad_vy': (np.array([tEnd]),np.array([0]) ), 
                'grad_vz': (np.array([tEnd]),np.array([0]) ),
             })
             
    def iniSequence(tEnd):
        expt.add_flodict({
                'grad_vx': (np.array([tEnd]),np.array([shimming[0]]) ), 
                'grad_vy': (np.array([tEnd]),np.array([shimming[1]]) ), 
                'grad_vz': (np.array([tEnd]),np.array([shimming[2]]) ),
             })

    def createSequence():
        phIndex = 0
        slIndex = 0
        nRepetitions = int(nPH*nSL/etl+dummyPulses)
        scanTime = 20e3+nRepetitions*repetitionTime
        rawData['scanTime'] = scanTime*1e-6
        if rdGradTime==0:   # Check if readout gradient is dc or pulsed
            dc = True
        else:
            dc = False
        # Set shimming
        iniSequence(20)
        for repeIndex in range(nRepetitions):
            # Initialize time
            tEx = 20e3+repetitionTime*repeIndex+inversionTime
            
            # Inversion pulse
            if repeIndex>=dummyPulses and inversionTime!=0:
                t0 = tEx-inversionTime-rfReTime/2-blkTime
                rfPulse(t0,rfReTime,rfReAmp/180*180,0)
                gradTrap(t0+blkTime+rfReTime, inversionTime*0.5, 0.2, axes[0])
                gradTrap(t0+blkTime+rfReTime, inversionTime*0.5, 0.2, axes[1])
                gradTrap(t0+blkTime+rfReTime, inversionTime*0.5, 0.2, axes[2])
            
            # DC radout gradient if desired
            if (repeIndex==0 or repeIndex>=dummyPulses) and dc==True:
                t0 = tEx-10e3
                gradTrap(t0, 10e3+echoSpacing*(etl+1), rdGradAmplitude, axes[0])
            
            # Slice selection gradient dephasing
            if (slThickness!=0 and repeIndex>=dummyPulses):
                t0 = tEx-rfExTime/2-gradRiseTime-gradDelay
                gradTrap(t0, rfExTime, ssGradAmplitude, axes[2])
            
            # Excitation pulse
            t0 = tEx-blkTime-rfExTime/2
            rfPulse(t0,rfExTime,rfExAmp,drfPhase*np.pi/180)
        
            # Slice selection gradient rephasing
            if (slThickness!=0 and repeIndex>=dummyPulses):
                t0 = tEx+rfExTime/2+gradRiseTime-gradDelay
                gradTrap(t0, 0., -ssGradAmplitude*ssPreemphasis, axes[2])
        
            # Dephasing readout
            t0 = tEx+rfExTime/2-gradDelay
            if (repeIndex==0 or repeIndex>=dummyPulses) and dc==False:
                gradTrap(t0, rdDephTime, rdDephAmplitude*rdPreemphasis, axes[0])
            
            # Echo train
            for echoIndex in range(etl):
                tEcho = tEx+echoSpacing*(echoIndex+1)
                
                # Crusher gradient
                if repeIndex>=dummyPulses:
                    t0 = tEcho-echoSpacing/2-rfReTime/2-gradRiseTime-gradDelay+ssDelay
                    gradTrap(t0, rfReTime, ssGradAmplitude, axes[2])
                
                # Refocusing pulse
                t0 = tEcho-echoSpacing/2-rfReTime/2-blkTime
                rfPulse(t0, rfReTime, rfReAmp, np.pi/2)
                
#                # post-crusher gradient
#                if repeIndex>=dummyPulses:
#                    t0 = tEcho-echoSpacing/2+rfReTime/2-gradDelay
#                    gradTrap(t0, rfExTime, 0.2, axes[2])

                # Dephasing phase and slice gradients
                t0 = tEcho-echoSpacing/2+rfReTime/2-gradDelay
                if repeIndex>=dummyPulses:         # This is to account for dummy pulses
                    gradTrap(t0, phGradTime, phGradients[phIndex], axes[1])
                    gradTrap(t0, phGradTime, slGradients[slIndex], axes[2])
                
                # Readout gradient
                t0 = tEcho-rdGradTime/2-gradRiseTime-gradDelay
                if (repeIndex==0 or repeIndex>=dummyPulses) and dc==False:         # This is to account for dummy pulses
                    gradTrap(t0, rdGradTime, rdGradAmplitude, axes[0])
    
                # Rx gate
                if (repeIndex==0 or repeIndex>=dummyPulses):
                    t0 = tEcho-acqTime/2-addRdPoints/BW
                    rxGate(t0, acqTime+2*addRdPoints/BW)
    
                # Rephasing phase and slice gradients
                t0 = tEcho+acqTime/2+addRdPoints/BW-gradDelay
                if (echoIndex<etl-1 and repeIndex>=dummyPulses):
                    gradTrap(t0, phGradTime, -phGradients[phIndex], axes[1])
                    gradTrap(t0, phGradTime, -slGradients[slIndex], axes[2])
                elif(echoIndex==etl-1 and repeIndex>=dummyPulses):
                    gradTrap(t0, phGradTime, +phGradients[phIndex], axes[1])
                    gradTrap(t0, phGradTime, +slGradients[slIndex], axes[2])
    
                # Update the phase and slice gradient
                if repeIndex>=dummyPulses:
                    if phIndex == nPH-1:
                        phIndex = 0
                        slIndex += 1
                    else:
                        phIndex += 1
                
            if repeIndex==nRepetitions-1:
                endSequence(scanTime)
    
    
    def createFreqCalSequence():
        t0 = 20
        
        # Shimming
        iniSequence(t0)
            
        # Excitation pulse
        rfPulse(t0,rfExTime,rfExAmp,drfPhase*np.pi/180)
        
        # Refocusing pulse
        t0 += rfExTime/2+echoSpacing/2-rfReTime/2
        rfPulse(t0, rfReTime, rfReAmp, np.pi/2)
        
        # Rx
        t0 += blkTime+rfReTime/2+echoSpacing/2-acqTime/2-addRdPoints/BW
        rxGate(t0, acqTime+2*addRdPoints/BW)
        
        # Finalize sequence
        endSequence(repetitionTime)
        
    
    # Changing time parameters to us
    rfExTime = rfExTime*1e6
    rfReTime = rfReTime*1e6
    echoSpacing = echoSpacing*1e6
    repetitionTime = repetitionTime*1e6
    gradRiseTime = gradRiseTime*1e6
    phGradTime = phGradTime*1e6
    rdGradTime = rdGradTime*1e6
    rdDephTime = rdDephTime*1e6
    inversionTime = inversionTime*1e6
    
    # Calibrate frequency
    if freqCal==1:
        expt = ex.Experiment(lo_freq=larmorFreq, rx_t=samplingPeriod, init_gpa=init_gpa, gpa_fhdo_offset_time=(1 / 0.2 / 3.1))
        samplingPeriod = expt.get_rx_ts()[0]
        BW = 1/samplingPeriod/oversamplingFactor
        acqTime = nPoints[0]/BW        # us
        rawData['bw'] = BW*1e6
        createFreqCalSequence()
        rxd, msgs = expt.run()
        dataFreqCal = sig.decimate(rxd['rx0']*13.788, oversamplingFactor, ftype='fir', zero_phase=True)
        dataFreqCal = dataFreqCal[addRdPoints:nPoints[0]+addRdPoints]
        # Plot fid
    #    plt.figure(1)
        tVector = np.linspace(-acqTime/2, acqTime/2, num=nPoints[0],endpoint=True)*1e-3
    #    plt.subplot(1, 2, 1)
    #    plt.plot(tVector, np.abs(dataFreqCal))
    #    plt.title("Signal amplitude")
    #    plt.xlabel("Time (ms)")
    #    plt.ylabel("Amplitude (mV)")
    #    plt.subplot(1, 2, 2)
        angle = np.unwrap(np.angle(dataFreqCal))
    #    plt.title("Signal phase")
    #    plt.xlabel("Time (ms)")
    #    plt.ylabel("Phase (rad)")
    #    plt.plot(tVector, angle)
        # Get larmor frequency
        dPhi = angle[-1]-angle[0]
        df = dPhi/(2*np.pi*acqTime)
        larmorFreq += df
        rawData['larmorFreq'] = larmorFreq*1e6
        print("f0 = %s MHz" % (round(larmorFreq, 5)))
        # Plot sequence:
    #    expt.plot_sequence()
    #    plt.show()
        # Delete experiment:
        expt.__del__()
    
    # Create full sequence
    expt = ex.Experiment(lo_freq=larmorFreq, rx_t=samplingPeriod, init_gpa=init_gpa, gpa_fhdo_offset_time=(1 / 0.2 / 3.1))
    samplingPeriod = expt.get_rx_ts()[0]
    BW = 1/samplingPeriod/oversamplingFactor
    acqTime = nPoints[0]/BW        # us
    createSequence()

    # Plot sequence:
#    expt.plot_sequence()
        
    # Run the experiment
    dataFull = []
    dummyData = []
    overData = []
    for ii in range(nScans):
        print("Scan %s ..." % (ii+1))
        rxd, msgs = expt.run()
        rxd['rx0'] = rxd['rx0']*13.788   # Here I normalize to get the result in mV
        # Get data
        if dummyPulses>0:
            dummyData = np.concatenate((dummyData, rxd['rx0'][0:nRD*etl*oversamplingFactor]), axis = 0)
            overData = np.concatenate((overData, rxd['rx0'][nRD*etl*oversamplingFactor::]), axis = 0)
        else:
            overData = np.concatenate((overData, rxd['rx0']), axis = 0)
    expt.__del__()
    print('Scans done!')
    rawData['overData'] = overData
    
    # Fix the echo position using oversampled data
    if dummyPulses>0:
        dummyData = np.reshape(dummyData,  (nScans, etl, nRD*oversamplingFactor))
        dummyData = np.average(dummyData, axis=0)
        rawData['dummyData'] = dummyData
        overData = np.reshape(overData, (nScans, int(nPH/etl*nSL), etl,  nRD*oversamplingFactor))
        for ii in range(nScans):
            overData[ii, :, :, :] = fixEchoPosition(dummyData, overData[ii, :, :, :])
        
    # Generate dataFull
    overData = np.squeeze(np.reshape(overData, (1, nRD*oversamplingFactor*nPH*nSL*nScans)))
    dataFull = sig.decimate(overData, oversamplingFactor, ftype='fir', zero_phase=True)
    
    # Get index for krd = 0
    # Average data
    dataProv = np.reshape(dataFull, (nScans, nRD*nPH*nSL))
    dataProv = np.average(dataProv, axis=0)
    # Reorganize the data acording to sweep mode
    dataProv = np.reshape(dataProv, (nSL, nPH, nRD))
    dataTemp = dataProv*0
    for ii in range(nPH):
        dataTemp[:, ind[ii], :] = dataProv[:,  ii, :]
    dataProv = dataTemp
    # Check where is krd = 0
    dataProv = dataProv[int(nPoints[2]/2), int(nPH/2), :]
    indkrd0 = np.argmax(np.abs(dataProv))
    if  indkrd0 < nRD/2-addRdPoints or indkrd0 > nRD+addRdPoints:
        indkrd0 = int(nRD/2)
    indkrd0 = int(nRD/2)

    # Get individual images
    dataFull = np.reshape(dataFull, (nScans, nSL, nPH, nRD))
    dataFull = dataFull[:, :, :, indkrd0-int(nPoints[0]/2):indkrd0+int(nPoints[0]/2)]
    dataTemp = dataFull*0
    for ii in range(nPH):
        dataTemp[:, :, ind[ii], :] = dataFull[:, :,  ii, :]
    dataFull = dataTemp
    imgFull = dataFull*0
    for ii in range(nScans):
        imgFull[ii, :, :, :] = np.fft.ifftshift(np.fft.ifftn(np.fft.ifftshift(dataFull[ii, :, :, :])))
    rawData['dataFull'] = dataFull
    rawData['imgFull'] = imgFull    
    
    # Average data
    data = np.average(dataFull, axis=0)
    data = np.reshape(data, (nSL, nPH, nPoints[0]))
    
    # Do zero padding
    dataTemp = np.zeros((nPoints[2], nPoints[1], nPoints[0]))
    dataTemp = dataTemp+1j*dataTemp
    if nSL==1 or (nSL>1 and parAcqLines==0):
        dataTemp = data
    elif nSL>1 and parAcqLines>0:
        dataTemp[0:nSL-1, :, :] = data[0:nSL-1, :, :]
    data = np.reshape(dataTemp, (1, nPoints[0]*nPoints[1]*nPoints[2]))
    
    # Fix the position of the sample according to dfov
    kMax = np.array(nPoints)/(2*np.array(fov))*np.array(axesEnable)
    kRD = np.linspace(-kMax[0],kMax[0],num=nPoints[0],endpoint=False)
#        kPH = np.linspace(-kMax[1],kMax[1],num=nPoints[1],endpoint=False)
    kSL = np.linspace(-kMax[2],kMax[2],num=nPoints[2],endpoint=False)
    kPH = kPH[::-1]
    kPH, kSL, kRD = np.meshgrid(kPH, kSL, kRD)
    kRD = np.reshape(kRD, (1, nPoints[0]*nPoints[1]*nPoints[2]))
    kPH = np.reshape(kPH, (1, nPoints[0]*nPoints[1]*nPoints[2]))
    kSL = np.reshape(kSL, (1, nPoints[0]*nPoints[1]*nPoints[2]))
    dPhase = np.exp(-2*np.pi*1j*(dfov[0]*kRD+dfov[1]*kPH+dfov[2]*kSL))
    data = np.reshape(data*dPhase, (nPoints[2], nPoints[1], nPoints[0]))
    rawData['kSpace3D'] = data
    img=np.fft.ifftshift(np.fft.ifftn(np.fft.ifftshift(data)))
    rawData['image3D'] = img
    data = np.reshape(data, (1, nPoints[0]*nPoints[1]*nPoints[2]))
    
    # Create sampled data
    kRD = np.reshape(kRD, (nPoints[0]*nPoints[1]*nPoints[2], 1))
    kPH = np.reshape(kPH, (nPoints[0]*nPoints[1]*nPoints[2], 1))
    kSL = np.reshape(kSL, (nPoints[0]*nPoints[1]*nPoints[2], 1))
    data = np.reshape(data, (nPoints[0]*nPoints[1]*nPoints[2], 1))
    rawData['kMax'] = kMax
    rawData['sampled'] = np.concatenate((kRD, kPH, kSL, data), axis=1)
    data = np.reshape(data, (nPoints[2], nPoints[1], nPoints[0]))
    
    # Save data
    dt = datetime.now()
    dt_string = dt.strftime("%Y.%m.%d.%H.%M.%S")
    dt2 = date.today()
    dt2_string = dt2.strftime("%Y.%m.%d")
    if not os.path.exists('experiments/acquisitions/%s' % (dt2_string)):
        os.makedirs('experiments/acquisitions/%s' % (dt2_string))
            
    if not os.path.exists('experiments/acquisitions/%s/%s' % (dt2_string, dt_string)):
        os.makedirs('experiments/acquisitions/%s/%s' % (dt2_string, dt_string)) 
    rawData['fileName'] = "%s.%s.mat" % ("RARE",dt_string)
    savemat("experiments/acquisitions/%s/%s/%s.%s.mat" % (dt2_string, dt_string, "Old_RARE",dt_string),  rawData) 
        
    # Plot data for 1D case
    if (nPH==1 and nSL==1):
        # Plot k-space
        plt.figure(3)
        dataPlot = data[0, 0, :]
        plt.subplot(1, 2, 1)
        if axesEnable[0]==0:
            tVector = np.linspace(-acqTime/2, acqTime/2, num=nPoints[0],endpoint=False)*1e-3
            sMax = np.max(np.abs(dataPlot))
            indMax = np.argmax(np.abs(dataPlot))
            timeMax = tVector[indMax]
            sMax3 = sMax/3
            dataPlot3 = np.abs(np.abs(dataPlot)-sMax3)
            indMin = np.argmin(dataPlot3)
            timeMin = tVector[indMin]
            T2 = np.abs(timeMax-timeMin)
            plt.plot(tVector, np.abs(dataPlot))
            plt.plot(tVector, np.real(dataPlot))
            plt.plot(tVector, np.imag(dataPlot))
            plt.xlabel('t (ms)')
            plt.ylabel('Signal (mV)')
            print("T2 = %s us" % (T2))
        else:
            plt.plot(kRD[:, 0], np.abs(dataPlot))
            plt.yscale('log')
            plt.xlabel('krd (mm^-1)')
            plt.ylabel('Signal (mV)')
            echoTime = np.argmax(np.abs(dataPlot))
            echoTime = kRD[echoTime, 0]
            print("Echo position = %s mm^{-1}" %round(echoTime, 1))
        
        # Plot image
        plt.subplot(122)
        img = img[0, 0, :]
        if axesEnable[0]==0:
            xAxis = np.linspace(-BW/2, BW/2, num=nPoints[0], endpoint=False)*1e3
            plt.plot(xAxis, np.abs(img), '.')
            plt.xlabel('Frequency (kHz)')
            plt.ylabel('Density (a.u.)')
            print("Smax = %s mV" % (np.max(np.abs(img))))
        else:
            xAxis = np.linspace(-fov[0]/2*1e2, fov[0]/2*1e2, num=nPoints[0], endpoint=False)
            plt.plot(xAxis, np.abs(img))
            plt.xlabel('Position RD (cm)')
            plt.ylabel('Density (a.u.)')
    else:
        # Plot k-space
        plt.figure(3)
        dataPlot = data[round(nSL/2), :, :]
        plt.subplot(131)
        plt.imshow(np.log(np.abs(dataPlot)),cmap='gray')
        plt.axis('off')
        # Plot image
        if sweepMode==3:
            imgPlot = img[round(nSL/2), round(nPH/4):round(3*nPH/4), :]
        else:
            imgPlot = img[round(nSL/2), :, :]
        plt.subplot(132)
        plt.imshow(np.abs(imgPlot), cmap='gray')
        plt.axis('off')
        plt.title("RARE.%s.mat" % (dt_string))
        plt.subplot(133)
        plt.imshow(np.angle(imgPlot), cmap='gray')
        plt.axis('off')
        
    # plot full image
    if nSL>1:
        plt.figure(4)
        img2d = np.zeros((nPoints[1], nPoints[0]*nPoints[2]))
        img2d = img2d+1j*img2d
        for ii in range(nPoints[2]):
            img2d[:, ii*nPoints[0]:(ii+1)*nPoints[0]] = img[ii, :, :]
        plt.imshow(np.abs(img2d), cmap='gray')
        plt.axis('off')
        plt.title("RARE.%s.mat" % (dt_string))
        
#    plt.figure(5)
#    plt.subplot(121)
#    data1d = data[int(nSL/2), :, int(nPoints[0]/2)]
#    plt.plot(abs(data1d))
#    plt.subplot(122)
#    img1d = img[int(nSL/2), :, int(nPoints[0]/2)]
#    plt.plot(np.abs(img1d)*1e3)
    
    plt.show()
示例#58
0
def butter_lowpass_filter(data, cutoff=50, order=5):
    y = data
    y = decimate(y, q=10)
    print(y.dtype)
    return y
示例#59
0
def _float_feature(value, downsampling_rate):
    #assert len(value) == 5000
    value = decimate(value, q=downsampling_rate)
    return tf.train.Feature(float_list=tf.train.FloatList(value=value))
示例#60
0
def downsample(audiodata, factor):
    downsampled_data = decimate(audiodata, factor)
    return downsampled_data