Example #1
0
def sim_v1():
    ideal_signals, noise_signals, _ = generate_dataset(
        data_type='lfm',
        nb_samples=10,
        fs=1e9,
        sample_length=10000,
        numerical_type='complex',
        return_am_phase=False,
        f0_range=[40e6, 80e6],
        bandwidth_range=[10e6, 20e6])
    plot_spectrum(ideal_signals[0], fs=1e9, normalize=False)
    plot_spectrum(noise_signals[0], fs=1e9, normalize=False)
Example #2
0
def sample_images_random(net, name="rand_sample"):
    with torch.no_grad():
        z = torch.randn(args.val_num, args.latent_dim)
        z = z.to(device)
        results = net.module.sample(z)
        image = results["image"]
        specturm = results["specturm"]
        save_binary_img(image.data,
                        os.path.join(args.checkpoint, f"{name}_image.png"),
                        nrow=args.val_num)
        plot_spectrum([specturm], ['spectrum'],
                      os.path.join(args.checkpoint, f"{name}_spectrum.png"))
Example #3
0
    def train(self,
              training_set_path,
              N_FFT,
              model_path,
              epochs=1000,
              batch=4,
              save_interval=100,
              max_height=1025,
              smoothing_factor=0.1):
        #Open files and fft them
        info('Training started')
        train_data = []
        sr_lists = []
        for filename in os.listdir(training_set_path):
            if "wav" not in filename:
                continue
            info('Creating FFT for file: %s', filename)
            spectrum, sr = wav2spect(training_set_path + '/' + filename, N_FFT)
            plot_spectrum(spectrum, filename + '.png')
            if len(spectrum[0]) > max_height:
                debug("Change max_height to %d", len(spectrum[0]))
            train_data.append(np.asarray(spectrum))
            debug("SR", sr)
            sr_lists.append(sr)

        # Set parameters for model
        width = len(train_data[0])
        height = max_height
        channels = 1

        info("Setting data size to: %d x %d x %d", width, height, channels)
        train_data_norm = normalize_spectrums(train_data, width, height)
        info("Train data size: %s" % (str(train_data_norm.shape)))

        # Check if batch is not bigger then training set.
        if batch > len(train_data_norm):
            batch = len(train_data_norm)
            info(
                'Batch is bigger then dataset sample, changing batch size to %d'
                % (batch))

        info("Generating GAN model")
        gan = RNNMusicGenerator(width, height, channels, model_path)

        #train model
        info("Start training")
        save_bucket_callback = None
        if self.model_bucket != None:
            info('Exporting models mode to GCP bucket is turned on')
            save_bucket_callback = self.save_callback
        gan.train(train_data_norm, epochs, batch, save_interval,
                  smoothing_factor, save_bucket_callback)
 def load_and_fft_dataset(self, training_set_path, sample_number, N_FFT):
     train_data = []
     sr_lists = []
     for filename in os.listdir(training_set_path):
         if "wav" not in filename:
             continue
         info('Creating FFT for file: %s', filename)
         spectrum, sr = wav2spect(training_set_path + '/' + filename, N_FFT)
         plot_spectrum(spectrum, filename + '.png')
         train_data.append(np.asarray(spectrum))
         debug("SR", sr)
         sr_lists.append(sr)
     width = len(train_data[0])
     train_data = normalize_spectrums(train_data, width, sample_number)
     train_data_norm = np.asarray(train_data)
     return width, train_data_norm, sr_lists
