Пример #1
0
    def convolve_cycle(self, audio_frame):
        '''
        Processing each track of the frame using the scipy overlap and add convolution, but also performing overlap and on the whole
        thing since we are processing the input by frames.
        '''
        # Setting up output arrays.
        audio_left = np.zeros((self.tracks_num, self.frame_count + self.M_left - 1), dtype=self.input_array.dtype)
        audio_right = np.zeros((self.tracks_num, self.frame_count + self.M_right - 1), dtype=self.input_array.dtype)

        for i in range(audio_frame.shape[0]):
            try:
                audio_left[i] = ss.oaconvolve(audio_frame[i], self.IR_left[i], mode='full') + self.leftover_left[i]
                audio_right[i] = ss.oaconvolve(audio_frame[i], self.IR_right[i], mode='full') + self.leftover_right[i]
            except ValueError:
                print(audio_frame[i].shape)
                print(self.IR_left[i].shape)

        # Saving last self.M - 1 elements for next cycle.
        self.leftover_left = np.hstack((audio_left[:, self.frame_count:],
                                        np.zeros((self.tracks_num, self.frame_count), dtype=self.input_array.dtype)))
        self.leftover_right = np.hstack((audio_right[:, self.frame_count:],
                                         np.zeros((self.tracks_num, self.frame_count), dtype=self.input_array.dtype)))

        # Discarding last self.M - 1 elements.
        audio_left = audio_left[:, :self.frame_count]
        audio_right = audio_right[:, :self.frame_count]

        return (audio_left, audio_right)
Пример #2
0
def add_room_response(spk, rir, early_energy=True, sr=16000):
    """
    Convolute source signal with selected rirs
    Args
        spk: S
        rir: N x R
    Return
        revb: N x S
    """
    if spk.ndim != 1:
        raise RuntimeError(f"Can not convolve rir with {spk.ndim}D signals")
    S = spk.shape[-1]
    # revb = [ss.fftconvolve(spk, r)[:S] for r in rir]
    revb = ss.oaconvolve(spk[None, ...], rir)[..., :S]
    revb = np.asarray(revb)

    if early_energy:
        rir_ch0 = rir[0]
        rir_peak = np.argmax(rir_ch0)
        rir_beg_idx = max(0, int(rir_peak - 0.001 * sr))
        rir_end_idx = min(rir_ch0.size, int(rir_peak + 0.05 * sr))
        early_rir = rir_ch0[rir_beg_idx:rir_end_idx]
        early_rev = ss.oaconvolve(spk, early_rir)[:S]
        return revb, np.mean(early_rev**2)
    else:
        return revb, np.mean(revb[0]**2)
Пример #3
0
    def start_blocking_processing(self):
        # Setting up output arrays.
        audio_left = np.zeros((self.tracks_num, self.L + self.M_left - 1), dtype=self.input_array.dtype)
        audio_right = np.zeros((self.tracks_num, self.L + self.M_right - 1), dtype=self.input_array.dtype)

        # Processing each track of the frame using the scipy overlap and add convolution, but also performing overlap and on the whole thing since we are processing the input by frames.
        for i in range(self.input_array.shape[0]):
            audio_left[i] = ss.oaconvolve(self.input_array[i], self.IR_left[i], mode='full')
            audio_right[i] = ss.oaconvolve(self.input_array[i], self.IR_right[i], mode='full')

        # Saving last self.M - 1 elements for next cycle.
        self.leftover_left = np.hstack(
            (audio_left[:, self.L:], np.zeros((self.tracks_num, self.L), dtype=self.input_array.dtype)))
        self.leftover_right = np.hstack(
            (audio_right[:, self.L:], np.zeros((self.tracks_num, self.L), dtype=self.input_array.dtype)))

        # Discarding last self.M - 1 elements.
        audio_left = audio_left[:, :self.L]
        audio_right = audio_right[:, :self.L]

        # Mixing tracks.
        audio_left = self.mixing_gain * np.sum(audio_left, axis=0)
        audio_right = self.mixing_gain * np.sum(audio_right, axis=0)

        # Preparing data to send to PyAudio.
        out_data = np.empty((audio_left.size + audio_right.size), dtype=self.input_array.dtype)
        # Odd elements are going to the left channel.
        out_data[1::2] = audio_left
        # Even elements are going to the right channel.
        out_data[0::2] = audio_right

        if self.save_output:
            self.output_array = np.hstack((audio_left, audio_right))
