def retrieve_footer(file, comment_marker='#'): """Retrieves the footer of a .txt file saved with np.savetxt. Parameters ---------- file : str File's root (must include directory and termination). comment_marker='#' : str, optional Sign that indicates a line is a comment on np.savetxt. Returns ------- last_line : str, dict File's footer Raises ------ ValueError : "Footer not found. Sorry!" When the last line doesn't begin with 'comment_marker'. See Also -------- fwp_save.savetxt """ with open(file, 'r') as f: for line in f: last_line = line if last_line[0] == comment_marker: try: last_line = last_line.split(comment_marker + ' ')[-1] last_line = last_line.split('\n')[0] footer = eval('dict({})'.format(last_line)) for key, value in footer.items(): try: number = fst.find_numbers(value) if len(number) == 1: number = number[0] if len(value.split(' ')) == 2: footer[key] = ( number, value.split(' ')[-1] ) else: footer[key] = number except TypeError: value = value except: footer = last_line return footer else: raise ValueError("No footer found. Sorry!")
else: if len(temp) != 0: rawfiles_by_sr[current_sr] = temp current_sr = this_sr temp = [f] # sorted list of sampling rates samplingrates = sorted(list(rawfiles_by_sr.keys())) cual = 10 archivos = rawfiles_by_sr[samplingrates[cual]] signal_freqs = {} #time, data = np.loadtxt(f, unpack=True) for f in archivos: freq = fstr.find_numbers(f)[1] signal_freqs[freq] = Struct() signal_freqs[freq].file = f time, data = np.loadtxt(f, unpack=True) signal_freqs[freq].duration = time[-1] signal_freqs[freq].nperiods = len(find_peaks(data)[0]) signal_freqs[freq].maybe_freq = len(find_peaks(data)[0]) / time[-1] #to be able to plot with lines, keys must be sorted actual_freq = sorted(list(signal_freqs.keys())) plt.plot(actual_freq, [signal_freqs[f].maybe_freq for f in actual_freq], '-o') #%% interbuffer time
def calc_vel(time, singal, **kwargs): ds = np.diff(signal) picos = find_peaks(ds, **kwargs)[0] vel = np.diff(picos).astype(float) t = time[picos[:-1]] return t, 1 / vel #def fix_vel(vel, threshold): #%% #Select file and extract stuff gen_freq = 10e3 #in Hz file = contenidos_completos[1] salto = fst.find_numbers(file)[0:1] samplerate = fst.find_1st_number(retrieve_footer(file)) #in Hz points_per_gen_period = samplerate / gen_freq #load data time, signal, gen = np.loadtxt(file, unpack=True) signal = np.abs(signal) gen /= np.mean(gen[gen > 4]) # gen now goes between 0 and 1, approx cada = 200 #integral = np.array([np.trapz(gen[i:i+cada]) for i in range(len(gen)-cada)]) #duty_cycle = np.array([np.mean(gen[i:i+cada]) for i in range(len(gen)-cada)]) ds = normalize(np.diff(signal), 3) signal = normalize(signal, 4) picos = find_peaks(ds, height=.6, prominence=.4)[0]