Skip to content

CSVData

Class for IQ Data CSV and TXT formats

xaratustrah@github Aug-2015

CSVData

Bases: IQBase

Source code in iqtools/csvdata.py
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
class CSVData(IQBase):
    def __init__(self, filename):
        super().__init__(filename)

        # Additional fields in this subclass
        self.center = 0.0
        self.date_time = time.ctime(os.path.getctime(self.filename))
        self.nsamples_total = sum(1 for line in open(filename)) - 1

    def read(self, nframes=10, lframes=1024, sframes=0):
        """Read a section of the file.

        Args:
            nframes (int, optional): Number of frames to be read. Defaults to 10.
            lframes (int, optional): Length of each frame. Defaults to 1024.
            sframes (int, optional): Starting frame. Defaults to 0.
        """        
        self.read_samples(nframes * lframes, offset=sframes * lframes)

    def read_samples(self, nsamples, offset=0):
        """Read samples. Requries the first line to be the header.
        Please also check the function:
            write_signal_to_csv
        in the `tools`.

        Args:
            nsamples (int): Number of samples to read from file
            offset (int, optional): _description_. Defaults to 0.

        Raises:
            ValueError: Raises if the requested number of samples is larger than available
        """        

        if nsamples > self.nsamples_total - offset:
            raise ValueError(
                'Requested number of samples is larger than the available {} samples.'.format(self.nsamples_total))

        x = np.genfromtxt(self.filename, dtype=np.float32, delimiter='|')
        self.fs = x[0, 0]
        self.center = x[0, 1]
        all_data = x[1:, :]
        all_data = all_data.view(np.complex64)[:, 0]

        self.data_array = all_data[offset:nsamples + offset]

read(nframes=10, lframes=1024, sframes=0)

Read a section of the file.

Parameters:

Name Type Description Default
nframes int

Number of frames to be read. Defaults to 10.

10
lframes int

Length of each frame. Defaults to 1024.

1024
sframes int

Starting frame. Defaults to 0.

0
Source code in iqtools/csvdata.py
24
25
26
27
28
29
30
31
32
def read(self, nframes=10, lframes=1024, sframes=0):
    """Read a section of the file.

    Args:
        nframes (int, optional): Number of frames to be read. Defaults to 10.
        lframes (int, optional): Length of each frame. Defaults to 1024.
        sframes (int, optional): Starting frame. Defaults to 0.
    """        
    self.read_samples(nframes * lframes, offset=sframes * lframes)

read_samples(nsamples, offset=0)

Read samples. Requries the first line to be the header. Please also check the function: write_signal_to_csv in the tools.

Parameters:

Name Type Description Default
nsamples int

Number of samples to read from file

required
offset int

description. Defaults to 0.

0

Raises:

Type Description
ValueError

Raises if the requested number of samples is larger than available

Source code in iqtools/csvdata.py
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
def read_samples(self, nsamples, offset=0):
    """Read samples. Requries the first line to be the header.
    Please also check the function:
        write_signal_to_csv
    in the `tools`.

    Args:
        nsamples (int): Number of samples to read from file
        offset (int, optional): _description_. Defaults to 0.

    Raises:
        ValueError: Raises if the requested number of samples is larger than available
    """        

    if nsamples > self.nsamples_total - offset:
        raise ValueError(
            'Requested number of samples is larger than the available {} samples.'.format(self.nsamples_total))

    x = np.genfromtxt(self.filename, dtype=np.float32, delimiter='|')
    self.fs = x[0, 0]
    self.center = x[0, 1]
    all_data = x[1:, :]
    all_data = all_data.view(np.complex64)[:, 0]

    self.data_array = all_data[offset:nsamples + offset]