title: Python f.read() and Octave fread(). => Reading a binary file showing the same values
body: <p>I'm reading a binary file with signal samples both in Octave and Python.</p>
  
  <p>The thing is, I want to obtain the same values for both codes, which is not the case.</p>
  
  <p>The binary file is basically a signal in complex format I,Q recorded as a 16bits Int. </p>
  
  <p>So, based on the Octave code:</p>
  
  <pre><code>[data, cnt_data] = fread(fid, 2 * secondOfData * fs, 'int16');
  </code></pre>
  
  <p>and then: </p>
  
  <pre><code>data = data(1:2:end) + 1i * data(2:2:end);
  </code></pre>
  
  <p>It seems simple, just reading the binary data as 16 bits ints. And then creating the final array of complex numbers.</p>
  
  <p>Threfore I assume that in Python I need to do as follows:</p>
  
  <pre><code>rel=int(f.read(2).encode("hex"),16)
  img=int(f.read(2).encode("hex"),16)
  in_clean.append(complex(rel,img))
  </code></pre>
  
  <p>Ok, the main problem I have is that both real and imaginary parts values are not the same.</p>
  
  <p>For instance, in Octave, the first value is:   <strong>-20390 - 10053i</strong></p>
  
  <p>While in Python (applying the code above), the value is: (23216+48088j) </p>
  
  <p>As signs are different, the first thing I thought was that maybe the endianness of the computer that recorded the file and the one I'm using for reading the file are different. So I turned to unpack function, as it allows you to force the endian type.</p>
  
  <p>I was not able to find an "int16" in the unpack documentation:
  <a href="https://docs.python.org/2/library/struct.html" rel="nofollow noreferrer">https://docs.python.org/2/library/struct.html</a></p>
  
  <p>Therefore I went for the "i" option adding "x" (padding bytes) in order to meet the requirement of 32 bits from the table in the "struct" documentation.</p>
  
  <p>So with:</p>
  
  <pre><code>struct.unpack("i","xx"+f.read(2))[0]
  </code></pre>
  
  <p>the result is (-1336248200-658802568j)  Using
  <code>struct.unpack("&lt;i","xx"+f.read(2))[0]</code>   provides the same result.</p>
  
  <p>With:</p>
  
  <pre><code>struct.unpack("&gt;i","xx"+f.read(2))[0]
  </code></pre>
  
  <p>The value is: (2021153456+2021178328j)</p>
  
  <p>With:</p>
  
  <pre><code>struct.unpack("&gt;i",f.read(2)+"xx")[0]
  </code></pre>
  
  <p>The value is: (1521514616-1143441288j)</p>
  
  <p>With:</p>
  
  <pre><code>struct.unpack("&lt;i",f.read(2)+"xx")[0]
  </code></pre>
  
  <p>The value is: (2021175386+2021185723j) </p>
  
  <p>I also tried with numpy and "frombuffer":</p>
  
  <pre><code>np.frombuffer(f.read(1).encode("hex"),dtype=np.int16)
  </code></pre>
  
  <p>With provides: (24885+12386j)</p>
  
  <p>So, any idea about what I'm doing wrong? I'd like to obtain the same value as in Octave. </p>
  
  <p>What is the proper way of reading and interpreting the values in Python so I can obtain the same value as in Octave by applying fread with an'int16'?</p>
  
  <p>I've been searching on the Internet for an answer for this but I was not able to find a method that provides the same value</p>
  
  <p>Thanks a lot
  Best regards</p>