Example #5
0
File: nrg.py Project: GiggleLiu/nrg
def NRGSolve(scaledchain,good_number='QM',maxN=600,show_spec=True):
    '''
    Using NRG iteration method to solve a chain.

    Parameters:
        :chain: <ScaledChain>, the scaled chain for nrg.
        :good_number: str('Q','QM','M' ...), the good quantum number.
        :maxN: integer, the maximum retained energy levels.
        :show_spec: bool, show spectrum during iteraction.

    Return:
        tuple of (evolutor, elist, bms), the <Evolutor>, energy of each iteraction, block marker of each iteraction.
        
        Note, elist is rescaled back.
    '''
    nsite=scaledchain.nsite
    is_ghost=isinstance(scaledchain.impurity,NullImp)
    scaling_factors=scaledchain.scaling_factors
    spaceconfig=scaledchain.impurity.spaceconfig
    h=scaledchain.get_opc()

    expander=FermionHGen(spaceconfig=spaceconfig,evolutor=MaskedEvolutor(hndim=spaceconfig.hndim))
    bmgen=get_bmgen(spaceconfig,good_number)
    elist=[]
    bms=[]
    for i in xrange(nsite):
        print 'Running iteraction %s'%(i+1)
        ops=h.filter(lambda sites:all(sites<=i) and (i in sites))
        H=expander.expand(ops)
        bm=bmgen.update_blockmarker(expander.block_marker,kpmask=expander.evolutor.kpmask(i-1))
        H_bd=bm.blockize(H)
        if not bm.check_blockdiag(H_bd):
            raise Exception('Hamiltonian is not block diagonal with good quantum number %s'%good_number)
        H=tobdmatrix(H_bd,bm)
        E,U=H.eigh()
        E_sorted=sort(E)
        kpmask=(E<=E_sorted[min(maxN,len(E_sorted))-1])
        expander.trunc(U=U.tocoo(),block_marker=bm,kpmask=kpmask)
        if i!=nsite-1:
            expander.H*=scaling_factors[i+1]/scaling_factors[i]
        if show_spec:
            plot_spectrum(E_sorted[:20]-E_sorted[0],offset=[i,0.],lw=1)
            gcf().canvas.draw()
            pause(0.01)
        elist.append(E/scaling_factors[i])
        bms.append(bm)
    return expander.evolutor,bms,elist
Example #6
0
def sample_images_spectrum(net, name="rand_sample"):
    dataloader_iterator = iter(valloader)
    with torch.no_grad():
        img, target = next(dataloader_iterator)
        img = img.to(device)
        target = target.to(device)
        z = torch.randn(args.val_num, args.latent_dim)
        z = z.to(device)
        results = net.module.sample(z, target)
        image = results["image"]
        specturm = results["specturm"]
        result_img = torch.cat([img, image], dim=0)
        save_binary_img(result_img.data,
                        os.path.join(args.checkpoint, f"{name}_image.png"),
                        nrow=args.val_num)
        plot_spectrum([target, specturm], ['target', 'spectrum'],
                      os.path.join(args.checkpoint, f"{name}_spectrum.png"))
def filter_fft(img_arr, Name=None, frac=0.1):
    # config
    keep_fraction = frac
    # transform into frequency domain
    img_freq_arr = fftpack.fft2(img_arr)
    result_freq = img_freq_arr.copy()
    r, c = result_freq.shape
    # filter by keep frac
    result_freq[int(r * keep_fraction):int(r * (1 - keep_fraction))] = 0
    result_freq[:, int(c * keep_fraction):int(c * (1 - keep_fraction))] = 0
    # plot spectrum graph
    if Name is not None:
        utils.plot_spectrum(img_freq_arr, "{0}_FD_origin".format(Name))
        utils.plot_spectrum(result_freq, "{0}_FD_filtered".format(Name))
    result_arr = fftpack.ifft2(result_freq).real
    result = utils.int_round(result_arr)
    return result
 def generate(self,
              model_path,
              content_path,
              otputfile,
              N_FFT,
              sample_number=1025,
              sr=22050):
     # TODO: Add no model found exception here
     gan = tf.keras.models.load_model(model_path + ModelsSufix.GEN)
     c_w, content_inputs, sr = self.load_and_fft_dataset(
         content_path, sample_number, N_FFT)
     spectrums = gan.predict(content_inputs)
     spec_num = 0
     for single_data in spectrums:
         sp_data = np.squeeze(single_data)
         plot_spectrum(sp_data, 'gen_spec_all_%d.png' % (spec_num))
         spect2wav(sp_data, sr,
                   otputfile + '/gen_music_%d.wav' % (spec_num), N_FFT)
