Skip to content

Instantly share code, notes, and snippets.

@damp11113
Last active June 15, 2023 15:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save damp11113/eafbcf59a7fbe6d1812526fd779a89a8 to your computer and use it in GitHub Desktop.
Save damp11113/eafbcf59a7fbe6d1812526fd779a89a8 to your computer and use it in GitHub Desktop.
multiplex4 (m4) is a compressed file hence has a small capacity. And can store multi IQ data simultaneously (sample 4bit)
import struct
def create_multiplex4_file(filename, sample_rate, data_format, data_streams):
with open(filename, 'wb') as file:
# Write header information
header = struct.pack('!If', sample_rate, data_format)
file.write(header)
# Write data streams
for stream_data in data_streams:
metadata = struct.pack('!I', stream_data['id']) # Example: Stream ID
file.write(metadata)
# Write IQ data for each stream
for iq_sample in stream_data['iq_data']:
iq_byte = struct.pack('!B', iq_sample) # Pack the 4-bit IQ sample into a byte
file.write(iq_byte)
def read_multiplex4_file(file_path):
with open(file_path, 'rb') as file:
# Read header information
header = file.read(8) # Assuming header is 8 bytes long (4 bytes for sample rate, 4 bytes for format)
sample_rate, data_format = struct.unpack('!If', header)
data_streams = []
# Read data streams
while True:
metadata = file.read(4) # Assuming metadata is 4 bytes long (e.g., stream ID)
if not metadata:
break # Reached the end of the file
stream_id = struct.unpack('!I', metadata)[0] # Extract the stream ID
iq_data = []
while True:
iq_byte = file.read(1) # Assuming each IQ sample is represented by 1 byte (8 bits)
if not iq_byte:
break # Reached the end of the current data stream
iq_sample = struct.unpack('!B', iq_byte)[0] # Unpack the byte as a single 4-bit IQ sample
iq_data.append(iq_sample)
data_streams.append({'id': stream_id, 'iq_data': iq_data})
for stream_data in data_streams:
iq = '|'.join([str(num) for num in stream_data['iq_data']])
iqlist = iq.split("|0|0|0")
iqdi = []
for id, iqidremove in enumerate(iqlist):
if id == 0:
iqdi.append(iqidremove)
else:
iqdi.append(iqidremove[3:])
iqdi2 = []
for iqreplace in iqdi:
iqdi2.append(iqreplace.replace('|', ','))
iqpr = [list(map(int, item.split(','))) for item in iqdi2]
data_streams = []
for id, iq in enumerate(iqpr):
data_streams.append({
'id': id,
'iq_data': iq
})
return sample_rate, data_format, data_streams
# Usage example
sample_rate = 1000000 # Example: 1 MHz
data_format = 2 # Example: 1 for complex float
data_streams = [
{
'id': 0, # Example: Stream ID 1
'iq_data': [1, 2, 3, 4, 5, 6] # Example: List of 4-bit IQ samples
},
{
'id': 1, # Example: Stream ID 2
'iq_data': [7, 8, 9, 10, 11, 12] # Example: List of 4-bit IQ samples
},
{
'id': 2, # Example: Stream ID 3
'iq_data': [13, 14, 15, 16, 17, 18] # Example: List of 4-bit IQ samples
},
{
'id': 3, # Example: Stream ID 4
'iq_data': [19, 20] # Example: List of 4-bit IQ samples
}
# Add more data streams if needed
]
file_path = 'file.m4' # Path to the .m4 file
create_multiplex4_file(file_path, sample_rate, data_format, data_streams)
# Usage example
sample_rate, data_format, data_streams = read_multiplex4_file(file_path)
# Print the retrieved data
print(f"Sample Rate: {sample_rate}")
print(f"Data Format: {data_format}")
for stream_data in data_streams:
print(f"IQ Data for ID {stream_data['id']}: {stream_data['iq_data']}")
print("---------------------------")
# Select data for a specific ID
selected_id = 0 # Replace with the desired stream ID
selected_data = None
for stream_data in data_streams:
if stream_data['id'] == selected_id:
selected_data = stream_data['iq_data']
break
# Print the selected data
if selected_data is not None:
print(f"IQ Data for ID {selected_id}: {selected_data}")
else:
print(f"No data found for ID {selected_id}")
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\.m4]
"Content Type"="sample/m4"
@="Multiplex4"
@damp11113
Copy link
Author

here this is output

Sample Rate: 1000000
Data Format: 2.0
IQ Data for ID 0: [1, 2, 3, 4, 5, 6]
IQ Data for ID 1: [7, 8, 9, 10, 11, 12]
IQ Data for ID 2: [13, 14, 15, 16, 17, 18]
IQ Data for ID 3: [19, 20]
---------------------------
IQ Data for ID 0: [1, 2, 3, 4, 5, 6]

image
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment