Skip to content

Commit

Permalink
Fix: Fixes for hdf5 generation
Browse files Browse the repository at this point in the history
  • Loading branch information
Grabt234 committed Apr 7, 2024
1 parent 54b68bc commit 914cb23
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 8 deletions.
72 changes: 71 additions & 1 deletion config_generators/fersxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def generate_chirp(ramp_period, sample_rate, bandwidth, init_freq=0, tau=0, phi=
phi (int, optional): Phase offset (rad). Defaults to 0.
Returns:
nnp.array: chirp data array
np.array: chirp data array
"""

import numpy as np
Expand All @@ -24,6 +24,76 @@ def generate_chirp(ramp_period, sample_rate, bandwidth, init_freq=0, tau=0, phi=


return np.exp(1.j*(np.pi*bandwidth/(2*max(t_chirp))*pow((t_chirp - tau), 2) + 2*np.pi*init_freq*(t_chirp - tau) + phi))

def write_hdf5(IQ_data, file_name):
"""Write IQ data to an HDF5 file.
Args:
IQ_data (_type_): _description_
filename (_type_): _description_
"""

import os as os

_, file_extension = os.path.splitext(file_name)
if file_extension != ".hdf5":
file_name = file_name + ".hdf5"

import h5py
import numpy as np

h5 = h5py.File(file_name, 'w')
h5.create_dataset('/I/value', data=np.real(IQ_data))
h5.create_dataset('/Q/value', data=np.imag(IQ_data))
h5.close()


def read_hdf5(filename):
""" Read IQ data from an HDF5 file.
Args:
filename (_type_): _description_
Returns:
_type_: _description_
"""

import os
import h5py
import numpy as np

if (os.path.exists(filename) == False):
print("HDF5 file not found. Please check the path.")
exit()

h5 = h5py.File(filename, 'r')

dataset_list = list(h5.keys())

# read attributes
# attribute_list = h5[dataset_list[0]].attrs.keys()
# for attr in attribute_list:
# print(attr, h5[dataset_list[0]].attrs[attr])

scale = np.float64(h5[dataset_list[0]].attrs['fullscale'])
# rate = np.float64(h5[dataset_list[0]].attrs['rate'])
# time = np.float64(h5[dataset_list[0]].attrs['time'])

n_pulses = int(np.floor(np.size(dataset_list)/2))
ns_pulse = int(np.size(h5[dataset_list[0]]))

i_matrix = np.zeros((n_pulses, ns_pulse), dtype='float64')
q_matrix = np.zeros((n_pulses, ns_pulse), dtype='float64')

for i in range(0, n_pulses):
i_matrix[i, :] = np.array(h5[dataset_list[2*i + 0]], dtype='float64')
q_matrix[i, :] = np.array(h5[dataset_list[2*i + 1]], dtype='float64')

dataset = np.array(i_matrix + 1j*q_matrix).astype('complex128')

dataset *= scale

return dataset

class SimulationConfiguration:
def __init__(self, name):
Expand Down
33 changes: 26 additions & 7 deletions config_generators/tutorial_notebook.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -64,7 +64,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 2,
"metadata": {},
"outputs": [
{
Expand All @@ -80,6 +80,7 @@
],
"source": [
"import fersxml as fersxml\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"from scipy.signal import spectrogram\n",
"\n",
Expand Down Expand Up @@ -114,11 +115,12 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"file_name = \"transmission\""
"file_name = \"lfm_transmision.hdf5\"\n",
"fersxml.SignalGenerator.write_hdf5(tx_chirp,file_name)"
]
},
{
Expand Down Expand Up @@ -147,9 +149,6 @@
"SimulationConfiguration.set_simulaion_parameters(\"0\", \"5\",\"299792458\", \"1\", \"2048000\", \"16\")\n",
"SimulationConfiguration.set_export_options(xml=\"false\",csv=\"false\",binary=\"false\",csvbinary=\"false\")\n",
"\n",
"# Define signals which may be used by platforms\n",
"SimulationConfiguration.define_pulse(\"pulse1\",\"pulse1.hdf5\",\"1\", \"1\",\"1000000\")\n",
"\n",
"# Define timing sources which may be used by platforms\n",
"SimulationConfiguration.define_timing_source(\"clock1\",\"true\",\"0\",\"0\",\"10000000\",\"0\",\"0\",\"0\",\"0\")\n",
"\n",
Expand All @@ -159,6 +158,26 @@
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Define Signal Used In Simulation\n",
"\n",
"Use the named defined when writing hdf files"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\n",
"# Define signals which may be used by platforms\n",
"SimulationConfiguration.define_pulse(\"pulse1\",file_name,\"1\", \"1\",\"1000000\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down

0 comments on commit 914cb23

Please sign in to comment.