pymodulation 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,165 @@
1
+ GNU LESSER GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+
9
+ This version of the GNU Lesser General Public License incorporates
10
+ the terms and conditions of version 3 of the GNU General Public
11
+ License, supplemented by the additional permissions listed below.
12
+
13
+ 0. Additional Definitions.
14
+
15
+ As used herein, "this License" refers to version 3 of the GNU Lesser
16
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
17
+ General Public License.
18
+
19
+ "The Library" refers to a covered work governed by this License,
20
+ other than an Application or a Combined Work as defined below.
21
+
22
+ An "Application" is any work that makes use of an interface provided
23
+ by the Library, but which is not otherwise based on the Library.
24
+ Defining a subclass of a class defined by the Library is deemed a mode
25
+ of using an interface provided by the Library.
26
+
27
+ A "Combined Work" is a work produced by combining or linking an
28
+ Application with the Library. The particular version of the Library
29
+ with which the Combined Work was made is also called the "Linked
30
+ Version".
31
+
32
+ The "Minimal Corresponding Source" for a Combined Work means the
33
+ Corresponding Source for the Combined Work, excluding any source code
34
+ for portions of the Combined Work that, considered in isolation, are
35
+ based on the Application, and not on the Linked Version.
36
+
37
+ The "Corresponding Application Code" for a Combined Work means the
38
+ object code and/or source code for the Application, including any data
39
+ and utility programs needed for reproducing the Combined Work from the
40
+ Application, but excluding the System Libraries of the Combined Work.
41
+
42
+ 1. Exception to Section 3 of the GNU GPL.
43
+
44
+ You may convey a covered work under sections 3 and 4 of this License
45
+ without being bound by section 3 of the GNU GPL.
46
+
47
+ 2. Conveying Modified Versions.
48
+
49
+ If you modify a copy of the Library, and, in your modifications, a
50
+ facility refers to a function or data to be supplied by an Application
51
+ that uses the facility (other than as an argument passed when the
52
+ facility is invoked), then you may convey a copy of the modified
53
+ version:
54
+
55
+ a) under this License, provided that you make a good faith effort to
56
+ ensure that, in the event an Application does not supply the
57
+ function or data, the facility still operates, and performs
58
+ whatever part of its purpose remains meaningful, or
59
+
60
+ b) under the GNU GPL, with none of the additional permissions of
61
+ this License applicable to that copy.
62
+
63
+ 3. Object Code Incorporating Material from Library Header Files.
64
+
65
+ The object code form of an Application may incorporate material from
66
+ a header file that is part of the Library. You may convey such object
67
+ code under terms of your choice, provided that, if the incorporated
68
+ material is not limited to numerical parameters, data structure
69
+ layouts and accessors, or small macros, inline functions and templates
70
+ (ten or fewer lines in length), you do both of the following:
71
+
72
+ a) Give prominent notice with each copy of the object code that the
73
+ Library is used in it and that the Library and its use are
74
+ covered by this License.
75
+
76
+ b) Accompany the object code with a copy of the GNU GPL and this license
77
+ document.
78
+
79
+ 4. Combined Works.
80
+
81
+ You may convey a Combined Work under terms of your choice that,
82
+ taken together, effectively do not restrict modification of the
83
+ portions of the Library contained in the Combined Work and reverse
84
+ engineering for debugging such modifications, if you also do each of
85
+ the following:
86
+
87
+ a) Give prominent notice with each copy of the Combined Work that
88
+ the Library is used in it and that the Library and its use are
89
+ covered by this License.
90
+
91
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
92
+ document.
93
+
94
+ c) For a Combined Work that displays copyright notices during
95
+ execution, include the copyright notice for the Library among
96
+ these notices, as well as a reference directing the user to the
97
+ copies of the GNU GPL and this license document.
98
+
99
+ d) Do one of the following:
100
+
101
+ 0) Convey the Minimal Corresponding Source under the terms of this
102
+ License, and the Corresponding Application Code in a form
103
+ suitable for, and under terms that permit, the user to
104
+ recombine or relink the Application with a modified version of
105
+ the Linked Version to produce a modified Combined Work, in the
106
+ manner specified by section 6 of the GNU GPL for conveying
107
+ Corresponding Source.
108
+
109
+ 1) Use a suitable shared library mechanism for linking with the
110
+ Library. A suitable mechanism is one that (a) uses at run time
111
+ a copy of the Library already present on the user's computer
112
+ system, and (b) will operate properly with a modified version
113
+ of the Library that is interface-compatible with the Linked
114
+ Version.
115
+
116
+ e) Provide Installation Information, but only if you would otherwise
117
+ be required to provide such information under section 6 of the
118
+ GNU GPL, and only to the extent that such information is
119
+ necessary to install and execute a modified version of the
120
+ Combined Work produced by recombining or relinking the
121
+ Application with a modified version of the Linked Version. (If
122
+ you use option 4d0, the Installation Information must accompany
123
+ the Minimal Corresponding Source and Corresponding Application
124
+ Code. If you use option 4d1, you must provide the Installation
125
+ Information in the manner specified by section 6 of the GNU GPL
126
+ for conveying Corresponding Source.)
127
+
128
+ 5. Combined Libraries.
129
+
130
+ You may place library facilities that are a work based on the
131
+ Library side by side in a single library together with other library
132
+ facilities that are not Applications and are not covered by this
133
+ License, and convey such a combined library under terms of your
134
+ choice, if you do both of the following:
135
+
136
+ a) Accompany the combined library with a copy of the same work based
137
+ on the Library, uncombined with any other library facilities,
138
+ conveyed under the terms of this License.
139
+
140
+ b) Give prominent notice with the combined library that part of it
141
+ is a work based on the Library, and explaining where to find the
142
+ accompanying uncombined form of the same work.
143
+
144
+ 6. Revised Versions of the GNU Lesser General Public License.
145
+
146
+ The Free Software Foundation may publish revised and/or new versions
147
+ of the GNU Lesser General Public License from time to time. Such new
148
+ versions will be similar in spirit to the present version, but may
149
+ differ in detail to address new problems or concerns.
150
+
151
+ Each version is given a distinguishing version number. If the
152
+ Library as you received it specifies that a certain numbered version
153
+ of the GNU Lesser General Public License "or any later version"
154
+ applies to it, you have the option of following the terms and
155
+ conditions either of that published version or of any later version
156
+ published by the Free Software Foundation. If the Library as you
157
+ received it does not specify a version number of the GNU Lesser
158
+ General Public License, you may choose any version of the GNU Lesser
159
+ General Public License ever published by the Free Software Foundation.
160
+
161
+ If the Library as you received it specifies that a proxy can decide
162
+ whether future versions of the GNU Lesser General Public License shall
163
+ apply, that proxy's public statement of acceptance of any version is
164
+ permanent authorization for you to choose that version for the
165
+ Library.
@@ -0,0 +1,108 @@
1
+ Metadata-Version: 2.4
2
+ Name: pymodulation
3
+ Version: 0.1.0
4
+ Summary: PyModulation library
5
+ Home-page: https://github.com/mgm8/pymodulation
6
+ Download-URL: https://github.com/mgm8/pymodulation/releases
7
+ Author: Gabriel Mariano Marcelino
8
+ Author-email: gabriel.mm8@gmail.com
9
+ Maintainer: Gabriel Mariano Marcelino
10
+ Maintainer-email: gabriel.mm8@gmail.com
11
+ License: LGPL-3.0-only
12
+ Platform: Linux
13
+ Platform: Windows
14
+ Platform: Solaris
15
+ Platform: Mac OS-X
16
+ Platform: Unix
17
+ Classifier: Development Status :: 5 - Production/Stable
18
+ Classifier: Environment :: Console
19
+ Classifier: Intended Audience :: Developers
20
+ Classifier: Intended Audience :: Education
21
+ Classifier: Intended Audience :: Science/Research
22
+ Classifier: Intended Audience :: Telecommunications Industry
23
+ Classifier: Natural Language :: English
24
+ Classifier: Operating System :: OS Independent
25
+ Classifier: Programming Language :: Python :: 3
26
+ Classifier: Topic :: Communications :: Ham Radio
27
+ Classifier: Topic :: Education
28
+ Classifier: Topic :: Scientific/Engineering
29
+ Classifier: Topic :: Software Development :: Libraries
30
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
31
+ Description-Content-Type: text/markdown
32
+ License-File: LICENSE
33
+ Requires-Dist: numpy
34
+ Requires-Dist: scipy
35
+ Dynamic: author
36
+ Dynamic: author-email
37
+ Dynamic: classifier
38
+ Dynamic: description
39
+ Dynamic: description-content-type
40
+ Dynamic: download-url
41
+ Dynamic: home-page
42
+ Dynamic: license
43
+ Dynamic: license-file
44
+ Dynamic: maintainer
45
+ Dynamic: maintainer-email
46
+ Dynamic: platform
47
+ Dynamic: requires-dist
48
+ Dynamic: summary
49
+
50
+ <h1 align="center">
51
+ <a href="https://mgm8.github.io/pymodulation/"><img src="docs/img/logo.jpg" alt="PyModulation" width="50%"></a>
52
+ </h1>
53
+
54
+ <a href="https://pypi.org/project/pymodulation/">
55
+ <img src="https://img.shields.io/pypi/v/pymodulation?style=for-the-badge">
56
+ </a>
57
+ <a href="https://pypi.org/project/pymodulation/">
58
+ <img src="https://img.shields.io/pypi/pyversions/pymodulation?style=for-the-badge">
59
+ </a>
60
+ <a href="https://github.com/mgm8/pymodulation/blob/main/LICENSE">
61
+ <img src="https://img.shields.io/github/license/mgm8/pymodulation?style=for-the-badge">
62
+ </a>
63
+ <a href="https://github.com/mgm8/pymodulation/actions">
64
+ <img src="https://img.shields.io/github/actions/workflow/status/mgm8/pymodulation/test.yml?style=for-the-badge">
65
+ </a>
66
+
67
+ ## Overview
68
+
69
+ PyModulation is a Python library that implements a collection of digital modulation and demodulation techniques with a strong focus on Software-Defined Radio (SDR) applications. The library is designed to provide a simple, consistent, and extensible interface for working with different modulation schemes, enabling rapid development, testing, and prototyping of wireless communication systems.
70
+
71
+ The main objective of PyModulation is to allow the direct use of supported modulation techniques with SDR hardware, while remaining flexible enough to be used in simulations, offline signal processing, and educational contexts. By abstracting common modulation tasks, the library helps users focus on system design and experimentation rather than low-level signal handling.
72
+
73
+ PyModulation is suitable for a wide range of applications, including SDR-based transmitters and receivers, communication protocol prototyping, academic research, and teaching digital communications concepts. Its modular architecture makes it easy to extend with new modulation schemes and integrate with existing Python-based SDR and signal-processing toolchains.
74
+
75
+ The following modulations are currently supported:
76
+
77
+ * GFSK/GMSK
78
+
79
+ ## Dependencies
80
+
81
+ * NumPy
82
+ * SciPy
83
+
84
+ ## Installing
85
+
86
+ This library can be installed directly from the source files:
87
+
88
+ * ```python setup.py install```
89
+
90
+ ## Documentation
91
+
92
+ The documentation page is available [here](https://mgm8.github.io/pymodulation/). Instructions to build the documentation page are described below.
93
+
94
+ Contributing instructions are also available [here](https://github.com/mgm8/pyngham/blob/main/CONTRIBUTING.md).
95
+
96
+ ### Dependencies
97
+
98
+ * [Sphinx](https://pypi.org/project/Sphinx/)
99
+
100
+ ### Building the Documentation
101
+
102
+ The documentation pages can be built with Sphinx by running the following command inside the ``docs`` folder:
103
+
104
+ * ```make html```
105
+
106
+ ## License
107
+
108
+ This project is licensed under LGPLv3 license.
@@ -0,0 +1,59 @@
1
+ <h1 align="center">
2
+ <a href="https://mgm8.github.io/pymodulation/"><img src="docs/img/logo.jpg" alt="PyModulation" width="50%"></a>
3
+ </h1>
4
+
5
+ <a href="https://pypi.org/project/pymodulation/">
6
+ <img src="https://img.shields.io/pypi/v/pymodulation?style=for-the-badge">
7
+ </a>
8
+ <a href="https://pypi.org/project/pymodulation/">
9
+ <img src="https://img.shields.io/pypi/pyversions/pymodulation?style=for-the-badge">
10
+ </a>
11
+ <a href="https://github.com/mgm8/pymodulation/blob/main/LICENSE">
12
+ <img src="https://img.shields.io/github/license/mgm8/pymodulation?style=for-the-badge">
13
+ </a>
14
+ <a href="https://github.com/mgm8/pymodulation/actions">
15
+ <img src="https://img.shields.io/github/actions/workflow/status/mgm8/pymodulation/test.yml?style=for-the-badge">
16
+ </a>
17
+
18
+ ## Overview
19
+
20
+ PyModulation is a Python library that implements a collection of digital modulation and demodulation techniques with a strong focus on Software-Defined Radio (SDR) applications. The library is designed to provide a simple, consistent, and extensible interface for working with different modulation schemes, enabling rapid development, testing, and prototyping of wireless communication systems.
21
+
22
+ The main objective of PyModulation is to allow the direct use of supported modulation techniques with SDR hardware, while remaining flexible enough to be used in simulations, offline signal processing, and educational contexts. By abstracting common modulation tasks, the library helps users focus on system design and experimentation rather than low-level signal handling.
23
+
24
+ PyModulation is suitable for a wide range of applications, including SDR-based transmitters and receivers, communication protocol prototyping, academic research, and teaching digital communications concepts. Its modular architecture makes it easy to extend with new modulation schemes and integrate with existing Python-based SDR and signal-processing toolchains.
25
+
26
+ The following modulations are currently supported:
27
+
28
+ * GFSK/GMSK
29
+
30
+ ## Dependencies
31
+
32
+ * NumPy
33
+ * SciPy
34
+
35
+ ## Installing
36
+
37
+ This library can be installed directly from the source files:
38
+
39
+ * ```python setup.py install```
40
+
41
+ ## Documentation
42
+
43
+ The documentation page is available [here](https://mgm8.github.io/pymodulation/). Instructions to build the documentation page are described below.
44
+
45
+ Contributing instructions are also available [here](https://github.com/mgm8/pyngham/blob/main/CONTRIBUTING.md).
46
+
47
+ ### Dependencies
48
+
49
+ * [Sphinx](https://pypi.org/project/Sphinx/)
50
+
51
+ ### Building the Documentation
52
+
53
+ The documentation pages can be built with Sphinx by running the following command inside the ``docs`` folder:
54
+
55
+ * ```make html```
56
+
57
+ ## License
58
+
59
+ This project is licensed under LGPLv3 license.
@@ -0,0 +1,26 @@
1
+ #
2
+ # __init__.py
3
+ #
4
+ # Copyright The PyModulation Contributors.
5
+ #
6
+ # This file is part of PyModulation library.
7
+ #
8
+ # PyModulation library is free software: you can redistribute it and/or modify
9
+ # it under the terms of the GNU Lesser General Public License as published by
10
+ # the Free Software Foundation, either version 3 of the License, or
11
+ # (at your option) any later version.
12
+ #
13
+ # PyModulation library is distributed in the hope that it will be useful,
14
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
+ # GNU Lesser General Public License for more details.
17
+ #
18
+ # You should have received a copy of the GNU Lesser General Public License
19
+ # along with PyModulation library. If not, see <http://www.gnu.org/licenses/>.
20
+ #
21
+ #
22
+
23
+ from pymodulation.version import __version__
24
+
25
+ from pymodulation.gfsk import GFSK
26
+ from pymodulation.gmsk import GMSK
@@ -0,0 +1,286 @@
1
+ #
2
+ # ax100.py
3
+ #
4
+ # Copyright The SpaceLab-Decoder Contributors.
5
+ #
6
+ # This file is part of SpaceLab-Decoder.
7
+ #
8
+ # SpaceLab-Decoder is free software; you can redistribute it
9
+ # and/or modify it under the terms of the GNU General Public License as
10
+ # published by the Free Software Foundation, either version 3 of the
11
+ # License, or (at your option) any later version.
12
+ #
13
+ # SpaceLab-Decoder is distributed in the hope that it will be useful,
14
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
+ # GNU General Public License for more details.
17
+ #
18
+ # You should have received a copy of the GNU General Public
19
+ # License along with SpaceLab-Decoder; if not, see <http://www.gnu.org/licenses/>.
20
+ #
21
+ #
22
+
23
+ from pymodulation.golay24 import Golay24
24
+ from pymodulation.reed_solomon import ReedSolomon
25
+
26
+ _AX100_PREAMBLE_DEFAULT = [0xAA]*50
27
+ _AX100_SYNC_WORD_DEFAULT = [147, 11, 81, 222]
28
+
29
+ # Repeats after 255 bits, but repeats byte-aligning after 255 byte
30
+ _AX100_CCSDS_POLY = [0xFF, 0x48, 0x0E, 0xC0, 0x9A, 0x0D, 0x70, 0xBC, 0x8E, 0x2C, 0x93, 0xAD, 0xA7,
31
+ 0xB7, 0x46, 0xCE, 0x5A, 0x97, 0x7D, 0xCC, 0x32, 0xA2, 0xBF, 0x3E, 0x0A, 0x10,
32
+ 0xF1, 0x88, 0x94, 0xCD, 0xEA, 0xB1, 0xFE, 0x90, 0x1D, 0x81, 0x34, 0x1A, 0xE1,
33
+ 0x79, 0x1C, 0x59, 0x27, 0x5B, 0x4F, 0x6E, 0x8D, 0x9C, 0xB5, 0x2E, 0xFB, 0x98,
34
+ 0x65, 0x45, 0x7E, 0x7C, 0x14, 0x21, 0xE3, 0x11, 0x29, 0x9B, 0xD5, 0x63, 0xFD,
35
+ 0x20, 0x3B, 0x02, 0x68, 0x35, 0xC2, 0xF2, 0x38, 0xB2, 0x4E, 0xB6, 0x9E, 0xDD,
36
+ 0x1B, 0x39, 0x6A, 0x5D, 0xF7, 0x30, 0xCA, 0x8A, 0xFC, 0xF8, 0x28, 0x43, 0xC6,
37
+ 0x22, 0x53, 0x37, 0xAA, 0xC7, 0xFA, 0x40, 0x76, 0x04, 0xD0, 0x6B, 0x85, 0xE4,
38
+ 0x71, 0x64, 0x9D, 0x6D, 0x3D, 0xBA, 0x36, 0x72, 0xD4, 0xBB, 0xEE, 0x61, 0x95,
39
+ 0x15, 0xF9, 0xF0, 0x50, 0x87, 0x8C, 0x44, 0xA6, 0x6F, 0x55, 0x8F, 0xF4, 0x80,
40
+ 0xEC, 0x09, 0xA0, 0xD7, 0x0B, 0xC8, 0xE2, 0xC9, 0x3A, 0xDA, 0x7B, 0x74, 0x6C,
41
+ 0xE5, 0xA9, 0x77, 0xDC, 0xC3, 0x2A, 0x2B, 0xF3, 0xE0, 0xA1, 0x0F, 0x18, 0x89,
42
+ 0x4C, 0xDE, 0xAB, 0x1F, 0xE9, 0x01, 0xD8, 0x13, 0x41, 0xAE, 0x17, 0x91, 0xC5,
43
+ 0x92, 0x75, 0xB4, 0xF6, 0xE8, 0xD9, 0xCB, 0x52, 0xEF, 0xB9, 0x86, 0x54, 0x57,
44
+ 0xE7, 0xC1, 0x42, 0x1E, 0x31, 0x12, 0x99, 0xBD, 0x56, 0x3F, 0xD2, 0x03, 0xB0,
45
+ 0x26, 0x83, 0x5C, 0x2F, 0x23, 0x8B, 0x24, 0xEB, 0x69, 0xED, 0xD1, 0xB3, 0x96,
46
+ 0xA5, 0xDF, 0x73, 0x0C, 0xA8, 0xAF, 0xCF, 0x82, 0x84, 0x3C, 0x62, 0x25, 0x33,
47
+ 0x7A, 0xAC, 0x7F, 0xA4, 0x07, 0x60, 0x4D, 0x06, 0xB8, 0x5E, 0x47, 0x16, 0x49,
48
+ 0xD6, 0xD3, 0xDB, 0xA3, 0x67, 0x2D, 0x4B, 0xBE, 0xE6, 0x19, 0x51, 0x5F, 0x9F,
49
+ 0x05, 0x08, 0x78, 0xC4, 0x4A, 0x66, 0xF5, 0x58]
50
+
51
+ class AX100Mode5:
52
+ """
53
+ AX100-Mode5 Protocol.
54
+ """
55
+ def __init__(self):
56
+ """
57
+ Constructor.
58
+ """
59
+ self._preamble = list()
60
+ self._sync_word = list()
61
+ self._decoder_pos = 0
62
+ self._decoder_pkt_len = 0
63
+ self._decoder_golay_buf = list()
64
+ self._decoder_rs_buf = list()
65
+ self._ignore_golay_error = False
66
+
67
+ self.set_preamble(_AX100_PREAMBLE_DEFAULT)
68
+ self.set_sync_word(_AX100_SYNC_WORD_DEFAULT)
69
+
70
+ def set_preamble(self, preamb):
71
+ """
72
+ Configure the preamble sequence.
73
+
74
+ :param preamb: Is the new preamble seuqence in list format.
75
+ :type: list[int]
76
+
77
+ :return: None
78
+ :rtype: None
79
+ """
80
+ self._preamble = preamb.copy()
81
+
82
+ def get_preamble(self):
83
+ """
84
+ Gets the preamble sequence.
85
+
86
+ :return: The pramble sequence as a list of integers.
87
+ :rtype: list[int]
88
+ """
89
+ return self._preamble
90
+
91
+ def set_sync_word(self, sw):
92
+ """
93
+ Configure the sync. word sequence.
94
+
95
+ :param sw: Is the new sync word in list format.
96
+ :type: list[int]
97
+
98
+ :return: None
99
+ :rtype: None
100
+ """
101
+ self._sync_word = sw.copy()
102
+
103
+ def get_sync_word(self):
104
+ """
105
+ Gets the sync. word sequence.
106
+
107
+ :return: The sync word as a list of integers.
108
+ :rtype: list[int]
109
+ """
110
+ return self._sync_word
111
+
112
+ def set_ignore_golay_error(self, enabled):
113
+ """
114
+ Configures the behaviour when a Golay24 field error is detected.
115
+
116
+ :param enabled:
117
+ :type: bool
118
+
119
+ :return: None
120
+ """
121
+ self._ignore_golay_error = enabled
122
+
123
+ def get_ignore_golay_error(self):
124
+ """
125
+ Gets the configuration flag to ignore or not a Golay24 error.
126
+
127
+ :return: True/False if a Golay24 error must be ignored or not.
128
+ :rtype: bool
129
+ """
130
+ return self._ignore_golay_error
131
+
132
+ def encode(self, data):
133
+ """
134
+ Encodes a given data in AX100-Mode5 format.
135
+
136
+ :param data: data to be encoded using the AX100-Mode5 protocol.
137
+ :type: list[int]
138
+
139
+ :return: The encoded AX100-Mode5 packet.
140
+ :rtype: list[int]
141
+ """
142
+ pkt = list()
143
+
144
+ # Preamble
145
+ pkt += self.get_preamble()
146
+
147
+ # Sync word
148
+ pkt += self.get_sync_word()
149
+
150
+ # Golay24
151
+ gol = Golay24()
152
+
153
+ pkt += gol.encode(len(data) + 32) # 32 = Reed-Solomon parity block
154
+
155
+ # Data
156
+ pkt += data
157
+
158
+ # Reed-Solomon parity
159
+ rs = ReedSolomon()
160
+
161
+ pkt += rs.encode(data, 255 - 32 - len(data))
162
+
163
+ # Scramble
164
+ pkt[len(self.get_preamble())+len(self.get_sync_word())+3:] = self._scrambling(pkt[len(self.get_preamble())+len(self.get_sync_word())+3:])
165
+
166
+ return pkt
167
+
168
+ def decode(self, pkt):
169
+ """
170
+ Decodes an AX100-Mode5 packet.
171
+
172
+ :note: The pkt must not contain the preamble and the sync word.
173
+
174
+ :param pkt: is the AX100-Mode5 packet as a list of integers.
175
+ :type: list[int]
176
+
177
+ :return: The decoded data of the given packet.
178
+ :rtype: list[int]
179
+ """
180
+ # Golay24
181
+ gol = Golay24()
182
+
183
+ pkt_len, golay_err = gol.decode(pkt[:3])
184
+
185
+ if pkt_len == -1:
186
+ if self.get_ignore_golay_error():
187
+ pkt_len = pkt[2]
188
+ else:
189
+ raise RuntimeError("Impossible to correct the Golay field!")
190
+
191
+ # De-scrambling
192
+ rs_block = self._scrambling(pkt[3:]) # 3 = Removing Golay24 bytes
193
+
194
+ # Applying the Reed-Solomon decoder
195
+ rs = ReedSolomon()
196
+
197
+ data, err_pos, err = rs.decode(rs_block, 255 - 32 - (pkt_len - 32))
198
+
199
+ # Return the payload data after Reed-Solomon correction
200
+ return data[:pkt_len-32] # 32 = Reed-Solomon parity block
201
+
202
+ def decode_byte(self, byte):
203
+ """
204
+ Decodes a single byte in a AX100-Mode5 packet stream.
205
+
206
+ :param byte: is a byte from a packet stream.
207
+ :type: int
208
+
209
+ :return: None if the packet is not decoded yet, the packet's data if the packet was decoded.
210
+ :rtype: None or list[int]
211
+ """
212
+ if self._decoder_pos < 2: # Receiving Golay24 block
213
+ self._decoder_golay_buf.append(byte)
214
+ self._decoder_pos += 1
215
+ elif self._decoder_pos == 3 - 1: # Golay24 block received
216
+ self._decoder_golay_buf.append(byte)
217
+ self._decoder_pos += 1
218
+
219
+ gol = Golay24()
220
+
221
+ self._decoder_pkt_len, golay_err = gol.decode(self._decoder_golay_buf)
222
+
223
+ if self._decoder_pkt_len == -1:
224
+ if self.get_ignore_golay_error():
225
+ self._decoder_pkt_len = byte - 32
226
+ else:
227
+ self.reset_decoder()
228
+ raise RuntimeError("Impossible to correct the Golay field!")
229
+ elif self._decoder_pkt_len > 255:
230
+ self.reset_decoder()
231
+ raise RuntimeError("Invalid packet length!")
232
+ else:
233
+ self._decoder_pkt_len -= 32 # 32 = Reed-Solomon parity block
234
+
235
+ self._decoder_golay_buf.clear()
236
+ elif self._decoder_pos < 3 + self._decoder_pkt_len - 1: # Receiving Reed-Solomon block (data part)
237
+ self._decoder_rs_buf.append(self._scrambling([byte], start_pos=self._decoder_pos-3)[0])
238
+ self._decoder_pos += 1
239
+ elif self._decoder_pos == 3 + self._decoder_pkt_len - 1: # Data part of the Reed-Solomon block received
240
+ self._decoder_rs_buf.append(self._scrambling([byte], start_pos=self._decoder_pos-3)[0])
241
+ self._decoder_pos += 1
242
+ elif self._decoder_pos < 3 + self._decoder_pkt_len + 32 - 1: # Receiving Reed-Solomon block (parity part)
243
+ self._decoder_rs_buf.append(self._scrambling([byte], start_pos=self._decoder_pos-3)[0])
244
+ self._decoder_pos += 1
245
+ elif self._decoder_pos == 3 + self._decoder_pkt_len + 32 - 1: # Parity part of the Reed-Solomon block received
246
+ self._decoder_rs_buf.append(self._scrambling([byte], start_pos=self._decoder_pos-3)[0])
247
+ self._decoder_pos = 0
248
+
249
+ rs = ReedSolomon()
250
+
251
+ data, err_pos, err = rs.decode(self._decoder_rs_buf.copy(), 255 - 32 - self._decoder_pkt_len)
252
+
253
+ self._decoder_rs_buf.clear()
254
+
255
+ return data[:self._decoder_pkt_len]
256
+ else: # Decoder is lost! Reset
257
+ self.reset_decoder()
258
+
259
+ return None
260
+
261
+ def reset_decoder(self):
262
+ """
263
+ Resets the byte stream decoder.
264
+
265
+ :return: None
266
+ """
267
+ self._decoder_pos = 0
268
+ self._decoder_pkt_len = 0
269
+ self._decoder_golay_buf.clear()
270
+ self._decoder_rs_buf.clear()
271
+
272
+ def _scrambling(self, data, start_pos=0):
273
+ """
274
+ :param data: Is the data to apply the CCSDS scrambling.
275
+ :type: list[int]
276
+
277
+ :param start_pos: Is the start position in the CCSDS polynomial.
278
+ :type: int
279
+
280
+ :return: The input data scrambled.
281
+ :rtype: list[int]
282
+ """
283
+ for i in range(len(data)):
284
+ data[i] ^= _AX100_CCSDS_POLY[(i + start_pos) % len(_AX100_CCSDS_POLY)]
285
+
286
+ return data