def _basic_tbn(fh, stands, nframes, **kwargs): """ Private function for generating a basic TBN signal. """ start_time = kwargs['start_time'] filter = kwargs['filter'] verbose = kwargs['verbose'] noise_strength = kwargs['noise_strength'] sample_rate = TBNFilters[filter] maxValue = 127 samplesPerFrame = 512 upperSpike = sample_rate / 4.0 lowerSpike = -sample_rate / 4.0 if verbose: print("Simulating %i frames of TBN Data @ %.2f kHz for %i stands:" % \ (nframes, sample_rate/1e3, len(stands))) for i in range(nframes): if i % 1000 == 0 and verbose: print(" frame %i" % (i + 1)) t = int(start_time * dp_common.fS) + int( i * dp_common.fS * samplesPerFrame / sample_rate) tFrame = t / dp_common.fS - start_time + numpy.arange( samplesPerFrame, dtype=numpy.float32) / sample_rate for stand in stands: cFrame = tbn.SimFrame(stand=stand, pol=0, central_freq=40e6, gain=20, frame_count=i + 1, obs_time=t) cFrame.data = numpy.zeros(samplesPerFrame, dtype=numpy.singlecomplex) cFrame.data += numpy.random.randn( samplesPerFrame) + 1j * numpy.random.randn(samplesPerFrame) cFrame.data *= maxValue * noise_strength cFrame.data += maxValue * numpy.exp( 2j * numpy.pi * upperSpike * tFrame) cFrame.write_raw_frame(fh) cFrame = tbn.SimFrame(stand=stand, pol=1, central_freq=40e6, gain=20, frame_count=i + 1, obs_time=t) cFrame.data = numpy.zeros(samplesPerFrame, dtype=numpy.singlecomplex) cFrame.data += numpy.random.randn( samplesPerFrame) + 1j * numpy.random.randn(samplesPerFrame) cFrame.data *= maxValue * noise_strength cFrame.data += maxValue * numpy.exp( 2j * numpy.pi * lowerSpike * tFrame) cFrame.write_raw_frame(fh)
def test_sim_frame(self): """Test the tbn.SimFrame class.""" # Read in a TBN frame from the test file fh = open(tbnFile, 'rb') origFrame = tbnReader.read_frame(fh) fh.close() fakeFrame = tbnWriter.SimFrame() fakeFrame.load_frame(origFrame) # Test the validity of the SimFrame self.assertTrue(fakeFrame.is_valid())
def test_frame_header_errors(self): """Test the header error scenarios when validating a TBN SimFrame.""" # Read in a TBN frame from the test file fh = open(tbnFile, 'rb') origFrame = tbnReader.read_frame(fh) fh.close() # Try to validate frame with the wrong stand number fakeFrame = tbnWriter.SimFrame() fakeFrame.load_frame(copy.deepcopy(origFrame)) fakeFrame.stand = 300 self.assertRaises(ValueError, fakeFrame.is_valid, raise_errors=True)
def test_frame_data_errors(self): """Test the data error scenarios when validating a TBN SimFrame.""" # Read in a TBN frame from the test file fh = open(tbnFile, 'rb') origFrame = tbnReader.read_frame(fh) fh.close() # Try to validate frame with the wrong data type fakeFrame = tbnWriter.SimFrame() fakeFrame.load_frame(copy.deepcopy(origFrame)) fakeFrame.data = fakeFrame.payload.data.real self.assertRaises(ValueError, fakeFrame.is_valid, raise_errors=True) # Try to validate frame with the wrong data size fakeFrame = tbnWriter.SimFrame() fakeFrame.load_frame(copy.deepcopy(origFrame)) fakeFrame.data = None self.assertRaises(ValueError, fakeFrame.is_valid, raise_errors=True) fakeFrame = tbnWriter.SimFrame() fakeFrame.load_frame(copy.deepcopy(origFrame)) fakeFrame.data = fakeFrame.payload.data[0:50] self.assertRaises(ValueError, fakeFrame.is_valid, raise_errors=True)
def _point_source_tbn(fh, stands, src, nframes, **kwargs): """ Private function to build TBN point sources. """ central_freq = kwargs['central_freq'] filter = kwargs['filter'] start_time = kwargs['start_time'] phase_center = kwargs['phase_center'] verbose = kwargs['verbose'] noise_strength = kwargs['noise_strength'] sample_rate = TBNFilters[filter] maxValue = 127 samplesPerFrame = 512 freqs = (numpy.fft.fftfreq(samplesPerFrame, d=1.0 / sample_rate)) + central_freq freqs = numpy.fft.fftshift(freqs) aa = _get_antennaarray(lwa_common.lwa1, stands, start_time, freqs) if verbose: print("Simulating %i frames of TBN Data @ %.2f kHz for %i stands:" % \ (nframes, sample_rate/1e3, len(stands))) for i in range(nframes): if i % 1000 == 0 and verbose: print(" frame %i" % (i + 1)) t = int(start_time * dp_common.fS) + int( i * dp_common.fS * samplesPerFrame / sample_rate) tFrame = t / dp_common.fS - start_time + numpy.arange( samplesPerFrame, dtype=numpy.float32) / sample_rate # Get the source parameters src_params = _get_source_parameters(aa, tFrame[0], src) # Generate the time series response of each signal at each frequency tdSignalsX = _build_signals(aa, stands, src_params, tFrame * 1e9, pol='x', phase_center=phase_center) tdSignalsY = _build_signals(aa, stands, src_params, tFrame * 1e9, pol='y', phase_center=phase_center) j = 0 for stand in stands: cFrame = tbn.SimFrame(stand=stand.stand.id, pol=0, central_freq=central_freq, gain=19, frame_count=i + 1, obs_time=t) cFrame.data = numpy.zeros(samplesPerFrame, dtype=numpy.singlecomplex) cFrame.data += numpy.random.randn( samplesPerFrame) + 1j * numpy.random.randn(samplesPerFrame) cFrame.data *= maxValue * noise_strength cFrame.data += maxValue * tdSignalsX[j, :].astype( numpy.singlecomplex) cFrame.write_raw_frame(fh) cFrame = tbn.SimFrame(stand=stand.stand.id, pol=1, central_freq=central_freq, gain=19, frame_count=i + 1, obs_time=t) cFrame.data = numpy.zeros(samplesPerFrame, dtype=numpy.singlecomplex) cFrame.data += numpy.random.randn( samplesPerFrame) + 1j * numpy.random.randn(samplesPerFrame) cFrame.data *= maxValue * noise_strength cFrame.data += maxValue * tdSignalsY[j, :].astype( numpy.singlecomplex) cFrame.write_raw_frame(fh) j += 1