Пример #1
0
def demodulate(samples_received, removed_freq_range,
               frequency_ranges_available, indices_available):
    """
    Perform a demodulation of the signal from pass-band to base-band.

    :param samples_received:            The filtered samples received from the server
    :param removed_freq_range:          The index of the removed frequency range
    :param frequency_ranges_available:  An array of booleans where True indicates that the corresponding frequency range
                                            is available
    :param indices_available:           An array of the indices of the available frequency ranges
    :return:                            The demodulated samples
    """
    if params.logs:
        print("Demodulation...")
    if params.MOD == 1 or params.MOD == 2:
        if params.MOD == 1:
            fc = np.mean(
                params.FREQ_RANGES[frequency_ranges_available.index(True)])

        # TODO: improve this in term of the frequency_ranges_available array above
        else:
            if removed_freq_range == 0 or removed_freq_range == 1:
                fc = np.mean(
                    [params.FREQ_RANGES[2][0], params.FREQ_RANGES[3][1]])
            else:
                fc = np.mean(
                    [params.FREQ_RANGES[0][0], params.FREQ_RANGES[2][1]])
        if params.logs:
            print("Chosen fc for demodulation: {}".format(fc))

        demodulated_samples = fourier_helper.demodulate(samples_received, fc)
        if params.plots:
            plot_helper.samples_fft_plots(demodulated_samples,
                                          "Demodulated received samples",
                                          shift=True)

    elif params.MOD == 3:
        demodulated_samples = []
        demodulation_frequencies = np.mean(params.FREQ_RANGES, axis=1)
        for i in range(len(params.FREQ_RANGES)):
            if frequency_ranges_available[i]:
                demodulated_samples.append(
                    fourier_helper.demodulate(samples_received,
                                              demodulation_frequencies[i]))
        if params.logs:
            print("Demodulation frequencies: {}".format(
                demodulation_frequencies))
        if params.plots:
            for i in range(len(indices_available)):
                plot_helper.samples_fft_plots(
                    demodulated_samples[i],
                    "Demodulated received samples {}".format(
                        indices_available[i]),
                    shift=True)

    else:
        raise ValueError('This modulation type does not exist yet... He he he')
    if params.logs:
        print("--------------------------------------------------------")
    return demodulated_samples
Пример #2
0
def prepare_data():
    """
    Prepare the data used to receive the signal, i.e retrieve the received samples and the original preamble

    :return: The received samples and the original preamble
    """
    if params.logs:
        print("Preparing the data...")

    # Load the input and output samples from their respective files
    input_samples = np.loadtxt(params.input_sample_file_path)
    samples_received = np.loadtxt(params.output_sample_file_path)

    # Plot the input and output samples in Time domain and Frequency domain
    if params.plots:
        plot_helper.samples_fft_plots(input_samples,
                                      "Sent samples",
                                      is_complex=False)
        plot_helper.samples_fft_plots(samples_received,
                                      "Received samples",
                                      is_complex=False)

    # Read the preamble samples saved previously
    preamble_samples_sent = read_write.read_preamble_samples()

    if params.logs:
        print("--------------------------------------------------------")
    return samples_received, preamble_samples_sent
Пример #3
0
def modulate_samples(p_data_p_samples):
    """
    Modulate the samples to the allowed frequency ranges

    :param p_data_p_samples:    The samples to modulate
    :return:                    The modulated samples
    """
    if params.logs:
        print(
            "Choosing the modulation frequencies and modulating the samples..."
        )
    if params.MOD == 1 or params.MOD == 3:
        modulating_frequencies = params.np.mean(params.FREQ_RANGES, axis=1)
    elif params.MOD == 2:
        modulating_frequencies = [
            params.FREQ_RANGES[0][1], params.FREQ_RANGES[2][1]
        ]
    else:
        raise ValueError("This mapping type does not exist yet... He he he")

    if np.any(np.iscomplex(p_data_p_samples)):
        if params.MOD == 1 or params.MOD == 2:
            p_data_p_modulated_samples = fourier_helper.modulate_complex_samples(
                p_data_p_samples, modulating_frequencies)
            if params.logs:
                print("Min and max sample after modulation: ({}, {})".format(
                    min(p_data_p_samples), max(p_data_p_samples)))
            if params.plots:
                plot_helper.samples_fft_plots(p_data_p_samples,
                                              "Samples to send",
                                              time=True,
                                              is_complex=True,
                                              shift=True)
        elif params.MOD == 3:
            modulated_samples = []
            for i in range(len(p_data_p_samples)):
                modulated_samples.append(
                    fourier_helper.modulate_complex_samples(
                        p_data_p_samples[i], [modulating_frequencies[i]]))
            p_data_p_modulated_samples = np.sum(modulated_samples,
                                                axis=0).flatten()
        else:
            raise ValueError(
                "This mapping type does not exist yet... He he he")
    else:
        raise ValueError("TODO: handle real samples (e.g SSB)")
    if params.logs:
        print("--------------------------------------------------------")
    return p_data_p_modulated_samples
