def selesnick_hwlet(K, L, min_phase=False): """ Return Selesnick's Hilbert transform wavelet pair (h, g). The parameter K determines the number of zeros at z=-1. The parameter L determines the support of the filter implementing the fractional delay. The length of both scaling filters is 2(K+L). This code is inspired by Selesnick's hwlet.m. """ d = allpass(1 / 2, L) # filter for z^(K+L) S(z) s1 = scipy.special.binom(2 * K, np.arange(2 * K + 1)) s2 = np.convolve(d, d[::-1]) s = np.convolve(s1, s2) # solve convolution system for z^(K+L-1) R(z) A = convmtx(s, 2 * (K + L) - 1) A = A[1::2] b = np.zeros(2 * (K + L) - 1) b[K + L - 1] = 1 r = np.linalg.solve(A, b) # find spectral factor Q(z) and compute filter for z^K F(z) q = sfact(r, min_phase=min_phase) b = scipy.special.binom(K, np.arange(K + 1)) f = np.convolve(q, b) h = np.convolve(f, d) g = np.convolve(f, d[::-1]) # build orthogonal wavelet h = orthogonal_wavelet.from_scaling_filter(signal(h)) g = orthogonal_wavelet.from_scaling_filter(signal(g)) return h, g
def main(argv): for arg in argv: df = pd.read_csv(arg, sep=' ', header=None) tt = df.iloc[23999:, 0] s = df.iloc[23999:, 1] nofiltersig = signal(s, tt, 0) medianfiltersig = signal(s, tt, 1) fig, axs = plt.subplots(2, 2, figsize=(5, 5)) axs[0, 0].plot(tt.values, nofiltersig) axs[1, 0].plot(tt.values, medianfiltersig) Fs = tt.values[1] - tt.values[0] F, Pxx = periodogram(nofiltersig, Fs) axs[0, 1].loglog(F, Pxx, linewidth=0.5) F, Pxx = periodogram(medianfiltersig, Fs) axs[1, 1].loglog(F, Pxx, linewidth=0.5) data = {'Frequence': F, 'Puissance': Pxx} df = pd.DataFrame(data) print(df.to_csv(path_or_buf=None)) plt.show()
def plot_gen_1(freqs, projs, time, sim_vars): """ Generates publication ready plots using compressive atomic sensing """ signal = pulse_gen(sim_vars["sig_freq"], tau=[sim_vars["tau"]], amp=sim_vars["sig_amp"], nte=[sim_vars["nte"]]) plt.style.use('dark_background') plt.subplot(2, 1, 1) plt.plot(time, signal(time), 'g-') # plt.grid(True) plt.title("Magnetic field signal", fontsize=18) plt.ylim([-1, 1]) plt.ylabel("Amplitude (G)", fontsize=14) plt.ticklabel_format(style='sci', axis='y', scilimits=(0, 3)) plt.xlabel("Time (s)", fontsize=14) plt.subplot(2, 1, 2) #plt.stem(freqs, projs, linewidth=0.6, bottom=np.mean(projs)) plt.plot( freqs, projs, 'o-', linewidth=0.6, alpha=0.3, ) plt.ylabel("$|\langle 1 | \psi(t) \\rangle |^2 $", fontsize=16) plt.xlabel("Tuning frequency (Hz)", fontsize=14) plt.savefig("original.png", dpi=1500) # plt.grid(True) plt.show()
def plot_gen_2(freqs, projs, comp_f, comp_p, time, recon, sim_vars, savefig=False): """ Generates publication ready plots using compressive atomic sensing """ # generate original signal signal = pulse_gen(sim_vars["sig_freq"], tau=[sim_vars["tau"]], amp=sim_vars["sig_amp"], nte=[sim_vars["nte"]]) signal = signal(time) signal /= np.max(np.abs(signal)) recon /= np.max(np.abs(recon)) # format projections to expectation value projs = 2 * projs - 1 comp_p = 2 * comp_p - 1 # get number of measuremens used measurements = sim_vars["measurements"] #plt.style.use('dark_background') plt.subplot(2, 1, 1) plt.plot(time, signal, 'g-') plt.plot(time, recon, 'r-') plt.legend(["Original", "Reconstruction"]) plt.title( "Magnetic field signal reconstruction with {} Fourier measurements using \"{}\" method" .format(measurements, sim_vars["method"]), fontsize=18) plt.ylabel(r"Amplitude", fontsize=14) plt.ticklabel_format(style='sci', axis='y', scilimits=(0, 3)) plt.xlabel("Time (s)", fontsize=14) plt.subplot(2, 1, 2) plt.plot(freqs, projs, 'o-', alpha=0.3, linewidth=0.6, label="_nolegend_") plt.plot(comp_f, comp_p, 'r*', linewidth=1.5, label="Sample frequencies") plt.legend() plt.ylabel(r"$\langle F_z \rangle $", fontsize=16) plt.xlabel("Tuning frequency (Hz)", fontsize=14) plt.figure(num=1, figsize=[12, 9]) if savefig: path = "C:\\Users\\Joshua\\Research\\Uni\\2018\\Project\\Latex\\Proj_Figures\\" # save parameters to text file fig_name = dict_write(path, sim_vars) plt.savefig(path + fig_name + ".png", transparent=True, dpi=800) plt.show()
def fourier(steps, electrodes, amplitude, frequency, time, offset, width, white, pink): xsignal = signal(steps, electrodes, amplitude, frequency, time, offset, 0)[1] xwhitenoise = whitenoise(steps, white)[1] xpinknoise = pinknoise(steps, pink)[1] window = rectangularwindow(steps, width, time)[1] combinedsignal = (xsignal + xwhitenoise + xpinknoise) * window OutputFFT = np.abs(np.fft.rfft(combinedsignal)) / time OutputFrequency = np.linspace(0, (steps / 2 + 1) / time, num=int(steps / 2 + 1)) return OutputFrequency, OutputFFT
def signal_generate(time, sim_vars): """ Generates the neural signal defined by sim_vars """ signal = pulse_gen(sim_vars["sig_freq"], tau=[sim_vars["tau"]], amp=sim_vars["sig_amp"], nte=[sim_vars["nte"]]) signal = signal(time) signal /= np.max(np.abs(signal)) return signal
def test_signal(): import matplotlib.pyplot as plt t1 = np.linspace(50, 5000, 5000) s = signal(t1, bijective=True, **D_SEQ_PARAMS) plt.plot(s, t1) plt.show() eff = np.array([0.9, 1.0, 1.1]) s0 = signal( 100, eff=1.0, # # n_gre=160, # # tr_gre=7.0, # ms a1=4.0 * eff, # deg a2=5.0, # deg # tr_seq': 8000.0, # ms # ti1': 1000.0, # ms # ti2': 3300.0, # ms ta=440.0, # ms tb=1180.0, # ms tc=4140.0, # ms bijective=True) print(s0)
def signal_generate(time, sim_vars): """ Generates the neural signal defined by sim_vars """ # assume single pulse if not defined if "nte" not in sim_vars.keys(): nte = sim_vars["tau"] + 1/sim_vars["sig_freq"] sim_vars["nte"] = nte signal = pulse_gen(sim_vars["sig_freq"], tau=[sim_vars["tau"]], amp=sim_vars["sig_amp"], nte=[sim_vars["nte"]]) signal = signal(time) signal /= np.max(np.abs(signal)) return signal
def main(argv): df = pd.read_csv(argv[0], sep=',', header=None) tt = df.iloc[23999:, 0] s = df.iloc[23999:, 1] nofiltersig = signal(s, tt, 0) medianfiltersig = signal(s, tt, 1) fig, axs = plt.subplots(2, 2, figsize=(5, 5)) axs[0, 0].plot(tt.values, nofiltersig) axs[1, 0].plot(tt.values, medianfiltersig) Fs = tt.values[1] - tt.values[0] F, Pxx = welch(nofiltersig, Fs) axs[0, 1].loglog(F, Pxx, linewidth=0.5) F, Pxx = welch(medianfiltersig, Fs) axs[1, 1].loglog(F, Pxx, linewidth=0.5) data = {'Frequence': F, 'Puissance': Pxx} df = pd.DataFrame(data) df.to_csv(argv[1], index=False)
def min_func(v): return np.sum( (PSD[1:] - signal(f[1:], v[0], v[1]) - noise(f[1:], v[2]))**2)
def signal(fname,nmin=3,nmax = None): roi = load(fname) sh = roi.shape[1] roi.sort(axis=1) roim = roi[:,:nmin].mean(axis=1) if nmax is None: nmax = nmin S = roi[:,nmin:].sum(axis=1)-roim*(sh-nmin) else: S = roi[:,-nmax:].sum(axis=1)-roim*nmax return(S) for fa,fb in dfiles: FA = signal(fa); FB = signal(fb); # Total signal St = FA+FB namesp = (fa.split(sep=".")[0]).split(sep="/")[-1] Sshape = FA.shape # ~ namefigure = namesp # ~ figure(namefigure,figsize=(16,6)) t = arange(0,Sshape[0])/freqadq # in seconds # Regularise meanS = mean(St) stS = std(St) Sn = (St-meanS)/stS
def sfact(h, min_phase=False, eps=1e-5): """ Return a mid-phase (or min-phase) spectral factorization of the polynomial h of degree 2n; i.e., a polynomial g of degree n such that h(X) = X^n g(X) g(1/X) The min_phase parameter is ignored if h is a complex signal. This code is inspired by Selesnick's sfactM.m and sfact.m. """ assert len(h) % 2 == 1, "Polynomial should have even degree." isreal = np.all(np.isreal(h)) # find roots of original polynomials roots = np.roots(h) # classify roots on unit circle roots_circ = roots[np.abs(np.abs(roots) - 1) < eps] if min_phase and roots_circ: raise NotImplementedError plus_one = np.abs(roots_circ - 1) < eps minus_one = np.abs(roots_circ + 1) < eps others = ~(plus_one | minus_one) pos_angle = (np.angle(roots_circ) > 0) & others neg_angle = (np.angle(roots_circ) < 0) & others num_plus_one = sum(plus_one) num_minus_one = sum(minus_one) num_pos_angle = sum(pos_angle) num_neg_angle = sum(neg_angle) assert num_plus_one % 2 == 0, "The root +1 should appear an even number of times." assert num_minus_one % 2 == 0, "The root -1 should appear an even number of times." assert (num_pos_angle == num_neg_angle ), "Should have as many eigenvalues e^{i phi} as e^{-i phi}." assert num_plus_one + num_minus_one + num_pos_angle + num_neg_angle == len( roots_circ) # collect half the +1's, half the -1's, and the intersperse positive with negative ones (to increase numerical stability) roots_pos_angle = roots_circ[pos_angle] roots_circ = np.r_[roots_pos_angle[::2], 1 / roots_pos_angle[1::2], [+1] * (num_plus_one // 2), [-1] * (num_minus_one // 2), ] # roots inside unit disk roots_int = roots[np.abs(roots) <= 1 - eps] if isreal and not min_phase: pos_imags, reals = scipy.signal.filter_design._cplxreal(roots_int) A1 = np.r_[pos_imags[::2], pos_imags[::2].conj()] A2 = np.r_[pos_imags[1::2], pos_imags[1::2].conj()] imags = np.r_[1 / A1, A2] reals = np.r_[1 / reals[::2], reals[1::2]] roots_int = np.r_[imags, reals] # roots of the spectral factorization roots = np.r_[roots_circ, roots_int] roots = leja(roots) # build corresponding polynomial g = np.poly(roots) if isreal: g = np.real(g) g = g * np.sqrt(h[-1] / (g[0] * g[-1])) if min(g) + max(g) < 0: g = -g # check that g is indeed a spectral factor of h assert signal(h).isclose(signal(np.convolve(g, g[::-1]))) return g
def wiener_filter(t, h, signal='gaussian', noise='flat', return_PSDs=False): """Compute a Wiener-filtered time-series Parameters ---------- t : array_like evenly-sampled time series, length N h : array_like observations at each t signal : str (optional) currently only 'gaussian' is supported noise : str (optional) currently only 'flat' is supported return_PSDs : bool (optional) if True, then return (PSD, P_S, P_N) Returns ------- h_smooth : ndarray a smoothed version of h, length N Notes ----- The Wiener filter operates by fitting a functional form to the PSD:: PSD = P_S + P_N The resulting frequency-space filter is given by:: Phi = P_S / (P_S + P_N) This entire operation is equivalent to a kernel smoothing by a kernel whose Fourier transform is Phi. See Also -------- scipy.signal.wiener : a static (non-adaptive) wiener filter """ if signal != 'gaussian': raise ValueError("only signal='gaussian' is supported") if noise != 'flat': raise ValueError("only noise='flat' is supported") t = np.asarray(t) h = np.asarray(h) if (t.ndim != 1) or (t.shape != h.shape): raise ValueError('t and h must be equal-length 1-dimensional arrays') # compute the PSD of the input N = len(t) Df = 1. / N / (t[1] - t[0]) f = fftpack.ifftshift(Df * (np.arange(N) - N / 2)) H = fftpack.fft(h) PSD = abs(H)**2 # Set up the Wiener filter: # fit a model to the PSD: sum of signal form and noise form signal = lambda x, A, width: A * np.exp(-0.5 * (x / width)**2) noise = lambda x, n: n * np.ones(x.shape) min_func = lambda v: np.sum( (PSD - signal(f, v[0], v[1]) - noise(f, v[2]))**2) v0 = [5000, 0.1, 10] v = optimize.fmin(min_func, v0) P_S = signal(f, v[0], v[1]) P_N = noise(f, v[2]) Phi = P_S / (P_S + P_N) # Use Phi to filter and smooth the values h_smooth = fftpack.ifft(Phi * H) if not np.iscomplexobj(h): h_smooth = h_smooth.real if return_PSDs: return h_smooth, PSD, P_S, P_N, Phi else: return h_smooth
import elephant.conversion as conv import elephant.kernels as kernels import math import neo import numpy as np import quantities as pq import scipy as sp import scipy.stats import scipy.signal import warnings from bartlett import bartlett # from bartlett.py import the Bartlett"s method function from neo import io from neo.core import SpikeTrain """ We can measure the information rate of spike firing using the following method. First, we estimate the signal from spikes (using the noise-reduction approaches), then we compute noise in the estimated signal (measuring the random errors and removing them), after that we compute the signal-to-noise-ratio of the estimated signal, and finally calcualte lower bound to information rate from the signal-to-noise ratio. """ class AsciiSpikeTrainIO(BaseIO): """ Class for reading/writing SpikeTrains in a text file. Each Spiketrain is a line. """ is_readable = True is_writable = True supported_objects = [Segment, SpikeTrain] readable_objects = [Segment]
def wiener_filter(t, h, signal='gaussian', noise='flat', return_PSDs=False): """Compute a Wiener-filtered time-series Parameters ---------- t : array_like evenly-sampled time series, length N h : array_like observations at each t signal : str (optional) currently only 'gaussian' is supported noise : str (optional) currently only 'flat' is supported return_PSDs : bool (optional) if True, then return (PSD, P_S, P_N) Returns ------- h_smooth : ndarray a smoothed version of h, length N Notes ----- The Wiener filter operates by fitting a functional form to the PSD:: PSD = P_S + P_N The resulting frequency-space filter is given by:: Phi = P_S / (P_S + P_N) This entire operation is equivalent to a kernel smoothing by a kernel whose Fourier transform is Phi. See Also -------- scipy.signal.wiener : a static (non-adaptive) wiener filter """ if signal != 'gaussian': raise ValueError("only signal='gaussian' is supported") if noise != 'flat': raise ValueError("only noise='flat' is supported") t = np.asarray(t) h = np.asarray(h) if (t.ndim != 1) or (t.shape != h.shape): raise ValueError('t and h must be equal-length 1-dimensional arrays') # compute the PSD of the input N = len(t) Df = 1. / N / (t[1] - t[0]) f = fftpack.ifftshift(Df * (np.arange(N) - N / 2)) H = fftpack.fft(h) PSD = abs(H) ** 2 # Set up the Wiener filter: # fit a model to the PSD: sum of signal form and noise form signal = lambda x, A, width: A * np.exp(-0.5 * (x / width) ** 2) noise = lambda x, n: n * np.ones(x.shape) min_func = lambda v: np.sum((PSD - signal(f, v[0], v[1]) - noise(f, v[2])) ** 2) v0 = [5000, 0.1, 10] v = optimize.fmin(min_func, v0) P_S = signal(f, v[0], v[1]) P_N = noise(f, v[2]) Phi = P_S / (P_S + P_N) # Use Phi to filter and smooth the values h_smooth = fftpack.ifft(Phi * H) if not np.iscomplexobj(h): h_smooth = h_smooth.real if return_PSDs: return h_smooth, PSD, P_S, P_N, Phi else: return h_smooth
def compute(doc: PreparedDocument, partition: Partition): return signal(doc, partition).astype(np.float32)
def wiener_filter(t, h, signal='gaussian', noise='flat', return_PSDs=False, signal_params=None, noise_params=None): """Compute a Wiener-filtered time-series Parameters ---------- t : array_like evenly-sampled time series, length N h : array_like observations at each t signal : str (optional) currently only 'gaussian' is supported noise : str (optional) currently only 'flat' is supported return_PSDs : bool (optional) if True, then return (PSD, P_S, P_N) signal_guess : tuple (optional) A starting guess at the parameters for the signal. If not specified, a suitable guess will be estimated from the data itself. (see Notes below) noise_guess : tuple (optional) A starting guess at the parameters for the noise. If not specified, a suitable guess will be estimated from the data itself. (see Notes below) Returns ------- h_smooth : ndarray a smoothed version of h, length N Notes ----- The Wiener filter operates by fitting a functional form to the PSD:: PSD = P_S + P_N The resulting frequency-space filter is given by:: Phi = P_S / (P_S + P_N) This entire operation is equivalent to a kernel smoothing by a kernel whose Fourier transform is Phi. Choosing Signal/Noise Parameters ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ the arguments ``signal_guess`` and ``noise_guess`` specify the initial guess for the characteristics of signal and noise used in the minimization. They are generally expected to be tuples, and the meaning varies depending on the form of signal and noise used. For ``gaussian``, the params are (amplitude, width). For ``flat``, the params are (amplitude,). See Also -------- scipy.signal.wiener : a static (non-adaptive) wiener filter """ # Validate signal if signal != 'gaussian': raise ValueError("only signal='gaussian' is supported") if signal_params is not None and len(signal_params) != 2: raise ValueError("signal_params should be length 2") # Validate noise if noise != 'flat': raise ValueError("only noise='flat' is supported") if noise_params is not None and len(noise_params) != 1: raise ValueError("noise_params should be length 1") # Validate t and hd t = np.asarray(t) h = np.asarray(h) if (t.ndim != 1) or (t.shape != h.shape): raise ValueError('t and h must be equal-length 1-dimensional arrays') # compute the PSD of the input N = len(t) Df = 1. / N / (t[1] - t[0]) f = fftpack.ifftshift(Df * (np.arange(N) - N / 2)) H = fftpack.fft(h) PSD = abs(H) ** 2 # fit signal/noise params if necessary if signal_params is None: amp_guess = np.max(PSD[1:]) width_guess = np.min(np.abs(f[PSD[1:] < np.mean(PSD[1:])])) signal_params = (amp_guess, width_guess) if noise_params is None: noise_params = (np.mean(PSD[1:]),) # Set up the Wiener filter: # fit a model to the PSD: sum of signal form and noise form def signal(x, A, width): width = abs(width) + 1E-99 # prevent divide-by-zero errors return A * np.exp(-0.5 * (x / width) ** 2) def noise(x, n): return n * np.ones(x.shape) # use [1:] here to remove the zero-frequency term: we don't want to # fit to this for data with an offset. min_func = lambda v: np.sum((PSD[1:] - signal(f[1:], v[0], v[1]) - noise(f[1:], v[2])) ** 2) v0 = tuple(signal_params) + tuple(noise_params) v = optimize.fmin(min_func, v0) P_S = signal(f, v[0], v[1]) P_N = noise(f, v[2]) Phi = P_S / (P_S + P_N) Phi[0] = 1 # correct for DC offset # Use Phi to filter and smooth the values h_smooth = fftpack.ifft(Phi * H) if not np.iscomplexobj(h): h_smooth = h_smooth.real if return_PSDs: return h_smooth, PSD, P_S, P_N, Phi else: return h_smooth
def I(t): return signal(t) * np.cos(2 * pi * F_LO * t)
def Q(t): return signal(t) * np.sin(2 * pi * F_LO * t)
def signal_substraction(file, window, samplerate, df, every, result_of_fft): # we asume, that our satellite signals are not contineously received # so we do a substraction on frequency level. the signal intensity of each frequency for the window kernel # is substracted from the overall, longtime average of this frequency. # this allows to see fluctuations better, because they are adding to the average. # so we can find a superposed signal easier this way. threshold = meaning(result_of_fft) signal_lowered = substract(result_of_fft, threshold) print(time.time(), timeit.default_timer()-time_start, "lowering done") v = np.zeros((len(signal_lowered), window)) #w = np.zeros((len(signal_lowered), window)) bandwidth = int(window / (2*2*2*2*2*2*2*2*2)) #print("band", bandwidth*df) for j in range(0, window, bandwidth): #print(j) u = [] for kk in range(len(signal_lowered)): u.append(signal_lowered[kk][j:j+bandwidth]) #u = u/np.max(u) #print("test", len(u), len(u[0])) ''' plt.imshow(u, interpolation='nearest') #plt.imshow(result) plt.gca().invert_yaxis() #plt.gca().invert_xaxis() # #plt.gca().set_xticks(xf) plt.show() ''' edged = signal(edge_detection1(u)) #print(np.max(edged)) #graved = centerofgravity(edged) #signal_strength.append(np.sum(edged)) for k in range(len(edged)): for l in range(len(edged[k])): v[k][j+l] = edged[k][l]# + signaaaal print(time.time(), timeit.default_timer()-time_start, "edging done") w3 = [] filename3 = file+"_all.png" for lll in range(len(v)): w3.append(signal_lowered[lll]) plt.imshow(w3, interpolation='nearest') #plt.imshow(result) plt.gca().invert_yaxis() #plt.figure(figsize=(1200, 800)) plt.savefig(filename3, format='png', dpi=500) #plt.savefig(filename, format='png', dpi=1000) #plt.show() del w3 return v
def signal(fname, nmin=3, nmax=None): roi = load(fname) sh = roi.shape[1] roi.sort(axis=1) roim = roi[:, :nmin].mean(axis=1) if nmax is None: nmax = nmin S = roi[:, nmin:].sum(axis=1) - roim * (sh - nmin) else: S = roi[:, -nmax:].sum(axis=1) - roim * nmax return (S) for fa, fb in dfiles: FA = signal(fa) FB = signal(fb) # Total signal St = FA #+FB namesp = (fa.split(sep=".")[0]).split(sep="/")[-1] Sshape = FA.shape # ~ namefigure = namesp # ~ figure(namefigure,figsize=(16,6)) t = arange(0, Sshape[0]) / freqadq # in seconds # Regularise meanS = mean(St) stS = std(St) Sn = (St - meanS) / stS
# if len(fields) != 4: # continue # sampleinfo.append(fields) sampleinfo = [] for s in args.sampleinfos: fields = s.split(",") if len(fields) != 4: ap.error("Invalid sampleinfo argument: %s" % s) sampleinfo.append(fields) vc, ws = get_averaged_transcript_rpf_densities_mutliple_samples(sampleinfo, args.txinfofile, DEFAULT_NGRID, args.minreads) fig = plot_average_transcript_rpf_densities(sampleinfo, vc, ws) fig.savefig(args.outfile, format="pdf", bbox_inches='tight') return if __name__ == "__main__": from signal import signal, SIGPIPE, SIG_DFL signal(SIGPIPE, SIG_DFL) try: main() except IOError as e: if e.errno != 32: raise except KeyboardInterrupt as e: pass