Пример #4
0
def add_reverb(cln_wav, rir_wav, channels=8, predelay=50, sample_rate=16000):
    """
    add reverberation
    args:
        cln_wav: L
        rir_wav: L x C
        rir_wav is always [Lr, C] 
        predelay is ms
    return:
        wav_tgt: L x C
    """
    rir_len = rir_wav.shape[0]
    wav_tgt = np.zeros([channels, cln_wav.shape[0] + rir_len - 1])
    dt = np.argmax(rir_wav, 0).min()
    et = dt + (predelay * sample_rate) // 1000
    et_rir = rir_wav[:et]
    wav_early_tgt = np.zeros(
        [channels, cln_wav.shape[0] + et_rir.shape[0] - 1])
    for i in range(channels):
        wav_tgt[i] = sps.oaconvolve(cln_wav, rir_wav[:, i])
        wav_early_tgt[i] = sps.oaconvolve(cln_wav, et_rir[:, i])
    # L x C
    wav_tgt = np.transpose(wav_tgt)
    wav_tgt = wav_tgt[:cln_wav.shape[0]]
    wav_early_tgt = np.transpose(wav_early_tgt)
    wav_early_tgt = wav_early_tgt[:cln_wav.shape[0]]
    return wav_tgt, wav_early_tgt
Пример #5
0
def morlet_wavelet(input_signal, dt=1, R=7, freq_interval=(), drawplot=1, eps=.0001, quick=False,COI = True):
    Ns = len(input_signal)
    try:
        minf = max(freq_interval[0], R / (Ns * dt))  # avoid wavelets with COI longer than the signal
    except:
        minf = R / (Ns * dt)
    try:
        maxf = min(freq_interval[1], .5 / dt)  # avoid wavelets above the Nyquist frequency
    except:
        maxf = .5 / dt
    try:
        Nf = freq_interval[2]
    except:
        Nf = int(np.ceil(np.log(maxf / minf) / np.log(1 / R + 1)))  # make spacing aproximately equal to sigma f
    
    alfa = (maxf / minf) ** (1 / Nf) - 1;  # According to the expression achived by fn = ((1+1/R)^n)*f0 where 1/R = alfa
    vf = ((1 + alfa) ** np.arange(0, Nf)) * minf;
    result = np.zeros((Nf, Ns), dtype='complex')

    # These for loops reaaally should be paralelied...
    if quick:
        for k in range(Nf):
            result[k, :] = exp_filter(input_signal, vf[k], dt, R)  # use the IIR filter exponetial wavelet instead of Morlet
    else:
        for k in range(Nf):
            N = int(2 * R / vf[k] / dt)  # Compute size of the wavelet
            wave = sg.morlet(N, w=R, s=1, complete=0) / N * np.pi * 2  # Normalize de amplitude returned by sg.morlet
            result[k, :] = sg.oaconvolve(input_signal, wave, mode='same')

    if drawplot:  # beautiful plots for matplotlib... too bad pyecog uses pyqtgraph! X'(
        plot_wavelet(result, dt, R, (minf,maxf,Nf), eps, COI)
    return result
Пример #6
0
def SpectralShiftFiltering(I1, I2, look_angles, ground_slope, opt_str):
    """

    INPUT
        I1: [Nr x Nc] baseband SLC
        I2: [Nr x Nc] baseband SLC
        look_angles: [Nr x Nc x 2] look angles associated with each SLC
        ground_slope: [Nr x Nc] ground slope in range direction
        opt_str.
              dt: sampling interval
              f0: central frequency
              B: bandwidth

    OUTPUT
        I1_filt: [Nr x Nc] baseband spectral shift filtered SLC
        I2_filt: [Nr x Nc] baseband spectral shift filtered SLC"""

    # Spectral shift (Hz)
    Df = opt_str.f0 * np.squeeze(np.diff(look_angles, axis=2)) / np.tan(np.mean(look_angles, axis=2) - ground_slope)

    # Space-varying demodulation phases (half)
    phi_half = np.pi * np.cumsum(Df * opt_str.dt, axis=0)

    # Demodulation
    I1_filt = I1 * np.exp(-1j * phi_half)
    I2_filt = I2 * np.exp(1j * phi_half)

    # Building filter
    if not (hasattr(opt_str, "Nfilter")):
        opt_str.Nfilter = int(np.round(I1.shape[0] / 4))

    curr_filter = firwin(numtaps=opt_str.Nfilter, cutoff=opt_str.B * opt_str.dt)
    curr_filter = curr_filter / np.sqrt(np.sum(np.abs(curr_filter) ** 2))
    curr_filter = curr_filter.reshape(opt_str.Nfilter, 1)

    # Filtering
    I1_filt = oaconvolve(I1_filt, curr_filter, mode="same", axes=0)
    I2_filt = oaconvolve(I2_filt, curr_filter, mode="same", axes=0)

    # Back to baseband
    I1_filt = I1_filt * np.exp(1j * phi_half)
    I2_filt = I2_filt * np.exp(-1j * phi_half)

    return I1_filt, I2_filt
Пример #7
0
def get_energy(x, win_size=100):
    """
    get energy of signal at each index from a given window size
    :param x: signal
    :param win_size: length of window
    :return: signal energy at each index
    """
    temp = (x / np.max(np.abs(x)))**2
    energy = signal.oaconvolve(temp, np.ones(win_size), mode='same') / win_size
    return energy
