Exemple #1
0
def generate(background_color, color, gap, height, manual_width, maximum_width,
             minimum_width, input_path, output_path):
    """
    \b
    Generating simple waveform image based on the given audio file.
    project home: https://github.com/danielbene/wform-cli

    \b
    usage examples:
        - wform-cli-win.exe c:\\path\\to\\audio.wav c:\\path\\to\\image.png
        - wform-cli-win.exe -bg '#FFFFFF' -c '#494949' c:\\audio.wav c:\\image.png
        - wform-cli-linux /home/user/Music/audio.wav /home/user/image.png
        - wform-cli-linux -manw 75000 /path/to/audio.wav image.png
        - wform-cli-linux -minw 20000 -maxw 200000 -c '#494949' /path/to/audio.wav image.png

    \b
    limitations:
        - audio files fully loaded into memory during generation
        - basic wav files supported out of the box,
            but everything else (mp3, ogg, etc) needs ffmpeg on the system
            (eg.: signed-integer wav encoding is ok, but a-law is not)
        - only png output is supported now (but it allows transparency)
    """
    waveform = Waveform(input_path, gap, height, manual_width, maximum_width,
                        minimum_width)
    wform_image = waveform.generate_waveform_image(background_color, color)
    waveform.save(output_path, wform_image)
Exemple #2
0
 def __init__(self, DR, SR, Fc, msg_len, Eb):
     self.data_rate = DR
     self.sampling_rate = SR
     self.sampling_frequency = SR * DR
     # samples per sec
     self.sampling_interval = 1.0 / self.sampling_frequency
     self.carrier_frequency = Fc  # (Hz)
     self.time = np.arange(0, self.sampling_interval * msg_len,
                           self.sampling_interval)  # time space
     self.bit_energy = Eb
     self.signal_power = DR * Eb
     self.amplitude = np.sqrt(self.signal_power)
     self.waveform1 = Waveform(self.amplitude, self.carrier_frequency,
                               self.time).cos()
     self.waveform2 = Waveform(self.amplitude, self.carrier_frequency,
                               self.time, np.pi).cos()
 def __init__(self):
     self.wind_speed = 5.0  # default wind speed
     self.coherent_pow_flg = True  # add coherent power
     self.coh_integrate_time = 0.001  # coherent integrateed time
     self.num_angles = 360  # integrated angle resolution
     self.interface_flg = 1  # curvature surface
     self.ddm_cov_factor = 5  # cov mode factor for ddm
     # # atmosphere loss for signal propagation 0.5 dB
     self.atmospheric_loss = pow(10.0, (0.5 / 10.0))
     # # members
     self.geometry = Geometry()
     self.nadir_RF = Antenna()
     self.zenith_RF = Antenna()
     self.signal = Signal()
     self.power_waveform = Waveform(True, 1000)
     self.interface = Interface()
Exemple #4
0
 def __init__(self, configs, exec_manager=None):
     self.configs = configs
     if exec_manager is None:
         exec_manager = MPITaskPool()
     self.exec_manager = exec_manager
     if self.exec_manager.is_parent():
         self.waveforms = [Waveform(*config) for config in configs]
     else:
         self.waveforms = None
    def __init__(self, freq, tau, T, samplerate, t0=None, phi=0):
       """Initialize signal without any noise.

       Arguments:
        * freq is the frequency of the signal in hertz
        * tau is the decay constant in seconds
        * T is the length of the signal in seconds
        * samplerate in Hz
        * t0 is the time the signal starts
        * dphi is the phase of the signal, 0 means cosine
        """

       Waveform.__init__(self, T, samplerate)

       self.samples = cos(2*pi*freq*self.time)*exp(-self.time/tau)

       if t0:
           self.samples = hstack((zeros(t0/selg.sampleinterval), self.samples))[:len(self.samples)]
Exemple #6
0
def read_file(f):
    x = []
    y = []
    with open(f, 'r') as csvfile:
        r = csv.reader(csvfile, delimiter='\t')
        for row in r:
            x.append(float(row[0]))
            y.append(float(row[1]))

    return Waveform(x, y, 442 / 0.01, 0.5, 0.001, f)
Exemple #7
0
class DAQmxDO(Instrument):
    def __init__(self, pxi):
        super().__init__(pxi, "DAQmxDO")
        self.physicalChannels = None
        self.startTrigger = StartTrigger()
        self.task = None

    def load_xml(self, node):
        """
        Initialize the instrument class attributes from XML received from CsPy
        
         Args:
            'node': type is ET.Element. tag should match self.expectedRoot
            node.tag == self.expectedRoot
        """

        self.is_initialized = False

        assert node.tag == self.expectedRoot, "expected node"+\
            f" <{self.expectedRoot}> but received <{node.tag}>"

        if not (self.exit_measurement or self.stop_connections):

            for child in node:

                if self.exit_measurement or self.stop_connections:
                    break

                try:

                    if child.tag == "enable":
                        self.enable = Instrument.str_to_bool(child.text)

                    elif child.tag == "resourceName":
                        self.physicalChannels = child.text

                    elif child.tag == "clockRate":
                        self.clockRate = float(child.text)

                    elif child.tag == "startTrigger":

                        # This is modifying the definitions of the outer for loop child and node. This
                        # seems dangerous.
                        for grandchild in child:

                            if node.text == "waitForStartTrigger":
                                self.startTrigger.wait_for_start_trigger = Instrument.str_to_bool(
                                    grandchild.text)
                            elif grandchild.text == "source":
                                self.startTrigger.source = grandchild.text
                            elif grandchild.text == "edge":
                                try:
                                    # CODO: could make dictionary keys in StartTrigger
                                    # lowercase and then just .lower() the capitalized keys
                                    # passed in elsewhere
                                    text = grandchild.text[0].upper(
                                    ) + grandchild.text[1:]
                                    self.startTrigger.edge = StartTrigger.nidaqmx_edges[
                                        text]
                                except KeyError as e:
                                    raise KeyError(
                                        f"Not a valid {grandchild.tag} value {grandchild.text} \n {e}"
                                    )
                            else:
                                self.logger.warning(
                                    f"Unrecognized XML tag \'{grandchild.tag}\'"
                                    +
                                    f" in <{child.tag}> in <{self.expectedRoot}>"
                                )

                    elif child.tag == "waveform":
                        self.waveform = Waveform()
                        self.waveform.init_from_xml(child)
                        self.samplesPerChannel = self.waveform.length  # the number of transitions

                        # reverse each state array
                        self.numChannels = len(self.waveform.states[0])
                        self.data = np.empty(
                            (self.samplesPerChannel, self.numChannels))
                        for i, state in enumerate(self.waveform.states):
                            self.data[i] = np.flip(state)

                    else:
                        self.logger.warning(
                            f"Unrecognized XML tag \'{child.tag}\' in <{self.expectedRoot}>"
                        )

                except (KeyError, ValueError):
                    raise XMLError(self, child)

    def init(self):
        """
        Initialize the device hardware with the attributes set in load_xml
        """

        if not (self.stop_connections
                or self.reset_connection) and self.enable:

            # Clear old task
            if self.task is not None:
                try:
                    self.close()
                except DaqError as e:
                    if e.error_code == DAQmxErrors.INVALID_TASK.value:
                        self.logger.warning(
                            "Tried to close DAQmxDO task that probably didn't exist"
                        )
                    else:
                        self.logger.exception(e)

            try:
                self.task = nidaqmx.Task()

                # Create digital out virtual channel
                self.task.do_channels.add_do_chan(
                    lines=self.physicalChannels,
                    name_to_assign_to_lines="",
                    line_grouping=LineGrouping.CHAN_FOR_ALL_LINES)

                # Setup timing. Use the onboard clock
                self.task.timing.cfg_samp_clk_timing(
                    rate=self.clockRate,
                    active_edge=Edge.RISING,  # default
                    sample_mode=AcquisitionType.FINITE,  # default
                    samps_per_chan=self.samplesPerChannel)

                # Optionally set up start trigger
                if self.startTrigger.wait_for_start_trigger:
                    self.task.triggers.start_trigger.cfg_dig_edge_start_trig(
                        trigger_source=self.startTrigger.source,
                        trigger_edge=self.startTrigger.edge)

                # Write digital waveform 1 chan N samp
                # by default, auto start is True
                self.task.write(self.data, timeout=10.0)  # default

            except DaqError:
                # end the task nicely
                self.stop()
                self.close()
                msg = '\n DAQmxDO hardware initialization failed'
                raise HardwareError(self, task=self.task, message=msg)

            self.is_initialized = True

    def is_done(self) -> bool:
        """
        Check if the tasks being run are completed
        
        Return:
            'done': True if tasks completed, connection was stopped or reset, 
                self.enable is False, or an NIDAQmx exception/warning occurred.
                False otherwise.
        """

        done = True
        if not (self.stop_connections
                or self.exit_measurement) and self.enable:

            # check if NI task is dones
            try:
                done = self.task.is_task_done()
            except DaqError:
                # end the task nicely
                self.stop()
                self.close()
                msg = '\n DAQmxDO check for task completion failed'
                raise HardwareError(self, task=self.task, message=msg)

        return done

    def start(self):
        """
        Start the task
        """

        if not (self.stop_connections
                or self.exit_measurement) and self.enable:

            try:
                self.task.start()
            except DaqError:
                # end the task nicely
                self.stop()
                self.close()
                msg = '\n DAQmxDO failed to start task'
                raise HardwareError(self, task=self.task, message=msg)

    def stop(self):
        """
        Stop the task
        """
        if self.task is not None:
            try:
                self.task.stop()
            except DaqError as e:
                msg = '\n DAQmxDO failed while attempting to stop current task'
                self.logger.warning(msg)
                self.logger.exception(e)

    def close(self):
        """
        Close the task
        """
        if self.task is not None:
            try:
                self.task.close()
            except DaqError as e:
                msg = '\n DAQmxDO failed to close current task'
                self.logger.warning(msg)
                self.logger.exception(e)
