Пример #1
0
def vectorization(N_theta=N_theta,
                  N_azimuth=N_azimuth,
                  N_eccentricity=N_eccentricity,
                  N_phase=N_phase,
                  N_X=N_X,
                  N_Y=N_Y,
                  rho=rho,
                  ecc_max=.8,
                  B_sf=.4,
                  B_theta=np.pi / N_theta / 2,
                  figure_type='',
                  save=False):
    retina = np.zeros((N_theta, N_azimuth, N_eccentricity, N_phase, N_X * N_Y))
    parameterfile = 'https://raw.githubusercontent.com/bicv/LogGabor/master/default_param.py'
    lg = LogGabor(parameterfile)
    lg.set_size((N_X, N_Y))
    # params = {'sf_0': .1, 'B_sf': lg.pe.B_sf,
    #           'theta': np.pi * 5 / 7., 'B_theta': lg.pe.B_theta}
    # phase = np.pi/4
    # edge = lg.normalize(lg.invert(lg.loggabor(
    #     N_X/3, 3*N_Y/4, **params)*np.exp(-1j*phase)))

    for i_theta in range(N_theta):
        for i_azimuth in range(N_azimuth):
            for i_eccentricity in range(N_eccentricity):
                ecc = ecc_max * (1 / rho)**(N_eccentricity - i_eccentricity)
                r = np.sqrt(N_X**2 + N_Y**2) / 2 * ecc  # radius
                sf_0 = 0.5 * 0.03 / ecc
                x = N_X/2 + r * \
                    np.cos((i_azimuth+(i_eccentricity % 2)*.5)*np.pi*2 / N_azimuth)
                y = N_Y/2 + r * \
                    np.sin((i_azimuth+(i_eccentricity % 2)*.5)*np.pi*2 / N_azimuth)
                for i_phase in range(N_phase):
                    params = {
                        'sf_0': sf_0,
                        'B_sf': B_sf,
                        'theta': i_theta * np.pi / N_theta,
                        'B_theta': B_theta
                    }
                    phase = i_phase * np.pi / 2
                    # print(r, x, y, phase, params)

                    retina[i_theta, i_azimuth, i_eccentricity,
                           i_phase, :] = lg.normalize(
                               lg.invert(
                                   lg.loggabor(x, y, **params) *
                                   np.exp(-1j * phase))).ravel()
    if figure_type == 'retina':
        FIG_WIDTH = 10
        fig, ax = plt.subplots(figsize=(FIG_WIDTH, FIG_WIDTH))
        for i_theta in range(N_theta):
            for i_azimuth in range(N_azimuth):
                for i_eccentricity in range(N_eccentricity):
                    env = np.sqrt(retina[i_theta, i_azimuth, i_eccentricity,
                                         0, :]**2 +
                                  retina[i_theta, i_azimuth, i_eccentricity,
                                         1, :]**2).reshape((N_X, N_Y))
                    ax.contourf(env,
                                levels=[env.max() / 1.2,
                                        env.max() / 1.00001],
                                lw=1,
                                colors=[plt.cm.viridis(i_theta / (N_theta))],
                                alpha=.1)
        fig.suptitle('Tiling of visual space using the retinal filters')
        ax.set_xlabel(r'$Y$')
        ax.set_ylabel(r'$X$')
        ax.axis('equal')
        if save: plt.savefig('retina_filter.pdf')
        plt.tight_layout()
        return fig, ax
    elif figure_type == 'colliculus':
        FIG_WIDTH = 10
        fig, ax = plt.subplots(figsize=(FIG_WIDTH, FIG_WIDTH))
        for i_azimuth in range(N_azimuth):
            for i_eccentricity in range(N_eccentricity):
                env = np.sqrt(colliculus[i_azimuth,
                                         i_eccentricity, :]**2.5).reshape(
                                             (N_X, N_Y))
                #ax.contour(colliculus[i_azimuth, i_eccentricity, :].reshape((N_X, N_Y)), levels=[env.max()/2], lw=1, colors=[plt.cm.viridis(i_theta/(N_theta))])
                ax.contourf(
                    env,
                    levels=[env.max() / 1.2,
                            env.max() / 1.00001],
                    lw=1,
                    colors=[plt.cm.viridis(i_eccentricity / (N_eccentricity))],
                    alpha=.1)
        fig.suptitle('Tiling of visual space using energy')
        ax.set_xlabel(r'$Y$')
        ax.set_ylabel(r'$X$')
        ax.axis('equal')
        plt.tight_layout()
        if save: plt.savefig('colliculus_filter.pdf')
        return fig, ax
    else:
        return retina