Пример #8
0
def morlet_wavelet(input_signal,
                   dt=1,
                   R=7,
                   freq_interval=(),
                   progress_signal=None,
                   kill_switch=None):
    if kill_switch is None:
        kill_switch = [False]
    print('morlet_wavelet called')
    Ns = len(input_signal)
    if len(freq_interval) > 0:
        minf = max(freq_interval[0], R /
                   (Ns * dt))  # avoid wavelets with COI longer than the signal
    else:
        minf = R / (Ns * dt)
    if len(freq_interval) > 1:
        maxf = min(freq_interval[1],
                   .5 / dt)  # avoid wavelets above the Nyquist frequency
    else:
        maxf = .5 / dt
    if len(freq_interval) > 2:
        Nf = freq_interval[2]
    else:
        Nf = int(np.ceil(
            np.log(maxf / minf) /
            np.log(1 / R + 1)))  # make spacing aproximately equal to sigma f

    alfa = (maxf / minf)**(
        1 / Nf
    ) - 1  # According to the expression achived by fn = ((1+1/R)^n)*f0 where 1/R = alfa
    vf = ((1 + alfa)**np.arange(0, Nf)) * minf
    print(Nf, Ns)
    result = np.zeros((Nf, Ns), dtype='complex')

    for k in range(Nf):
        if kill_switch[0]:
            break
        N = int(2 * R / vf[k] /
                dt)  # Compute size of the wavelet: 2 standard deviations
        wave = sg.morlet(
            N, w=R, s=1, complete=0
        ) / N * np.pi * 2  # Normalize de amplitude returned by sg.morlet
        # result[k, :] = sg.fftconvolve(input_signal, wave, mode='same')
        result[k, :] = sg.oaconvolve(input_signal, wave, mode='same')
        if progress_signal is not None:
            progress_signal.emit(int(100 * k / Nf))

    mask = np.zeros(result.shape)

    Nlist = (.5 * R / vf / dt).astype('int')  # 3 sigma COI
    for k in range(len(Nlist)):
        mask[k, :Nlist[k]] = np.nan
        mask[k, -Nlist[k]:] = np.nan

    return (result, mask, vf, kill_switch)
Пример #9
0
    def filter(self, x):
        """
        convolve signal x with impulse response h
        :param x: 1-D signal
        :return: filtered signal
        """
        assert (isinstance(x, np.ndarray)), 'x must be numpy array'
        assert (x.ndim == 1), 'x must be one dimensional'
        assert (len(x) >=
                self.length), 'x must be at least as long as filter response'

        return signal.oaconvolve(x, self.h, mode='same')
Пример #10
0
def add_reverb(cln_wav, rir_wav):
    """
    Args:
        :@param cln_wav: the clean wav
        :@param rir_wav: the rir wav
    Return:
        :@param wav_tgt: the reverberant signal
    """
    rir_wav = np.array(rir_wav)
    rir_wav = rir_wav[:, np.newaxis]
    wav_tgt = np.zeros([cln_wav.shape[0]+(len(rir_wav)-1), rir_wav.shape[1]])
    for i in range(1):
        wav_tgt[:, i] = ss.oaconvolve(cln_wav, rir_wav[:,i]/np.max(np.abs(rir_wav[:,i])))
    return wav_tgt
Пример #11
0
def add_reverb(cln_wav, rir_wav, channels=8):
    """
    add reverberation
    args:
        cln_wav: L
        rir_wav: L x C
        rir_wav is always [Lr, C]
    return:
        wav_tgt: L x C
    """
    rir_len = rir_wav.shape[0]
    wav_tgt = np.zeros([channels, cln_wav.shape[0] + rir_len - 1])
    for i in range(channels):
        wav_tgt[i] = sps.oaconvolve(cln_wav, rir_wav[:, i])
    # L x C
    wav_tgt = np.transpose(wav_tgt)
    wav_tgt = wav_tgt[:cln_wav.shape[0]]
    return wav_tgt
Пример #12
0
 def __init__(self, x, filter=None, k=.01, spring_type = 'quadratic', projected = True,mapfile = 'final.tif'
              ):
     self.x , self.spring_type, self.k, self.projected = x, spring_type, k, projected
     self.mapfile = mapfile
     self.m = x.shape[0]
     self.n = x.shape[1]
     self.solved = False
     self.eps = 1e-5
     self.image = np.array(Image.open(self.mapfile))
     self.original = np.copy(self.image)
     if filter is not None:
         numtaps = 129
         filt = remez(numtaps, [0,filter-.02,filter+.02, .5],desired = [1,1e-4])
         filt = np.outer(filt,filt)
         # plt.imshow(filt)
         # plt.show()
         self.image = oaconvolve(self.image, filt,mode='same')
     self.map = RGI([np.linspace(0,self.image.shape[1]-1,self.image.shape[1]),
                     np.linspace(0,self.image.shape[0]-1,self.image.shape[0])],
                     self.image.T)
