<commit_before>"""
Create waveform files for hfradar
<NAME>
"""
from numpy import empty,zeros, array,arange,exp,complex64,pi
from numpy.fft import ifft,fft
from numpy.random import seed,random
import scipy.signal
try:
    import stuffr
    from matplotlib.pyplot import show
except ImportError:
    show=None


def create_pseudo_random_code(clen=10000,rseed=0):
    """
    seed is a way of reproducing the random code without having to store all actual codes.
    the seed can then act as a sort of station_id.
    """
    seed(rseed)
    phases = array(exp(1j*2.0*pi*random(clen)),
                         dtype=complex64)

    if show is not None:
        stuffr.plot_cts(phases[0:100])
        show()

    return phases

def rep_seq(x,rep):
    """
    oversample a phase code by a factor of rep
    """
    L = len(x)*rep
    res = empty(L,dtype=x.dtype)
    idx = arange(len(x))*rep
    for i in range(rep):
        res[idx+i] = x

    return res

def waveform_to_file(station=0,clen=10000,oversample=10,filter_output=False,outpath=None):
    """
    lets use 0.1 s code cycle and coherence assumption
    our transmit bandwidth is 100 kHz, and with a clen=10e3 baud code,
    that is 0.1 seconds per cycle as a coherence assumption.
    furthermore, we use a 1 MHz bandwidth, so we oversample by a factor of 10.

    TODO: this writing method doesn't store any metadata--how will rpitx know the sample rate?
    rpitx makes an assumption of 48kHz I think.
    """

    a = rep_seq(create_pseudo_random_code(clen=clen,rseed=station),rep=oversample)

    if filter_output == True:
        w = zeros([oversample*clen],dtype=complex64) # yes, zeros for zero-padded
        fl = (int(oversample+(0.1*oversample)))
        w[:fl]= scipy.signal.blackmanharris(fl) # W[fl:] \equiv 0
        aa = ifft(fft(w) * fft(a))
        a = (aa/abs(aa).max()).astype(complex64) #normalized, single prec complex

    if outpath:
        ofn = "code-l{}-b{}-{:06d}.bin".format(clen,oversample,station)
        print('writing {}'.format(ofn))
        a.tofile(ofn)

    return a
<commit_msg>user output path<commit_after>"""
Create waveform files for hfradar
<NAME>
"""
try:
    from pathlib import Path
    Path().expanduser()
except (ImportError,AttributeError):
    from pathlib2 import Path

from numpy import empty,zeros, array,arange,exp,complex64,pi
from numpy.fft import ifft,fft
from numpy.random import seed,random
import scipy.signal
try:
    import stuffr
    from matplotlib.pyplot import show
except ImportError:
    show=None


def create_pseudo_random_code(clen=10000,rseed=0):
    """
    seed is a way of reproducing the random code without having to store all actual codes.
    the seed can then act as a sort of station_id.
    """
    seed(rseed)
    phases = array(exp(1j*2.0*pi*random(clen)),
                         dtype=complex64)

    if show is not None:
        stuffr.plot_cts(phases[0:100])
        show()

    return phases

def rep_seq(x,rep):
    """
    oversample a phase code by a factor of rep
    """
    L = len(x)*rep
    res = empty(L,dtype=x.dtype)
    idx = arange(len(x))*rep
    for i in range(rep):
        res[idx+i] = x

    return res

def waveform_to_file(station=0,clen=10000,oversample=10,filter_output=False,outpath=None):
    """
    lets use 0.1 s code cycle and coherence assumption
    our transmit bandwidth is 100 kHz, and with a clen=10e3 baud code,
    that is 0.1 seconds per cycle as a coherence assumption.
    furthermore, we use a 1 MHz bandwidth, so we oversample by a factor of 10.

    TODO: this writing method doesn't store any metadata--how will rpitx know the sample rate?
    rpitx makes an assumption of 48kHz I think.
    """

    a = rep_seq(create_pseudo_random_code(clen=clen,rseed=station),rep=oversample)

    if filter_output == True:
        w = zeros([oversample*clen],dtype=complex64) # yes, zeros for zero-padded
        fl = (int(oversample+(0.1*oversample)))
        w[:fl]= scipy.signal.blackmanharris(fl) # W[fl:] \equiv 0
        aa = ifft(fft(w) * fft(a))
        a = (aa/abs(aa).max()).astype(complex64) #normalized, single prec complex

    if outpath:
        p = Path(outpath).expanduser()
        p.mkdir(parents=True,exist_ok=True)
        ofn = p/"code-l{}-b{}-{:06d}.bin".format(clen,oversample,station)
        print('writing {}'.format(ofn))
        a.tofile(str(ofn))

    return a

{'fraction_non_alphanumeric': 0.11136519551795106, 'fraction_numerical': 0.022638920649439745, 'mean_word_length': 3.1109022556390977, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5858445', 'n_tokens_mistral': 1572, 'n_tokens_neox': 1456, 'n_words': 425}