def __init__(self, label='data', folder='.', filename=False, Col1=1, Col2=3, sens1=1000, sens2=5, tmin=False, tmax=False, tshift=False, fmin=False, fmax=False, t=False, ref=False, samp=False): if filename: self.sens1 = sens1 self.sens2 = sens2 self.label = label self.Col1 = Col1 self.Col2 = Col2 # load data, extract reference & sample field, calc FFT self.dat = loadtxt(folder + '/' + filename) self.t0 = -self.dat[:, 0] L10 = self.dat[:, Col1] * sqrt(2) * sens1 L20 = self.dat[:, Col2] * sens2 # create waveform objects (note, extra assignments to save mem.) self.ref0 = waveform(t=self.t0, wf=L10 + L20) self.samp0 = waveform(t=self.t0, wf=L10 - L20) self.ref0.t = self.t0 self.samp0.t = self.t0 self.f0 = self.ref0.f self.samp0.f = self.ref0.f if type(t) != bool: assert type(ref) != bool, 'must provide reference data' assert type(samp) != bool, 'must provide sample data' self.t0 = t self.ref0 = waveform(t=self.t0, wf=ref) self.samp0 = waveform(t=self.t0, wf=samp) self.ref0.t = self.t0 self.samp0.t = self.t0 self.f0 = self.ref0.f self.samp0.f = self.ref0.f # trim times & frequencies self.ref = deepcopy(self.ref0) self.samp = deepcopy(self.samp0) self.trimTime(tmin, tmax) self.samp.f = self.ref.f self.f = self.ref.f self.trimFreq(fmin, fmax) self.calcTrans() return
def send_wfms(self, ch_id, **kwargs): id_list = kwargs.get('id_list', [ch_id,]) if ch_id in self.ch1_ids() and ch_id == self.ch1_ids(id_list)[0]: bundle = waveform([self.ch1, self.mk11, self.mk12]) suffix = '_ch1' elif ch_id in self.ch2_ids() and ch_id == self.ch2_ids(id_list)[0]: bundle = waveform([self.ch2, self.mk21, self.mk22]) suffix = '_ch2' else: return file_list, len_list, name_list = bundle.format_MAGIC1000() for index, wfm_length in enumerate(len_list): self._check_wfm_length(wfm_length, index) for name, wfm_file in zip(name_list, file_list): self._OPC() self.instr.write_raw(':MMEM:DATA "%s%s.wfm",%s' % (name, suffix, IEEE_block_format(wfm_file)))
def waveform_creator(empty_elements,nonempty_elements): WaveForm = [] Empty = [] for x in range(len(nonempty_elements)): ch1 = [nonempty_elements[x][0],nonempty_elements[x][1],'ch1'] ch2 = [nonempty_elements[x][0],nonempty_elements[x][1],'ch2'] ch3 = [nonempty_elements[x][0],nonempty_elements[x][1],'ch3'] ch4 = [nonempty_elements[x][0],nonempty_elements[x][1],'ch4'] for y in range(2,len(nonempty_elements[x])): if (nonempty_elements[x][y].channel == 'ch1'): ch1.append(nonempty_elements[x][y]) elif (nonempty_elements[x][y].channel == 'ch2'): ch2.append(nonempty_elements[x][y]) elif (nonempty_elements[x][y].channel == 'ch3'): ch3.append(nonempty_elements[x][y]) elif (nonempty_elements[x][y].channel == 'ch4'): ch4.append(nonempty_elements[x][y]) ch1 = waveform(ch1) ch2 = waveform(ch2) ch3 = waveform(ch3) ch4 = waveform(ch4) ch1.waveGenerator() ch2.waveGenerator() ch3.waveGenerator() ch4.waveGenerator() WaveForm.append(ch1) WaveForm.append(ch2) WaveForm.append(ch3) WaveForm.append(ch4) for x in range(len(empty_elements)): Empty.append(waveform(empty_elements[x])) return (WaveForm,Empty)
def proc_RX(WF_file, shots, rxData=None, sigmas=np.arange(0, 5, 0.25), deltas=np.arange(-1, 1.5, 0.5), TX=None, countPeaks=True, WF_library=None, TX_file=None, scat_file=None, catalogBuffer=None): """ Routine to process the wavefoms in an ATM waveform file. Can be run inside the loop of fit_ATM_scat, or run on a few waveforms at a time for plot generation """ if TX is None and TX_file is not None: with h5py.file(TX_file) as h5f: TX = waveform(np.array(h5f['/TX/t']), np.array(h5f(['/TX/p']))) TX.t -= TX.nSigmaMean()[0] TX.tc = 0 # make the library of templates if WF_library is None: WF_library = dict() WF_library.update({0.: TX}) if scat_file is not None: WF_library.update(make_rx_scat_catalog(TX, h5_file=scat_file)) if rxData is None: # make the return waveform structure D = read_ATM_file(WF_file, shot0=shots[0], nShots=shots[-1] - shots[0]) rxData = D['RX'] rxData.t -= np.nanmean(rxData.t) else: D = dict() if countPeaks: rxData.nPeaks = rxData.count_peaks(W=4) else: rxData.nPeaks = np.ones(rxData.size) threshold_mask = rxData.p >= 255 rxData.subBG(t50_minus=3.) rxData.tc = [ii.nSigmaMean()[0] for ii in rxData] #rxData.tc=rxData.t50() rxData.p[threshold_mask] = np.NaN # fit the data. Catalogbuffer contains waveform templates that have already been tried D_out, catalogBuffer=fit_catalog(rxData, WF_library, sigmas, deltas, return_data_est=True, \ return_catalog=True, catalog=catalogBuffer) return D_out, rxData, D, catalogBuffer
def _preprocessing(self, filename, start=0, end=float('inf')): """ Return the sub-fingerprint matrix from featureExtract.py overlap: For the sake of avoiding unpleasant segmentation in the retrieving procedure. """ if start != 0: start = int(float(start)*1000) if end != float('inf'): end = int(float(end)*1000) pcm, samplerate = waveform(filename, start, end) pcm = downsample(pcm, samplerate, self.sr) if type(pcm) == int: landmarks = 0 else: landmarks = find_landmark(pcm, self.sr, self.density) return landmarks
def main(): parser = argparse.ArgumentParser( description= 'Fit the waveforms from an ATM file with a set of scattering parameters' ) parser.add_argument('input_file', type=str) parser.add_argument('output_file', type=str) parser.add_argument('--startShot', '-s', type=int, default=0) parser.add_argument('--scat_file', '-f', type=str, default=None) parser.add_argument('--nShots', '-n', type=int, default=np.Inf) parser.add_argument('--DOPLOT', '-P', action='store_true') parser.add_argument('--IR', '-I', action='store_true') parser.add_argument('--TXfromRX', action='store_true') parser.add_argument('--skipRX', action='store_true', default=False) parser.add_argument('--fitTX', action='store_true') parser.add_argument('--everyNTX', type=int, default=100) parser.add_argument('--TXfile', '-T', type=str, default=None) parser.add_argument('--waveforms', '-w', action='store_true', default=False) args = parser.parse_args() # get the waveform count from the output file nWFs = np.minimum( args.nShots, read_ATM_file(args.input_file, getCountAndReturn=True) - args.startShot) lastShot = args.startShot + nWFs # make the output file if os.path.isfile(args.output_file): os.remove(args.output_file) outDS = ['shot', 'R', 'A', 'B', 'delta_t', 'sigma', 't0', 'K0', 'Kmin', 'Kmax',\ 'latitude', 'longitude', 'elevation'] out_h5 = h5py.File(args.output_file, 'w') for DS in outDS: out_h5.create_dataset(DS, (nWFs, ), dtype='f8') if args.waveforms: out_h5.create_dataset('RX/p', (192, nWFs)) out_h5.create_dataset('RX/p_fit', (192, nWFs)) TxP = None # get the transmit pulse if args.TXfile is not None: with h5py.File(args.TXfile, 'r') as fh: TX = waveform(np.array(fh['/TX/t']), np.array(fh['/TX/p'])) TX.t -= TX.nSigmaMean()[0] TX.tc = np.array(TX.nSigmaMean()[0]) TX.normalize() else: if args.TXfromRX is False: TX, TxP = get_tx_est(args.input_file, nShots=5000) else: TX, TxP = get_tx_est(args.input_file, source='RX', pMin=150) if args.fitTX: print('fitting the transmit pulse') dummy, TxP = get_tx_est(args.input_file, source='TX', TX0=TX, skip_make_TX=True, skip_n_tx=args.everyNTX) out_h5.create_dataset("TX/t", data=TX.t.ravel()) out_h5.create_dataset("TX/p", data=TX.p.ravel()) if TxP is not None: for field in ['t0', 'A', 'R', 'shot', 'sigma']: out_h5.create_dataset('/TX/' + field, data=TxP[field]) if args.skipRX is True: out_h5.close() return # check whether the scattering file exists if args.scat_file is not None: scat_file = args.scat_file else: # look for the default scattering file in the directory where the source file is found scat_file = os.path.dirname( os.path.abspath(__file__)) + '/subsurface_srf_no_BC.h5' if not os.path.isfile(scat_file): print("%s does not exist" % scat_file) exit() # make the library of templates WF_library = dict() WF_library.update({0.: TX}) if args.IR is False: WF_library.update(make_rx_scat_catalog(TX, h5_file=scat_file)) print("Returns:") # loop over start vals (one block at a time...) # choose how to divide the output blocksize = 1000 start_vals = args.startShot + np.arange(0, nWFs, blocksize, dtype=int) catalogBuffer = None time_old = time() if args.IR is True: countPeaks = False sigmas = np.arange(0, 5, 0.125) else: countPeaks = True sigmas = np.arange(0, 5, 0.25) # choose a set of delta t values deltas = np.arange(-1, 1.5, 0.5) for shot0 in start_vals: shots = np.arange(shot0, np.minimum(shot0 + blocksize, lastShot)) tic = time() D_out, rxData, D, catalogBuffer=proc_RX(args.input_file, shots, sigmas=sigmas, deltas=deltas,\ TX=TX, WF_library=WF_library, catalogBuffer=catalogBuffer, countPeaks=countPeaks) delta_time = time() - tic N_out = D_out['shot'].size outShot0 = shot0 - args.startShot for key in outDS: try: if key in D_out: out_h5[key][outShot0:outShot0 + N_out] = D_out[key].ravel() else: if key in D: out_h5[key][outShot0:outShot0 + N_out] = D[key].ravel() except OSError: print("OSError for key=%s, outshot0=%d, outshotN=%d, nDS=%d" % (key, outShot0, outShot0 + N_out, out_h5[key].size)) if args.waveforms: out_h5['RX/p_fit'][:, outShot0:outShot0 + N_out] = D_out['wf_est'] out_h5['RX/p'][:, outShot0:outShot0 + N_out] = rxData.p print(" shot=%d out of %d, N_keys=%d, dt=%5.1f" % (shot0 + blocksize, start_vals[-1] + blocksize, len(catalogBuffer.keys()), delta_time)) print(" time to fit RX=%3.2f" % (time() - time_old)) if args.waveforms: out_h5.create_dataset('RX/t', data=rxData.t.ravel()) out_h5.close()
if asp.ch_play != 1: raise ValueError("El código no está preparado para reproducir \ en 2 canales") print("Canales para grabar: ch_rec=%i" % asp.ch_rec) #%% p = pyaudio.PyAudio() s_f = [] n = int(bsp.dT / bsp.dt) if form == 'freq': s_0 = np.sin(2 * np.pi * np.arange(asp.sr * bsp.dT) * bsp.freq / asp.sr) else: s_0 = waveform(form, n) s_0 = bsp.vol * s_0.astype(np.float32) def callback(in_data, frame_count, time_info, status): return (s_0, pyaudio.paContinue) streamplay = p.open(format=asp.ft_play, channels=asp.ch_play, rate=asp.sr, output=True, stream_callback=callback) streamrecord = p.open(format=asp.ft_rec, channels=asp.ch_rec,
def play_record( T, # duración en seg de la grabación form='freq', # forma de onda {'freq', 'sin', 'squ', 'tri', 'saw'} mode='txt', # modo de funcionamiento del programa {'txt', 'wav'} savetxt=False, showplot=True, buffer={ 'dT': 1, 'dt': 0.005, 'freq': 440, 'vol': 1 }, audio={ 'sr': 44000, 'ch_play': 1, 'ch_rec': 1, 'ft_play': pyaudio.paInt16, 'ft_rec': pyaudio.paInt16 }, filendir={ 'fname': 'output', 'fdir': os.getcwd() }, ): """Reproduce una señal de sonido y graba por jack al mismo tiempo. Crea una señal cuya forma de onda está dada por 'form'. Ésta crea un \ buffer cuyos frames duran 'dt'. Reproduce y graba durante un tiempo \ 'T'. Devuelve el array reproducido y la lista grabada. Tiene dos modos de funcionamiento dados por 'mode'. Si 'mode=="wav", \ guarda un archivo de audio. Y si 'mode=="txt"', Además, si 'savetxt==True', guarda un archivo de texto. Y si \ 'showplot=="True", muestra un gráfico. """ import pyaudio import numpy as np import os import wave import matplotlib.pyplot as plt from new_name import new_name from waveform import waveform from argparse import Namespace home = os.getcwd() ref = { 'dT': 1, 'dt': 0.005, 'freq': 440, 'vol': 1, 'sr': 44000, 'ch_play': 1, 'ch_rec': 1, 'ft_play': pyaudio.paInt16, 'ft_rec': pyaudio.paInt16, 'fname': 'output', 'fdir': os.getcwd() } bkey = ['dT', 'dt', 'freq', 'vol'] akey = ['sr', 'ch_play', 'ch_rec', 'ft_play', 'ft_rec'] fkey = ['fname', 'fdir'] for key in bkey: if key not in buffer: buffer.update({key: ref[key]}) for key in akey: if key not in audio: audio.update({key: ref[key]}) for key in fkey: if key not in filendir: filendir.update({key: ref[key]}) bsp = Namespace(**buffer) # buffer space asp = Namespace(**audio) # audio space fsp = Namespace(**filendir) # filendir space if mode != 'txt': print("Modo: 'wav'") else: print("Modo: 'txt'") print("Guardar txt: %s" % savetxt) print("Mostrar gráfico: %s" % showplot) print("Longitud del buffer: %i" % int(bsp.dT / bsp.dt)) if int(bsp.dT / bsp.dt) != bsp.dT / bsp.dt: print("¡Ojo! El intervalo dt no entra un número entero \ de veces en dT") bsp.dt = bsp.dT / int(bsp.dT / bsp.dt) print("Intervalo redefinido a %f s" % bsp.dt) if int(T / bsp.dT) != T / bsp.dT: print("¡Ojo! El tiempo de grabación \ no es un número entero de veces dT") if int(T / bsp.dT) != 0: T = int(T / bsp.dT) * bsp.dT else: print("¡Ojo! El tiempo de grabación era menor a dT") T = bsp.dT print("Tiempo de grabación redefinido a %f s" % T) p = pyaudio.PyAudio() s_f = [] n = int(bsp.dT / bsp.dt) if form == 'freq': s_0 = np.sin(2 * np.pi * np.arange(asp.sr * bsp.dT) * bsp.freq / asp.sr) else: s_0 = waveform(form, n) s_0 = bsp.vol * s_0.astype(np.float32) def callback(in_data, frame_count, time_info, status): return (s_0, pyaudio.paContinue) streamplay = p.open(format=asp.ft_play, channels=asp.ch_play, rate=asp.sr, output=True, stream_callback=callback) streamrecord = p.open(format=asp.ft_rec, channels=asp.ch_rec, rate=asp.sr, input=True, frames_per_buffer=n) streamplay.start_stream() print("* recording") streamrecord.start_stream() data = streamrecord.read(int(asp.sr * T)) print("* done recording") streamrecord.stop_stream() streamplay.stop_stream() streamrecord.close() streamplay.close() p.terminate() if mode == 'txt': s_f.extend(np.fromstring(data, 'Float32')) if savetxt: os.chdir(fsp.fdir) savetxt((new_name(fsp.fname, 'txt', fsp.fdir) + '.txt'), s_f) os.chdir(home) if showplot: plt.figure() plt.plot(s_f[2000:3000], 'ro-') plt.ylabel('señal grabada') plt.grid() plt.show() return s_0, s_f else: s_f.append(data) fsp.fname = new_name(fsp.fname, 'wav', fsp.fdir) os.chdir(fsp.fdir) wf = wave.open((fsp.fname + '.wav'), 'wb') wf.setnchannels(asp.ch_rec) wf.setsampwidth(p.get_sample_size(asp.ft_rec)) wf.setframerate(asp.sr) wf.writeframes(b''.join(s_f)) wf.close() os.chdir(home)
def send_wfms(self, ch_id, **kwargs): if ('id_list' in kwargs and ch_id == kwargs['id_list'][0]) or kwargs.get( 'clear_buffer', False): self.safe_write('SLISt:SEQuence:DELete ALL') self.safe_write('WLISt:WAVeform:DELete ALL') id_list = kwargs.get('id_list', [ ch_id, ]) if ch_id in self.ch1_ids() and ch_id == self.ch1_ids(id_list)[0]: bundle = waveform([self.ch1, self.mk11, self.mk12]) suffix = '_ch1' elif ch_id in self.ch2_ids() and ch_id == self.ch2_ids(id_list)[0]: bundle = waveform([self.ch2, self.mk21, self.mk22]) suffix = '_ch2' else: return chunk_size = 2**20 wave_data, mk_data, len_list, name_list = bundle.format_TekWFM_AWG70000( chunk_size=chunk_size) for index, wfm_length in enumerate(len_list): self._check_wfm_length(wfm_length, index) self._OPC() #complete all pending operations for wave_block_chunked, mk_block_chunked, wfm_length, name in zip( wave_data, mk_data, len_list, name_list): info = ({'name': name + suffix, 'size': wfm_length}) self.safe_write( 'WLISt:WAVeform:NEW "{name}",{size}'.format(**info)) #self.safe_write('WLISt:WAVeform:NEW "{name}",{size},REAL'.format(**info)) for chunk_index in range(len(wave_block_chunked)): numpnts = chunk_size if chunk_index < len( wave_block_chunked ) - 1 else wfm_length - chunk_index * chunk_size wave_block, mk_block = wave_block_chunked[ chunk_index], mk_block_chunked[chunk_index] info.update({ 'start_index': chunk_index * chunk_size, 'size': numpnts }) info.update({ 'wave_block_data': wave_block, 'marker_block_data': mk_block }) self.instr.write_raw( 'WLISt:WAVeform:DATA "{name}",{start_index},{size},{wave_block_data}' .format(**info)) self.instr.write_raw( 'WLISt:WAVeform:MARKer:DATA "{name}",{start_index},{size},{marker_block_data}' .format(**info)) # cmd = 'WLISt:WAVeform:DATA "{name}",{start_index},{size},{wave_block_data}'.format(**info) # header = 'WLISt:WAVeform:DATA "{name}",{start_index},{size},'.format(**info) # print cmd[:len(header)+10] # cmd = 'WLISt:WAVeform:MARKer:DATA "{name}",{start_index},{size},{marker_block_data}'.format(**info) # header = 'WLISt:WAVeform:MARKer:DATA "{name}",{start_index},{size},'.format(**info) # print cmd[:len(header)+10] self._OPC()