Пример #13
0
 def filtering(self, xin):
     # filtering process via overlap add convolution, using scipy > 1.4.1
     return signal.oaconvolve(xin, self.bz, axes=0)[0: - self.nth]
Пример #14
0
def calc_csr_kick(
    beam,
    charges,
    Np=None,
    gamma=None,
    rho=None,
    Nz=100,
    sigma_z=1e-3,
    Nx=100,
    sigma_x=1e-3,
    reuse_psi_grids=False,
    psi_s_grid_old=None,
    psi_x_grid_old=None,
    verbose=True,
):
    """
    """
    (x_b, xp_b, y_b, yp_b, z_b, zp_b) = beam
    zx_positions = np.stack((z_b, x_b)).T

    # Fix the grid here
    # The grid needs to enclose most particles in z-x space
    mins = np.array([-6 * sigma_z, -6 * sigma_x])  # Lower bounds of the grid
    maxs = np.array([6 * sigma_z, 6 * sigma_x])  # Upper bounds of the grid
    sizes = np.array([Nz, Nx])

    (zmin, xmin) = mins
    (zmax, xmax) = maxs
    (Nz, Nx) = sizes
    (dz, dx) = (maxs - mins) / (sizes - 1)  # grid steps

    # indexes, contrib = split_particles(zx_positions, charges, mins, maxs, sizes)
    # t1 = time.time();
    # charge_grid = deposit_particles(Np, sizes, indexes, contrib)
    # t2 = time.time();

    # Remi's fast code
    t1 = time.time()
    charge_grid = histogram_cic_2d(z_b, x_b, charges, Nz, zmin, zmax, Nx, xmin, xmax)
    t2 = time.time()

    # Normalize the grid so its integral is unity
    norm = np.sum(charge_grid) * dz * dx
    lambda_grid = charge_grid / norm

    # Apply savgol filter
    lambda_grid_filtered = np.array(
        [savgol_filter(lambda_grid[:, i], 13, 2) for i in np.arange(Nx)]
    ).T

    # Differentiation in z
    lambda_grid_filtered_prime = central_difference_z(
        lambda_grid_filtered, Nz, Nx, dz, order=1
    )

    zvec = np.linspace(zmin, zmax, Nz)
    xvec = np.linspace(xmin, xmax, Nx)

    beta = (1 - 1 / gamma ** 2) ** (1 / 2)

    t3 = time.time()

    if reuse_psi_grids == True:
        psi_s_grid = psi_s_grid_old
        psi_x_grid = psi_x_grid_old

    else:
        # Creating the potential grids
        zvec2 = np.linspace(2 * zmin, 2 * zmax, 2 * Nz)
        xvec2 = np.linspace(2 * xmin, 2 * xmax, 2 * Nx)
        zm2, xm2 = np.meshgrid(zvec2, xvec2, indexing="ij")
        # psi_s_grid = psi_s(zm2,xm2,beta)

        beta_grid = beta * np.ones(zm2.shape)
        with cf.ProcessPoolExecutor(max_workers=12) as executor:
            temp = executor.map(psi_s, zm2 / 2 / rho, xm2, beta_grid)
            psi_s_grid = np.array(list(temp))
            temp2 = executor.map(psi_x, zm2 / 2 / rho, xm2, beta_grid)
            psi_x_grid = np.array(list(temp2))

    t4 = time.time()

    if verbose:
        print("Depositting particles takes:", t2 - t1, "s")
        print("Computing potential grids take:", t4 - t3, "s")

    # Compute the wake via 2d convolution
    conv_s = oaconvolve(lambda_grid_filtered_prime, psi_s_grid, mode="same")
    conv_x = oaconvolve(lambda_grid_filtered_prime, psi_x_grid, mode="same")

    Ws_grid = (beta ** 2 / rho) * (conv_s) * (dz * dx)
    Wx_grid = (beta ** 2 / rho) * (conv_x) * (dz * dx)

    # Interpolate Ws and Wx everywhere within the grid
    Ws_interp = RectBivariateSpline(zvec, xvec, Ws_grid)
    Wx_interp = RectBivariateSpline(zvec, xvec, Wx_grid)

    r_e = 2.8179403227e-15
    q_e = 1.602176634e-19
    Nb = np.sum(charge_grid) / q_e
    kick_factor = r_e * Nb / gamma

    # Calculate the kicks at the paritcle locations
    delta_kick = kick_factor * Ws_interp.ev(z_b, x_b)
    xp_kick = kick_factor * Wx_interp.ev(z_b, x_b)

    return {
        "zvec": zvec,
        "xvec": xvec,
        "delta_kick": delta_kick,
        "xp_kick": xp_kick,
        "Ws_grid": Ws_grid,
        "Wx_grid": Wx_grid,
        "psi_s_grid": psi_s_grid,
        "psi_x_grid": psi_x_grid,
        "charge_grid": charge_grid,
    }