Example #9
0
def test_plot_spectrum():
    wrapper = WaveWrapper('./6ch/F01_050C0103_STR.CH3.wav')
    spectrum = utils.compute_spectrum(wrapper)
    utils.plot_spectrum(spectrum, wrapper.frame_duration,
                        "F01_050C0103_STR.CH3.wav")
Example #10
0
    os.path.join(lamost.LAMOST_PATH, "observed_ivar.memmap"),
    mode="r", dtype='float32', shape=(N, P))

all_model_flux = np.memmap(
    os.path.join(lamost.LAMOST_PATH, "model_flux.memmap"),
    mode="r", dtype="float32", shape=(N, P))


# Plot a special star.
star_index = 93

observed_flux = all_observed_flux[star_index]
observed_ivar = all_observed_ivar[star_index]
model_flux = all_model_flux[star_index]

fig = utils.plot_spectrum(wavelengths, observed_flux, observed_ivar, model_flux)





# Fit a Gaussian to the line at 6707 Angstroms.
x, y, y_err, indices = lamost.get_data_to_fit(wavelengths, observed_flux,
    observed_ivar, model_flux, 4534, 4574)

p0 = np.array([-0.1,  4554, 2])
p_opt, p_cov = lamost.fit_gaussian(x, y, y_err, p0)


fig, ax = plt.subplots()
ax.scatter(x, y, facecolor="k")
def test_spectrum():
    data = DATA[0]
    plot_spectrum(data, 1024)
Example #12
0
 def __generate_and_save_image(self, epoch, noise):
     generated_data = self.g_model.predict(noise)
     plot_spectrum(np.squeeze(generated_data[0]),
                   'tmp/epoch_' + str(epoch) + '.png')
Example #13
0
def sim_v0():
    # setting: name(unit)
    # sampling rate:fs(GHz); band width: bw(MHz); time width: tw(us)
    # start frequency:f0(MHz); sampling time: st(us);
    fs = 1.0 * 1e9
    bw = 50.0 * 1e6
    tw = 1e-5
    f0 = 150 * 1e6
    st = 10e-6
    stype = 'lfm'
    if stype == 'lfm':
        y = lfm(fs,
                bw,
                tw,
                f0,
                st,
                init_phase=0,
                max_length=None,
                signal_type='complex')
    elif stype == 'tone':
        y = tone_signal(fs, f0, st, signal_type='complex')
    elif stype == 'multi_tone':
        y = multi_tone_signal(fs, [f0, -30e6], st, signal_type='complex')
    plot_spectrum(y, fs, normalize=False)
    print('signal power:', np.mean(np.abs(y)**2))
    freq_am = get_spectrum(y, return_content='am')
    max_freq_am = np.max(20 * np.log10(freq_am))

    # am modulation
    am = tone_signal(fs, f0 / 1e4, sampling_time=st, signal_type='real')
    am = am * 0.2 + 0.8
    y = am * y
    plot_spectrum(y, fs, normalize=False)

    # add non-linearity
    non_liner_y = add_nonlinerity(y)
    plot_spectrum(non_liner_y, fs, normalize=False)

    # white noise
    noise_y = add_white_noise(non_liner_y, dbc=80 + 10)
    plot_spectrum(noise_y, fs, normalize=False)

    # add dc offset
    dc_y = add_dc_offset(noise_y, dc_dbc=[40, 60], fs=fs)
    plot_spectrum(dc_y, fs, normalize=False)

    # add image
    img_y = add_image(dc_y, fs=fs)
    plot_spectrum(img_y, fs, normalize=False)

    # quantization
    quant_y = quantization_fixed(dc_y, bit=8)
    plot_spectrum(quant_y, fs, normalize=False)

    print('SFDR: ', compute_sfdr(noise_y, img_y))
def test_spectrum():
    data = DATA[0]
    plot_spectrum(data, 1024)