Exemple #8
0
def GetWaveData(configFileName,
                getZeroCrossingIntegral=True,
                getWaves=True,
                loud=False,
                getTail=False,
                getTot=False,
                getPH=False,
                getAmp=False,
                getRMS=False,
                getExtras=False,
                getFullTime=False,
                getCFD=False,
                getflags=False):

    # --------------------------------------------------------------------------------------------------------------------- #
    #    Configuration
    # --------------------------------------------------------------------------------------------------------------------- #

    config = configparser.ConfigParser()
    config.read(configFileName)

    # Setup data info
    # Directories
    data_directory = config['Directories']['data_directory']
    data_file_name = config['Directories']['data_file_name']
    goodInd = False
    if 'goodind_file_name' in config['Directories']:
        goodInd = True
        goodind_file_name = config['Directories']['goodind_file_name']
    pywaves_directory = config['Directories']['pywaves_directory']

    # Load pywaves
    sys.path.extend([pywaves_directory])
    from dataloader import DataLoader
    from waveform import Waveform

    # Digitizer
    global dataFormatStr
    global nSamples
    global ns_per_sample
    global number_of_bits
    global dynamic_range_volts
    global polarity
    global baselineOffset
    global nBaselineSamples
    global nCh
    global nWavesPerLoad
    global nWaves
    global startFolder
    global nFolders
    global unevenFactor
    global cfdFraction
    global integralEnd
    global totalIntegralStart
    global tailIntegralStart
    global applyCRRC4
    global CRRC4Tau

    dataFormatStr = config['Digitizer']['dataFormat']
    nSamples = int(config['Digitizer']['samples_per_waveform'])
    ns_per_sample = int(config['Digitizer']['ns_per_sample'])
    number_of_bits = int(config['Digitizer']['number_of_bits'])
    dynamic_range_volts = float(config['Digitizer']['dynamic_range_volts'])
    polarity = int(config['Digitizer']['polarity'])
    baselineOffset = int(config['Digitizer']['baseline_offset'])
    nBaselineSamples = int(config['Digitizer']['baseline_samples'])
    nCh = int(config['Digitizer']['number_of_channels'])
    nWavesPerLoad = int(config['Data Management']['waves_per_load'])
    nWaves = int(config['Data Management']['waves_per_folder'])  # per folder
    startFolder = int(config['Data Management']['start_folder'])
    nFolders = int(config['Data Management']['number_of_folders'])
    unevenFactor = int(config['Data Management']['uneven_factor'])
    cfdFraction = float(config['Pulse Processing']['cfd_fraction'])
    integralEnd = int(config['Pulse Processing']['integral_end'])
    totalIntegralStart = int(
        config['Pulse Processing']['total_integral_start'])
    tailIntegralStart = int(config['Pulse Processing']['tail_integral_start'])
    applyCRRC4 = bool(int(config['Pulse Processing']['apply_crrc4']))
    CRRC4Tau = float(config['Pulse Processing']['crrc4_shaping_time'])

    # Pre-calc
    if dataFormatStr == 'DPP_MIXED':
        dataFormat = DataLoader.DAFCA_DPP_MIXED
    elif dataFormatStr == 'STANDARD':
        dataFormat = DataLoader.DAFCA_STD
    if platform.system() is 'Windows':
        directory_separator = '\\'
    else:
        directory_separator = '/'
    nLoads = int(nWaves / nWavesPerLoad)
    chBufferSize = int(nFolders * nWaves * unevenFactor / nCh)
    VperLSB = dynamic_range_volts / (2**number_of_bits)
    fileTimeGap = 2**43  # Note: no more than 3 hours per measurement!

    # --------------------------------------------------------------------------------------------------------------------- #
    #    Data structure setup
    # --------------------------------------------------------------------------------------------------------------------- #

    dataGetter = {}
    dataStorage = {}

    if getTail == True:
        dataGetter['tail'] = getWaveTail
        dataStorage['tail'] = np.zeros((nCh, chBufferSize))
    if getTot == True:
        dataGetter['total'] = getWaveTot
        dataStorage['total'] = np.zeros((nCh, chBufferSize))
    if getPH == True:
        dataGetter['ph'] = getWavePH
        dataStorage['ph'] = np.zeros((nCh, chBufferSize))
    if getCFD == True:
        dataGetter['cfd'] = getWaveCFD
        dataStorage['cfd'] = np.zeros((nCh, chBufferSize))
    if getRMS == True:
        dataGetter['rms'] = getWaveRMS
        dataStorage['rms'] = np.zeros((nCh, chBufferSize))
    if getAmp == True:
        dataGetter['amp'] = getWaveAmp
        dataSotage['amp'] = np.zeros((nCh, chBufferSize))

    if getExtras == True and dataFormatStr == 'DPP_MIXED':
        dataSotage['extra'] = np.zeros((nCh, chBufferSize), dtype=np.uint32)
    if getFullTime == True and dataFormatStr == 'DPP_MIXED':
        dataSotage['fulltime'] = np.zeros((nCh, chBufferSize), dtype=np.uint64)

    # Setup mandatory channel queues
    ttt = np.zeros((nCh, chBufferSize), dtype=np.uint32)
    chCount = np.zeros(nCh, dtype=np.uint32)
    flags = np.zeros((nCh, chBufferSize), dtype=np.uint32)

    # --------------------------------------------------------------------------------------------------------------------- #
    #    Data aquisition
    # --------------------------------------------------------------------------------------------------------------------- #

    startTime = time.time()
    print("Running GetWaveData!")
    print("Starting at " + time.strftime('%H:%M:%S'))

    # Setup data loader
    waveform = Waveform(np.zeros(nSamples), polarity, baselineOffset,
                        nBaselineSamples)

    pulses = []
    # Queue up waves
    for f in range(startFolder, startFolder + nFolders):
        print('\n Folder {}:'.format(f))
        fullDFileName = data_directory + directory_separator + str(
            f) + directory_separator + data_file_name
        print(fullDFileName)

        datloader = DataLoader(fullDFileName, dataFormat, nSamples)
        nWavesInFile = datloader.GetNumberOfWavesInFile()

        if (nWavesInFile < nWaves):
            print('Warning: requested more waves than exists in file!')
            loadsInFile = int(np.ceil(nWavesInFile / nWavesPerLoad))
            print('Loading all {} waves instead...'.format(nWavesInFile))
            lastLoad = nWavesInFile - (loadsInFile - 1) * nWavesPerLoad

        else:
            loadsInFile = nLoads
            lastLoad = nWavesPerLoad

        if goodInd == True:
            goodIndFileName = data_directory + directory_separator + str(
                f) + directory_separator + goodind_file_name
            goodIndices = []
            with open(goodIndFileName, "r") as goodfi:
                good_lines = goodfi.readlines()
                for line in good_lines:
                    goodIndices.append(int(line) - 1)
            goodIndices = np.array(goodIndices)
        else:
            goodIndices = np.arange(0, nWavesInFile - 1)

        waveNum = 0
        printProgressBar(0,
                         loadsInFile,
                         prefix='Progress: ',
                         suffix='Complete')
        for load in range(loadsInFile):
            if (load == loadsInFile - 1):
                wavesThisLoad = lastLoad
            else:
                wavesThisLoad = nWavesPerLoad
            waves = datloader.LoadWaves(wavesThisLoad)
            for w in range(0, wavesThisLoad):
                if waveNum == goodIndices[0]:
                    goodIndices = goodIndices[1:]
                    ch = waves[w]['Channel']
                    chCount[ch] += 1

                    # set up waveform structure
                    waveform = Waveform(waves[w]['Samples'],
                                        polarity,
                                        baselineOffset,
                                        nBaselineSamples,
                                        ch=waves[w]['Channel'],
                                        time=waves[w]['TimeTag'])
                    waveform.BaselineSubtract()

                    # CCR4
                    if applyCRRC4:
                        waveform.ApplyCRRC4(ns_per_sample, CRRC4Tau)

                    # add timing data to mandatory channel structure
                    ttt[ch][chCount[ch]] = waves[w]['TimeTag']

                    # populate data structure for optional
                    for key, value in dataGetter.items():
                        dataStorage[key][ch][chCount[ch]] = getGetter[key](
                            waveform)

                        ttt[ch][chCount[ch]] = waves[w]['TimeTag']
                    # get the whole wave
                    if getWaves == True:
                        pulses.append(waveform)

                waveNum += 1  # increment the wave counter in the current load
            # end iteration over waves in load - end load

            # update progress bar following new load
            printProgressBar(load + 1,
                             loadsInFile,
                             prefix='Progress: ',
                             suffix='Complete')

    endTime = time.time()
    runTime = endTime - startTime
    print("\nGetWaveData took {} s".format(runTime))
    if getWaves == False:
        return chCount, ttt, dataStorage
    else:
        return chCount, ttt, dataStorage, pulses
