Ejemplo n.º 1
0
                        help="Use lookup table beam profile",
                        default="")
    args = parser.parse_args()

    res_buffer = ResultBuffer()

    sim_types = ["gpu"]
    if args.enable_cpu:
        sim_types.append("cpu")

    for sim_type in sim_types:
        sim = RfSimulator(sim_type)
        device_name = ""
        if sim_type == "gpu":
            sim.set_parameter("gpu_device", "%d" % args.device_no)
            device_name = sim.get_parameter("cur_device_name")
        elif sim_type == "cpu":
            sim.set_parameter("num_cpu_cores", "%d" % args.num_cpu_cores)
        res_buffer.add_msg("=== SIMULATION RESULTS WITH %s %s ===" %
                           (sim_type.upper(), device_name))
        sim.set_parameter("verbose", "0")
        sim.set_parameter("sound_speed", "1540.0")
        sim.set_parameter("radial_decimation", "30")
        sim.set_parameter("use_arc_projection", args.arc_proj)
        res_buffer.add_msg("Arc projection: %s" % args.arc_proj)
        sim.set_parameter("phase_delay", args.phase_delay)
        res_buffer.add_msg("Complex phase delay: %s" % args.phase_delay)

        # configure the RF excitation
        ts = 1.0 / args.fs
        tc = 1.0 / args.fc
Ejemplo n.º 2
0
def do_simulation(args):
    if args.use_gpu:
        sim = RfSimulator("gpu")
        sim.set_parameter("gpu_device", "%d" % args.device_no)
        gpu_name = sim.get_parameter("cur_device_name")
        print "Using device %d: %s" % (args.device_no, gpu_name)
    else:
        sim = RfSimulator("cpu")

    sim.set_parameter("verbose", "0")

    with h5py.File(args.h5_file, "r") as f:
        scatterers_data = f["data"][()]
    sim.add_fixed_scatterers(scatterers_data)
    print "The number of scatterers is %d" % scatterers_data.shape[0]

    # configure simulation parameters
    sim.set_parameter("sound_speed", "1540.0")
    sim.set_parameter("radial_decimation", "10")
    sim.set_parameter("phase_delay", "on")
    sim.set_parameter("noise_amplitude", "%f" % args.noise_ampl)

    # configure the RF excitation
    fs = 80e6
    ts = 1.0 / fs
    fc = 5.0e6
    tc = 1.0 / fc
    t_vector = np.arange(-16 * tc, 16 * tc, ts)
    bw = 0.3
    samples = np.array(gausspulse(t_vector, bw=bw, fc=fc), dtype="float32")
    center_index = int(len(t_vector) / 2)
    sim.set_excitation(samples, center_index, fs, fc)

    # define the scan sequence
    origins = np.zeros((args.num_lines, 3), dtype="float32")
    origins[:, 0] = np.linspace(args.x0, args.x1, args.num_lines)
    x_axis = np.array([1.0, 0.0, 0.0])
    z_axis = np.array([0.0, 0.0, 1.0])
    directions = np.array(np.tile(z_axis, (args.num_lines, 1)),
                          dtype="float32")
    length = 0.06
    lateral_dirs = np.array(np.tile(x_axis, (args.num_lines, 1)),
                            dtype="float32")
    timestamps = np.zeros((args.num_lines, ), dtype="float32")
    sim.set_scan_sequence(origins, directions, length, lateral_dirs,
                          timestamps)

    # configure the beam profile
    sim.set_analytical_beam_profile(1e-3, 1e-3)

    frame_sim_times = []
    for frame_no in range(args.num_frames):
        start_time = time()
        iq_lines = sim.simulate_lines()
        frame_sim_times.append(time() - start_time)

    if args.save_simdata_file != "":
        with h5py.File(args.save_simdata_file, "w") as f:
            f["sim_data_real"] = np.array(np.real(iq_lines), dtype="float32")
            f["sim_data_imag"] = np.array(np.imag(iq_lines), dtype="float32")
        print "Simulation output written to %s" % args.save_simdata_file

    print "Simulation time: %f +- %f s  (N=%d)" % (
        np.mean(frame_sim_times), np.std(frame_sim_times), args.num_frames)

    if args.pdf_file != "" and not args.visualize:
        import matplotlib as mpl
        mpl.use("Agg")
    if args.pdf_file != "" or args.visualize:
        import matplotlib.pyplot as plt
        num_samples, num_lines = iq_lines.shape
        plt.figure(1, figsize=(18, 9))
        plt.subplot(1, 2, 1)
        plt.plot(np.real(iq_lines[:, num_lines / 2]))
        plt.title("Middle RF line")
        plt.subplot(1, 2, 2)
        plt.imshow(np.real(abs(iq_lines)),
                   aspect="auto",
                   interpolation="nearest")
        if args.pdf_file != "":
            plt.savefig(args.pdf_file)
            print "Image written to disk."
    if args.visualize:
        plt.show()