Пример #4
0
def shape_symbols(h, p_data_p_symbols):
    """
    Use the pulse h to shape the p_data_p_symbols given, and up-sample by USF before

    :param h:                   The pulse to use to do the pulse shaping
    :param p_data_p_symbols:    The preamble-data-preamble symbols to shape
    :return:                    The preamble-data-preamble sample resulting from the pulse shaping
    """
    if params.logs:
        print("Pulse shaping the symbols...")

    if params.MOD == 1 or params.MOD == 2:
        p_data_p_samples = upfirdn(h, p_data_p_symbols, params.USF)
        if params.logs:
            print("Samples: {}\n{}".format(np.shape(p_data_p_samples),
                                           p_data_p_samples))
            print("Up-sampling factor: {}".format(params.USF))
        if params.plots:
            plot_helper.samples_fft_plots(p_data_p_samples,
                                          "Samples after the pulse shaping",
                                          shift=True)
    elif params.MOD == 3:
        p_data_p_samples = []
        for i in range(len(p_data_p_symbols)):
            p_data_p_samples.append(upfirdn(h, p_data_p_symbols[i],
                                            params.USF))
        if params.plots:
            for i in range(len(p_data_p_samples)):
                plot_helper.samples_fft_plots(
                    p_data_p_samples[i],
                    "Samples {} after the pulse shaping".format(i),
                    shift=True)
    else:
        raise ValueError("This mapping type does not exist yet... He he he")

    if params.logs:
        print("--------------------------------------------------------")
    return p_data_p_samples
Пример #5
0
def low_pass(demodulated_samples, indices_available):
    """
    Low pass the demodulated samples to get rid of the noise and the other frequencies

    :param demodulated_samples: The samples received from the server, demodulated (i.e base-band)
    :param indices_available:   An array of the indices of the available frequency ranges
    :return:                    The demodulated samples low-passed, i.e the only frequencies of interest for the samples
    """
    if params.logs:
        print("Low passing...")
    _, h = pulses.root_raised_cosine()
    h_matched = np.conjugate(h[::-1])

    if params.MOD == 1 or params.MOD == 2:
        y = np.convolve(demodulated_samples, h_matched)
        if params.plots:
            plot_helper.samples_fft_plots(y, "Low-passed samples", shift=True)
        if params.logs:
            print("Length of y: {}".format(len(y)))
    elif params.MOD == 3:
        y = []
        for i in range(len(demodulated_samples)):
            y.append(np.convolve(demodulated_samples[i], h_matched))
        if params.plots:
            for i in range(len(y)):
                plot_helper.samples_fft_plots(y[i],
                                              "Low-passed samples {}".format(
                                                  indices_available[i]),
                                              shift=True)
        if params.logs:
            print("Y shape:")
            for i in range(len(y)):
                print(np.shape(y[i]))
    else:
        raise ValueError('This modulation type does not exist yet... He he he')
    if params.logs:
        print("--------------------------------------------------------")
    return y
Пример #6
0
def shape_preamble_samples(h, preamble_symbols):
    """
    Use the pulse h to shape the preamble_symbols given, and up-sample by USF before

    :param h:                   The pulse to use to do the pulse shaping
    :param preamble_symbols:    The preamble symbols to shape
    :return:                    The preamble sample resulting from the pulse shaping
    """
    if params.logs:
        print("Shaping the preamble...")

    preamble_samples = upfirdn(h, preamble_symbols, params.USF)
    read_write.write_preamble_samples(preamble_samples)

    if params.logs:
        print("Number of samples for the preamble: {}".format(
            len(preamble_samples)))
    if params.plots:
        plot_helper.samples_fft_plots(preamble_samples,
                                      "Preamble samples",
                                      shift=True)
    if params.logs:
        print("--------------------------------------------------------")
    return None