def done(self): if args.append: for i,v in enumerate(np_convolve(args.function, [findNumber(val[args.column]) for val in self.vals], mode=args.mode)): if args.mode == args.outfile.write(self.vals[i] + [v]) else: for v in np_convolve(args.function, self.vals, mode=args.mode): args.outfile.write(self.tup + [v])
#analisi in frequenza #fft data0 from scipy import fftpack f_s = 30000 #sampling frequency ###### FIR FILTER ###### from scipy.signal import firwin from numpy import convolve as np_convolve # b coefficient (order, [band], ..,..,sampling freq) b = firwin(10, [300, 3000], width=0.05, pass_zero='bandpass', fs=f_s) ###### CONVOLUTION ###### CH = 4 # SELECT THE NUMBER OF CHANNEL datafir = np.array( list(map(lambda x: np_convolve(x, b, mode='valid'), dat[0:CH, :]))) datafir0 = datafir[0, :]
w = [0] * len(time) v = [0] * len(time) error = [0] * len(time) #Create a FIR filter b = firwin(5, 0.9) #b = [-0.0161, 0.5161, 0.5161, -0.0161] print b for t in time: csum[ind(t)] = cumsum(norm_msg, ind(t)) for t in time: fmmsg[ind(t)] = exp(1j * (2 * pi * fc * t + 2 * pi * kf * csum[ind(t)])) fconv = np_convolve(fmmsg, b) print(len(fconv)) print fmmsg[0:10] conv_offtime = len(b) / 2 print fconv[conv_offtime:conv_offtime + 10] for t in time: y[ind(t)] = fconv[ind(t) + conv_offtime] * exp(-1j * 2 * pi * fc * (1 - demod_fc_error) * t) itertime = iter(time) next(itertime) for t in itertime: w[ind(t)] = y[ind(t)] * conj(y[ind(t) - 1])
diff_list = [] diff2_list = [] diff3_list = [] ntaps_list = 2**np.arange(2, 14) for ntaps in ntaps_list: # Create a FIR filter. b = firwin(ntaps, [0.05, 0.95], width=0.05, pass_zero=False) # Signal convolve. tstart = time.time() conv_result = sig_convolve(x, b[np.newaxis, :], mode="valid") conv_time.append(time.time() - tstart) # --- numpy.convolve --- tstart = time.time() npconv_result = np.array([np_convolve(xi, b, mode="valid") for xi in x]) npconv_time.append(time.time() - tstart) # fft convolve (fast fourier transform) convolution. tstart = time.time() fftconv_result = fftconvolve(x, b[np.newaxis, :], mode="valid") fftconv_time.append(time.time() - tstart) # 1-dimensional (one-dimensional) convolution. tstart = time.time() # convolve1d doesn"t have a "valid" mode, so we expliclity slice out # the valid part of the result. conv1d_result = convolve1d(x, b)[:, (len(b) - 1) // 2:-(len(b) // 2)] conv1d_time.append(time.time() - tstart) tstart = time.time() lfilt_result = lfilter(b, [1.0], x)[:, len(b) - 1:] lfilt_time.append(time.time() - tstart) diff = np.abs(fftconv_result - lfilt_result).max()
def artificialRaw(): int215 = 2**15 # Whether to add voltage spikes at the upward square wave transition. addSpikes = True doFilter = False ntaps = 1001 cutoff = 10 # (Hz) if addSpikes: descript = 'spikes' else: descript = 'no spikes' if doFilter: descript += ' filtered' else: descript += ' unfiltered' at = cs.emptyClass() at.descript = descript folderPath = os.path.join( r'C:\Users\timl\Documents\IP_data_plots\190415_artificialSpikes') # Change . rawFolderPath = os.path.join(folderPath, 'rawData') at.fileDateStr = '190415' # folderName[:6] at.fileNum = 2 fileName = '%s_%d.txt' % (at.fileDateStr, at.fileNum) filePath = os.path.join(rawFolderPath, fileName) if doFilter: at.minor = '%.0f Hz cutoff, %d taps' % (cutoff, ntaps) else: at.minor = 'None.' at.major = '' at.scanChCount = 8 at.writeChCount = 8 at.n = 8192 at.fs = 8192 # (Hz) at.xmitFund = 4 # (Hz) at.rCurrentMeas = 1.25 # (Ohm) at.rExtraSeries = 0 # (Ohm) at.measStr = at.writeChCount * ['N/A'] at.measStr[0] = 'currentMeas' at.measStr[1] = 'voltage' at.In5BHi = sp.array([10, 0.1, 0.1, 0.1, 0.1, 1, 10, 10]) at.In5BHi = at.In5BHi[:at.writeChCount] at.Out5BHi = sp.array([5, 10, 10, 10, 10, 5, 5, 5]) at.Out5BHi = at.Out5BHi[:at.writeChCount] at.ALoadQHi = at.Out5BHi # Artificially generate raw current and voltage waveforms. if False: at.pktCount = 1 at.raw = sp.zeros((at.writeChCount, at.pktCount, at.n)) oilFolderPath = r'C:\Users\Public\Documents\oil_data' oilFilePath = os.path.join(oilFolderPath, at.descript + 'i.txt') at.raw[0, 0, :] = readOilFile(oilFilePath, at.n) oilFilePath = os.path.join(oilFolderPath, at.descript + 'd.txt') at.raw[1, 0, :] = readOilFile(oilFilePath, at.n) elif True: # Number of packets saved to the file. at.pktCount = 1 # Time series. timeVec = sp.linspace(0, at.n / at.fs, at.n, endpoint=False) # Add a time offset from zero. timeVec += 0.1 # Basic wave parameters to work from. amp = 6 # (%) freq = 4 # (Hz) # Wave phases by channel. phaseVec = sp.array(7 * [17]) / 1000 # (rad) phaseVec = sp.hstack((sp.zeros(1), phaseVec)) # Measurment string. for ch in range(at.writeChCount): if addSpikes: if ch == 0: at.measStr[ch] = 'Ch %d. No Spikes.' % ch else: at.measStr[ch] = 'Ch %d. With Spikes.' % ch else: at.measStr[ch] = 'Ch %d. No Spikes.' % ch listTime = at.scanChCount * [timeVec] for ch in range(at.writeChCount): # Add channel skew error. deltaT = ch / (at.fs * at.scanChCount) # (s) listTime[ch] = timeVec + deltaT at.raw = sp.zeros((at.writeChCount, at.pktCount, at.n)) for p in range(at.pktCount): for ch in range(at.writeChCount): at.raw[ch, p, :] = amp * sp.signal.square( 2 * sp.pi * freq * listTime[ch] + phaseVec[ch]) # Add voltage spikes to 100% at the transition from negative to # positive. if addSpikes and ch != 0: lastSign = at.raw[ch, p, 0] > 0 for tIdx in range(1, len(at.raw[ch, p, :])): newSign = at.raw[ch, p, tIdx] > 0 if newSign and (newSign != lastSign): at.raw[ch, p, tIdx] = 100 lastSign = newSign # Convert from percentages of a range to a 16-bit integer scale. at.raw = scaleAndShift(at.raw) if doFilter: # Create an FIR filter. filtWin = firwin(ntaps, cutoff, fs=at.fs, pass_zero=True) # Apply the FIR filter to each channel. for p in range(at.pktCount): for ch in range(at.writeChCount): # filteredSig = sig_convolve(at.raw[ch, p, :], # filtWin) x = at.raw[ch, p, :] x = x[sp.newaxis, :] filteredSig = np.array( [np_convolve(xi, filtWin, mode='same') for xi in x]) at.raw[ch, p, :] = filteredSig.copy() with open(filePath, 'w') as f: # Write the file header. line = '%s,%d\n' % (at.fileDateStr, at.fileNum) f.write(line) line = '%s\n%s\n%s\n' % (at.descript, at.minor, at.major) f.write(line) line = '%d,%d,%d,%d,%.2f\n' % (at.scanChCount, at.writeChCount, at.n, at.fs, at.xmitFund) f.write(line) line = '%.1f,%.1f\n' % (at.rCurrentMeas, at.rExtraSeries) f.write(line) line = '' for ch in range(at.writeChCount): line = line + at.measStr[ch] + ',' # Remove the last comma, and include a carriage return line feed. line = line[:-1] + '\n' f.write(line) line = float2lineStr(at.In5BHi, 3) f.write(line) line = float2lineStr(at.Out5BHi, 3) f.write(line) line = float2lineStr(at.ALoadQHi, 3) f.write(line) # Write packets of data to the file. cpuTimeStr = '133130.621' gpsDateStr = '000000' gpsTimeStr = '000000.000' lat = 'NaN' longi = 'NaN' maskUp = at.raw >= int215 maskDn = at.raw < int215 for p in range(at.pktCount): line = '$%d\n' % (p + 1) f.write(line) line = '\'%s,%s\n' % (at.fileDateStr, cpuTimeStr) f.write(line) line = '@%s,%s,%s,%s\n' % (gpsDateStr, gpsTimeStr, lat, longi) f.write(line) clipHi = sp.sum(at.raw[:, p, :] == (2**16 - 1), axis=1) line = float2lineStr(clipHi, 0) f.write(line) clipLo = sp.sum(at.raw[:, p, :] == 0, axis=1) line = float2lineStr(clipLo, 0) f.write(line) mean = sp.mean(at.raw[:, p, :], axis=1) mean = shiftAndScale(mean, int215) line = float2lineStr(mean, 1) f.write(line) meanUp = sp.zeros(at.writeChCount) meanDn = sp.zeros(at.writeChCount) for ch in range(at.writeChCount): meanUp[ch] = sp.mean(at.raw[ch, p, maskUp[ch, p, :]]) meanDn[ch] = sp.mean(at.raw[ch, p, maskDn[ch, p, :]]) meanUp = shiftAndScale(meanUp, int215) meanDn = shiftAndScale(meanDn, int215) line = float2lineStr(meanUp, 1) f.write(line) line = float2lineStr(meanDn, 1) f.write(line) countUp = sp.sum(maskUp[:, p, :], 1) line = float2lineStr(countUp, 0) f.write(line) countDn = sp.sum(maskDn[:, p, :], 1) line = float2lineStr(countDn, 0) f.write(line) for s in range(at.n): line = float2lineStr(at.raw[:, p, s], 0) f.write(line) # Asterisk character to end the packet. line = '*\n' f.write(line)
# num_read += read_size # if samples.size > N: # samples = samples[:N] # I think that slice expression is correct, would have to verify end = time.time() print("Reading time: ", end - start) start = time.time() # To mix the data down, generate a digital complex exponential # (with the same length as x1) with phase -F_offset/Fs fc1 = np.exp(-1.0j * 2.0 * np.pi * F_offset / Fs * np.arange(len(samples))) # Now, just multiply x1 and the digital complex expontential samples2 = samples * fc1 print("Downconversion time: ", end - start) start = time.time() filtered = np_convolve(b, samples2) end = time.time() print("Filter time: ", end - start) start = time.time() decimated = filtered[0::downsample_rate] end = time.time() print("Decimate time: ", end - start) start = time.time() v = [0] * len(decimated) prev_s = 0 for i, s in enumerate(decimated): w = s * conj(prev_s) v[i] = np.arctan2(w.imag, w.real)
for ntaps in ntaps_list: # Create a FIR filter. b = firwin(ntaps, [0.05, 0.95], width=0.05, pass_zero=False) if ntaps <= 2 ** 9: # --- signal.convolve --- # We know this is slower than the others when ntaps is # large, so we only compute it for small values. tstart = time.time() conv_result = sig_convolve(x, b[np.newaxis, :], mode='valid') conv_time.append(time.time() - tstart) # --- numpy.convolve --- tstart = time.time() npconv_result = np.array([np_convolve(xi, b, mode='valid') for xi in x]) npconv_time.append(time.time() - tstart) # --- signal.fftconvolve --- tstart = time.time() fftconv_result = fftconvolve(x, b[np.newaxis, :], mode='valid') fftconv_time.append(time.time() - tstart) # --- convolve1d --- tstart = time.time() # convolve1d doesn't have a 'valid' mode, so we expliclity slice out # the valid part of the result. conv1d_result = convolve1d(x, b)[:, (len(b)-1)//2 : -(len(b)//2)] conv1d_time.append(time.time() - tstart) # --- lfilter ---
print "coefficients type:", coefficients.dtype.name print "Data lenght:", n print "Input data size:", inputData.size print "FIR lenght:", len(coefficients) iterations = 3 if not debug else 1 print "iterations:", iterations print "numpy.convolve computation times in seconds" # --- numpy.convolve --- npconv_time = [] for iteration in xrange(1,iterations+1): tstart = time.time() npconv_result = np_convolve(inputData, coefficients, mode='valid') duration = time.time() - tstart npconv_time.append(duration); print "#%d duration: %.6f" % (iteration, duration) if debug: print "coeffs:", coefficients print "intput:", inputData print "result length:", npconv_result.size print "result:", npconv_result # pprint.pprint(npconv_time)