Ejemplo n.º 3
0
    # create two simulator instances - one for spline-only and one fixed-only
    sim_fixed = RfSimulator("gpu")
    sim_spline = RfSimulator("gpu")
    sim_fixed.set_parameter("verbose", "0")
    sim_spline.set_parameter("verbose", "0")
    sim_fixed.set_print_debug(False)
    sim_spline.set_print_debug(False)
    sim_fixed.set_parameter("sound_speed", "%f" % c0)
    sim_spline.set_parameter("sound_speed", "%f" % c0)
    sim_fixed.set_parameter("phase_delay", "on")
    sim_spline.set_parameter("phase_delay", "on")
    sim_fixed.set_parameter("radial_decimation", "5")
    sim_spline.set_parameter("radial_decimation", "5")

    num_gpus = int(sim_fixed.get_parameter("num_cuda_devices"))
    print "System has %d CUDA devices" % num_gpus
    sim_fixed.set_parameter("gpu_device", "%d" % args.gpu_device_no)
    sim_spline.set_parameter("gpu_device", "%d" % args.gpu_device_no)
    print "Fixed simulator uses %s" % sim_fixed.get_parameter(
        "cur_device_name")
    print "Spline simulator uses %s" % sim_spline.get_parameter(
        "cur_device_name")

    # define excitation signal
    t_vector = np.arange(-16 / args.fc, 16 / args.fc, 1.0 / args.fs)
    samples = np.array(gausspulse(t_vector, bw=args.bw, fc=args.fc),
                       dtype="float32")
    center_index = int(len(t_vector) / 2)
    demod_freq = args.fc
    sim_fixed.set_excitation(samples, center_index, args.fs, demod_freq)