Пример #2
0
def generate_gabors_coordinates(theta, params, N_X, N_Y, centers_coordinates,
                                B_theta=15, sf_0=.05, B_sf=.5,
                                distrib_size=8, grid_res=3,
                                on_thresh=.1, off_thresh=-.1,
                                verbose=True):
    '''
    Given some gabor parameters, a set of coordinates for centering gabors, returns a set of 
    coordinates for filters belonging into the gabors


    Params :
        theta : gabor theta angle
        params : the default parameters dictionnary for the gabor generation
        N_X, N_Y : Gabor size, usually the same as the video

        centers_coordinates : a 2D array giving the centers of each gabor

        B_theta, sf_0, B_sf : Parameters for the LogGabor shape
                            B_theta is the opening of the gabor, 
                            sf_0 is the spatial frequency 
                            b_sf is the bandwidth frequency

        distrib_size : the size of each group of filters, in image coordinates
        grid_res : resolution of the group of filters, passed in a np.mgrid

        on_thresh, off_thresh : threshold at which a filter is selected to 
                                be on/off, by scanning the Gabor phi-space

        verbose : display the filter size as a sanity check  
    '''

    xs = centers_coordinates[0]
    ys = centers_coordinates[1]
    nbr_gabors = len(xs)

    N_X = int(N_X)
    N_Y = int(N_Y)
    N_phase = 2

    lg = LogGabor(params)
    lg.set_size((N_X, N_Y))

    B_theta = B_theta / 180 * np.pi
    params = {'sf_0': sf_0, 'B_sf': B_sf, 'B_theta': B_theta}
    params.update(theta=theta)

    phi = np.zeros((1, N_phase, N_X, N_Y))

    filters_per_gab = []
    for gab in range(nbr_gabors):
        x = xs[gab]
        y = ys[gab]

        for i_phase in range(N_phase):
            phase = i_phase * np.pi/2
            kernel = lg.invert(lg.loggabor(
                x, y, **params)*np.exp(-1j*phase))
            phi[0, i_phase, :] = lg.normalize(kernel)

        fx_min = x - distrib_size
        fx_max = x + distrib_size
        fy_min = y - distrib_size
        fy_max = y + distrib_size
        filters_coordinates = np.mgrid[fx_min:fx_max:grid_res,
                                       fy_min:fy_max:grid_res].reshape(2, -1).T
        if verbose and gab == 0:
            print('Thread started !\nFilter grid shape',
                  filters_coordinates.shape, '\n')

        filters_in_gabor = gabor_connectivity(filters=filters_coordinates,
                                              phi=phi, theta=0, threshold=on_thresh)
        off_filters_in_gabor = gabor_connectivity(filters=filters_coordinates,
                                                  phi=phi, theta=0, threshold=off_thresh, on=False)

        filters_per_gab.append((filters_in_gabor, off_filters_in_gabor))

    return filters_per_gab
Пример #3
0
def generate_spectrum_from_RDC(filename,
                               numFrames=500,
                               numADCSamples=128,
                               numTxAntennas=3,
                               numRxAntennas=4,
                               numLoopsPerFrame=128,
                               numAngleBins=64,
                               chirpPeriod=0.06,
                               logGabor=False,
                               accumulate=True,
                               save_full=False):
    numChirpsPerFrame = numTxAntennas * numLoopsPerFrame

    # =============================================================================
    #     numADCSamples = number of range bins
    #     numLoopsPerFrame = number of doppler bins
    # =============================================================================

    range_resolution, bandwidth = dsp.range_resolution(numADCSamples)
    doppler_resolution = dsp.doppler_resolution(bandwidth)

    if filename[-4:] != '.bin':
        filename += '.bin'

    adc_data = np.fromfile(filename, dtype=np.int16)
    adc_data = adc_data.reshape(numFrames, -1)
    adc_data = np.apply_along_axis(DCA1000.organize,
                                   1,
                                   adc_data,
                                   num_chirps=numChirpsPerFrame,
                                   num_rx=numRxAntennas,
                                   num_samples=numADCSamples)
    print("Data Loaded!")

    dataCube = adc_data
    micro_doppler_data = np.zeros((numFrames, numLoopsPerFrame, numADCSamples),
                                  dtype=np.float64)
    theta_data = np.zeros((numFrames, numLoopsPerFrame,
                           numTxAntennas * numRxAntennas, numADCSamples),
                          dtype=np.complex)

    for i, frame in enumerate(dataCube):
        # (2) Range Processing
        from mmwave.dsp.utils import Window

        radar_cube = dsp.range_processing(frame,
                                          window_type_1d=Window.BLACKMAN)
        assert radar_cube.shape == (
            numChirpsPerFrame, numRxAntennas,
            numADCSamples), "[ERROR] Radar cube is not the correct shape!"

        # (3) Doppler Processing
        det_matrix, theta_data[i] = dsp.doppler_processing(
            radar_cube,
            num_tx_antennas=3,
            clutter_removal_enabled=True,
            window_type_2d=Window.HAMMING)

        # --- Shifts & Store
        det_matrix_vis = np.fft.fftshift(det_matrix, axes=1)
        micro_doppler_data[i, :, :] = det_matrix_vis
        # Data should now be ready. Needs to be in micro_doppler_data, a 3D-numpy array with shape [numDoppler, numRanges, numFrames]

        # LOG GABOR
        if logGabor:
            if accumulate:
                image = micro_doppler_data.sum(axis=1).T
            else:
                image = micro_doppler_data.T

            from LogGabor import LogGabor
            import holoviews as hv

            lg = LogGabor("default_param.py")
            lg.set_size(image)
            lg.pe.datapath = 'database/'

            image = lg.normalize(image, center=True)

            # display input image
            # hv.Image(image)

            # display log gabor'd image
            image = lg.whitening(image) * lg.mask
            hv.Image(image)

            uDoppler = image
        elif accumulate:
            uDoppler = micro_doppler_data.sum(axis=1).T
        else:
            uDoppler = micro_doppler_data.T

    if save_full:
        return range_resolution, doppler_resolution, uDoppler, theta_data
    else:
        return range_resolution, doppler_resolution, uDoppler
        if accumulate:
            image = micro_doppler_data.sum(axis=1).T
        else:
            image = micro_doppler_data[:,120,:].T

        from LogGabor import LogGabor
        import holoviews as hv
        import os
        fig_width = 12
        figsize=(fig_width, .618*fig_width)

        lg = LogGabor("default_param.py")
        lg.set_size(image)
        lg.pe.datapath = 'database/'

        image = lg.normalize(image, center=True)

        # display input image
        # hv.Image(image)

        # display log gabor'd image
        image = lg.whitening(image)*lg.mask
        hv.Image(image)

        uDoppler = image
    elif accumulate:
        uDoppler = micro_doppler_data.sum(axis=1).T
    else:
        uDoppler = micro_doppler_data[:,80,:].T