From 411479d70d60be19d98fafa7f1eb6ef029468427 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 13 Feb 2024 13:37:01 +0800 Subject: [PATCH] start work on the raw import functionality for MATLAB --- ipc.py | 16 +++++++++ matlab/sendReimageData.m | 71 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 matlab/sendReimageData.m diff --git a/ipc.py b/ipc.py index db38303..cb9ef78 100644 --- a/ipc.py +++ b/ipc.py @@ -16,6 +16,7 @@ class ReimageListenerThread(QThread): EXPORT_COMMAND = b'1' EXPORT_RAW_COMMAND = b'2' IMPORT_COMMAND = b'3' + IMPORT_RAW_COMMAND = b'4' RAW_DTYPE = { np.dtype('complex64'): b'0', np.dtype('complex128'): b'1' @@ -77,6 +78,21 @@ def run(self): package['fs'] ) + elif cmd == self.IMPORT_RAW_COMMAND: + # Use for importing from MATLAB or any non-pickle interface + print("Importing raw data array.") + # Custom header packing + # 1) fs: 8-byte double + # 2) fc: 8-byte double + # 3) nperseg: 4-byte int32 + # 4) noverlap: 4-byte int32 + rawheader = conn.recv_bytes(24) + fs = np.frombuffer(rawheader[:8], dtype=np.float64)[0] + fc = np.frombuffer(rawheader[8:16], dtype=np.float64)[0] + nperseg = np.frombuffer(rawheader[16:20], dtype=np.int32)[0] + noverlap = np.frombuffer(rawheader[20:24], dtype=np.int32)[0] + print("fs: {}, fc: {}, nperseg: {}, noverlap: {}".format(fs, fc, nperseg, noverlap)) + # data = np.frombuffer(conn.recv_bytes(), dtype=np.complex64) else: raise TypeError("Unknown command: %s" % (str(cmd))) diff --git a/matlab/sendReimageData.m b/matlab/sendReimageData.m new file mode 100644 index 0000000..1b3ef93 --- /dev/null +++ b/matlab/sendReimageData.m @@ -0,0 +1,71 @@ +function data = sendReimageData(data, varargin) + % Write defaults + fs = 1.0; + fc = 0.0; + nperseg = 128; + noverlap = 16; + ipaddr = 'localhost'; + port = 5000; + + % Parse varargin + if nargin <= 2 + fs = varargin{1}; + end + if nargin <= 3 + fc = varargin{2}; + end + if nargin <= 4 + nperseg = varargin{3}; + end + if nargin <= 5 + noverlap = varargin{4}; + end + if nargin <= 6 + ipaddr = varargin{5}; + end + if nargin <= 7 + port = varargin{6}; + end + + client = tcpclient(ipaddr, port); + + % Note that python's multiprocessing.connection reads/writes + % with the first 4 bytes specifying the length of the payload + + % Write '4' == 52 + msg = uint8([0, 0, 0, 1, 52]); % we write it in this order which python expects + client.write(msg); + + % Create the header of the 4 values + % Cast explicitly to double, then typecast to uint8 + + % Same for fc + + % Cast explicitly to int32, then typecast to uint8 + + % Same for noverlap + + % Send header + + + + % Read the type (4 bytes + 1 byte payload always) + datatype = client.read(5); % we can ignore the size since we know it + datatype = datatype(5); + % Read the data (4 bytes + the payload remainder) + rawbyteslength = client.read(4); + % must swapbytes to get correct length + rawbyteslength = swapbytes(typecast(rawbyteslength, 'uint32')); + % Read the actual payload + rawbytes = client.read(rawbyteslength); + + % Cast the data + if datatype == uint8('0') + data = typecast(rawbytes, 'single'); + elseif datatype == uint8('1') + data = typecast(rawbytes, 'double'); + end + + data = complex(data(1:2:end), data(2:2:end)); + +end \ No newline at end of file