Ejemplo n.º 4
0
    # create two simulator instances - one for spline-only and one fixed-only
    sim_fixed = RfSimulator("gpu")
    sim_spline = RfSimulator("gpu")
    sim_fixed.set_parameter("verbose", "0")
    sim_spline.set_parameter("verbose", "0")
    sim_fixed.set_print_debug(False)
    sim_spline.set_print_debug(False)
    sim_fixed.set_parameter("sound_speed", "%f" % c0)
    sim_spline.set_parameter("sound_speed", "%f" % c0)
    sim_fixed.set_parameter("phase_delay", "on")
    sim_spline.set_parameter("phase_delay", "on")
    sim_fixed.set_parameter("radial_decimation", "5")
    sim_spline.set_parameter("radial_decimation", "5")

    num_gpus = int(sim_fixed.get_parameter("num_cuda_devices"))
    print("System has %d CUDA devices" % num_gpus)
    sim_fixed.set_parameter("gpu_device", "%d" % args.gpu_device_no)
    sim_spline.set_parameter("gpu_device", "%d" % args.gpu_device_no)
    print("Fixed simulator uses %s" %
          sim_fixed.get_parameter("cur_device_name"))
    print("Spline simulator uses %s" %
          sim_spline.get_parameter("cur_device_name"))

    # define excitation signal
    t_vector = np.arange(-16 / args.fc, 16 / args.fc, 1.0 / args.fs)
    samples = np.array(gausspulse(t_vector, bw=args.bw, fc=args.fc),
                       dtype="float32")
    center_index = int(len(t_vector) / 2)
    demod_freq = args.fc
    sim_fixed.set_excitation(samples, center_index, args.fs, demod_freq)
     if cur_time >= args.end_time: cur_time = args.start_time
 timestamps = np.array(timestamps, dtype="float32")
     
 # precompute fixed-scatterer datasets
 fixed_scatterers = create_fixed_datasets(args, control_points, amplitudes, spline_degree, knot_vector, timestamps)
 
 # create two simulator instances - one for spline-only and one fixed-only
 sim_fixed  = RfSimulator("gpu")
 sim_spline = RfSimulator("gpu")
 sim_fixed.set_parameter("verbose", "0");            sim_spline.set_parameter("verbose", "0")
 sim_fixed.set_print_debug(False);                   sim_spline.set_print_debug(False)
 sim_fixed.set_parameter("sound_speed", "%f" % c0);  sim_spline.set_parameter("sound_speed", "%f" % c0)
 sim_fixed.set_parameter("phase_delay", "on");       sim_spline.set_parameter("phase_delay", "on")
 sim_fixed.set_parameter("radial_decimation", "5");  sim_spline.set_parameter("radial_decimation", "5")
 
 num_gpus = int(sim_fixed.get_parameter("num_cuda_devices"))
 print "System has %d CUDA devices" % num_gpus
 sim_fixed.set_parameter("gpu_device", "%d" % args.gpu_device_no)
 sim_spline.set_parameter("gpu_device", "%d" % args.gpu_device_no)
 print "Fixed simulator uses %s" % sim_fixed.get_parameter("cur_device_name")
 print "Spline simulator uses %s" % sim_spline.get_parameter("cur_device_name")
 
 # define excitation signal
 t_vector = np.arange(-16/args.fc, 16/args.fc, 1.0/args.fs)
 samples = np.array(gausspulse(t_vector, bw=args.bw, fc=args.fc), dtype="float32")
 center_index = int(len(t_vector)/2) 
 demod_freq = args.fc
 sim_fixed.set_excitation(samples, center_index, args.fs, demod_freq)
 sim_spline.set_excitation(samples, center_index, args.fs, demod_freq)    
     
 # create big scan sequence with all M-mode beams (for the spline algorithm)
Ejemplo n.º 6
0
def do_simulation(args):
    if args.use_gpu:
        sim = RfSimulator("gpu")
        sim.set_parameter("gpu_device", "%d"%args.device_no)
        gpu_name = sim.get_parameter("cur_device_name")
        print "Using device %d: %s" % (args.device_no, gpu_name)
    else:
        sim = RfSimulator("cpu")

    sim.set_parameter("verbose", "0")

    with h5py.File(args.h5_file, "r") as f:
        scatterers_data = f["data"][()]
    sim.add_fixed_scatterers(scatterers_data)
    print "The number of scatterers is %d" % scatterers_data.shape[0]

    # configure simulation parameters
    sim.set_parameter("sound_speed", "1540.0")
    sim.set_parameter("radial_decimation", "10")
    sim.set_parameter("phase_delay", "on")
    sim.set_parameter("noise_amplitude", "%f" % args.noise_ampl)

    # configure the RF excitation
    fs = 80e6
    ts = 1.0/fs
    fc = 5.0e6
    tc = 1.0/fc
    t_vector = np.arange(-16*tc, 16*tc, ts)
    bw = 0.3
    samples = np.array(gausspulse(t_vector, bw=bw, fc=fc), dtype="float32")
    center_index = int(len(t_vector)/2) 
    sim.set_excitation(samples, center_index, fs, fc)

    # configure the beam profile
    sim.set_analytical_beam_profile(1e-3, 1e-3)

    for i, y in enumerate(np.linspace(-0.005, 0.005, 100)):
        print(f"Simulating frame {i}")
        # define the scan sequence
        origins = np.zeros((args.num_lines, 3), dtype="float32")
        origins[:,1] = y
        origins[:,0] = np.linspace(args.x0, args.x1, args.num_lines)
        x_axis = np.array([1.0, 0.0, 0.0])
        z_axis = np.array([0.0, 0.0, 1.0])
        directions = np.array(np.tile(z_axis, (args.num_lines, 1)), dtype="float32")
        length = 0.06
        lateral_dirs = np.array(np.tile(x_axis, (args.num_lines, 1)), dtype="float32")
        timestamps = np.zeros((args.num_lines,), dtype="float32")
        sim.set_scan_sequence(origins, directions, length, lateral_dirs, timestamps)

        iq_lines = sim.simulate_lines()
        bmode = np.array(abs(iq_lines), dtype="float32")
        gain = 1
        dyn_range = 40
        normalize_factor = np.max(bmode.flatten())
        bmode = 20*np.log10(gain*bmode/normalize_factor)
        bmode = 255.0*(bmode+dyn_range)/dyn_range
        # clamp to [0, 255]
        bmode[bmode < 0]     = 0.0
        bmode[bmode > 255.0] = 255.0

        fig = plt.figure(frameon=False)
        fig.set_size_inches(2*bmode.shape[1], bmode.shape[0])
        ax = plt.Axes(fig, [0., 0., 1., 1.])
        ax.set_axis_off()
        fig.add_axes(ax)
        ax.imshow(np.real(abs(iq_lines)), aspect="auto", cmap=plt.get_cmap("gray"))
        plt.savefig(f"sweep_{i:02d}.png", dpi=1)
        plt.close(fig)
Ejemplo n.º 7
0
    parser.add_argument("--enable_cpu", help="Also time CPU impl (slow)", action="store_true")
    parser.add_argument("--lut_file", help="Use lookup table beam profile", default="")
    args = parser.parse_args()

    res_buffer = ResultBuffer()
    
    sim_types = ["gpu"]
    if args.enable_cpu:
        sim_types.append("cpu")
    
    for sim_type in sim_types:
        sim = RfSimulator(sim_type)
        device_name = ""
        if sim_type == "gpu":
            sim.set_parameter("gpu_device", "%d" % args.device_no)
            device_name = sim.get_parameter("cur_device_name")
        elif sim_type == "cpu":
            sim.set_parameter("num_cpu_cores", "%d" % args.num_cpu_cores)
        res_buffer.add_msg("=== SIMULATION RESULTS WITH %s %s ===" % (sim_type.upper(), device_name))
        sim.set_parameter("verbose", "0")
        sim.set_parameter("sound_speed", "1540.0")
        sim.set_parameter("radial_decimation", "30")
        sim.set_parameter("use_arc_projection", args.arc_proj)
        res_buffer.add_msg("Arc projection: %s" % args.arc_proj)
        sim.set_parameter("phase_delay", args.phase_delay)
        res_buffer.add_msg("Complex phase delay: %s" % args.phase_delay)
            
        # configure the RF excitation
        ts = 1.0/args.fs
        tc = 1.0/args.fc
        t_vector = np.arange(-16*tc, 16*tc, ts)
Ejemplo n.º 8
0
def do_simulation(args):
    if args.use_gpu:
        sim = RfSimulator("gpu")
        sim.set_parameter("gpu_device", "%d"%args.device_no)
        gpu_name = sim.get_parameter("cur_device_name")
        print("Using device %d: %s" % (args.device_no, gpu_name))
    else:
        sim = RfSimulator("cpu")

    sim.set_parameter("verbose", "0")

    with h5py.File(args.h5_file, "r") as f:
        scatterers_data = f["data"].value # TODO: inspect type
    sim.add_fixed_scatterers(scatterers_data)
    print("The number of scatterers is %d" % scatterers_data.shape[0])

    # configure simulation parameters
    sim.set_parameter("sound_speed", "1540.0")
    sim.set_parameter("radial_decimation", "10")
    sim.set_parameter("phase_delay", "on")
    sim.set_parameter("noise_amplitude", "%f" % args.noise_ampl)

    # configure the RF excitation
    fs = 80e6
    ts = 1.0/fs
    fc = 5.0e6
    tc = 1.0/fc
    t_vector = np.arange(-16*tc, 16*tc, ts)
    bw = 0.3
    samples = np.array(gausspulse(t_vector, bw=bw, fc=fc), dtype="float32")
    center_index = int(len(t_vector)/2)
    sim.set_excitation(samples, center_index, fs, fc)

    # define the scan sequence
    origins = np.zeros((args.num_lines, 3), dtype="float32")
    origins[:,0] = np.linspace(args.x0, args.x1, args.num_lines)
    x_axis = np.array([1.0, 0.0, 0.0])
    z_axis = np.array([0.0, 0.0, 1.0])
    directions = np.array(np.tile(z_axis, (args.num_lines, 1)), dtype="float32")
    length = 0.06
    lateral_dirs = np.array(np.tile(x_axis, (args.num_lines, 1)), dtype="float32")
    timestamps = np.zeros((args.num_lines,), dtype="float32")
    sim.set_scan_sequence(origins, directions, length, lateral_dirs, timestamps)

    # configure the beam profile
    sim.set_analytical_beam_profile(1e-3, 1e-3)

    frame_sim_times = []
    for frame_no in range(args.num_frames):
        start_time = time()
        iq_lines = sim.simulate_lines()
        frame_sim_times.append(time()-start_time)

    if args.save_simdata_file != "":
        with h5py.File(args.save_simdata_file, "w") as f:
            f["sim_data_real"] = np.array(np.real(iq_lines), dtype="float32")
            f["sim_data_imag"] = np.array(np.imag(iq_lines), dtype="float32")
        print("Simulation output written to %s" % args.save_simdata_file)

    print("Simulation time: %f +- %f s  (N=%d)" % (np.mean(frame_sim_times), np.std(frame_sim_times), args.num_frames))

    if args.pdf_file != "" and not args.visualize:
        import matplotlib as mpl
        mpl.use("Agg")
    if args.pdf_file != "" or args.visualize:
        num_samples, num_lines = iq_lines.shape

        center_data = abs (iq_lines[:, num_lines//2].real)
        data = abs (iq_lines)
        # data = 20 * np.log10(1e-2 + data / data.mean ())

        import matplotlib.pyplot as plt

        print ('iq_lines.shape = (num_samples: {}, num_lines: {})'.format (num_samples, num_lines))
        fig = plt.figure(1, figsize=(24, 12))
        # fig = plt.figure(1, figsize=(9, 6))
        ax = plt.subplot(1,2,1)
        plt.plot(center_data, color=(153/255,102/255,204/255))
        plt.xlabel ('Depth', fontsize=14, labelpad=15)
        plt.ylabel ('Amplitude', fontsize=14, labelpad=15)
        plt.yticks ([])
        plt.grid ()
        plt.title ('Middle RF-Line', fontsize=16, pad=15)

        for side in ['top', 'right', 'left']:
            ax.spines[side].set_visible (False)

        ax = plt.subplot(1,2,2)
        image = plt.imshow (data, cmap='gray', aspect=2, interpolation="nearest")
        # cbar = fig.colorbar (image)
        # cbar.set_label ('  (dB)', fontsize=12)
        plt.xlabel ('Width', fontsize=14, labelpad=15)
        plt.ylabel ('Depth', fontsize=14, labelpad=15)
        from os import path
        name = path.basename (args.h5_file).split ('.')[0].replace ('_', ' ').title ()
        plt.title ('Simulated "{}"'.format (name), fontsize=16, pad=15)
        plt.grid ()
        plt.tick_params (axis='both', which='both', bottom=True, top=False,
                        labelbottom=True, left=True, right=False, labelleft=True)
        # cbar.ax.tick_params (axis='y', which='both', bottom=False, top=False,
                        # labelbottom=False, left=False, right=False, labelright=True)

        # for side in ['top', 'right', 'bottom', 'left']:
            # cbar.ax.spines[side].set_visible (False)

        for side in ['top', 'right', 'bottom', 'left']:
            ax.spines[side].set_visible (False)

        # plt.xticks (tuple (np.arange (0, num_lines, 50)) + (num_lines,))

        for side in ['bottom', 'left']:
            ax.spines[side].set_position(('outward', 1))


        if args.pdf_file != "":
            plt.savefig(args.pdf_file)
            print("Image written to disk.")

    if args.visualize:
        plt.show()
Ejemplo n.º 9
0
def do_simulation(args):
    if args.use_gpu:
        sim = RfSimulator("gpu")
        sim.set_parameter("gpu_device", "%d"%args.device_no)
        gpu_name = sim.get_parameter("cur_device_name")
        print "Using device %d: %s" % (args.device_no, gpu_name)
    else:
        sim = RfSimulator("cpu")

    sim.set_parameter("verbose", "0")

    with h5py.File(args.h5_file, "r") as f:
        scatterers_data = f["data"].value
    sim.add_fixed_scatterers(scatterers_data)
    print "The number of scatterers is %d" % scatterers_data.shape[0]

    # configure simulation parameters
    sim.set_parameter("sound_speed", "1540.0")
    sim.set_parameter("radial_decimation", "10")
    sim.set_parameter("phase_delay", "on")
    sim.set_parameter("noise_amplitude", "%f" % args.noise_ampl)

    # configure the RF excitation
    fs = 80e6
    ts = 1.0/fs
    fc = 5.0e6
    tc = 1.0/fc
    t_vector = np.arange(-16*tc, 16*tc, ts)
    bw = 0.3
    samples = np.array(gausspulse(t_vector, bw=bw, fc=fc), dtype="float32")
    center_index = int(len(t_vector)/2) 
    sim.set_excitation(samples, center_index, fs, fc)

    # define the scan sequence
    origins = np.zeros((args.num_lines, 3), dtype="float32")
    origins[:,0] = np.linspace(args.x0, args.x1, args.num_lines)
    x_axis = np.array([1.0, 0.0, 0.0])
    z_axis = np.array([0.0, 0.0, 1.0])
    directions = np.array(np.tile(z_axis, (args.num_lines, 1)), dtype="float32")
    length = 0.06
    lateral_dirs = np.array(np.tile(x_axis, (args.num_lines, 1)), dtype="float32")
    timestamps = np.zeros((args.num_lines,), dtype="float32")
    sim.set_scan_sequence(origins, directions, length, lateral_dirs, timestamps)

    # configure the beam profile
    sim.set_analytical_beam_profile(1e-3, 1e-3)

    frame_sim_times = []
    for frame_no in range(args.num_frames):
        start_time = time()
        iq_lines = sim.simulate_lines()
        frame_sim_times.append(time()-start_time)
        
    if args.save_simdata_file != "":
        with h5py.File(args.save_simdata_file, "w") as f:
            f["sim_data_real"] = np.array(np.real(iq_lines), dtype="float32")
            f["sim_data_imag"] = np.array(np.imag(iq_lines), dtype="float32")
        print "Simulation output written to %s" % args.save_simdata_file
        
    print "Simulation time: %f +- %f s  (N=%d)" % (np.mean(frame_sim_times), np.std(frame_sim_times), args.num_frames)    

    if args.pdf_file != "" and not args.visualize:
        import matplotlib as mpl
        mpl.use("Agg")
    if args.pdf_file != "" or args.visualize:
        import matplotlib.pyplot as plt
        num_samples, num_lines = iq_lines.shape
        plt.figure(1, figsize=(18,9))
        plt.subplot(1,2,1)
        plt.plot(np.real(iq_lines[:, num_lines/2]))
        plt.title("Middle RF line")
        plt.subplot(1,2,2)
        plt.imshow(np.real(abs(iq_lines)), aspect="auto", interpolation="nearest")
        if args.pdf_file != "":
            plt.savefig(args.pdf_file)
            print "Image written to disk."
    if args.visualize:
        plt.show()