Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doesn't support complex types in NPY files #9

Open
darkstar007 opened this issue Mar 7, 2019 · 0 comments
Open

Doesn't support complex types in NPY files #9

darkstar007 opened this issue Mar 7, 2019 · 0 comments

Comments

@darkstar007
Copy link

darkstar007 commented Mar 7, 2019

As the subject says!

Here is my quick hack to allow complex numpy files to be read:

--- a/npy-matlab/readNPYheader.m
+++ b/npy-matlab/readNPYheader.m
@@ -22,8 +22,8 @@ end
 
 try
     
-    dtypesMatlab = {'uint8','uint16','uint32','uint64','int8','int16','int32','int64','single','double', 'logical'};
-    dtypesNPY = {'u1', 'u2', 'u4', 'u8', 'i1', 'i2', 'i4', 'i8', 'f4', 'f8', 'b1'};
+    dtypesMatlab = {'uint8','uint16','uint32','uint64','int8','int16','int32','int64','single','double', 'logical', 'complex8', 'complex16'};
+    dtypesNPY = {'u1', 'u2', 'u4', 'u8', 'i1', 'i2', 'i4', 'i8', 'f4', 'f8', 'b1', 'c8', 'c16'};
     
--- a/npy-matlab/readNPY.m
+++ b/npy-matlab/readNPY.m
@@ -20,8 +20,16 @@ try
     [~] = fread(fid, totalHeaderLength, 'uint8');
 
     % read the data
-    data = fread(fid, prod(shape), [dataType '=>' dataType]);
-
+    if strcmp(dataType, "complex8") == 1
+       data = fread(fid, prod(shape)*2, 'single=>single');
+       data = data(1:2:end) + 1j * data(2:2:end);
+    elseif strcmp(dataType, "complex16") == 1
+       data = fread(fid, prod(shape)*2, 'double=>double');
+       data = data(1:2:end) + 1j * data(2:2:end);
+    else
+       data = fread(fid, prod(shape), [dataType '=>' dataType]);
+    end
+    

The above seems to work, but not very well tested.

Thanks,

Mat

curtcorum added a commit to curtcorum/npy-matlab that referenced this issue Aug 23, 2021
curtcorum added a commit to curtcorum/npy-matlab that referenced this issue Aug 23, 2021
modifications from:
kwikteam#9
DANAJK added a commit to UCL/npy-matlab that referenced this issue Oct 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant