def ar2lsp(a): """ Convert LPC ai to LSP :param a: LPC(order = p, lpc size = p + 1) :return lsf: LSP angle frequency """ a = np.array(a) a = a.reshape(-1, ) if (np.real(a) != a).all(): print( 'Line spectral frequencies are not defined for complex polynomials.' ) return if a[0] != 1.0: a = a / a[0] # print(a) a0 = np.poly1d(a) if np.max(np.abs(a0.roots)) >= 1.0: print('The polynomial must have all roots inside of the unit circle. ') return p = len(a) - 1 # polynomial order a1 = np.concatenate((a, np.zeros(1))) a2 = a1[::-1] P1 = a1 + a2 Q1 = a1 - a2 if (p % 2): Q = deconvolve(Q1, np.array([1, 0, -1])) P = P1 else: Q = deconvolve(Q1, np.array([1, -1])) P = deconvolve(P1, np.array([1, 1])) P0 = np.poly1d(P[0]) Q0 = np.poly1d(Q[0]) rP = P0.roots rQ = Q0.roots aP = np.angle(rP[0:len(rP):2]) aQ = np.angle(rQ[0:len(rQ):2]) aLSF = np.concatenate((aP, aQ)) lsf = np.sort(aLSF) return lsf
def lpc_to_lsf(all_lpc): if len(all_lpc.shape) < 2: all_lpc = all_lpc[None] order = all_lpc.shape[1] - 1 all_lsf = np.zeros((len(all_lpc), order)) for i in range(len(all_lpc)): lpc = all_lpc[i] lpc1 = np.append(lpc, 0) lpc2 = lpc1[::-1] sum_filt = lpc1 + lpc2 diff_filt = lpc1 - lpc2 if order % 2 != 0: deconv_diff, _ = sg.deconvolve(diff_filt, [1, 0, -1]) deconv_sum = sum_filt else: deconv_diff, _ = sg.deconvolve(diff_filt, [1, -1]) deconv_sum, _ = sg.deconvolve(sum_filt, [1, 1]) roots_diff = np.roots(deconv_diff) roots_sum = np.roots(deconv_sum) angle_diff = np.angle(roots_diff[::2]) angle_sum = np.angle(roots_sum[::2]) lsf = np.sort(np.hstack((angle_diff, angle_sum))) if len(lsf) != 0: all_lsf[i] = lsf return np.squeeze(all_lsf)
def poly2lsf(a): """Prediction polynomial to line spectral frequencies. converts the prediction polynomial specified by A, into the corresponding line spectral frequencies, LSF. normalizes the prediction polynomial by A(1). .. doctest:: >>> from spectrum import poly2lsf >>> a = [1.0000, 0.6149, 0.9899, 0.0000 ,0.0031, -0.0082] >>> lsf = poly2lsf(a) >>> lsf = array([0.7842, 1.5605, 1.8776, 1.8984, 2.3593]) .. seealso:: lsf2poly, poly2rc, poly2qc, rc2is """ #Line spectral frequencies are not defined for complex polynomials. # Normalize the polynomial a = numpy.array(a) if a[0] != 1: a/=a[0] if max(numpy.abs(numpy.roots(a))) >= 1.0: error('The polynomial must have all roots inside of the unit circle.'); # Form the sum and differnce filters p = len(a)-1 # The leading one in the polynomial is not used a1 = numpy.concatenate((a, numpy.array([0]))) a2 = a1[-1::-1] P1 = a1 - a2 # Difference filter Q1 = a1 + a2 # Sum Filter # If order is even, remove the known root at z = 1 for P1 and z = -1 for Q1 # If odd, remove both the roots from P1 if p%2: # Odd order P, r = deconvolve(P1,[1, 0 ,-1]) Q = Q1 else: # Even order P, r = deconvolve(P1, [1, -1]) Q, r = deconvolve(Q1, [1, 1]) rP = numpy.roots(P) rQ = numpy.roots(Q) aP = numpy.angle(rP[1::2]) aQ = numpy.angle(rQ[1::2]) lsf = sorted(numpy.concatenate((-aP,-aQ))) return lsf
def poly2lsf(a): """Prediction polynomial to line spectral frequencies. converts the prediction polynomial specified by A, into the corresponding line spectral frequencies, LSF. normalizes the prediction polynomial by A(1). .. doctest:: >>> from spectrum import poly2lsf >>> a = [1.0000, 0.6149, 0.9899, 0.0000 ,0.0031, -0.0082] >>> lsf = poly2lsf(a) >>> lsf = array([0.7842, 1.5605, 1.8776, 1.8984, 2.3593]) .. seealso:: lsf2poly, poly2rc, poly2qc, rc2is """ #Line spectral frequencies are not defined for complex polynomials. # Normalize the polynomial a = numpy.array(a) if a[0] != 1: a /= a[0] if max(numpy.abs(numpy.roots(a))) >= 1.0: error('The polynomial must have all roots inside of the unit circle.') # Form the sum and differnce filters p = len(a) - 1 # The leading one in the polynomial is not used a1 = numpy.concatenate((a, numpy.array([0]))) a2 = a1[-1::-1] P1 = a1 - a2 # Difference filter Q1 = a1 + a2 # Sum Filter # If order is even, remove the known root at z = 1 for P1 and z = -1 for Q1 # If odd, remove both the roots from P1 if p % 2: # Odd order P, r = deconvolve(P1, [1, 0, -1]) Q = Q1 else: # Even order P, r = deconvolve(P1, [1, -1]) Q, r = deconvolve(Q1, [1, 1]) rP = numpy.roots(P) rQ = numpy.roots(Q) aP = numpy.angle(rP[1::2]) aQ = numpy.angle(rQ[1::2]) lsf = sorted(numpy.concatenate((-aP, -aQ))) return lsf
def get_irf( a=2, b=10, resolution=1, ): my_x = np.linspace(0,100,100*resolution) my_y = stats.beta.pdf(my_x/100, a, b) my_z = np.linspace(0,0,100*resolution) my_z[:20*resolution]=1 irf = signal.deconvolve(my_y, my_z)[1] block_response = signal.convolve(irf,my_z) basis_function = signal.deconvolve(block_response, my_z)[1] #should be equal to irf return irf/irf.sum()
def deconvolve_padded(inpvec, convvec): ninp = len(inpvec) nconv = len(convvec) padinpvec = zeros(len(inpvec) + len(convvec)) * 0j padinpvec[nconv / 2:nconv / 2 + ninp] = inpvec[:] deconvvec = deconvolve(padinpvec, convvec) return deconvvec
def xas_deconvolve(energy, norm=None, group=None, form='gaussian', esigma=1.0, eshift=0.0, _larch=None): """XAS spectral deconvolution This function de-convolves a normalized mu(E) spectra with a peak shape, enhancing separation of XANES features. This can be unstable -- Use results with caution! Arguments ---------- energy: array of x-ray energies, in eV or group norm: array of normalized mu(E) group: output group form: form of deconvolution function. One of 'gaussian' (default) or 'lorentzian' esigma energy sigma to pass to gaussian() or lorentzian() [in eV, default=1.0] eshift energy shift to apply to result. [in eV, default=0] Returns ------- None The array 'deconv' will be written to the output group. Notes ----- Support See First Argument Group convention, requiring group members 'energy' and 'norm' """ if _larch is None: raise Warning("cannot deconvolve -- larch broken?") energy, mu, group = parse_group_args(energy, members=('energy', 'norm'), defaults=(norm,), group=group, fcn_name='xas_deconv') eshift = eshift + 0.5 * esigma en = remove_dups(energy) en = en - en[0] estep = max(0.001, 0.001*int(min(en[1:]-en[:-1])*1000.0)) npts = 1 + int(max(en) / estep) x = np.arange(npts)*estep y = _interp(en, mu, x, kind='linear', _larch=_larch) kernel = gaussian if form.lower().startswith('lor'): kernel = lorentzian yext = np.concatenate((y, np.arange(len(y))*y[-1])) ret, err = deconvolve(yext, kernel(x, 0, esigma)) nret = min(len(x), len(ret)) ret = ret[:nret]*yext[nret-1]/ret[nret-1] out = _interp(x+eshift, ret, en, kind='linear', _larch=_larch) group = set_xafsGroup(group, _larch=_larch) group.deconv = out
def range_deconv(data, matched_code): print("\nRange deconvolution starts ...") range_recover = np.zeros((EFFECTIVE_LENGTH, data.shape[1]), dtype='complex') for i in tqdm(range(data.shape[1])): range_recover[:, i] = deconvolve(data[:, i], matched_code) print("Range deconvolution finished!") return range_recover
def _deconvoleSignals(self): """ Deconvolves the generator signal from the microphone signal, removing the effects of the generator on the signal. """ self.logger.debug("Entering _deconvoleSignals") self.system_response = deconvolve(self.microphone_response,self.generator_response) # deconvolve function returns a tuple (remainder, deconvolved signal) self.system_response = ifft(fft(self.microphone_response) / fft(self.generator_response))
def poly2lsf(a): a = a / a[0] A = np.r_[a, 0.0] B = A[::-1] P = A - B Q = A + B P = deconvolve(P, np.array([1.0, -1.0]))[0] Q = deconvolve(Q, np.array([1.0, 1.0]))[0] roots_P = np.roots(P) roots_Q = np.roots(Q) angles_P = np.angle(roots_P[::2]) angles_Q = np.angle(roots_Q[::2]) angles_P[angles_P < 0.0] += np.pi angles_Q[angles_Q < 0.0] += np.pi lsf = np.sort(np.r_[angles_P, angles_Q]) return lsf
def magabs_cs2(): #b.line_plot("magabs_cs", a.frequency*1e-9, a.MagAbs[:, 362]) #b.line_plot("magabs_cs", a.frequency*1e-9, a.MagAbs[:, 0]) bg=mean(a.Magcom[:, 0:50], axis=1) from scipy.signal import deconvolve from numpy import polydiv #from numpy import print shape(deconvolve(a.Magcom[:, 362], bg)[1]) print shape(a.frequency[1:]*1e-9) b.line_plot("magabs_sub", a.frequency[:]*1e-9, absolute(mean(a.Magcom[:, 342:382], axis=1))-absolute(bg)+0.02)#a.MagAbs[756:1093, 0])+0.02)
def deconvolveWFM(wfm, impulse_resp, plot=True): padL = len(impulse_resp) - 1 wfm = np.pad(wfm, (0, padL), 'constant') precomp, remainder = signal.deconvolve(wfm, impulse_resp) if plot: plt.figure() plt.plot(wfm) plt.plot(precomp) plt.xlabel('Sample') plt.ylabel('Amplitude') return precomp, remainder
def magabs_cs2(): #b.line_plot("magabs_cs", a.frequency*1e-9, a.MagAbs[:, 362]) #b.line_plot("magabs_cs", a.frequency*1e-9, a.MagAbs[:, 0]) bg = mean(a.Magcom[:, 0:50], axis=1) from scipy.signal import deconvolve from numpy import polydiv #from numpy import print shape(deconvolve(a.Magcom[:, 362], bg)[1]) print shape(a.frequency[1:] * 1e-9) b.line_plot("magabs_sub", a.frequency[:] * 1e-9, absolute(mean(a.Magcom[:, 342:382], axis=1)) - absolute(bg) + 0.02) #a.MagAbs[756:1093, 0])+0.02)
def deconvolution_harmonic(sig1k, sig1kresp, sig1kh, plot_figure=True): deconvolved1kq, deconvolved1kr, = deconvolve(sig1k[1::], sig1kresp[1::]) #convolve() if plot_figure is True: figd = plt.figure() axd = figd.add_subplot(1, 1, 1) axd.plot(t[:-1:], deconvolved1kr, label='original') axd.axis([T-1, T,]) axd.xlim(0, 0.2) figd.legend() figd.show()
def ma_infinity(phi, theta, Q_long): t = [theta] if np.isscalar(theta) else theta.flatten() ph = phi.flatten() Q = len(t) # MA order P = len(ph) # AR order i1 = np.array([1.0, *t, *np.zeros(Q_long + P + Q)]) i2 = np.array([1.0, *(-ph)]) theta_inf = sps.deconvolve(i1, i2)[0] theta_inf = theta_inf[1:Q_long + 1] return theta_inf
def haroldpolydiv(dividend, divisor): """ Polynomial division wrapped around scipy deconvolve function. Takes two arguments and divides the first by the second. Returns, two arguments: the factor and the remainder, both passed through a left zeros trimming function. """ h_factor, h_remainder = (np.trim_zeros(x, 'f') for x in deconvolve(dividend, divisor)) return h_factor, h_remainder
def deblur(img): gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY).astype(np.float) kernel = np.array([1, 2, 4, 2, 1]) output = np.zeros((gray.shape[0], gray.shape[1] - len(kernel) + 1)) for idx in range(gray.shape[0]): line = gray[idx, :] res, remainder = deconvolve(line, kernel) output[idx, :] = res plt.figure() plt.plot(gray[-1, :-1] - gray[-1, 1:]) plt.figure() plt.plot(gray[0, :-1] - gray[0, 1:]) plt.show() return output
def postprocess(g=None, vt=None): length = g.shape[0] b = np.array([1, -0.99]) w = np.zeros(1) for j in range(length): gj = g[j] vtj = vt[j] if vtj[0] != 0: vtinvj = dsp.deconvolve([1, 0, 0, 0, 0, 0, 0, 0], vtj)[0] dgj = dsp.lfilter(b, 1, gj) z = dsp.lfilter(vtinvj, 1, dgj) else: z = np.zeros(256) w = np.append(w, z) return w
def test_deconv_for_dummies(): """ I am an idiot who does not understand convolution and deconvolution. This test function does things in a very simple manner so I can get to understand them. This is how it should, in principal, work. However, division by zero kills the scipy function, so I will need my own... """ print '' # Simple signal sig = np.array([1., 1., 1., 1., 1., 2., 2., 2., 2.,]) # Simple filter filt = np.array([2., 2., 1., 1., 1., 1., 1., 1., 1.]) # Convolving the signal with the filter... res = signal.convolve(sig, filt) # quotient and remainder after deconvolution q, r = signal.deconvolve(res, filt) # Still don't have much intuition, but deconvolution removes the filter... # i.e. filter = quotient print 'signal ', sig print 'filter ', filt print 'convolution result ', res print 'deconvolving the filter from the result: quotient ', q print 'deconvolving the filter from the result: remainder ', r # Can we get the filter from the signal and the result of the convolution? q1, r1 = signal.deconvolve(res, sig) print 'Can we get the filter from the signal and the result of the convolution?' print 'q1, r1 = signal.deconvolve(res, sig)' print 'q1 = filter ', q1 print 'r1 = remainder ', r1 print 'Yes! The quotient is now the value I put in for the filter!'
def lpc_to_lsf(lpc_polynomial): """This code is inspired by the following: https://uk.mathworks.com/help/dsp/ref/lpctolsflspconversion.html Args: lpc_polynomial (list): length n + 1 list of lpc coefficients. Requirements is that the polynomial is ordered so that lpc_polynomial[0] == 1 Returns: list: length n list of line spectral frequencies. """ lpc_polynomial = np.array(lpc_polynomial) assert lpc_polynomial[0] == 1, \ 'First value in the polynomial must be 1. Considering normalizing.' assert max(np.abs(np.roots(lpc_polynomial))) <= 1.0, \ 'The polynomial must have all roots inside of the unit circle.' lhs = np.concatenate((lpc_polynomial, np.array([0]))) rhs = lhs[-1::-1] diff_filter = lhs - rhs sum_filter = lhs + rhs poly_1 = deconvolve(diff_filter, [1, 0, -1])[0] if (len(lpc_polynomial) - 1) % 2 else deconvolve(diff_filter, [1, -1])[0] poly_2 = sum_filter if (len(lpc_polynomial) - 1) % 2 else deconvolve(sum_filter, [1, 1])[0] roots_poly1 = np.roots(poly_1) roots_poly2 = np.roots(poly_2) angles_poly1 = np.angle(roots_poly1[1::2]) angles_poly2 = np.angle(roots_poly2[1::2]) return sorted( np.concatenate((-angles_poly1, -angles_poly2)) )
def prepare_gs(A_matrix, b_vector, S_matrix, s_vector, N, xs): num_states = len(A_matrix[0]) padded_N = 2 * N - 1 zero_pads = np.zeros(N - 1) kronecker_delt = np.asarray([1] + ([0] * (N - 1))) L = np.empty((padded_N, num_states, 1), dtype=complex) # FFT of the reciprocal of z # step: create reciprocals for i in range(num_states): temp_v = kronecker_delt - (A_matrix[i, i] * S_matrix[i, i].pmf(xs)) reciprocal, _ = deconvolve(np.append(kronecker_delt, zero_pads), temp_v) L[:, i, 0] = pfftw(np.append(reciprocal, zero_pads)) # FFT of reciprocals # step: deconv entire problem matrix and vector C_matrix = np.empty((padded_N, num_states, num_states), dtype=complex) # FFT matrix d_vector = np.empty((padded_N, num_states, 1), dtype=complex) # FFT vector for i in range(num_states): d_vector[:, i, 0] = pfftw( np.append(b_vector[i] * s_vector[i].pmf(xs), zero_pads)) for j in range(num_states): if i == j: C_matrix[:, i, j] = pfftw(np.zeros(padded_N)) continue C_matrix[:, i, j] = pfftw(np.append(S_matrix[i, j].pmf(xs), zero_pads)) K_matrix = A_matrix * C_matrix * L kappa_vector = d_vector * L # step: zeropad H matrix for i in range(num_states): temp_v = ipfftw(kappa_vector[:, i, 0]) temp_v[N:] = 0 kappa_vector[:, i, 0] = pfftw(temp_v) for j in range(num_states): temp_v = ipfftw(K_matrix[:, i, j]) temp_v[N:] = 0 K_matrix[:, i, j] = pfftw(temp_v) return K_matrix, kappa_vector
def deconvolve_signal(L: scipysig.dlti, x: np.ndarray) -> np.ndarray: """Deconvolve a signal x using a specified transfer function L(z) Parameters ---------- L : scipy.signal.dlti Discrete-time rational transfer function used to deconvolve the signal x : np.ndarray Signal to deconvolve Returns ------- signal : np.ndarray Deconvolved signal """ dt = L.dt impulse = scipysig.dimpulse(L)[1][0].flatten() idx1 = np.argwhere(impulse != 0)[0].item() idx2 = np.argwhere(np.isclose(impulse[idx1:], 0.) == True) idx2 = -1 if idx2.size == 0 else idx2[0].item() signal, _ = scipysig.deconvolve(x, impulse[idx1:idx2]) return signal[np.argwhere(impulse != 0)[0].item():]
def do_continuous_deconv(data, kernel, times): from scipy.signal import deconvolve # zero padding kernel_nsamp = np.round(kernel.shape[-1]).astype(int) zeropad = np.zeros(data.shape[:-1] + (kernel_nsamp, )) zeropadded = np.c_[zeropad, data, zeropad] print(' Continuous deconvolution...') len_deconv = zeropadded.shape[-1] - kernel_nsamp + 1 times = times[:len_deconv - 2 * kernel_nsamp] # no zeropad deconvolved = np.full(zeropadded.shape[:-1] + (len_deconv, ), np.inf) # do deconvolution for _trial in range(zeropadded.shape[0]): for _gap in range(zeropadded.shape[1]): for _attn in range(zeropadded.shape[2]): for _band in range(zeropadded.shape[3]): signal = zeropadded[_trial, _gap, _attn, _band, :] (deconvolved[_trial, _gap, _attn, _band, :], _) = deconvolve(signal, kernel) assert np.all(np.isfinite(deconvolved)) # remove zero padding deconvolved = deconvolved[:, :, :, :, kernel_nsamp:-kernel_nsamp] return deconvolved, times
def smart_sub_shit(mask, div_length): max_val = max(mask) divisor = [] quotient = [] subdivisor = [] found_divisor = False for submask in itertools.product(range(-max_val, max_val)[::-1], repeat=div_length): if not submask[0]: continue quotient, remainder = deconvolve(mask, submask) if sum(abs(remainder)) == 0: divisor = submask found_divisor = True break if not found_divisor: if div_length < len(mask)-1: smart_sub_shit(mask, div_length+1) else: print("Divisor {} can't be split anymore.".format(mask)) else: if len(quotient) > 2: subdivisor = smart_sub_shit(quotient.astype("int"), 2) return {"Quotient": quotient, "Divisor": list(divisor), "Subdivisor": subdivisor}
def haroldpolydiv(dividend, divisor): """ Polynomial division wrapped around :func:`scipy.signal.deconvolve` function. Takes two arguments and divides the first by the second. Parameters ---------- dividend : (n,) array_like The polynomial to be divided divisor : (m,) array_like The polynomial that divides Returns ------- factor : ndarray The resulting polynomial coeffients of the factor remainder : ndarray The resulting polynomial coefficients of the remainder Examples -------- >>> a = np.array([2, 3, 4 ,6]) >>> b = np.array([1, 3, 6]) >>> haroldpolydiv(a, b) (array([ 2., -3.]), array([ 1., 24.])) >>> c = np.array([1, 3, 3, 1]) >>> d = np.array([1, 2, 1]) >>> haroldpolydiv(c, d) (array([1., 1.]), array([], dtype=float64)) """ h_factor, h_remainder = (np.trim_zeros(x, 'f') for x in deconvolve(dividend, divisor)) return h_factor, h_remainder
def haroldpolydiv(dividend, divisor): """ Polynomial division wrapped around :func:`scipy.signal.deconvolve` function. Takes two arguments and divides the first by the second. Parameters ---------- dividend : (n,) array_like The polynomial to be divided divisor : (m,) array_like The polynomial that divides Returns ------- factor : ndarray The resulting polynomial coeffients of the factor remainder : ndarray The resulting polynomial coefficients of the remainder Examples -------- >>> a = np.array([2, 3, 4 ,6]) >>> b = np.array([1, 3, 6]) >>> haroldpolydiv(a, b) (array([ 2., -3.]), array([ 1., 24.])) >>> c = np.array([1, 3, 3, 1]) >>> d = np.array([1, 2, 1]) >>> haroldpolydiv(c, d) (array([1., 1.]), array([], dtype=float64)) """ h_factor, h_remainder = (np.trim_zeros(x, 'f') for x in sig.deconvolve(dividend, divisor)) return h_factor, h_remainder
rhohat4a, cov_x4a, infodict, mesg, ier = arest02.fit((0,0,2)) print rhohat4a print cov_x4a err4a = arest02.errfn(x=y4) print np.var(err4a) sige = np.sqrt(np.dot(err4a,err4a)/nsample) print sige print sige * np.sqrt(np.diag(cov_x4a)) print np.sqrt(np.diag(cov_x4a)) print arest02.rhoy print arest02.rhoe print "true" print ar print ma import gwstatsmodels.api as sm print sm.regression.yule_walker(y4, order=2, method='mle', inv=True) import matplotlib.pyplot as plt plt.plot(arest2.forecast()[-100:]) #plt.show() ar1, ar2 = ([1, -0.4], [1, 0.5]) ar2 = [1, -1] lagpolyproduct = np.convolve(ar1, ar2) print deconvolve(lagpolyproduct, ar2, n=None) print signal.deconvolve(lagpolyproduct, ar2) print deconvolve(lagpolyproduct, ar2, n=10)
xx_arr = screen_meas00.x gauss = np.exp(-(xx_arr-np.mean(xx_arr))**2/(2*beamsize**2))/(np.sqrt(2*np.pi)*beamsize) mask_gauss = gauss>0.2*gauss.max() cut_gauss = gauss[mask_gauss] cut_xx = xx_arr[mask_gauss] cut_gauss /= cut_gauss.sum() convoluted_screen = convolve(screen_meas00.intensity, cut_gauss) diff = np.diff(xx_arr).mean() convolved_xx = np.arange(0, len(convoluted_screen)*diff, diff) - len(cut_gauss)/2*diff zero_arr = np.zeros([len(cut_gauss)//2]) intensity_padded = np.concatenate([zero_arr, screen_meas.intensity, zero_arr[:-1]]) deconvolved, remainder = deconvolve(intensity_padded, cut_gauss) #random_distortion = np.random.randn(len(convoluted_screen))*0.01*convoluted_screen.max() #random_distortion = 0 #deconvolved, remainder = deconvolve(intensity_padded, cut_gauss) screen_decon = tracking.ScreenDistribution(screen_meas.x, deconvolved) ms.figure('Screens') subplot = ms.subplot_factory(2,2) sp_ctr = 1 sp0 = subplot(1, title='Screens')
step[len(step)/2:] = 1.0 plt.figure() plt.plot(x_times, step, color='black', label='Input step fn') # Convolve step function with impulse response step_response = np.convolve(h(h_times, tau), step, mode='valid') plt.plot(h_times, step_response[:-1]/step_response.max(), color='green', marker='.', label='system response') plt.xlabel('t') plt.ylabel('h(t)') plt.title('Response to Step Function') # Deconvolve to recover original signal h_array = h(h_times, tau) # Pad step_response(t) to be longer than h(t) step_response = np.concatenate([np.zeros(len(h_times)), step_response, np.ones(len(h_times))]) x_reconstructed = signal.deconvolve(step_response, h_array) x_len = len(step_response) - len(h_times) + 1 x_reconstructed_times = np.arange(-dt*x_len/2, dt*(x_len/2-1), dt) plt.plot(x_reconstructed_times, x_reconstructed[0][:-1], color='blue', ls='', marker='s', label='recovered step fn') plt.legend(loc='best') plt.show()
def estimate_drivers(t_gsr, gsr, T1=0.75, T2=2, MX=1, DELTA_PEAK=0.02, FS=None, k_near=5, grid_size=5, s=0.2): """ TIME_DRV, DRV, PH_DRV, TN_DRV = estimate_drivers(TIME_GSR, GSR, T1, T2, MX, DELTA_PEAK): Estimates the various driving components of a GSR signal. The IRF is a bateman function defined by the gen_bateman function. T1, T2, MX and DELTA_PEAK are modificable parameters (optimal 0.75, 2, 1, 0.02) k_near and grid_size are optional parameters, relative to the process s= t in seconds of gaussian smoothing """ if FS==None: FS = 1/( t_gsr[1] - t_gsr[0]) #====================== # step 1: DECONVOLUTION #====================== # generating bateman function and tailored gsr bateman, t_bat, gsr_in = gen_bateman(MX, T1, T2, FS, gsr) L =len(bateman[0:np.argmax(bateman)]) # deconvolution driver, residuals=spy.deconvolve(gsr_in, bateman) driver = driver * FS # gaussian smoothing (s=200 ms) degree = int(np.ceil(s*FS)) driver=smoothGaussian(driver, degree) # generating times t_driver = np.arange(-L/FS, -L/FS+len(driver)/FS, 1/FS) + t_gsr[0] driver = driver[L*2:] t_driver = t_driver[L*2:] #====================== # step 2: IDENTIFICATION OF INTER IMPULSE SECTIONS #====================== # peak detection # we will use "our" peakdet algorithm # DELTA_PEAK = np.median(abs(np.diff(driver, n=2))) max_driv, min_driv_temp = peakdet(driver, DELTA_PEAK) DELTA_MIN = DELTA_PEAK/4 max_driv_temp, min_driv = peakdet(driver, DELTA_MIN) # check alternance algorithm based on mean>0 maxmin=np.zeros(len(driver)) maxmin[(max_driv[:,0]).astype(np.uint32)] = 1 maxmin[(min_driv[:,0]).astype(np.uint32)] = -1 ''' OLD CODE index=2 prev=1 markers= np.zeros(len(driver)) while (index<len(maxmin)): if maxmin[index]==-1: portion = maxmin[prev+1: index-1] if np.mean(portion)<=0: # there is not a maximum between two mins markers[prev] = markers[prev] + 1 markers[index] = markers[index] - 1 prev=index index +=1 start = np.arange(len(markers))[markers==1] end = np.arange(len(markers))[markers==-1] # create array of interimpulse indexes inter_impulse_indexes = np.array([0]) for i in range(len(start)): inter_impulse_indexes = np.r_[inter_impulse_indexes, range(start[i], end[i]+1)] inter_impulse_indexes = np.r_[inter_impulse_indexes, len(markers)-1] ''' inter_impulse_indexes, min_idx=get_interpolation_indexes(max_driv, driver, n=k_near) inter_impulse = driver[inter_impulse_indexes.astype(np.uint16)] t_inter_impulse = t_driver[inter_impulse_indexes.astype(np.uint16)] #====================== # ESTIMATION OF THE TONIC DRIVER #====================== # interpolation with time grid 10s t_inter_impulse_grid = np.arange(t_driver[0], t_driver[-1], grid_size) # estimating values on the time-grid inter_impulse_10=np.array([driver[0]]) for index in range(1, len(t_inter_impulse_grid)-1): ind_start = np.argmin(abs(t_inter_impulse - t_inter_impulse_grid[index-1])) ind_end = np.argmin(abs(t_inter_impulse - t_inter_impulse_grid[index+1])) if ind_end>ind_start: value=np.mean(inter_impulse[ind_start:ind_end]) else: value=inter_impulse[ind_start] inter_impulse_10 = np.r_[inter_impulse_10, value] inter_impulse_10 = np.r_[inter_impulse_10, np.mean(inter_impulse[ind_end:])] t_inter_impulse_grid = np.r_[t_inter_impulse_grid , t_driver[-1]] inter_impulse_10 = np.r_[inter_impulse_10, driver[-1]] f = interp1d(t_inter_impulse_grid, inter_impulse_10, kind='cubic') tonic_driver = f(t_driver) phasic_driver = driver - tonic_driver return t_driver, driver, phasic_driver, tonic_driver
rhohat4a, cov_x4a, infodict, mesg, ier = arest02.fit((0,0,2)) print rhohat4a print cov_x4a err4a = arest02.errfn(x=y4) print np.var(err4a) sige = np.sqrt(np.dot(err4a,err4a)/nsample) print sige print sige * np.sqrt(np.diag(cov_x4a)) print np.sqrt(np.diag(cov_x4a)) print arest02.rhoy print arest02.rhoe print "true" print ar print ma import statsmodels.api as sm print sm.regression.yule_walker(y4, order=2, method='mle', inv=True) import matplotlib.pyplot as plt plt.plot(arest2.forecast()[-100:]) #plt.show() ar1, ar2 = ([1, -0.4], [1, 0.5]) ar2 = [1, -1] lagpolyproduct = np.convolve(ar1, ar2) print deconvolve(lagpolyproduct, ar2, n=None) print signal.deconvolve(lagpolyproduct, ar2) print deconvolve(lagpolyproduct, ar2, n=10)
zscores_zeropadded = np.c_[zeropad, zscores_downsamp, zeropad] print(' Continuous deconvolution...') len_deconv = zscores_zeropadded.shape[-1] - ksamp + 1 t_cont = t_downsamp[:len_deconv - 2 * ksamp] # don't zeropad fit_continuous_deconv = np.empty(zscores_zeropadded.shape[:-1] + (len_deconv,)) fit_continuous_deconv[:] = np.inf for _trial in range(zscores_zeropadded.shape[0]): for _gap in range(zscores_zeropadded.shape[1]): for _attn in range(zscores_zeropadded.shape[2]): for _band in range(zscores_zeropadded.shape[3]): signal = zscores_zeropadded[_trial, _gap, _attn, _band, :] (fit_continuous_deconv[_trial, _gap, _attn, _band, :], _) = ss.deconvolve(signal, kernel_downsamp) assert not np.any(fit_continuous_deconv == np.inf) # remove zero padding fit_continuous_deconv = fit_continuous_deconv[:, :, :, :, ksamp:-ksamp] # combine results from different kernels kernel_fits.append(fits_structured) kernel_zscores.append(zscores_structured) if run_continuous_deconv: kernel_fits_continuous.append(fit_continuous_deconv) # finish subject fits.append(kernel_fits) zscores.append(kernel_zscores) if run_continuous_deconv: fits_continuous.append(kernel_fits_continuous) print(' Done: {} sec.'.format(str(round(time.time() - t0, 1))))
else: #ToDo: automatic parameters choice when None is recognized print( "Functionality in development, please enter parameters manually.\n" ) params = [2.2, 0.91] #default parameters ker = kernel_selection('lognormal', params, time) #default kernel # ToDo: initial+final ramp, which will change dim_t # decovolution script twoseconds = int(round(2 / t_step)) # index corresponding to 2s newdim_t = dim_t - twoseconds + 1 # deconvolution samples : n-m+1 activities = np.zeros((newdim_t, dim_x, dim_y)) for i in range(dim_x): for j in range(dim_y): activities[:, i, j] = deconvolve(imgseq_array[:, i, j], ker[:twoseconds])[0] #ToDo: add kernel plot and original/processed normalized plot """ Old method, it doesn't work properly (the ouput block doesn't work as input for the other pipeline modules) # re-converting into analogsignal signal = activity.reshape((dim_t, dim_x * dim_y)) asig = block.segments[0].analogsignals[0].duplicate_with_new_data(signal) asig.array_annotate(**block.segments[0].analogsignals[0].array_annotations) asig.name += "" asig.description += "Deconvoluted activity using the given {} kernel"\ .format(args.kernel) block.segments[0].analogsignals[0] = asig """
def xas_deconvolve(energy, norm=None, group=None, form='lorentzian', esigma=1.0, eshift=0.0, smooth=True, sgwindow=None, sgorder=3, _larch=None): """XAS spectral deconvolution de-convolve a normalized mu(E) spectra with a peak shape, enhancing the intensity and separation of peaks of a XANES spectrum. The results can be unstable, and noisy, and should be used with caution! Arguments ---------- energy: array of x-ray energies (in eV) or XAFS data group norm: array of normalized mu(E) group: output group form: functional form of deconvolution function. One of 'gaussian' or 'lorentzian' [default] esigma energy sigma to pass to gaussian() or lorentzian() [in eV, default=1.0] eshift energy shift to apply to result. [in eV, default=0] smooth whether to smooth result with savitzky_golay method [True] sgwindow window size for savitzky_golay [found from data step and esigma] sgorder order for savitzky_golay [3] Returns ------- None The array 'deconv' will be written to the output group. Notes ----- Support See First Argument Group convention, requiring group members 'energy' and 'norm' Smoothing with savitzky_golay() requires a window and order. By default, window = int(esigma / estep) where estep is step size for the gridded data, approximately the finest energy step in the data. """ energy, mu, group = parse_group_args(energy, members=('energy', 'norm'), defaults=(norm,), group=group, fcn_name='xas_deconvolve') eshift = eshift + 0.5 * esigma en = remove_dups(energy) en = en - en[0] estep = max(0.001, 0.001*int(min(en[1:]-en[:-1])*1000.0)) npts = 1 + int(max(en) / estep) x = np.arange(npts)*estep y = interp(en, mu, x, kind='cubic') kernel = lorentzian if form.lower().startswith('g'): kernel = gaussian yext = np.concatenate((y, np.arange(len(y))*y[-1])) ret, err = deconvolve(yext, kernel(x, center=0, sigma=esigma)) nret = min(len(x), len(ret)) ret = ret[:nret]*yext[nret-1]/ret[nret-1] if smooth: if sgwindow is None: sgwindow = int(1.0*esigma/estep) sqwindow = int(sgwindow) if sgwindow < (sgorder+1): sgwindow = sgorder + 2 if sgwindow % 2 == 0: sgwindow += 1 ret = savitzky_golay(ret, sgwindow, sgorder) out = interp(x+eshift, ret, en, kind='cubic') group = set_xafsGroup(group, _larch=_larch) group.deconv = out
def pmtx(mtx): print ('\n'.join(''.join(' %4s' % col for col in row) for row in mtx)) def convolve(f, h): g = [0] * (len(f) + len(h) - 1) for hindex, hval in enumerate(h): for findex, fval in enumerate(f): g[hindex + findex] += fval * hval return g def deconvolve(g, f): lenh = len(g) - len(f) + 1 mtx = [[0 for x in range(lenh+1)] for y in g] for hindex in range(lenh): for findex, fval in enumerate(f): gindex = hindex + findex mtx[gindex][hindex] = fval for gindex, gval in enumerate(g): mtx[gindex][lenh] = gval ToReducedRowEchelonForm( mtx ) return [mtx[i][lenh] for i in range(lenh)] # h h = [-8,-9,-3,-1,-6,7] f = [-3,-6,-1,8,-6,3,-1,-9,-9,3,-2,5,2,-2,-7,-1] g = [24,75,71,-34,3,22,-45,23,245,25,52,25,-67,-96,96,31,55,36,29,-43,-7] print h print f print g out =signal.deconvolve(g, f) print out
# orig = np.r_[0, 4, 6, 9, 7, 5, np.zeros(14)] # pmf = poisson.pmf(range(9), b) # plt.plot(pmf) # plt.show() blur = convolve(orig, pmf, mode="full") plt.plot(orig) plt.plot(blur) plt.show() # http://freerangestats.info/blog/2020/07/18/victoria-r-convolution def deconv(observed, kernel): k = len(kernel) padded = np.r_[np.zeros(k), observed] def error(x): return sum(convolve(x, kernel, mode="same")[:len(padded)] - padded)**2 res = minimize(error, np.r_[observed, np.zeros(k)], method="L-BFGS-B") return res.x I_deconv, _ = deconvolve(obs, pmf) # plt.plot(orig, label = "original") plt.plot(obs, label="observed") plt.plot(I_deconv, label="deconvolved") plt.legend() plt.show()
cv2.waitKey(0) cv2.destroyAllWindows() img = cv2.imread("ronaldo.jpg") cv2.imshow('img1', img) cv2.waitKey(0) cv2.destroyAllWindows() dimen = (np.shape(img)) print(dimen) imgkey2 = cv2.resize(imgkey, (dimen[1], dimen[0])) cv2.imshow('img2', (imgkey2)) cv2.waitKey(0) cv2.destroyAllWindows() imgkeystraight = np.reshape(imgkey2, (np.size(img))) sign = np.reshape(img, (np.size(img))) encrypimg = np.int32(sign) * imgkeystraight key = np.int32(10 * np.sin(np.linspace(1, 10, 10))) newsig = sig.convolve(key, encrypimg) print("Start") recoveredsig, remain = sig.deconvolve(newsig, key) print("End") cv2.imshow( 'img3', np.reshape(np.uint8(np.divide(recoveredsig, imgkeystraight)), dimen)) cv2.waitKey(0) cv2.destroyAllWindows()
TS = np.arange(T / ws) * ws FS = np.arange(fs * ws) / ws FS, TS = np.meshgrid(FS, TS) fig1 = plt.figure() ax1 = fig1.add_subplot(111, projection="3d") ax1.plot_surface(TS, FS, np.abs(SIG1KRESPS), cmap="autumn_r", lw=0.5, rstride=1, cstride=1) ax1.azim = -90 # } set axit to top view ax1.elev = 90 # } # F = np.arange(-fs / 2, fs / 2, 1 / T) F = np.arange(0, fs, 1 / T) #from scipy import fftpack #from scipy import signal deconvolved1kq, deconvolved1kr,= deconvolve(sig1k[1::], sig1kresp[1::]) #convolve() plt.figure() plt.plot(t, sig1k, t, sig1kresp) #plt.axis([T-1, T,]) plt.xlim(0, 0.2) plt.figure() plt.plot(F, np.abs(SIG1K), F, np.abs(SIG1KRESP)) plt.xlim(0, fs / 2) #plt.figure() #plt.plot(t[:-1:], deconvolved1kr) #plt.axis([T-1, T,])
CI=CI, smoothing=smooth, totals=False) (dates_D, Rt_D, Rtu_D, Rtl_D, *_) = analytical_MPVS(df[state][:, "delta", "deceased"], CI=CI, smoothing=smooth, totals=False) plt.Rt(dates_I, Rt_I, Rtu_I, Rtl_I, CI)\ .title(f"{state} - $R_t(I)$ estimator") plt.figure() plt.Rt(dates_D, Rt_D, Rtu_D, Rtl_D, CI)\ .title(f"{state} - $R_t(D)$ estimator") plt.show() KA_dD = df["KA"][:, "delta", "deceased"] KA_D = KA_dD.cumsum() L = 14 dist = Exponential(scale=1 / L) pmf = dist.pdf(np.linspace(dist.ppf(0.005), dist.ppf(1 - 0.005))) pmf /= pmf.sum() D_deconv, _ = deconvolve(KA_D, pmf) D_deconv *= 1 / 0.02 plt.plot(KA_D.values.clip(0.1), label="deaths") plt.plot(D_deconv.clip(0.1), label="deconv") plt.legend() # plt.semilogy() plt.show()
step_response[:-1] / step_response.max(), color='green', marker='.', label='system response') plt.xlabel('t') plt.ylabel('h(t)') plt.title('Response to Step Function') # Deconvolve to recover original signal h_array = h(h_times, tau) # Pad step_response(t) to be longer than h(t) step_response = np.concatenate( [np.zeros(len(h_times)), step_response, np.ones(len(h_times))]) x_reconstructed = signal.deconvolve(step_response, h_array) x_len = len(step_response) - len(h_times) + 1 x_reconstructed_times = np.arange(-dt * x_len / 2, dt * (x_len / 2 - 1), dt) plt.plot(x_reconstructed_times, x_reconstructed[0][:-1], color='blue', ls='', marker='s', label='recovered step fn') plt.legend(loc='best') plt.show()
grad_4_part_1 = (final - current_label) / 1 grad_4_part_3 = layer_3_act grad_4 = grad_4_part_3.T.dot(grad_4_part_1) grad_3_part_1 = grad_4_part_1.dot(w4.T) grad_3_part_2 = d_sigmoid(layer_3) grad_3_part_3 = layer_2_act.reshape((1,4*4)) grad_3 = grad_3_part_3.T.dot(grad_3_part_1 * grad_3_part_2) grad_2_part_1 = (grad_3_part_1* grad_3_part_2).dot(w3.T).reshape((4,4)) grad_2_part_2 = d_sigmoid(layer_2) grad_2_part_3 = layer_1_act grad_2 = signal.convolve2d(grad_2_part_3,(grad_2_part_1 * grad_2_part_2), mode='valid') grad_1_part_1,s = signal.deconvolve( (grad_2_part_1 * grad_2_part_2)[-1,:],w2.T[-1,:]) grad_1_part_2 = d_sigmoid(layer_1) grad_1_part_3 = current_x_data # grad_1 = print (grad_2_part_1 * grad_2_part_2).shape print w2.shape print grad_1_part_2.shape # ----- END CODE -----
dmax = 20.0 # Maximum depth up to which to generate model output d = np.linspace(0,dmax,N) # Depth grid dd = d[1] - d[0] # Depth grid spacing Na = int(da/dd) # Grid point index corresponding to d = da Nb = int(db/dd) # Grid point index corresponding to d = db g_avs = impulse(g,d) # Impulse response function (with averaging applied to remove singularity) M = N # Length of inverse filter response w(R) d_w = np.arange(M)*dd # Depth grid for w(R) Nd = M + N - 1 # Number of output samples needed to obtain the inverse filter response of the required length yd = D0*np.ones(Nd) # Heaviside step function of length Nd # Perform deconvolution of the step function with the averaged impulse response w, remainder = signal.deconvolve(yd,g_avs) w /= dd # Divide by grid spacing to obtain an approxiamtion of the continuous-valued weighting function w2 = back_transform(w,da,db,d) # Obtain the spread-out Bragg peak by deconvolution (cf. Bortfeld & Schlegel (1996), Eq. (B1)) SOBP = dd * signal.convolve(w2,g_avs[::-1]) SOBP = SOBP[N-1:] # Remove boundary effects at the beginning of the convolution output # Figure 1: Illustration of the Bragg peak plt.figure(1) Ncont = 2001 # More finely spaced, 'continuous' grid for plotting exact solution dcont = np.linspace(0,dmax,Ncont) dx_cont = dcont[1] - dcont[0] gx = Bragg_peak(db,dcont) # Exact Bragg peak (Bortfeld & Schlegel, Eq. (3))