Пример #15
0
 def time_convolve2d(self, mode, size):
     signal.oaconvolve(self.a, self.b, mode=mode)
Пример #16
0
def csr2d_kick_calc(
    z_b,
    x_b,
    weight,
    *,
    gamma=None,
    rho=None,
    nz=100,
    nx=100,
    xlim=None,
    zlim=None,
    reuse_psi_grids=False,
    psi_s_grid_old=None,
    psi_x_grid_old=None,
    map_f=map,
    species="electron",
    debug=False,
):
    """
    
    Calculates the 2D CSR kick on a set of particles with positions `z_b`, `x_b` and charges `charges`.
    
    
    Parameters
    ----------
    z_b : np.array
        Bunch z coordinates in [m]

    x_b : np.array
        Bunch x coordinates in [m]
  
    weight : np.array
        weight array (positive only) in [C]
        This should sum to the total charge in the bunch
        
    gamma : float
        Relativistic gamma
        
    rho : float
        bending radius in [m]
        if neagtive, particles with a positive x coordinate is on the inner side of the magnet
        
    nz : int
        number of z grid points
        
    nx : int
        number of x grid points        
    
    zlim : floats (min, max) or None
        z grid limits in [m]
        
    xlim : floats (min, max) or None  
        x grid limits in [m]
        
    map_f : map function for creating potential grids.
            Examples:
                map (default)
                executor.map
    
    species : str
        Particle species. Currently required to be 'electron'
    
    debug: bool
        If True, returns the computational grids. 
        Default: False
        
              
    Returns
    -------
    dict with:
    
        ddelta_ds : np.array
            relative z momentum kick [1/m]
            
        dxp_ds : np.array
            relative x momentum kick [1/m]
        
    
        
    """
    assert species == "electron", "TODO: support species {species}"
    # assert np.sign(rho) == 1, 'TODO: negative rho'

    rho_sign = np.sign(rho)
    if rho_sign == -1:
        rho = -rho
        x_b = -x_b  # flip the beam x coordinate

    # Grid setup
    if zlim:
        zmin = zlim[0]
        zmax = zlim[1]
    else:
        zmin = z_b.min()
        zmax = z_b.max()

    if xlim:
        xmin = xlim[0]
        xmax = xlim[1]
    else:
        xmin = x_b.min()
        xmax = x_b.max()

    dz = (zmax - zmin) / (nz - 1)
    dx = (xmax - xmin) / (nx - 1)

    # Charge deposition

    # Old method
    # zx_positions = np.stack((z_b, x_b)).T
    # indexes, contrib = split_particles(zx_positions, charges, mins, maxs, sizes)
    # t1 = time.time();
    # charge_grid = deposit_particles(Np, sizes, indexes, contrib)
    # t2 = time.time();

    # Remi's fast code
    t1 = time.time()
    charge_grid = histogram_cic_2d(z_b, x_b, weight, nz, zmin, zmax, nx, xmin,
                                   xmax)

    if debug:
        t2 = time.time()
        print("Depositing particles takes:", t2 - t1, "s")

    # Normalize the grid so its integral is unity
    norm = np.sum(charge_grid) * dz * dx
    lambda_grid = charge_grid / norm

    # Apply savgol filter
    lambda_grid_filtered = np.array(
        [savgol_filter(lambda_grid[:, i], 13, 2) for i in np.arange(nx)]).T

    # Differentiation in z
    lambda_grid_filtered_prime = central_difference_z(lambda_grid_filtered,
                                                      nz,
                                                      nx,
                                                      dz,
                                                      order=1)

    # Grid axis vectors
    zvec = np.linspace(zmin, zmax, nz)
    xvec = np.linspace(xmin, xmax, nx)

    beta = np.sqrt(1 - 1 / gamma**2)

    t3 = time.time()

    if reuse_psi_grids == True:
        psi_s_grid = psi_s_grid_old
        psi_x_grid = psi_x_grid_old

    else:
        # Creating the potential grids
        #zvec2 = np.linspace(2 * zmin, 2 * zmax, 2 * nz)
        #xvec2 = np.linspace(2 * xmin, 2 * xmax, 2 * nx)
        zvec2 = np.arange(-nz, nz, 1) * dz  # center = 0 is at [nz]
        xvec2 = np.arange(-nx, nx, 1) * dx  # center = 0 is at [nx]
        zm2, xm2 = np.meshgrid(zvec2, xvec2, indexing="ij")

        beta_grid = beta * np.ones(zm2.shape)

        # Map (possibly parallel)
        temp = map_f(psi_s, zm2 / 2 / rho, xm2 / rho, beta_grid)
        psi_s_grid = np.array(list(temp))
        temp2 = map_f(psi_x, zm2 / 2 / rho, xm2 / rho, beta_grid)
        psi_x_grid = np.array(list(temp2))

        # Replacing the fake zeros along the x_axis ( due to singularity) with averaged value from the nearby grid
        psi_x_grid[:, nx] = psi_x_where_x_equals_zero(zvec2, dx / rho, beta)

    if debug:
        t4 = time.time()
        print("Computing potential grids take:", t4 - t3, "s")

    # Compute the wake via 2d convolution
    conv_s = oaconvolve(lambda_grid_filtered_prime, psi_s_grid, mode="same")
    conv_x = oaconvolve(lambda_grid_filtered_prime, psi_x_grid, mode="same")

    if debug:
        t5 = time.time()
        print("Convolution takes:", t5 - t4, "s")

    Ws_grid = (beta**2 / rho) * (conv_s) * (dz * dx)
    Wx_grid = (beta**2 / rho) * (conv_x) * (dz * dx)

    # Interpolate Ws and Wx everywhere within the grid
    Ws_interp = RectBivariateSpline(zvec, xvec, Ws_grid)
    Wx_interp = RectBivariateSpline(zvec, xvec, Wx_grid)

    # Overall factor
    Nb = np.sum(weight) / e_charge
    kick_factor = r_e * Nb / gamma  # m

    # Calculate the kicks at the particle locations
    delta_kick = kick_factor * Ws_interp.ev(z_b, x_b)
    xp_kick = kick_factor * Wx_interp.ev(z_b, x_b)

    if debug:
        t6 = time.time()
        print("Interpolation takes:", t6 - t5, "s")

    if rho_sign == -1:
        xp_kick = -xp_kick

    result = {"ddelta_ds": delta_kick, "dxp_ds": xp_kick}

    if debug:
        result.update({
            "zvec": zvec,
            "xvec": xvec,
            "zvec2": zvec2,
            "xvec2": xvec2,
            "Ws_grid": Ws_grid,
            "Wx_grid": Wx_grid,
            "psi_s_grid": psi_s_grid,
            "psi_x_grid": psi_x_grid,
            "charge_grid": charge_grid,
            "lambda_grid_filtered_prime": lambda_grid_filtered_prime,
        })

    return result
Пример #17
0
    #
    parser = argparse.ArgumentParser(
        description='overlap-add convolve with impulse response waveform')
    parser.add_argument('--wav_file',
                        '-w',
                        default='test.wav',
                        help='wav file name(16bit)')
    parser.add_argument(
        '--wav_32_file',
        '-i',
        default='impulse_1sec_100_1sec_44100-TwoTube-output-rtwdf.wav',
        help='impulse response wav file name (mono 32bit)')
    args = parser.parse_args()

    path0 = args.wav_file
    # overwrite path0
    # path0='test_882.wav'
    yg, sr = load_wav(path0)

    path2 = args.wav_32_file
    # overwrite path2
    # path2='impulse_1sec_10_1sec_88200-output-rtwdf.wav
    yg2, sr2 = load_wav32(path2, 0.150, yg)

    # overlap-add convolve with impulse response waveform
    out1 = signal.oaconvolve(yg, yg2, axes=0)  # need scipy > 1.4.1
    # set output file name
    path_out0 = os.path.splitext(
        os.path.basename(path0))[0] + '_overlapadd_out.wav'
    save_wav(path_out0, out1, sr, normalize=True)
Пример #18
0
import numpy as np
import pandas as pd
from scipy.signal import remez, oaconvolve, savgol_filter

# import sys
# from os import path 
# sys.path.append(path.abspath('../..'))

import smooth_component_analysis as sca


hrv_lf_h = remez(25, [0., 0.1, 0.15, 1.],[1., 0.],fs =2.)
hrv_rms_filter = lambda y: savgol_filter(y, 11, 2)

filter_hrv_lf = lambda y: oaconvolve(y, hrv_lf_h, 'valid')
ecg_lf_filter = sca.dataframe_filter(
    filter_hrv_lf, 
    naming_func=lambda s: s.replace('_raw','_lf'),
    pad_output=[12,12]
    )

ecg_rms_filter = sca.dataframe_filter(
    hrv_rms_filter,
    naming_func=lambda s: s.replace('_hf','_rms'),
    pad_output=None
)

INTERPOLATION_SERIES_ID_FOR_HRV = 3
# 1005 does not have cohesions so removed for now
valid_ecg_groups = [1004,  1006, 1007, 1008, 1009, 1010, 1011, 1012, 1013, 1014,
       1016, 1019, 1020, 1022, 1024, 1025, 1026, 1027, 1028, 1029, 1030,
Пример #19
0
# yy = np.expand_dims(yy, axis=2)
yy = np.tile(yy, (n_points, 1, 1)) 

kernel = (np.square(xx) + np.square(yy)) / np.square(sigmas.reshape(n_points, 1, 1))

kernel = np.exp(-0.5 * kernel)
kernel /= kernel.max()
print("kernel shape", kernel.shape)
plt.figure()
plt.title("ker")
plt.imshow(kernel[3, :])

image = np.zeros((n_points, 50,50))
points = np.random.randint(0, 50, size=(2, n_points))
points = np.vstack([points, np.arange(n_points).reshape(1, n_points)])

print(image.shape)
print(points.shape)
print(points[2], points[0], points[1], "end")
image[points[2], points[0], points[1]] = 1

conv_image = oaconvolve(image, kernel, mode="same", axes=(1, 2))

print(conv_image.shape)

plt.figure()
plt.title("im")
plt.imshow(conv_image.sum(axis=0))
plt.colorbar()
plt.show()
Пример #20
0
import numpy as np
import soundfile as sf
from scipy.signal import oaconvolve
from scipy.io.wavfile import write

rir = './circle_n8_az210_el0_r1.5.npz'
audio = './p229_008.wav'
rir = np.load(rir, allow_pickle=True)
rir = dict(zip(("data1{}".format(k) for k in rir),
               (rir[k] for k in rir)))['data1rir'] * 4

audio, sr = sf.read(audio)

result = []
for i in range(rir.shape[-1]):
    data = oaconvolve(audio, rir[:, i], mode='same')
    result.append(data)
result = np.array(result).T * 5
print(result.shape[1])
write('test.wav', sr, result)
# sf.write(result,'test.wav',  sr)
def synthesize_reverb(mode):
    if mode == 'tr':
        save_loc = './dataset/tr/'
        audio_csv = pd.read_csv('tr_audio.csv')
        rir_csv = pd.read_csv('tr_rir.csv')
        noise_csv = pd.read_csv('noise_train.csv')
        input_prefix = 'train_input_'
        label_prefix = 'train_label_'
        out_csv = 'tr.csv'

    elif mode == 'cv':
        save_loc = './dataset/cv/'
        audio_csv = pd.read_csv('cv_audio.csv')
        rir_csv = pd.read_csv('cv_rir.csv')
        noise_csv = pd.read_csv('noise_cv.csv')
        input_prefix = 'cv_input_'
        label_prefix = 'cv_label_'
        out_csv = 'cv.csv'

    elif mode == 'tt':
        save_loc = './dataset/tt/'
        audio_csv = pd.read_csv('test_audio.csv')
        rir_csv = pd.read_csv('./test_rir.csv')
        noise_csv = pd.read_csv('noise_test.csv')
        input_prefix = 'test_input_'
        label_prefix = 'test_label_'
        out_csv = 'tt.csv'

    del rir_csv['Unnamed: 0']
    # print(noise_csv)
    # exit()

    audio_csv = sklearn.utils.shuffle(audio_csv)
    rir_csv = sklearn.utils.shuffle(rir_csv)
    noise_csv = sklearn.utils.shuffle(noise_csv)
    len_rir = len(rir_csv)
    len_noise = len(noise_csv)
    data_dict = {
        'input_path': [],
        'label_path': [],
        'speech_path': [],
        'speech_RIR': [],
        'noise_path': [],
        'noise_RIR': [],
        'room': [],
        'SNR': [],
        'vad_path': []
    }
    SNR_list = [0, 5, 10, 15]

    for num in range(len(audio_csv)):
        first = True
        # print(len(rir_csv))
        audio = audio_csv.iloc[num]
        rir = rir_csv.iloc[num % len_rir]
        # print(rir)
        # exit()

        noise = noise_csv.iloc[num % len_noise]

        audio_file, fs = sf.read(audio['audio_directory'])
        noise_file, fs = sf.read(noise['noise_directory'])

        audio_rir_file = np.load(rir['rir_directory'])['rir']

        room = rir['room']
        noise_rir = rir_csv.drop([num % len_rir], axis=0)
        noise_rir = noise_rir.loc[rir_csv['room'] == room]

        # print(noise_rir, room)
        noise_rir = noise_rir.sample()['rir_directory']

        noise_rir_file = np.load(noise_rir.item())['rir']

        input_name = save_loc + 'input/' + input_prefix + str(num) + '.wav'
        output_name = save_loc + 'label/' + label_prefix + str(num) + '.wav'

        data_dict['input_path'].append(input_name)
        data_dict['label_path'].append(output_name)
        data_dict['speech_path'].append(audio['audio_directory'])
        data_dict['speech_RIR'].append(rir['rir_directory'])
        data_dict['noise_path'].append(noise['noise_directory'])
        data_dict['noise_RIR'].append(noise_rir.item())
        data_dict['room'].append(room)
        data_dict['vad_path'].append(audio['VAD_directory'])
        SNR = random.choice(SNR_list)
        data_dict['SNR'].append(SNR)
        # print(SNR)
        # exit()
        long_noise = True
        if noise_file.shape[0] > audio_file.shape[0]:
            start = np.random.randint(
                0, noise_file.shape[0] - audio_file.shape[0])
            noise_file = noise_file[start:start + audio_file.shape[0], ]

        else:
            pad_front = np.random.randint(
                0, audio_file.shape[0] - noise_file.shape[0])
            # print(pad_front)
            # pad_front=np.random.randint(0, audio_file.shape[0]-noise_file.shape[0])
            long_noise = False
        # print(audio_file.shape, noise_file.shape)
        # exit()

        for n in range(audio_rir_file.shape[-1]):

            audio_rired = oaconvolve(audio_file,
                                     audio_rir_file[:, n],
                                     mode='full')
            noise_rired = oaconvolve(noise_file,
                                     noise_rir_file[:, n],
                                     mode='full')
            audio_rired = audio_rired[:audio_file.shape[0], ]
            noise_rired = noise_rired[:audio_file.shape[0], ]

            if first == True:
                sf.write(output_name, audio_rired, fs)
                snr_mul = snr_count(audio_rired, noise_rired, SNR)
                result = np.zeros(
                    (audio_rired.shape[0], audio_rir_file.shape[-1]))
                first = False

            if long_noise == False:
                result[:, n] = audio_rired
                result[pad_front + noise_rired.shape[0],
                       n] += noise_rired * snr_mul

            else:
                result[:, n] = audio_rired + noise_rired * snr_mul

        sf.write(input_name, result, fs)
        # break
        # exit()
    pd.DataFrame(data_dict).to_csv('./dataset/' + out_csv)
Пример #22
0
from scipy.signal import convolve, fftconvolve, oaconvolve
import matplotlib.pyplot as plt

speaker=[276, 274, 246, 244, 262, 260, 227, 225]
rir_list=['ellipsoid_n8_az120_el0_r1.5.npz', 'ellipsoid_n8_az270_el0_r1.0.npz', 'ellipsoid_n8_az60_el0_r0.5.npz', 'ellipsoid_n8_az0_el0_r1.5.npz', 'ellipsoid_n8_az300_el0_r1.5.npz', 'ellipsoid_n8_az210_el0_r1.5.npz', 'ellipsoid_n8_az180_el0_r1.0.npz']
total_data=[]
length=0
for wav, rir in zip(speaker, rir_list):
    wav_name='p'+str(wav)+'_004.wav'
    wav_data, fs=sf.read(wav_name)
    rir_data=np.load(rir, allow_pickle=True)
    rir_data=rir_data['rir']
    final=None
    
    for num in range(rir_data.shape[1]):
        result=oaconvolve(wav_data, rir_data[:,num])
        result=np.expand_dims(result, axis=-1)
        if num==0:
            final=result
        else:
        
            final=np.concatenate((final, result), axis=1)
    length+=final.shape[0]
    total_data.append(final)

white_rir='ellipsoid_n8_az240_el0_r1.5.npz'
white_rir=np.load(white_rir, allow_pickle=True)
white_rir=white_rir['rir']
white_noise = np.random.normal(0, 0.04, size=length)

for z in range(white_rir.shape[1]):
Пример #23
0
    def extract_features(self, image):
        grayscale_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

        if self.smoothing_radius is not None:
            grayscale_image = signal.oaconvolve(grayscale_image, self.smoothing_kernel, mode="same")

        print(grayscale_image.shape, self.feature_mask.shape)
        print(grayscale_image.dtype, self.feature_mask.dtype)

        keypoints = self.keypoint_detector.detect(grayscale_image, self.feature_mask)

        # Take a subset of the key points in random order
        shuffle(keypoints)
        keypoints = keypoints[0:min(len(keypoints), self.n_samples_per_image)]

        x_length = self.kernel_a.shape[0]
        y_length = self.kernel_a.shape[1]

        if x_length % 2 != 1:
            exit("ERROR: even kernel dimension cannot be centered")

        if y_length % 2 != 1:
            exit("ERROR: even kernel dimension cannot be centered")

        x_radius = int((x_length - 1) / 2)
        y_radius = int((y_length - 1) / 2)

        features = numpy.zeros([self.n_samples_per_image, self.n_samples_per_kernel])

        i = 0
        for n in range(self.n_samples_per_image):
            x = int(keypoints[i].pt[0])
            y = int(keypoints[i].pt[1])

            i += 1

            if i == len(keypoints):
                break

            a = grayscale_image[y - y_radius:y + y_radius + 1, x - x_radius:x + x_radius + 1]
            b = grayscale_image[y - y_radius:y + y_radius + 1, x - x_radius:x + x_radius + 1]

            # fig, axes = pyplot.subplots(ncols=2)
            # axes[0].imshow(grayscale_image)
            # r = pyplot.Rectangle(xy=(x-x_radius,y-y_radius), width=x_radius*2+1, height=y_radius*2+1, linewidth=1,edgecolor='r',facecolor='none')
            # axes[0].add_patch(r)
            # axes[1].imshow(a)
            # pyplot.show()
            # pyplot.close()

            # print(a.shape)
            # print(b.shape)
            # print(self.kernel_a.shape)
            # print(self.kernel_b.shape)

            a = a[self.kernel_a]
            b = b[self.kernel_b]

            features[n] = (a > b)

        return keypoints, features