Skip to content

Commit

Permalink
start work on the raw import functionality for MATLAB
Browse files Browse the repository at this point in the history
  • Loading branch information
icyveins7 committed Feb 13, 2024
1 parent 2f780e2 commit 411479d
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
16 changes: 16 additions & 0 deletions ipc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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)))

Expand Down
71 changes: 71 additions & 0 deletions matlab/sendReimageData.m
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 411479d

Please sign in to comment.