Exemple #9
0
#!/usr/bin/python

# Usage: ./run.py freq

from device import Device
from waveform import Waveform
import sys

w1 = Waveform()
w1.loadFromFile(sys.argv[1])

w2 = Waveform()
w2.loadFromFile(sys.argv[2])


w1.dumpData()
print w1.numSamples, w2.numSamples
w2.dumpData()

w3 = w1.getDeviation(w2)
print w1.numSamples, w2.numSamples, w3.numSamples
w3.dumpData()
from waveform import Waveform

# create waveforms
sample_cnt = 96
fs = 1e9  # sampling frequency
f = fs / 32
# mu=30, sigma=10, dirAmpl=1.0 fits 64 samples nicely
mu = 30e-9
sigma = 10e-9
dir_ampl = 1.0
# mu2=15, sigma2=5, dirAmpl2=1.0 fits 64 samples nicely
mu2 = 15e-9
sigma2 = 5e-9
dir_ampl2 = 1.0

wv_cos = Waveform.cos(fs, sample_cnt, f)
wv_sin = Waveform.sin(fs, sample_cnt, f)
wv_zero = Waveform.DC(fs, sample_cnt)
wv_hi = Waveform.DC(fs, sample_cnt, 1.0)
wv_lo = Waveform.DC(fs, sample_cnt, -1.0)
wv_gauss = Waveform.gauss(fs, sample_cnt, mu, sigma)
wv_deriv_gauss = Waveform.derivGauss(fs, sample_cnt, mu, sigma, dir_ampl)
wv_gauss2 = Waveform.gauss(fs, sample_cnt, mu2, sigma2)
wv_deriv_gauss2 = Waveform.derivGauss(fs, sample_cnt, mu2, sigma2, dir_ampl2)

qwg = QWG('qwg_9', IPTransport('192.168.0.191'))
qwg.init()


def run(continuous=True):
def run(continuous=True):

    if continuous:
        qwg.create_waveform_real('cos', wv_cos)
        qwg.create_waveform_real('sin', wv_sin)
        qwg.create_waveform_real('zero', wv_zero)
        qwg.create_waveform_real('hi', wv_hi)
        qwg.create_waveform_real('lo', wv_lo)
        qwg.create_waveform_real('gauss', wv_gauss)
        qwg.create_waveform_real('derivGauss', wv_deriv_gauss)

        # qwg1.set('ch1_default_waveform', 'hi')
        # qwg1.set('ch2_default_waveform', 'zero')
        qwg.set('ch3_default_waveform', 'hi')
        # qwg1.set('ch4_default_waveform', 'zero')

        qwg.run_mode('CONt')

    else:  # codeword based
        qwg.create_waveform_real('zero', wv_zero)
        qwg.create_waveform_real('hi', wv_hi)
        qwg.create_waveform_real('lo', wv_lo)
        qwg.create_waveform_real('gauss', wv_gauss)
        qwg.create_waveform_real('derivGauss', wv_deriv_gauss)
        qwg.create_waveform_real('gauss2', wv_gauss2)
        qwg.create_waveform_real('derivGauss2', wv_deriv_gauss2)
        qwg.create_waveform_real('gaussNeg', -wv_gauss)

        # segment 0: idle
        qwg.set('ch1_default_waveform', 'zero')
        qwg.set('ch2_default_waveform', 'zero')
        qwg.set('ch3_default_waveform', 'zero')
        qwg.set('ch4_default_waveform', 'zero')

        # set some standard waveform to all codewords
        for seg in range(8):
            qwg.set('wave_ch{}_cw{:03}'.format(1, seg), wv_gauss)
            qwg.set('wave_ch{}_cw{:03}'.format(2, seg), wv_deriv_gauss)
            qwg.set('wave_ch{}_cw{:03}'.format(3, seg), wv_gauss2)
            qwg.set('wave_ch{}_cw{:03}'.format(4, seg), wv_deriv_gauss2)

        qwg.run_mode('CODeword')

    qwg.ch_pair1_sideband_frequency.set(100e6)
    qwg.ch_pair3_sideband_frequency.set(100e6)
    qwg.sync_sideband_generators()

    qwg.ch1_state.set(True)
    qwg.ch2_state.set(True)
    qwg.ch3_state.set(True)
    qwg.ch4_state.set(True)

    qwg.start()

    # read back
    qwg.get_operation_complete()

    if 1:
        if 0:  # FIXME: fails
            wv_cos_read_back = qwg.get_waveform_data_float('cos')
            plt.plot(wv_cos_read_back)
            plt.ylabel('cos')
            plt.show()

        # waveform upload performance
        sizes = [100, 500, 1000, 1500, 2000, 2500]
        nr_iter = 50
        durations = []
        mega_bytes_per_second = []
        for size in sizes:
            wv_test = Waveform.sin(fs, size, f)
            qwg.get_operation_complete()
            mark_start = time.perf_counter()
            for i in range(nr_iter):
                qwg.create_waveform_real(f'testSize{size}Nr{i}', wv_test)
            qwg.get_operation_complete()
            mark_end = time.perf_counter()
            duration = (mark_end - mark_start) / nr_iter
            durations.append(duration * 1e3)
            mega_bytes_per_second.append(size * 4 / duration / 1e6)
        print(sizes)
        print(durations)
        print(mega_bytes_per_second)
        plt.figure(1)
        plt.subplot(211)
        plt.plot(sizes, durations, 'bs')
        plt.xlabel('upload size [samples]')
        plt.ylabel('duration per upload [ms]')
        #        plt.axis([0, 2600, 0, 5])
        plt.subplot(212)
        plt.plot(sizes, mega_bytes_per_second, 'g^')
        plt.xlabel('upload size [samples]')
        plt.ylabel('performance [MB/s]')
        #        plt.axis([0, 2600, 0, 20])
        plt.show()

        # list waveforms
        print('WLIST size: ', qwg.get_wlist_size())
        print('WLIST: ', qwg.get_wlist())

    print('Identity: ', qwg.get_identity())
    qwg.check_errors()
def load_tmu(infile,song):

	index = 0 

	with open(infile,"rb") as file:
		data = file.read()
		file.close()	
			
		val = data[index]
		index += 1
		song.set_version(val & 0x0f)		# version
		song.set_type(val >> 4)				# type
		
		str = data[index:index+32]			# name
		index += 32		
		song.name = str.decode('utf-8')

		
		str = data[index:index+32]			# by
		index += 32		
		song.by = str.decode('utf-8')

		val = data[index]
		index += 1	
		song.set_speed(val)					#speed
		
		val = data[index]
		index += 1	
		song.restart = val					#order restart	
		
		val = data[index]
		index += 1	
		song.set_length(val)				#order length	
		
		lst = data[index:index+song.length]
		index += song.length
		song.order_list = lst				# order list
		
		for i in range(0,31):				# set 16 instrument names.
			ins = Instrument(i+1)
			str = data[index:index+16]	
			index += 16	
			try:
				ins.name = str.decode('utf-8')	
			except:
				raise SystemExit(f"Error reading instrument name. Invalid data!")
			i += 1
			song.ins.append(ins)

		for i in range(0,31):
			ins = song.ins[i]
			l = data[index]					# length
			ins.set_length(l)
			index += 1	
			r = data[index]					# restart
			ins.restart = r
			index += 1	
			
			if song.type == "SCC":
				w = data[index]				# waveform
				ins.waveform = w
			else:
				v = data[index]				# voice
				ins.voice = v
			index += 1				
			
			ins_data = data[index:index+(l*4)]
			r = 0
			for r in range(0,l):
				row = ins_data[r*4:(r*4)+4]
				ins.rows.append(row)
				r+=1
			index += (l*4)
			i += 1
		
		if song.type == "SCC":				# Waveform data
			for x in range(0,32):
				waveform = Waveform(x)
				waveform.data = data[index:index+32]
				song.waveforms.append(waveform)
				index +=32

		else:
			for x in range(0,16):			# Custom FM voices 
				voice = Voice(x+178)
				voice.data = data[index:index+8]
				song.voices.append(voice)
				index +=8

			
											# Drum names
			for d in range(0,20):
				drum = Drum(d)
				str = data[index:index+16]
				drum.name = str.decode('utf-8')
				d+=1
				index+=16
				song.drums.append(drum)
				
											#drum macros
			for d in range(0,20):
				drum = song.drums[d]
				l = data[index]
				drum.set_length(l)
				index+=1
				drm_data = data[index:index+(l*7)]
				index+=l*7
				r=0
				for r in range(0,l):
					row = drm_data[r*7:(r*7)+7]
					drum.rows.append(row)
					r+=1
				d+=1
				
			# THIS IS TO OVERCOME AN ERROR IN THE FILES
			index = index -1	
			
			
			p = 0							# Patterns
			t = 0
			while p != -1:
				num = data[index]
				if num == 255:
					break
				index+=1
				l = data[index]
				l += ord(data[index+1:index+2])*256
				index+=2
				# decompress pattern
				cmp_pat = data[index:index+l]
				pat = decompress_pattern(cmp_pat)
				index+=l
				# store pattern
				pattern = Pattern(num,t)
				song.patterns.append(pattern)
				#print (pattern.tracks)
				# store tracks
				chan = 0 
				for chan in range(0,8):
					#print(f"Pat:{num} Track: {t}")
					track = Track(t)
					row = 0
					for row in range(0,64):
						note = pat[(chan*4+row*32)+0]
						ins = pat[(chan*4+row*32)+1]
						tmp = int(pat[(chan*4+row*32)+2])
						par = pat[(chan*4+row*32)+3]

						vol = tmp >> 4
						cmd = tmp & 0x0f
						track.rows.append([note,ins,vol,cmd,par])
						row += 1
					chan += 1
					t += 1
					song.tracks.append(track)
Exemple #13
0
  def play(self): 
    
    maxhistos=0
    plots = {}
    for chan in self._work.keys():
      plots[chan] = {}
      if len(self._work[chan]["histograms"]) > maxhistos:
        maxhistos = len(self._work[chan]["histograms"])
      for plot in self._work[chan]["histograms"]:
        plots[chan][plot["name"]] = TH1F(plot["name"], plot["name"], plot['nbins'], plot['xmin'], plot['xmax'])
          


    if not self._batch:
      c=TCanvas();
      c.Divide(maxhistos+1,len(work.keys()));
    read=0
    for event in self.mainchain:
    #for event in self.mainchain:
      try:
        if read < self._first:
          read += 1 
          #print "moving forward"
          continue
        if self._last != -1 and read >= self._last:
          break
        if read%100== 0:
          print "-->read event", read
        waveforms = {}
        for channel in self._work.keys():
          theampl = eval("event."+self._work[channel]['branch_prefix']+"_ampl")
          thetime = eval("event."+self._work[channel]['branch_prefix']+"_time")
          waveforms[channel] = None
          if len(thetime) > 0:
            wf = Waveform(thetime, theampl, channel, 1, self._batch)
            wf.setBaselineLimits(self._work[channel]["baseline"]["limit_low"], self._work[channel]["baseline"]["limit_high"])
            wf.setMaxCalculatorLimits(self._work[channel]["maximum"]["limit_low"], self._work[channel]["maximum"]["limit_high"])
            wf.setAreaCalculatorLimits(self._work[channel]["area"]["limit_low"], self._work[channel]["area"]["limit_high"])
            #wf.setCrossingThresholdSlope(0.5, 'down')
            waveforms[channel] = wf
          del theampl
          del thetime
          
        plots_refs = []
        for i,channel in enumerate(self._work.keys()):
          if waveforms[channel] == None:
            continue
          if self._work[channel]["scaleBy"]!=None:
            waveforms[channel].scaleBy(self._work[channel]["scaleBy"])
          if self._work[channel]["scaleTo"]!="":
            waveforms[channel].scaleTo(waveforms[self._work[channel]["scaleTo"]])
          waveforms[channel].computeAll()
          self._channels[channel]['maximum'][0] = waveforms[channel].content['maximum']['value']
          self._channels[channel]['baseline'][0] = waveforms[channel].content['baseline']['value']
          self._channels[channel]['area'][0] = waveforms[channel].content['area']['value'] 
          self._channels[channel]['timecross'][0] = waveforms[channel].content['crossings']['value']
          
          for plot in self._work[channel]["histograms"]:
            plots[channel][plot["name"]].Fill(waveforms[channel].content[plot['what']]["value"])
        if not self._batch:
          if read%self.drawEvery == 0:    
            for i,channel in enumerate(self._work.keys()): 
              c.cd(i+i*maxhistos+1)
              if waveforms[channel] == None:
                continue;
              o=waveforms[channel].draw(gPad, self._work[channel]["graph"]["ymin"], self._work[channel]["graph"]["ymax"], "h"+str(read))
              plots_refs.append(o)
              for ip, plot in enumerate(self._work[channel]["histograms"]):
                c.cd(i+i*maxhistos+ip+2)
                plots[channel][plot["name"]].Draw()
            c.Update()
        for i,channel in enumerate(self._work.keys()):  
          if waveforms[channel] != None:
            waveforms[channel].clear()
            del waveforms[channel]
        read+=1
        self._summarytree.Fill()

      except KeyboardInterrupt:
        self._out.cd()
        self._summarytree.Write()
        a=raw_input("hit a key to continue...")  

  
       
    self._out.cd()
    self._summarytree.Write()
    if not self._batch:
      a=raw_input("hit a key to continue...")
Exemple #14
0
def start_audio():
    # Initial parameters.
    saw = Waveform(saw_wave)
    sine = Waveform(sine_wave)
    square = Waveform(square_wave)
    sine.magnitude = 0.7
    saw.magnitude = 0.3
    saw.offset = 0.1
    square.magnitude = 0.0
    square.offset = 0.3

    sig_gen = SignalGenerator([sine, saw, square])
    audio = Audio(sig_gen, 5)
    audio.start_audio()
    return audio, sig_gen
Exemple #15
0
    def load_xml(self, node):
        """
        Initialize the instrument class attributes from XML received from CsPy
        
         Args:
            'node': type is ET.Element. tag should match self.expectedRoot
            node.tag == self.expectedRoot
        """

        self.is_initialized = False

        assert node.tag == self.expectedRoot, "expected node"+\
            f" <{self.expectedRoot}> but received <{node.tag}>"

        if not (self.exit_measurement or self.stop_connections):

            for child in node:

                if self.exit_measurement or self.stop_connections:
                    break

                try:

                    if child.tag == "enable":
                        self.enable = Instrument.str_to_bool(child.text)

                    elif child.tag == "resourceName":
                        self.physicalChannels = child.text

                    elif child.tag == "clockRate":
                        self.clockRate = float(child.text)

                    elif child.tag == "startTrigger":

                        # This is modifying the definitions of the outer for loop child and node. This
                        # seems dangerous.
                        for grandchild in child:

                            if node.text == "waitForStartTrigger":
                                self.startTrigger.wait_for_start_trigger = Instrument.str_to_bool(
                                    grandchild.text)
                            elif grandchild.text == "source":
                                self.startTrigger.source = grandchild.text
                            elif grandchild.text == "edge":
                                try:
                                    # CODO: could make dictionary keys in StartTrigger
                                    # lowercase and then just .lower() the capitalized keys
                                    # passed in elsewhere
                                    text = grandchild.text[0].upper(
                                    ) + grandchild.text[1:]
                                    self.startTrigger.edge = StartTrigger.nidaqmx_edges[
                                        text]
                                except KeyError as e:
                                    raise KeyError(
                                        f"Not a valid {grandchild.tag} value {grandchild.text} \n {e}"
                                    )
                            else:
                                self.logger.warning(
                                    f"Unrecognized XML tag \'{grandchild.tag}\'"
                                    +
                                    f" in <{child.tag}> in <{self.expectedRoot}>"
                                )

                    elif child.tag == "waveform":
                        self.waveform = Waveform()
                        self.waveform.init_from_xml(child)
                        self.samplesPerChannel = self.waveform.length  # the number of transitions

                        # reverse each state array
                        self.numChannels = len(self.waveform.states[0])
                        self.data = np.empty(
                            (self.samplesPerChannel, self.numChannels))
                        for i, state in enumerate(self.waveform.states):
                            self.data[i] = np.flip(state)

                    else:
                        self.logger.warning(
                            f"Unrecognized XML tag \'{child.tag}\' in <{self.expectedRoot}>"
                        )

                except (KeyError, ValueError):
                    raise XMLError(self, child)
class Simulator(object):
    def __init__(self):
        self.wind_speed = 5.0  # default wind speed
        self.coherent_pow_flg = True  # add coherent power
        self.coh_integrate_time = 0.001  # coherent integrateed time
        self.num_angles = 360  # integrated angle resolution
        self.interface_flg = 1  # curvature surface
        self.ddm_cov_factor = 5  # cov mode factor for ddm
        # # atmosphere loss for signal propagation 0.5 dB
        self.atmospheric_loss = pow(10.0, (0.5 / 10.0))
        # # members
        self.geometry = Geometry()
        self.nadir_RF = Antenna()
        self.zenith_RF = Antenna()
        self.signal = Signal()
        self.power_waveform = Waveform(True, 1000)
        self.interface = Interface()

    def plot_delay_waveform(self, flg=False):
        """ plot delay simulated waveform """
        if flg:
            waveform = self.integrate_waveform
            title = "Integrated waveform"
        else:
            waveform = self.waveform
            title = "Delay waveform"
        noise_level = waveform[0]
        plt.figure()
        plt.plot(self.wave_range / 1000.0,
                 10.0 * np.log10(waveform / noise_level), '*-')
        plt.grid()
        plt.title(title)
        plt.xlabel("Range from specular [km]")
        plt.ylabel("SNR [dB]")
        plt.ylim(0, 5)
        plt.xlim(-1.0, 5.0)
        plt.tight_layout()
        plt.show()

    def plot_power_ddm(self):
        """ plot scattered power DDM """
        plt.figure(figsize=(4, 3))
        noise_level = self.ddm.min()
        extent = [
            self.wave_range[0] / 1000.0, self.wave_range[-1] / 1000.0,
            -self.dopp_bin * self.center_dopp / 1000.0,
            self.dopp_bin * self.center_dopp / 1000.0
        ]
        plt.imshow(self.ddm,
                   extent=extent,
                   vmin=noise_level,
                   vmax=max(self.ddm[self.center_dopp, :]),
                   cmap='jet',
                   aspect='auto')
        plt.title("Scattered power DDM")
        plt.xlabel("Range from specular [km]")
        plt.ylabel("Doppler [kHz]")
        plt.colorbar()
        plt.tight_layout()
        plt.show()

    def plot_scattered_area(self, flg=True):
        """ plot scattered area """
        if flg:
            sca_area = self.eff_area
            title = "Effective scatter area"
        else:
            sca_area = self.phy_area
            title = "Physical scatter area"
        plt.figure(figsize=(4, 3))
        noise_level = sca_area.min()
        max_pow = max(sca_area[self.center_dopp, :])
        extent = [
            self.wave_range[0] / 1000.0, self.wave_range[-1] / 1000.0,
            -self.dopp_bin * self.center_dopp / 1000.0,
            self.dopp_bin * self.center_dopp / 1000.0
        ]
        plt.imshow(sca_area,
                   extent=extent,
                   vmin=noise_level,
                   vmax=max_pow,
                   cmap='jet',
                   aspect='auto')
        plt.title(title)
        plt.xlabel("Range from specular [km]")
        plt.ylabel("Doppler [kHz]")
        plt.colorbar()
        plt.tight_layout()
        plt.show()

    ###########################################################################
    def compute_direct_power(self, transmit_pow):
        """ compute direct signal power """
        # # receiver body frame
        bf_e, bf_h, bf_k = self.zenith_RF.get_antenna_bf()
        # # receiver body frame to specular
        self.geometry.BF2spfs(bf_e, bf_h, bf_k)
        scat_vec, rang = self.geometry.compute_r2t_vector()
        angles = self.geometry.compute_antenna_gain_pos(scat_vec)
        directivity = pow(10.0,
                          (self.nadir_RF.get_receiver_gain(angles)) / 10.0)
        tmp = const.C_LIGHT / (self.signal.freq * 4.0 * const.PI * rang)
        self.direct_pow = pow(tmp, 2)
        self.direct_pow *= transmit_pow * directivity / self.atmospheric_loss

    def correlate_direct_signal(self):
        sin_arg = 2 * self.zenith_RF.filter_bb_bw / self.sampling_rate
        sin_arg *= np.arange(0, self.range_samples_len)
        sin_arg[sin_arg == 0.0] = 1.0
        sin_arg = np.sinc(sin_arg)
        self.nd_nr_cov = abs(sin_arg)
        self.sd_nr_cov = np.convolve(sin_arg,
                                     self.signal.lambda_fun,
                                     mode="same")
        self.sd_nr_cov = np.abs(self.sd_nr_cov)
        max_value = self.sd_nr_cov.max()
        if max_value > 0.0:
            self.sd_nr_cov = self.sd_nr_cov / max_value
        for i in range(self.dopps):
            power = self.power[:, i]
            pass

    ###########################################################################
    def compute_coherent_power(self, scat_vec):
        """
        compute coherent power at scat_vec diretion, commonly coherent
        relection accur at specular point
        """
        # # compute receiver antenna gain at scat_vec diretion
        # # receiver body frame
        bf_e, bf_h, bf_k = self.nadir_RF.get_antenna_bf()
        # # receiver body frame to specular
        self.geometry.BF2spfs(bf_e, bf_h, bf_k)
        angles = self.geometry.compute_antenna_gain_pos(scat_vec)
        directivity = pow(10.0,
                          (self.nadir_RF.get_receiver_gain(angles)) / 10.0)
        # # specular point sinc function
        self.cosi = cos(self.geometry.tx_inc)
        self.tani = tan(self.geometry.tx_inc)
        self.sinc_dopps = np.zeros(self.dopps)
        sinc_arg = self.dopp_bin * self.coh_integrate_time
        if sinc_arg == 0.0:
            self.sinc_dopps[self.center_dopp] = 1.0
        else:
            self.sinc_dopps[self.center_dopp] = pow(np.sinc(sinc_arg), 2)
        # # compute fresnel coefficients
        self.interface.compute_fresnel(self.geometry.tx_inc)
        # # get corherent power
        tmp = 4.0 * const.PI * self.interface.sigma_z * self.cosi
        coherent_pow = self.signal.trans_pow * self.interface.fresnel
        coherent_pow *= exp(-1.0 * pow(tmp / self.signal.wavelen, 2))
        tmp = const.C_LIGHT * self.coh_integrate_time
        tmp /= (4.0 * const.PI * self.signal.freq)
        tmp /= (norm(self.geometry.tx_spf) + norm(self.geometry.rx_spf))
        coherent_pow *= directivity * self.sinc_dopps[self.center_dopp]
        coherent_pow *= pow(tmp, 2) / self.atmospheric_loss
        self.coherent_pow = coherent_pow

    def set_pow_waveform(self, init_range, sampling_rate):
        """ set power waveform for delays computation """
        self.power_waveform.sampling_rate = sampling_rate
        self.power_waveform.set_init_range(init_range)

    def set_nadir_antenna(self,
                          gain,
                          temp,
                          figure,
                          filter_bb_bw,
                          antenna_flg=True):
        """
        set antenna attitude information for receiver antenna gain
        computation
        """
        self.nadir_RF.set_receiver(gain, temp, figure, filter_bb_bw,
                                   antenna_flg)

    def set_radar_signal(self, sampling_rate, filter_bb_bw, exponent):
        """ initailze the bistatic radar signal """
        self.signal.set_radar_signal(sampling_rate, filter_bb_bw)
        # # compute corelation function of WAF
        self.signal.compute_lambda()
        self.isotropic_factor = (self.signal.wavelen**2) / (4.0 * const.PI)**3
        self.dt = const.C_LIGHT / sampling_rate  # just for computation later
        # # compute the transmit power
        ele = const.PI / 2 - self.geometry.tx_inc
        self.signal.compute_transmit_power(self.signal, ele)

    def set_interface(self, wind_speed):
        self.interface.ws = wind_speed
        self.interface.set_polarization(self.polar_mode)

    def configure_radar_geometry(self, tx, tv, rx, rv, undulation_flg=True):
        """
        set bistatic radar configuration, need the ecef postion and velocity
        of transimiter and receiver, compute specular point postion. function
        can also account for the undualtion of geoid
        """
        self.geometry.tx_pos = np.asarray(tx)
        self.geometry.tx_vel = np.asarray(tv)
        self.geometry.rx_pos = np.asarray(rx)
        self.geometry.rx_vel = np.asarray(rv)
        # # compute the specular point
        self.geometry.compute_sp_pos(undulation_flg)

    def earth_curvature_appro(self, tau, x):
        """ modified surface glisten zone coordinations for earth curvature """
        rad = norm(x[:2])
        az = atan2(x[1], x[0])
        x[2] = sqrt(const.RE_WGS84**2 - rad**2) - const.RE_WGS84
        rr = norm(self.geometry.rx_spf - x)
        rt = norm(self.geometry.tx_spf - x)
        delay = rr + rt - self.geometry.rrt - self.sp_delay
        rad *= sqrt(tau / delay)
        x[0] = rad * cos(az)
        x[1] = rad * sin(az)
        x[2] = sqrt(const.RE_WGS84**2 - rad**2) - const.RE_WGS84

    def compute_sinc(self, dopp):
        """ compute doppler sinc function """
        sinc_arg = (np.arange(self.dopps) - self.center_dopp) * self.dopp_bin
        sinc_arg = (dopp - sinc_arg) * self.coh_integrate_time
        ind = sinc_arg != 0.0
        self.sinc_dopps[ind] = pow(np.sinc(sinc_arg[ind]), 2)
        self.sinc_dopps[~ind] = 1.0

    def delay_integration(self, tau, a, c, delta):
        """
        integration points over the surface ellipse for each range sample
        """
        x = np.zeros(3)
        pow_tau = np.zeros(self.dopps)
        phy_area = np.zeros(self.dopps)
        eff_area = np.zeros(self.dopps)
        left_side = -1.0 * (self.center_dopp + 0.5) * self.dopp_bin
        for i in range(self.num_angles):
            # # surface point calculation
            theta = i * delta
            x[0] = a * self.cosi * cos(theta)
            x[1] = a * sin(theta) + c
            # # surface point earth curvature modified
            if self.interface_flg == 1:
                self.earth_curvature_appro(tau, x)
            # # surface point scatter vector and scatter area
            inc_vec, sca_vec, jacob, coeff = self.geometry.compute_scattering_vector(
                x)
            # # surface point relative doppler shift to the specular point
            dopp = self.geometry.doppler_shift(inc_vec, sca_vec,
                                               self.signal.freq) - self.sp_dopp
            # if self.dopps % 2 == 0:
            #     dopp -= self.dopp_bin
            # # sinc function
            self.compute_sinc(dopp)
            # # reflected coeffcient
            simga0 = self.interface.compute_scattered_coeff(
                inc_vec, sca_vec, self.geometry.tx_az)
            # # receicer antenna gain at the surface point direction
            angles = self.geometry.compute_antenna_gain_pos(inc_vec)
            rev_gain_db = self.nadir_RF.get_receiver_gain(angles)
            directivity = pow(10.0, rev_gain_db / 10.0)
            # # a factor for correlation calculation
            factor = directivity * self.isotropic_factor * simga0 * jacob * coeff
            if i == 0:
                # # restore the first surface point
                fx = np.copy(x[:2])
                px = np.copy(x[:2])  # the former point relative to current one
                fst_dopp = pre_dopp = dopp
                fst_jac = pre_jac = jacob
                fst_ft = factor * self.sinc_dopps
                pre_ft = fst_ft.copy()
                fst_area = jacob * self.sinc_dopps
                pre_area = fst_area.copy()
                continue
            diff_ang = abs(atan2(x[1], x[0]) - atan2(px[1], px[0]))
            new_theta = min(diff_ang, 2.0 * const.PI - diff_ang)
            px = np.copy(x[:2])
            tmp = factor * self.sinc_dopps
            area = jacob * self.sinc_dopps
            pow_tau += new_theta * (tmp + pre_ft) / 2.0  # accumulate the power
            ind = int(((dopp + pre_dopp) / 2.0 - left_side) // self.dopp_bin)
            if (ind >= 0) and (ind < self.dopps):
                phy_area[ind] += (jacob + pre_jac) / 2.0 * new_theta
            eff_area += new_theta * (area + pre_area) / 2.0
            pre_dopp = dopp
            pre_jac = jacob
            pre_ft = tmp.copy()
            pre_area = area.copy()
            if i == self.num_angles - 1:
                # # intergration to finish the whole ellipse, connect
                # # the last point to the first point
                diff_ang = abs(atan2(fx[1], fx[0]) - atan2(x[1], x[0]))
                new_theta = min(diff_ang, 2.0 * const.PI - diff_ang)
                pow_tau += new_theta * (fst_ft +
                                        tmp) / 2.0  # accumulate the power
                ind = int(
                    ((dopp + fst_dopp) / 2.0 - left_side) // self.dopp_bin)
                if (ind >= 0) and (ind < self.dopps):
                    phy_area[ind] += (jacob + fst_jac) / 2.0 * new_theta
                eff_area += new_theta * (fst_area + area) / 2.0
        return pow_tau, phy_area, eff_area

    def compute_noise_floor(self):
        """ compute noise floor """
        eta_factor = 1.0
        self.noise_floor = eta_factor * pow(10.0,
                                            self.nadir_RF.noise_power / 10.0)
        self.noise_floor /= self.nadir_RF.filter_bb_bw * self.coh_integrate_time

    def compute_power_waveform(self, ind):
        """ get lambda function """
        lam_len = self.signal.lambda_size
        half_lam = lam_len // 2
        waveform_conv = np.convolve(self.power[:, ind],
                                    self.signal.lambda_fun,
                                    mode="same")
        area_conv = np.convolve(self.dis_eff_area[:, ind],
                                self.signal.lambda_fun,
                                mode="same")
        # # compute delay power waveform
        tmp = waveform_conv[half_lam:half_lam + self.delays]
        tmp *= self.signal.trans_pow * self.dt / self.atmospheric_loss
        tmp += self.noise_floor
        if lam_len > self.delays:
            lam_len = self.delays
        tmp[:lam_len] += self.coherent_pow * self.signal.lambda_fun[:lam_len]
        return abs(tmp), area_conv[half_lam:half_lam + self.delays]

    def compute_ddm(self):
        """ compute ddm of scattered surface """
        half_lam = self.signal.lambda_size // 2
        self.ddm = np.zeros((self.dopps, self.delays))
        self.eff_area = np.zeros((self.dopps, self.delays))
        self.phy_area = self.dis_phy_area[half_lam:half_lam + self.delays,
                                          range(self.dopps)].T
        # # zero doppler shift delay waveform
        for i in range(self.dopps):
            sca_pow, eff_area = self.compute_power_waveform(i)
            self.ddm[i, :] = sca_pow
            self.eff_area[i, :] = eff_area
        # # integrated waveform
        self.integrate_waveform = self.ddm.sum(axis=0)
        if not hasattr(self, "wave_range"):
            self.power_waveform.set_waveform(self.ddm[self.center_dopp, :])
            self.power_waveform.compute_waveform_delays()
            self.wave_range = self.power_waveform.get_range_waveform()
            self.wave_range -= self.geometry.geometric_delay

    def compute_center_waveform(self):
        """ compute delay power waveform  """
        self.compute_noise_floor()
        self.waveform, _ = self.compute_power_waveform(self.center_dopp)
        # # compute spatical delays of delay waveform in meters
        self.power_waveform.set_waveform(self.waveform)
        self.power_waveform.compute_waveform_delays()
        self.wave_range = self.power_waveform.get_range_waveform()
        self.wave_range -= self.geometry.geometric_delay

    def compute_power_distribution(self):
        """
        Computation power distribution over reflecting surface
        origin located at sample = gnss_signal.lambda_size
        """
        # # signal correlation starting postion
        lam_len = self.signal.lambda_size
        end = self.range_samples_len - lam_len
        self.power = np.zeros((self.range_samples_len, self.dopps))
        self.dis_phy_area = np.zeros((self.range_samples_len, self.dopps))
        self.dis_eff_area = np.zeros((self.range_samples_len, self.dopps))
        for i in range(lam_len, end):
            # # compute relative delay
            tau = (i - lam_len) * self.dt
            tau = 1.0e-6 if i == lam_len else tau
            # # compute absolute delay
            tau_abs = tau + self.sp_delay
            a = tau_abs / self.cosi**2 * sqrt(1.0 - self.sp_delay / tau_abs)
            c = self.tani / self.cosi * tau
            delta = 2.0 * const.PI / self.num_angles
            sca_pow, phy_area, eff_area = self.delay_integration(
                tau, a, c, delta)
            self.power[i, :] = sca_pow
            self.dis_phy_area[i, :] = phy_area
            self.dis_eff_area[i, :] = eff_area

    def compute_sp_info(self):
        """
        compute the delay/dopper on the specular point, also calculate the
        coherent power on the specular point for the coherent reflection
        """
        # # compute scattering vector
        inc_vec = -1.0 * self.geometry.tx_spf / norm(self.geometry.tx_spf)
        scat_vec = self.geometry.rx_spf / norm(self.geometry.rx_spf)
        # # delay and dopper at specular point
        self.sp_dopp = self.geometry.doppler_shift(inc_vec, scat_vec,
                                                   self.signal.freq)
        self.sp_delay = self.geometry.geometric_delay
        # # coherent power
        if self.coherent_pow_flg:
            self.compute_coherent_power(scat_vec)

    def set_model(self, rx_pos, rx_vel, tx_pos, tx_vel, cov_mode=False):
        """ set model for simulator initalization"""
        self.ddm_cov_mode = cov_mode
        # # equal 244ns( 1/(1/1023000/4)), it is ddm delay sampling rate,
        self.sampling_rate = 4091750.0  # 4092000
        self.range_len_exponent = 8
        self.delays = 17  # DDM delay chips
        self.dopps = 11  # DDM doppler bins
        self.dopp_bin = 500.0  # doppler resolution unit in Hz
        self.filter_bb_bw = 5000000.0  # receiver baseband bandwidth in Hz
        self.polar_mode = "RL"  # poliariztion of reflected signal
        # # variable initialize
        if self.ddm_cov_mode:
            self.dopps = (self.dopps - 1) * 2 * self.ddm_cov_factor
        self.center_dopp = self.dopps // 2
        self.range_samples_len = 2**self.range_len_exponent
        # # set bistatic radar geometry
        self.configure_radar_geometry(tx_pos, tx_vel, rx_pos, rx_vel, True)
        # # set interface
        self.set_interface(self.wind_speed)
        # # set radar signal
        self.set_radar_signal(self.sampling_rate, self.filter_bb_bw,
                              self.range_len_exponent)
        # # set intrument information
        self.gain = 0.0
        self.antenna_temperature = 200
        self.noise_figure = 3.0
        self.set_nadir_antenna(self.gain, self.antenna_temperature,
                               self.noise_figure, self.filter_bb_bw, False)
        # # set power waveform information
        init_range = self.geometry.geometric_delay
        init_range -= (self.signal.lambda_size // 2 + 1) * self.dt
        self.set_pow_waveform(init_range, self.sampling_rate)

    def simulate(self, rx_pos, rx_vel, tx_pos, tx_vel):
        self.set_model(rx_pos, rx_vel, tx_pos, tx_vel)
        self.compute_sp_info()
        self.compute_power_distribution()
        self.compute_center_waveform()
        self.compute_ddm()
        # self.output_subddm()
        return self.waveform, self.integrate_waveform, self.wave_range
Exemple #17
0
def upload_route():
    if request.method == 'POST':
        if 'file' not in request.files:
            options = {"noFile": True}
            return render_template("upload.html", **options)

        files = request.files.getlist('file')
        model_file = os.path.join(os.getcwd(), 'model2', 'model')
        identify = classiFier(model_file=model_file, verbose=True)

        matches = []
        file_num = len(files)
        for file in files:
            if file.filename == '':
                options = {"emptyname": True}
                return render_template("upload.html", **options)

            else:
                filename = secure_filename(file.filename)
                user_file_temp = os.path.join(config.env['UPLOAD_FOLDER'],
                                              filename)

                file.save(user_file_temp)

                with open(user_file_temp, 'rb') as file_contents:
                    sample_id = crc32(file_contents.read())

                filename = str(sample_id) + '.WAV'
                user_file = os.path.join(config.env['UPLOAD_FOLDER'], filename)
                os.rename(user_file_temp, user_file)

                result = identify.classFile(user_file,
                                            username=current_user.get_id())
                first_match_name = result["values"][8]
                first_match = [{
                    "name":
                    first_match_name[1:first_match_name.find('_')].title(),
                    "value":
                    float(result["values"][9])
                }, {
                    "name": "Other",
                    "value": 1 - float(result["values"][9])
                }]
                second_match_name = result["values"][10]
                second_match = [{
                    "name":
                    second_match_name[1:second_match_name.find('_')].title(),
                    "value":
                    float(result["values"][11])
                }, {
                    "name": "Other",
                    "value": 1 - float(result["values"][11])
                }]
                third_match_name = result["values"][12]
                third_match = [{
                    "name":
                    third_match_name[1:third_match_name.find('_')].title(),
                    "value":
                    float(result["values"][13])
                }, {
                    "name": "Other",
                    "value": 1 - float(result["values"][13])
                }]

                user_waveform = Waveform(user_file)
                user_waveform.save()
                user_waveform_file = user_file.replace(
                    user_file.split('.')[-1], 'png').replace(os.getcwd(), '')
                user_file = user_file.replace(os.getcwd(), '..')

                activity_file = os.path.join(config.env['UPLOAD_FOLDER'],
                                             'activity/' + filename)
                if os.path.isfile(activity_file):
                    activity_waveform = Waveform(activity_file)
                    activity_waveform.save()
                    activity_waveform_file = activity_file.replace(
                        activity_file.split('.')[-1],
                        'png').replace(os.getcwd(), '')
                    activity_file = activity_file.replace(os.getcwd(), '..')
                else:
                    activity_waveform_file = None
                    activity_file = None

                noise_file = os.path.join(config.env['UPLOAD_FOLDER'],
                                          'noise/' + filename)
                if os.path.isfile(noise_file):
                    noise_waveform = Waveform(noise_file)
                    noise_waveform.save()
                    noise_waveform_file = noise_file.replace(
                        noise_file.split('.')[-1],
                        'png').replace(os.getcwd(), '')
                    noise_file = noise_file.replace(os.getcwd(), '..')
                else:
                    noise_waveform_file = None
                    noise_file = None

                user_clean_file = os.path.join(config.env['UPLOAD_FOLDER'],
                                               'users_clean/' + filename)
                if os.path.isfile(user_clean_file):
                    user_clean_waveform = Waveform(user_clean_file)
                    user_clean_waveform.save()
                    user_clean_waveform_file = user_clean_file.replace(
                        user_clean_file.split('.')[-1],
                        'png').replace(os.getcwd(), '')
                    user_clean_file = user_clean_file.replace(
                        os.getcwd(), '..')
                else:
                    user_clean_waveform_file = None
                    user_clean_file = None

                db = extensions.connect_to_database()

                cur = db.cursor()
                cur.execute("SELECT * FROM sampleInfo WHERE sampleid = %s",
                            (result['sample_id'], ))
                result_sample = cur.fetchall()

                cur.close()

                latitude = result_sample[0]['latitude']
                longitude = result_sample[0]['longitude']
                humidity = result_sample[0]['humidity']
                temp = result_sample[0]['temp']
                if temp:
                    temp = int(round(temp))
                light = result_sample[0]['light']
                if light:
                    light = int(round(light))

                matches.append({
                    'user': user_waveform_file,
                    'filename': user_file,
                    'sample_id': result['sample_id'],
                    'first_match': json.dumps(first_match),
                    'second_match': json.dumps(second_match),
                    'third_match': json.dumps(third_match),
                    'activity': activity_waveform_file,
                    'activity_audio': activity_file,
                    'noise': noise_waveform_file,
                    'noise_audio': noise_file,
                    'user_clean': user_clean_waveform_file,
                    'user_clean_audio': user_clean_file,
                    'file_num': file_num,
                    'latitude': latitude,
                    'longitude': longitude,
                    'humidity': humidity,
                    'temperature': temp,
                    'light': light
                })

        options = {
            'matches': matches,
        }

        return render_template("upload.html", **options)

    return render_template("upload.html")
Exemple #18
0
def apiUpload_route():
	
		

         	#basically copied and pastedd from ../controllers/upload.py. Difference is missing html rendering right here at the beginning and at end. See end for details. 
		files = request.files.getlist('file')
               
       	 	model_file = os.path.join(os.getcwd(), 'model2', 'model')
        	identify = classiFier(model_file=model_file, verbose=True)

        	matches = []
       		file_num = len(files)
       
                filename = secure_filename(file.filename)
                user_file_temp = os.path.join(config.env['UPLOAD_FOLDER'], filename)
                file.save(user_file_temp)

                with open(user_file_temp, 'rb') as file_contents:
                    sample_id = crc32(file_contents.read())

                filename = str(sample_id) + '.WAV'
                user_file = os.path.join(config.env['UPLOAD_FOLDER'], filename)
                os.rename(user_file_temp, user_file)

                result = identify.classFile(user_file, username=current_user.get_id())
                first_match_name = result["values"][8]
                first_match = [{"name": first_match_name[1:first_match_name.find('_')].title(), "value": float(result["values"][9])}, {"name": "Other", "value": 1 - float(result["values"][9])}]
                second_match_name = result["values"][10]
                second_match = [{"name": second_match_name[1:second_match_name.find('_')].title(), "value": float(result["values"][11])}, {"name": "Other", "value": 1 - float(result["values"][11])}]
                third_match_name = result["values"][12]
                third_match = [{"name": third_match_name[1:third_match_name.find('_')].title(), "value": float(result["values"][13])}, {"name": "Other", "value": 1 - float(result["values"][13])}]

                user_waveform = Waveform(user_file)
                user_waveform.save()
                user_waveform_file = user_file.replace(user_file.split('.')[-1], 'png').replace(os.getcwd(), '')
                user_file = user_file.replace(os.getcwd(), '..')

                activity_file = os.path.join(config.env['UPLOAD_FOLDER'], 'activity/' + filename)
                if os.path.isfile(activity_file):
                    activity_waveform = Waveform(activity_file)
                    activity_waveform.save()
                    activity_waveform_file = activity_file.replace(activity_file.split('.')[-1], 'png').replace(os.getcwd(), '')
                    activity_file = activity_file.replace(os.getcwd(), '..')
                else:
                    activity_waveform_file = None
                    activity_file = None

                noise_file = os.path.join(config.env['UPLOAD_FOLDER'], 'noise/' + filename)
                if os.path.isfile(noise_file):
                    noise_waveform = Waveform(noise_file)
                    noise_waveform.save()
                    noise_waveform_file = noise_file.replace(noise_file.split('.')[-1], 'png').replace(os.getcwd(), '')
                    noise_file = noise_file.replace(os.getcwd(), '..')
                else:
                    noise_waveform_file = None
                    noise_file = None

                user_clean_file = os.path.join(config.env['UPLOAD_FOLDER'], 'users_clean/' + filename)
                if os.path.isfile(user_clean_file):
                    user_clean_waveform = Waveform(user_clean_file)
                    user_clean_waveform.save()
                    user_clean_waveform_file = user_clean_file.replace(user_clean_file.split('.')[-1], 'png').replace(os.getcwd(), '')
                    user_clean_file = user_clean_file.replace(os.getcwd(), '..')
                else:
                    user_clean_waveform_file = None
                    user_clean_file = None

                cur = db.cursor()
                cur.execute("SELECT * FROM sampleInfo WHERE sampleid = %s", (result['sample_id'], ))
                result_sample = cur.fetchall()
                latitude = result_sample[0]['latitude']
                longitude = result_sample[0]['longitude']
                humidity = result_sample[0]['humidity']
                temp = result_sample[0]['temp']
                if temp:
                    temp = int(round(temp))
                light = result_sample[0]['light']
                if light:
                    light = int(round(light))

                matches.append({
                                'user': user_waveform_file,
                                'filename': user_file,
                                'sample_id': result['sample_id'], 
                                'first_match': json.dumps(first_match),
                                'second_match': json.dumps(second_match),
                                'third_match': json.dumps(third_match),
                                'activity': activity_waveform_file,
                                'activity_audio': activity_file,
                                'noise': noise_waveform_file,
                                'noise_audio': noise_file,
                                'user_clean': user_clean_waveform_file,
                                'user_clean_audio': user_clean_file,
                                'file_num': file_num,
                                'latitude': latitude,
                                'longitude': longitude,
                                'humidity': humidity,
                                'temperature': temp,
                                'light': light
                                })
        
        	options = {
        	    'matches': matches,
        	}
		#difference here. Instead of return html, returns file. 
		return jsonify(options)
def GetWaveDataR(configFileName,
                 directoryName,
                 fileNum=1,
                 getZeroCrossingIntegral=True):
    startTime = time.time()
    print("Running GetWaveData!")
    print("Starting at " + time.strftime('%H:%M:%S'))
    config = configparser.ConfigParser()
    config.read(configFileName)

    # Setup data info
    # Directories

    data_directory = directoryName  # New

    data_file_name = config['Directories']['data_file_name']
    #pywaves_directory = config['Directories']['pywaves_directory']

    # Digitizer
    dataFormatStr = config['Digitizer']['dataFormat']
    nSamples = int(config['Digitizer']['samples_per_waveform'])
    ns_per_sample = int(config['Digitizer']['ns_per_sample'])
    number_of_bits = int(config['Digitizer']['number_of_bits'])
    dynamic_range_volts = float(config['Digitizer']['dynamic_range_volts'])
    polarity0 = int(
        config['Digitizer']['polarity0'])  # Polarity of first several channels
    p0ch = int(config['Digitizer']
               ['p0ch'])  # Number of channels to apply first polarity to
    polarity1 = int(
        config['Digitizer']['polarity1'])  # Polarity of remaining channels
    baselineOffset = int(config['Digitizer']['baseline_offset'])
    nBaselineSamples = int(config['Digitizer']['baseline_samples'])
    nCh = int(config['Digitizer']['number_of_channels'])

    # nWavesPerLoad = int(config['Data Management']['waves_per_load'])
    nWavesPerLoad = 10000  # Chosen pretty arbitrarily
    # nWaves = int(config['Data Management']['waves_per_folder']) # per folder
    nWaves = 1000000  # Large number
    # Let's just do all of the folders!
    startFolder = int(config['Data Management']['start_folder'])
    nFolders = fileNum
    # nFolders = int(config['Data Management']['number_of_folders'])

    unevenFactor = int(config['Data Management']['uneven_factor'])
    cfdFraction = float(config['Pulse Processing']['cfd_fraction'])
    integralEnd = int(config['Pulse Processing']['integral_end'])
    totalIntegralStart = int(
        config['Pulse Processing']['total_integral_start'])
    tailIntegralStart = int(config['Pulse Processing']['tail_integral_start'])
    applyCRRC4 = bool(int(config['Pulse Processing']['apply_crrc4']))
    CRRC4Tau = float(config['Pulse Processing']['crrc4_shaping_time'])

    # Load pywaves
    # sys.path.extend([pywaves_directory])
    from dataloader import DataLoader
    from waveform import Waveform

    # Pre-calc
    if dataFormatStr == 'DPP_MIXED':
        dataFormat = DataLoader.DAFCA_DPP_MIXED
    elif dataFormatStr == 'STANDARD':
        dataFormat = DataLoader.DAFCA_STD
    if platform.system() is 'Windows':
        directory_separator = '\\'
    else:
        directory_separator = '/'

    # Initialize data arrays
    dataFile1 = data_directory + directory_separator + str(
        1) + directory_separator + data_file_name
    datloader1 = DataLoader(dataFile1, dataFormat, nSamples)
    nWavesIn1 = datloader1.GetNumberOfWavesInFile()
    # print(str(nWavesIn1))

    nLoads = int(nWavesIn1 / nWavesPerLoad)
    if nLoads < 1:
        nLoads = 1
    chBufferSize = int(nFolders * nWavesIn1 * unevenFactor / nCh)
    VperLSB = dynamic_range_volts / (2**number_of_bits)
    fileTimeGap = 2**43  # Note: no more than 3 hours per measurement!

    # Setup channel queues
    ph = np.zeros((nCh, chBufferSize))
    amp = np.zeros((nCh, chBufferSize))
    tailInt = np.zeros((nCh, chBufferSize))
    totalInt = np.zeros((nCh, chBufferSize))
    rms = np.zeros((nCh, chBufferSize))
    ttt = np.zeros((nCh, chBufferSize), dtype=np.uint32)
    extras = np.zeros((nCh, chBufferSize), dtype=np.uint32)
    fullTime = np.zeros((nCh, chBufferSize), dtype=np.uint64)
    cfd = np.zeros((nCh, chBufferSize))
    chCount = np.zeros(nCh, dtype=np.uint32)
    flags = np.zeros((nCh, chBufferSize), dtype=np.uint32)

    # Setup data loader
    waveform = Waveform(np.zeros(nSamples), polarity0, baselineOffset,
                        nBaselineSamples)
    print('polarity0 =' + str(polarity0))
    print('p0ch = ' + str(p0ch))
    print('polarity1 =' + str(polarity1))

    # Queue up waves
    for f in range(startFolder, startFolder + nFolders):
        print('Folder {}:'.format(f))
        fullDFileName = data_directory + directory_separator + str(
            f) + directory_separator + data_file_name
        datloader = DataLoader(fullDFileName, dataFormat, nSamples)
        nWavesInFile = datloader.GetNumberOfWavesInFile()
        nWaves = nWavesInFile + 1
        if (nWavesInFile < nWaves):
            print('Warning: requested more waves than exists in file!')
            loadsInFile = int(np.ceil(nWavesInFile / nWavesPerLoad))
            print('Loading all {} waves instead...'.format(nWavesInFile))
            lastLoad = nWavesInFile - (loadsInFile - 1) * nWavesPerLoad
        else:
            loadsInFile = nLoads
            lastLoad = nWavesPerLoad
        if nWavesInFile % 2 == 0 or nWavesInFile % 2 == 1 or int(nCh) == 2:
            for load in range(loadsInFile):
                if (load == loadsInFile - 1):
                    wavesThisLoad = lastLoad
                else:
                    wavesThisLoad = nWavesPerLoad
                waves = datloader.LoadWaves(wavesThisLoad)
                for w in range(wavesThisLoad):
                    ch = waves[w]['Channel']
                    waveform.SetSamples(waves[w]['Samples'])
                    if (ch >= p0ch):
                        waveform.Polarize(polarity1)
                    else:
                        waveform.Polarize(polarity0)
                    #print(str( waveform.polarity))
                    if applyCRRC4:
                        waveform.ApplyCRRC4(ns_per_sample, CRRC4Tau)
                    if getZeroCrossingIntegral:
                        ph[ch][
                            chCount[ch]] = waveform.GetIntegralToZeroCrossing(
                            ) * VperLSB * ns_per_sample
                    amp[ch][chCount[ch]] = waveform.GetMax()
                    tailInt[ch][chCount[ch]] = waveform.GetIntegralFromPeak(
                        tailIntegralStart,
                        integralEnd) * VperLSB * ns_per_sample
                    totalInt[ch][chCount[ch]] = waveform.GetIntegralFromPeak(
                        totalIntegralStart,
                        integralEnd) * VperLSB * ns_per_sample
                    cfd[ch][chCount[ch]] = waveform.GetCFDTime(
                        cfdFraction) * ns_per_sample
                    ttt[ch][chCount[ch]] = waves[w]['TimeTag']
                    rms[ch][chCount[ch]] = waveform.GetRMSbls(nBaselineSamples)
                    if dataFormatStr == 'DPP_MIXED':
                        extras[ch][chCount[ch]] = waves[w]['Extras']
                        #                    fullTime[ch][chCount[ch]] = ((waves[w]['TimeTag'] +
                        #                                                ((waves[w]['Extras'] & 0xFFFF0000)
                        #                                                << 15)))*ns_per_sample
                        fullTime[ch][chCount[ch]] = (
                            (waves[w]['TimeTag'] +
                             ((waves[w]['Extras'] & 0xFFFF0000) << 15)) +
                            fileTimeGap * f) * ns_per_sample
                    chCount[ch] += 1
    endTime = time.time()
    runTime = endTime - startTime
    print("GetWaveDataR took {} s".format(runTime))
    return chCount, ph, amp, tailInt, totalInt, cfd, ttt, extras, fullTime, flags, rms
Exemple #20
0
 def setup_waveform(self) -> None:
     self.waveform = Waveform()
     self.layout.addItem(self.waveform)